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 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
612 unsigned long req = kvmppc_get_gpr(vcpu, 3);
613 unsigned long target, ret = H_SUCCESS;
614 struct kvm_vcpu *tvcpu;
617 if (req <= MAX_HCALL_OPCODE &&
618 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
625 target = kvmppc_get_gpr(vcpu, 4);
626 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
631 tvcpu->arch.prodded = 1;
633 if (vcpu->arch.ceded) {
634 if (waitqueue_active(&vcpu->wq)) {
635 wake_up_interruptible(&vcpu->wq);
636 vcpu->stat.halt_wakeup++;
641 target = kvmppc_get_gpr(vcpu, 4);
644 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
649 kvm_vcpu_yield_to(tvcpu);
652 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
653 kvmppc_get_gpr(vcpu, 5),
654 kvmppc_get_gpr(vcpu, 6));
657 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
660 idx = srcu_read_lock(&vcpu->kvm->srcu);
661 rc = kvmppc_rtas_hcall(vcpu);
662 srcu_read_unlock(&vcpu->kvm->srcu, idx);
669 /* Send the error out to userspace via KVM_RUN */
672 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
673 kvmppc_get_gpr(vcpu, 5),
674 kvmppc_get_gpr(vcpu, 6),
675 kvmppc_get_gpr(vcpu, 7));
676 if (ret == H_TOO_HARD)
685 if (kvmppc_xics_enabled(vcpu)) {
686 ret = kvmppc_xics_hcall(vcpu, req);
692 kvmppc_set_gpr(vcpu, 3, ret);
693 vcpu->arch.hcall_needed = 0;
697 static int kvmppc_hcall_impl_hv(unsigned long cmd)
705 #ifdef CONFIG_KVM_XICS
716 /* See if it's in the real-mode table */
717 return kvmppc_hcall_impl_hv_realmode(cmd);
720 static int kvmppc_emulate_debug_inst(struct kvm_run *run,
721 struct kvm_vcpu *vcpu)
725 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
728 * Fetch failed, so return to guest and
729 * try executing it again.
734 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
735 run->exit_reason = KVM_EXIT_DEBUG;
736 run->debug.arch.address = kvmppc_get_pc(vcpu);
739 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
744 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
745 struct task_struct *tsk)
749 vcpu->stat.sum_exits++;
751 run->exit_reason = KVM_EXIT_UNKNOWN;
752 run->ready_for_interrupt_injection = 1;
753 switch (vcpu->arch.trap) {
754 /* We're good on these - the host merely wanted to get our attention */
755 case BOOK3S_INTERRUPT_HV_DECREMENTER:
756 vcpu->stat.dec_exits++;
759 case BOOK3S_INTERRUPT_EXTERNAL:
760 case BOOK3S_INTERRUPT_H_DOORBELL:
761 vcpu->stat.ext_intr_exits++;
764 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
765 case BOOK3S_INTERRUPT_HMI:
766 case BOOK3S_INTERRUPT_PERFMON:
769 case BOOK3S_INTERRUPT_MACHINE_CHECK:
771 * Deliver a machine check interrupt to the guest.
772 * We have to do this, even if the host has handled the
773 * machine check, because machine checks use SRR0/1 and
774 * the interrupt might have trashed guest state in them.
776 kvmppc_book3s_queue_irqprio(vcpu,
777 BOOK3S_INTERRUPT_MACHINE_CHECK);
780 case BOOK3S_INTERRUPT_PROGRAM:
784 * Normally program interrupts are delivered directly
785 * to the guest by the hardware, but we can get here
786 * as a result of a hypervisor emulation interrupt
787 * (e40) getting turned into a 700 by BML RTAS.
789 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
790 kvmppc_core_queue_program(vcpu, flags);
794 case BOOK3S_INTERRUPT_SYSCALL:
796 /* hcall - punt to userspace */
799 /* hypercall with MSR_PR has already been handled in rmode,
800 * and never reaches here.
803 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
804 for (i = 0; i < 9; ++i)
805 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
806 run->exit_reason = KVM_EXIT_PAPR_HCALL;
807 vcpu->arch.hcall_needed = 1;
812 * We get these next two if the guest accesses a page which it thinks
813 * it has mapped but which is not actually present, either because
814 * it is for an emulated I/O device or because the corresonding
815 * host page has been paged out. Any other HDSI/HISI interrupts
816 * have been handled already.
818 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
819 r = RESUME_PAGE_FAULT;
821 case BOOK3S_INTERRUPT_H_INST_STORAGE:
822 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
823 vcpu->arch.fault_dsisr = 0;
824 r = RESUME_PAGE_FAULT;
827 * This occurs if the guest executes an illegal instruction.
828 * If the guest debug is disabled, generate a program interrupt
829 * to the guest. If guest debug is enabled, we need to check
830 * whether the instruction is a software breakpoint instruction.
831 * Accordingly return to Guest or Host.
833 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
834 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
835 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
836 swab32(vcpu->arch.emul_inst) :
837 vcpu->arch.emul_inst;
838 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
839 r = kvmppc_emulate_debug_inst(run, vcpu);
841 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
846 * This occurs if the guest (kernel or userspace), does something that
847 * is prohibited by HFSCR. We just generate a program interrupt to
850 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
851 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
855 kvmppc_dump_regs(vcpu);
856 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
857 vcpu->arch.trap, kvmppc_get_pc(vcpu),
858 vcpu->arch.shregs.msr);
859 run->hw.hardware_exit_reason = vcpu->arch.trap;
867 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
868 struct kvm_sregs *sregs)
872 memset(sregs, 0, sizeof(struct kvm_sregs));
873 sregs->pvr = vcpu->arch.pvr;
874 for (i = 0; i < vcpu->arch.slb_max; i++) {
875 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
876 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
882 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
883 struct kvm_sregs *sregs)
887 /* Only accept the same PVR as the host's, since we can't spoof it */
888 if (sregs->pvr != vcpu->arch.pvr)
892 for (i = 0; i < vcpu->arch.slb_nr; i++) {
893 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
894 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
895 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
899 vcpu->arch.slb_max = j;
904 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
907 struct kvmppc_vcore *vc = vcpu->arch.vcore;
910 spin_lock(&vc->lock);
912 * If ILE (interrupt little-endian) has changed, update the
913 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
915 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
916 struct kvm *kvm = vcpu->kvm;
917 struct kvm_vcpu *vcpu;
920 mutex_lock(&kvm->lock);
921 kvm_for_each_vcpu(i, vcpu, kvm) {
922 if (vcpu->arch.vcore != vc)
924 if (new_lpcr & LPCR_ILE)
925 vcpu->arch.intr_msr |= MSR_LE;
927 vcpu->arch.intr_msr &= ~MSR_LE;
929 mutex_unlock(&kvm->lock);
933 * Userspace can only modify DPFD (default prefetch depth),
934 * ILE (interrupt little-endian) and TC (translation control).
935 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
937 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
938 if (cpu_has_feature(CPU_FTR_ARCH_207S))
941 /* Broken 32-bit version of LPCR must not clear top bits */
944 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
945 spin_unlock(&vc->lock);
948 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
949 union kvmppc_one_reg *val)
955 case KVM_REG_PPC_DEBUG_INST:
956 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
958 case KVM_REG_PPC_HIOR:
959 *val = get_reg_val(id, 0);
961 case KVM_REG_PPC_DABR:
962 *val = get_reg_val(id, vcpu->arch.dabr);
964 case KVM_REG_PPC_DABRX:
965 *val = get_reg_val(id, vcpu->arch.dabrx);
967 case KVM_REG_PPC_DSCR:
968 *val = get_reg_val(id, vcpu->arch.dscr);
970 case KVM_REG_PPC_PURR:
971 *val = get_reg_val(id, vcpu->arch.purr);
973 case KVM_REG_PPC_SPURR:
974 *val = get_reg_val(id, vcpu->arch.spurr);
976 case KVM_REG_PPC_AMR:
977 *val = get_reg_val(id, vcpu->arch.amr);
979 case KVM_REG_PPC_UAMOR:
980 *val = get_reg_val(id, vcpu->arch.uamor);
982 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
983 i = id - KVM_REG_PPC_MMCR0;
984 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
986 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
987 i = id - KVM_REG_PPC_PMC1;
988 *val = get_reg_val(id, vcpu->arch.pmc[i]);
990 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
991 i = id - KVM_REG_PPC_SPMC1;
992 *val = get_reg_val(id, vcpu->arch.spmc[i]);
994 case KVM_REG_PPC_SIAR:
995 *val = get_reg_val(id, vcpu->arch.siar);
997 case KVM_REG_PPC_SDAR:
998 *val = get_reg_val(id, vcpu->arch.sdar);
1000 case KVM_REG_PPC_SIER:
1001 *val = get_reg_val(id, vcpu->arch.sier);
1003 case KVM_REG_PPC_IAMR:
1004 *val = get_reg_val(id, vcpu->arch.iamr);
1006 case KVM_REG_PPC_PSPB:
1007 *val = get_reg_val(id, vcpu->arch.pspb);
1009 case KVM_REG_PPC_DPDES:
1010 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1012 case KVM_REG_PPC_DAWR:
1013 *val = get_reg_val(id, vcpu->arch.dawr);
1015 case KVM_REG_PPC_DAWRX:
1016 *val = get_reg_val(id, vcpu->arch.dawrx);
1018 case KVM_REG_PPC_CIABR:
1019 *val = get_reg_val(id, vcpu->arch.ciabr);
1021 case KVM_REG_PPC_CSIGR:
1022 *val = get_reg_val(id, vcpu->arch.csigr);
1024 case KVM_REG_PPC_TACR:
1025 *val = get_reg_val(id, vcpu->arch.tacr);
1027 case KVM_REG_PPC_TCSCR:
1028 *val = get_reg_val(id, vcpu->arch.tcscr);
1030 case KVM_REG_PPC_PID:
1031 *val = get_reg_val(id, vcpu->arch.pid);
1033 case KVM_REG_PPC_ACOP:
1034 *val = get_reg_val(id, vcpu->arch.acop);
1036 case KVM_REG_PPC_WORT:
1037 *val = get_reg_val(id, vcpu->arch.wort);
1039 case KVM_REG_PPC_VPA_ADDR:
1040 spin_lock(&vcpu->arch.vpa_update_lock);
1041 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1042 spin_unlock(&vcpu->arch.vpa_update_lock);
1044 case KVM_REG_PPC_VPA_SLB:
1045 spin_lock(&vcpu->arch.vpa_update_lock);
1046 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1047 val->vpaval.length = vcpu->arch.slb_shadow.len;
1048 spin_unlock(&vcpu->arch.vpa_update_lock);
1050 case KVM_REG_PPC_VPA_DTL:
1051 spin_lock(&vcpu->arch.vpa_update_lock);
1052 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1053 val->vpaval.length = vcpu->arch.dtl.len;
1054 spin_unlock(&vcpu->arch.vpa_update_lock);
1056 case KVM_REG_PPC_TB_OFFSET:
1057 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1059 case KVM_REG_PPC_LPCR:
1060 case KVM_REG_PPC_LPCR_64:
1061 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1063 case KVM_REG_PPC_PPR:
1064 *val = get_reg_val(id, vcpu->arch.ppr);
1066 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1067 case KVM_REG_PPC_TFHAR:
1068 *val = get_reg_val(id, vcpu->arch.tfhar);
1070 case KVM_REG_PPC_TFIAR:
1071 *val = get_reg_val(id, vcpu->arch.tfiar);
1073 case KVM_REG_PPC_TEXASR:
1074 *val = get_reg_val(id, vcpu->arch.texasr);
1076 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1077 i = id - KVM_REG_PPC_TM_GPR0;
1078 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1080 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1083 i = id - KVM_REG_PPC_TM_VSR0;
1085 for (j = 0; j < TS_FPRWIDTH; j++)
1086 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1088 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1089 val->vval = vcpu->arch.vr_tm.vr[i-32];
1095 case KVM_REG_PPC_TM_CR:
1096 *val = get_reg_val(id, vcpu->arch.cr_tm);
1098 case KVM_REG_PPC_TM_LR:
1099 *val = get_reg_val(id, vcpu->arch.lr_tm);
1101 case KVM_REG_PPC_TM_CTR:
1102 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1104 case KVM_REG_PPC_TM_FPSCR:
1105 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1107 case KVM_REG_PPC_TM_AMR:
1108 *val = get_reg_val(id, vcpu->arch.amr_tm);
1110 case KVM_REG_PPC_TM_PPR:
1111 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1113 case KVM_REG_PPC_TM_VRSAVE:
1114 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1116 case KVM_REG_PPC_TM_VSCR:
1117 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1118 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1122 case KVM_REG_PPC_TM_DSCR:
1123 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1125 case KVM_REG_PPC_TM_TAR:
1126 *val = get_reg_val(id, vcpu->arch.tar_tm);
1129 case KVM_REG_PPC_ARCH_COMPAT:
1130 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1140 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1141 union kvmppc_one_reg *val)
1145 unsigned long addr, len;
1148 case KVM_REG_PPC_HIOR:
1149 /* Only allow this to be set to zero */
1150 if (set_reg_val(id, *val))
1153 case KVM_REG_PPC_DABR:
1154 vcpu->arch.dabr = set_reg_val(id, *val);
1156 case KVM_REG_PPC_DABRX:
1157 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1159 case KVM_REG_PPC_DSCR:
1160 vcpu->arch.dscr = set_reg_val(id, *val);
1162 case KVM_REG_PPC_PURR:
1163 vcpu->arch.purr = set_reg_val(id, *val);
1165 case KVM_REG_PPC_SPURR:
1166 vcpu->arch.spurr = set_reg_val(id, *val);
1168 case KVM_REG_PPC_AMR:
1169 vcpu->arch.amr = set_reg_val(id, *val);
1171 case KVM_REG_PPC_UAMOR:
1172 vcpu->arch.uamor = set_reg_val(id, *val);
1174 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1175 i = id - KVM_REG_PPC_MMCR0;
1176 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1178 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1179 i = id - KVM_REG_PPC_PMC1;
1180 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1182 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1183 i = id - KVM_REG_PPC_SPMC1;
1184 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1186 case KVM_REG_PPC_SIAR:
1187 vcpu->arch.siar = set_reg_val(id, *val);
1189 case KVM_REG_PPC_SDAR:
1190 vcpu->arch.sdar = set_reg_val(id, *val);
1192 case KVM_REG_PPC_SIER:
1193 vcpu->arch.sier = set_reg_val(id, *val);
1195 case KVM_REG_PPC_IAMR:
1196 vcpu->arch.iamr = set_reg_val(id, *val);
1198 case KVM_REG_PPC_PSPB:
1199 vcpu->arch.pspb = set_reg_val(id, *val);
1201 case KVM_REG_PPC_DPDES:
1202 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1204 case KVM_REG_PPC_DAWR:
1205 vcpu->arch.dawr = set_reg_val(id, *val);
1207 case KVM_REG_PPC_DAWRX:
1208 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1210 case KVM_REG_PPC_CIABR:
1211 vcpu->arch.ciabr = set_reg_val(id, *val);
1212 /* Don't allow setting breakpoints in hypervisor code */
1213 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1214 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1216 case KVM_REG_PPC_CSIGR:
1217 vcpu->arch.csigr = set_reg_val(id, *val);
1219 case KVM_REG_PPC_TACR:
1220 vcpu->arch.tacr = set_reg_val(id, *val);
1222 case KVM_REG_PPC_TCSCR:
1223 vcpu->arch.tcscr = set_reg_val(id, *val);
1225 case KVM_REG_PPC_PID:
1226 vcpu->arch.pid = set_reg_val(id, *val);
1228 case KVM_REG_PPC_ACOP:
1229 vcpu->arch.acop = set_reg_val(id, *val);
1231 case KVM_REG_PPC_WORT:
1232 vcpu->arch.wort = set_reg_val(id, *val);
1234 case KVM_REG_PPC_VPA_ADDR:
1235 addr = set_reg_val(id, *val);
1237 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1238 vcpu->arch.dtl.next_gpa))
1240 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1242 case KVM_REG_PPC_VPA_SLB:
1243 addr = val->vpaval.addr;
1244 len = val->vpaval.length;
1246 if (addr && !vcpu->arch.vpa.next_gpa)
1248 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1250 case KVM_REG_PPC_VPA_DTL:
1251 addr = val->vpaval.addr;
1252 len = val->vpaval.length;
1254 if (addr && (len < sizeof(struct dtl_entry) ||
1255 !vcpu->arch.vpa.next_gpa))
1257 len -= len % sizeof(struct dtl_entry);
1258 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1260 case KVM_REG_PPC_TB_OFFSET:
1261 /* round up to multiple of 2^24 */
1262 vcpu->arch.vcore->tb_offset =
1263 ALIGN(set_reg_val(id, *val), 1UL << 24);
1265 case KVM_REG_PPC_LPCR:
1266 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1268 case KVM_REG_PPC_LPCR_64:
1269 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
1271 case KVM_REG_PPC_PPR:
1272 vcpu->arch.ppr = set_reg_val(id, *val);
1274 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1275 case KVM_REG_PPC_TFHAR:
1276 vcpu->arch.tfhar = set_reg_val(id, *val);
1278 case KVM_REG_PPC_TFIAR:
1279 vcpu->arch.tfiar = set_reg_val(id, *val);
1281 case KVM_REG_PPC_TEXASR:
1282 vcpu->arch.texasr = set_reg_val(id, *val);
1284 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1285 i = id - KVM_REG_PPC_TM_GPR0;
1286 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1288 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1291 i = id - KVM_REG_PPC_TM_VSR0;
1293 for (j = 0; j < TS_FPRWIDTH; j++)
1294 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1296 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1297 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1302 case KVM_REG_PPC_TM_CR:
1303 vcpu->arch.cr_tm = set_reg_val(id, *val);
1305 case KVM_REG_PPC_TM_LR:
1306 vcpu->arch.lr_tm = set_reg_val(id, *val);
1308 case KVM_REG_PPC_TM_CTR:
1309 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1311 case KVM_REG_PPC_TM_FPSCR:
1312 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1314 case KVM_REG_PPC_TM_AMR:
1315 vcpu->arch.amr_tm = set_reg_val(id, *val);
1317 case KVM_REG_PPC_TM_PPR:
1318 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1320 case KVM_REG_PPC_TM_VRSAVE:
1321 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1323 case KVM_REG_PPC_TM_VSCR:
1324 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1325 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1329 case KVM_REG_PPC_TM_DSCR:
1330 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1332 case KVM_REG_PPC_TM_TAR:
1333 vcpu->arch.tar_tm = set_reg_val(id, *val);
1336 case KVM_REG_PPC_ARCH_COMPAT:
1337 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1347 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1349 struct kvmppc_vcore *vcore;
1351 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1356 INIT_LIST_HEAD(&vcore->runnable_threads);
1357 spin_lock_init(&vcore->lock);
1358 spin_lock_init(&vcore->stoltb_lock);
1359 init_waitqueue_head(&vcore->wq);
1360 vcore->preempt_tb = TB_NIL;
1361 vcore->lpcr = kvm->arch.lpcr;
1362 vcore->first_vcpuid = core * threads_per_subcore;
1365 vcore->mpp_buffer_is_valid = false;
1367 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1368 vcore->mpp_buffer = (void *)__get_free_pages(
1369 GFP_KERNEL|__GFP_ZERO,
1375 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1378 struct kvm_vcpu *vcpu;
1381 struct kvmppc_vcore *vcore;
1383 core = id / threads_per_subcore;
1384 if (core >= KVM_MAX_VCORES)
1388 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1392 err = kvm_vcpu_init(vcpu, kvm, id);
1396 vcpu->arch.shared = &vcpu->arch.shregs;
1397 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1399 * The shared struct is never shared on HV,
1400 * so we can always use host endianness
1402 #ifdef __BIG_ENDIAN__
1403 vcpu->arch.shared_big_endian = true;
1405 vcpu->arch.shared_big_endian = false;
1408 vcpu->arch.mmcr[0] = MMCR0_FC;
1409 vcpu->arch.ctrl = CTRL_RUNLATCH;
1410 /* default to host PVR, since we can't spoof it */
1411 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1412 spin_lock_init(&vcpu->arch.vpa_update_lock);
1413 spin_lock_init(&vcpu->arch.tbacct_lock);
1414 vcpu->arch.busy_preempt = TB_NIL;
1415 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1417 kvmppc_mmu_book3s_hv_init(vcpu);
1419 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1421 init_waitqueue_head(&vcpu->arch.cpu_run);
1423 mutex_lock(&kvm->lock);
1424 vcore = kvm->arch.vcores[core];
1426 vcore = kvmppc_vcore_create(kvm, core);
1427 kvm->arch.vcores[core] = vcore;
1428 kvm->arch.online_vcores++;
1430 mutex_unlock(&kvm->lock);
1435 spin_lock(&vcore->lock);
1436 ++vcore->num_threads;
1437 spin_unlock(&vcore->lock);
1438 vcpu->arch.vcore = vcore;
1439 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1441 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1442 kvmppc_sanity_check(vcpu);
1447 kmem_cache_free(kvm_vcpu_cache, vcpu);
1449 return ERR_PTR(err);
1452 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1454 if (vpa->pinned_addr)
1455 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1459 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1461 spin_lock(&vcpu->arch.vpa_update_lock);
1462 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1463 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1464 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1465 spin_unlock(&vcpu->arch.vpa_update_lock);
1466 kvm_vcpu_uninit(vcpu);
1467 kmem_cache_free(kvm_vcpu_cache, vcpu);
1470 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1472 /* Indicate we want to get back into the guest */
1476 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1478 unsigned long dec_nsec, now;
1481 if (now > vcpu->arch.dec_expires) {
1482 /* decrementer has already gone negative */
1483 kvmppc_core_queue_dec(vcpu);
1484 kvmppc_core_prepare_to_enter(vcpu);
1487 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1489 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1491 vcpu->arch.timer_running = 1;
1494 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1496 vcpu->arch.ceded = 0;
1497 if (vcpu->arch.timer_running) {
1498 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1499 vcpu->arch.timer_running = 0;
1503 extern void __kvmppc_vcore_entry(void);
1505 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1506 struct kvm_vcpu *vcpu)
1510 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1512 spin_lock_irq(&vcpu->arch.tbacct_lock);
1514 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1515 vcpu->arch.stolen_logged;
1516 vcpu->arch.busy_preempt = now;
1517 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1518 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1520 list_del(&vcpu->arch.run_list);
1523 static int kvmppc_grab_hwthread(int cpu)
1525 struct paca_struct *tpaca;
1526 long timeout = 10000;
1530 /* Ensure the thread won't go into the kernel if it wakes */
1531 tpaca->kvm_hstate.hwthread_req = 1;
1532 tpaca->kvm_hstate.kvm_vcpu = NULL;
1535 * If the thread is already executing in the kernel (e.g. handling
1536 * a stray interrupt), wait for it to get back to nap mode.
1537 * The smp_mb() is to ensure that our setting of hwthread_req
1538 * is visible before we look at hwthread_state, so if this
1539 * races with the code at system_reset_pSeries and the thread
1540 * misses our setting of hwthread_req, we are sure to see its
1541 * setting of hwthread_state, and vice versa.
1544 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1545 if (--timeout <= 0) {
1546 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1554 static void kvmppc_release_hwthread(int cpu)
1556 struct paca_struct *tpaca;
1559 tpaca->kvm_hstate.hwthread_req = 0;
1560 tpaca->kvm_hstate.kvm_vcpu = NULL;
1563 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1566 struct paca_struct *tpaca;
1567 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1569 if (vcpu->arch.timer_running) {
1570 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1571 vcpu->arch.timer_running = 0;
1573 cpu = vc->pcpu + vcpu->arch.ptid;
1575 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1576 tpaca->kvm_hstate.kvm_vcore = vc;
1577 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1578 vcpu->cpu = vc->pcpu;
1580 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1581 if (cpu != smp_processor_id()) {
1583 if (vcpu->arch.ptid)
1589 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1595 while (vc->nap_count < vc->n_woken) {
1596 if (++i >= 1000000) {
1597 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1598 vc->nap_count, vc->n_woken);
1607 * Check that we are on thread 0 and that any other threads in
1608 * this core are off-line. Then grab the threads so they can't
1611 static int on_primary_thread(void)
1613 int cpu = smp_processor_id();
1616 /* Are we on a primary subcore? */
1617 if (cpu_thread_in_subcore(cpu))
1621 while (++thr < threads_per_subcore)
1622 if (cpu_online(cpu + thr))
1625 /* Grab all hw threads so they can't go into the kernel */
1626 for (thr = 1; thr < threads_per_subcore; ++thr) {
1627 if (kvmppc_grab_hwthread(cpu + thr)) {
1628 /* Couldn't grab one; let the others go */
1630 kvmppc_release_hwthread(cpu + thr);
1631 } while (--thr > 0);
1638 static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1640 phys_addr_t phy_addr, mpp_addr;
1642 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1643 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1645 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1646 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1648 vc->mpp_buffer_is_valid = true;
1651 static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1653 phys_addr_t phy_addr, mpp_addr;
1655 phy_addr = virt_to_phys(vc->mpp_buffer);
1656 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1658 /* We must abort any in-progress save operations to ensure
1659 * the table is valid so that prefetch engine knows when to
1660 * stop prefetching. */
1661 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1662 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1666 * Run a set of guest threads on a physical core.
1667 * Called with vc->lock held.
1669 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1671 struct kvm_vcpu *vcpu, *vnext;
1674 int i, need_vpa_update;
1676 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1678 /* don't start if any threads have a signal pending */
1679 need_vpa_update = 0;
1680 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1681 if (signal_pending(vcpu->arch.run_task))
1683 if (vcpu->arch.vpa.update_pending ||
1684 vcpu->arch.slb_shadow.update_pending ||
1685 vcpu->arch.dtl.update_pending)
1686 vcpus_to_update[need_vpa_update++] = vcpu;
1690 * Initialize *vc, in particular vc->vcore_state, so we can
1691 * drop the vcore lock if necessary.
1695 vc->entry_exit_count = 0;
1696 vc->preempt_tb = TB_NIL;
1697 vc->vcore_state = VCORE_STARTING;
1699 vc->napping_threads = 0;
1702 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1703 * which can't be called with any spinlocks held.
1705 if (need_vpa_update) {
1706 spin_unlock(&vc->lock);
1707 for (i = 0; i < need_vpa_update; ++i)
1708 kvmppc_update_vpas(vcpus_to_update[i]);
1709 spin_lock(&vc->lock);
1713 * Make sure we are running on primary threads, and that secondary
1714 * threads are offline. Also check if the number of threads in this
1715 * guest are greater than the current system threads per guest.
1717 if ((threads_per_core > 1) &&
1718 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
1719 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1720 vcpu->arch.ret = -EBUSY;
1725 vc->pcpu = smp_processor_id();
1726 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1727 kvmppc_start_thread(vcpu);
1728 kvmppc_create_dtl_entry(vcpu, vc);
1729 trace_kvm_guest_enter(vcpu);
1732 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1733 get_paca()->kvm_hstate.kvm_vcore = vc;
1734 get_paca()->kvm_hstate.ptid = 0;
1736 vc->vcore_state = VCORE_RUNNING;
1739 trace_kvmppc_run_core(vc, 0);
1741 spin_unlock(&vc->lock);
1745 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1747 if (vc->mpp_buffer_is_valid)
1748 kvmppc_start_restoring_l2_cache(vc);
1750 __kvmppc_vcore_entry();
1752 spin_lock(&vc->lock);
1755 kvmppc_start_saving_l2_cache(vc);
1757 /* disable sending of IPIs on virtual external irqs */
1758 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1760 /* wait for secondary threads to finish writing their state to memory */
1761 if (vc->nap_count < vc->n_woken)
1762 kvmppc_wait_for_nap(vc);
1763 for (i = 0; i < threads_per_subcore; ++i)
1764 kvmppc_release_hwthread(vc->pcpu + i);
1765 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1766 vc->vcore_state = VCORE_EXITING;
1767 spin_unlock(&vc->lock);
1769 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1771 /* make sure updates to secondary vcpu structs are visible now */
1778 spin_lock(&vc->lock);
1780 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1781 /* cancel pending dec exception if dec is positive */
1782 if (now < vcpu->arch.dec_expires &&
1783 kvmppc_core_pending_dec(vcpu))
1784 kvmppc_core_dequeue_dec(vcpu);
1786 trace_kvm_guest_exit(vcpu);
1789 if (vcpu->arch.trap)
1790 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1791 vcpu->arch.run_task);
1793 vcpu->arch.ret = ret;
1794 vcpu->arch.trap = 0;
1796 if (vcpu->arch.ceded) {
1797 if (!is_kvmppc_resume_guest(ret))
1798 kvmppc_end_cede(vcpu);
1800 kvmppc_set_timer(vcpu);
1805 vc->vcore_state = VCORE_INACTIVE;
1806 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1808 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
1809 kvmppc_remove_runnable(vc, vcpu);
1810 wake_up(&vcpu->arch.cpu_run);
1814 trace_kvmppc_run_core(vc, 1);
1818 * Wait for some other vcpu thread to execute us, and
1819 * wake us up when we need to handle something in the host.
1821 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1825 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1826 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1828 finish_wait(&vcpu->arch.cpu_run, &wait);
1832 * All the vcpus in this vcore are idle, so wait for a decrementer
1833 * or external interrupt to one of the vcpus. vc->lock is held.
1835 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1837 struct kvm_vcpu *vcpu;
1842 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1845 * Check one last time for pending exceptions and ceded state after
1846 * we put ourselves on the wait queue
1848 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1849 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
1856 finish_wait(&vc->wq, &wait);
1860 vc->vcore_state = VCORE_SLEEPING;
1861 trace_kvmppc_vcore_blocked(vc, 0);
1862 spin_unlock(&vc->lock);
1864 finish_wait(&vc->wq, &wait);
1865 spin_lock(&vc->lock);
1866 vc->vcore_state = VCORE_INACTIVE;
1867 trace_kvmppc_vcore_blocked(vc, 1);
1870 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1873 struct kvmppc_vcore *vc;
1874 struct kvm_vcpu *v, *vn;
1876 trace_kvmppc_run_vcpu_enter(vcpu);
1878 kvm_run->exit_reason = 0;
1879 vcpu->arch.ret = RESUME_GUEST;
1880 vcpu->arch.trap = 0;
1881 kvmppc_update_vpas(vcpu);
1884 * Synchronize with other threads in this virtual core
1886 vc = vcpu->arch.vcore;
1887 spin_lock(&vc->lock);
1888 vcpu->arch.ceded = 0;
1889 vcpu->arch.run_task = current;
1890 vcpu->arch.kvm_run = kvm_run;
1891 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1892 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1893 vcpu->arch.busy_preempt = TB_NIL;
1894 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1898 * This happens the first time this is called for a vcpu.
1899 * If the vcore is already running, we may be able to start
1900 * this thread straight away and have it join in.
1902 if (!signal_pending(current)) {
1903 if (vc->vcore_state == VCORE_RUNNING &&
1904 VCORE_EXIT_COUNT(vc) == 0) {
1905 kvmppc_create_dtl_entry(vcpu, vc);
1906 kvmppc_start_thread(vcpu);
1907 trace_kvm_guest_enter(vcpu);
1908 } else if (vc->vcore_state == VCORE_SLEEPING) {
1914 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1915 !signal_pending(current)) {
1916 if (vc->vcore_state != VCORE_INACTIVE) {
1917 spin_unlock(&vc->lock);
1918 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1919 spin_lock(&vc->lock);
1922 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1924 kvmppc_core_prepare_to_enter(v);
1925 if (signal_pending(v->arch.run_task)) {
1926 kvmppc_remove_runnable(vc, v);
1927 v->stat.signal_exits++;
1928 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1929 v->arch.ret = -EINTR;
1930 wake_up(&v->arch.cpu_run);
1933 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1937 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1938 if (!v->arch.pending_exceptions)
1939 n_ceded += v->arch.ceded;
1943 if (n_ceded == vc->n_runnable)
1944 kvmppc_vcore_blocked(vc);
1946 kvmppc_run_core(vc);
1950 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1951 (vc->vcore_state == VCORE_RUNNING ||
1952 vc->vcore_state == VCORE_EXITING)) {
1953 spin_unlock(&vc->lock);
1954 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1955 spin_lock(&vc->lock);
1958 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1959 kvmppc_remove_runnable(vc, vcpu);
1960 vcpu->stat.signal_exits++;
1961 kvm_run->exit_reason = KVM_EXIT_INTR;
1962 vcpu->arch.ret = -EINTR;
1965 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1966 /* Wake up some vcpu to run the core */
1967 v = list_first_entry(&vc->runnable_threads,
1968 struct kvm_vcpu, arch.run_list);
1969 wake_up(&v->arch.cpu_run);
1972 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
1973 spin_unlock(&vc->lock);
1974 return vcpu->arch.ret;
1977 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1982 if (!vcpu->arch.sane) {
1983 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1987 kvmppc_core_prepare_to_enter(vcpu);
1989 /* No need to go into the guest when all we'll do is come back out */
1990 if (signal_pending(current)) {
1991 run->exit_reason = KVM_EXIT_INTR;
1995 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1996 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1999 /* On the first time here, set up HTAB and VRMA */
2000 if (!vcpu->kvm->arch.rma_setup_done) {
2001 r = kvmppc_hv_setup_htab_rma(vcpu);
2006 flush_fp_to_thread(current);
2007 flush_altivec_to_thread(current);
2008 flush_vsx_to_thread(current);
2009 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
2010 vcpu->arch.pgdir = current->mm->pgd;
2011 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
2014 r = kvmppc_run_vcpu(run, vcpu);
2016 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2017 !(vcpu->arch.shregs.msr & MSR_PR)) {
2018 trace_kvm_hcall_enter(vcpu);
2019 r = kvmppc_pseries_do_hcall(vcpu);
2020 trace_kvm_hcall_exit(vcpu, r);
2021 kvmppc_core_prepare_to_enter(vcpu);
2022 } else if (r == RESUME_PAGE_FAULT) {
2023 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2024 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2025 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2026 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2028 } while (is_kvmppc_resume_guest(r));
2031 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
2032 atomic_dec(&vcpu->kvm->arch.vcpus_running);
2036 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2039 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2043 (*sps)->page_shift = def->shift;
2044 (*sps)->slb_enc = def->sllp;
2045 (*sps)->enc[0].page_shift = def->shift;
2046 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
2048 * Add 16MB MPSS support if host supports it
2050 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2051 (*sps)->enc[1].page_shift = 24;
2052 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2057 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2058 struct kvm_ppc_smmu_info *info)
2060 struct kvm_ppc_one_seg_page_size *sps;
2062 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2063 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2064 info->flags |= KVM_PPC_1T_SEGMENTS;
2065 info->slb_size = mmu_slb_size;
2067 /* We only support these sizes for now, and no muti-size segments */
2068 sps = &info->sps[0];
2069 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2070 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2071 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2077 * Get (and clear) the dirty memory log for a memory slot.
2079 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2080 struct kvm_dirty_log *log)
2082 struct kvm_memory_slot *memslot;
2086 mutex_lock(&kvm->slots_lock);
2089 if (log->slot >= KVM_USER_MEM_SLOTS)
2092 memslot = id_to_memslot(kvm->memslots, log->slot);
2094 if (!memslot->dirty_bitmap)
2097 n = kvm_dirty_bitmap_bytes(memslot);
2098 memset(memslot->dirty_bitmap, 0, n);
2100 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
2105 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2110 mutex_unlock(&kvm->slots_lock);
2114 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2115 struct kvm_memory_slot *dont)
2117 if (!dont || free->arch.rmap != dont->arch.rmap) {
2118 vfree(free->arch.rmap);
2119 free->arch.rmap = NULL;
2123 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2124 unsigned long npages)
2126 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2127 if (!slot->arch.rmap)
2133 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2134 struct kvm_memory_slot *memslot,
2135 struct kvm_userspace_memory_region *mem)
2140 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2141 struct kvm_userspace_memory_region *mem,
2142 const struct kvm_memory_slot *old)
2144 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2145 struct kvm_memory_slot *memslot;
2147 if (npages && old->npages) {
2149 * If modifying a memslot, reset all the rmap dirty bits.
2150 * If this is a new memslot, we don't need to do anything
2151 * since the rmap array starts out as all zeroes,
2152 * i.e. no pages are dirty.
2154 memslot = id_to_memslot(kvm->memslots, mem->slot);
2155 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2160 * Update LPCR values in kvm->arch and in vcores.
2161 * Caller must hold kvm->lock.
2163 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2168 if ((kvm->arch.lpcr & mask) == lpcr)
2171 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2173 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2174 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2177 spin_lock(&vc->lock);
2178 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2179 spin_unlock(&vc->lock);
2180 if (++cores_done >= kvm->arch.online_vcores)
2185 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2190 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2193 struct kvm *kvm = vcpu->kvm;
2195 struct kvm_memory_slot *memslot;
2196 struct vm_area_struct *vma;
2197 unsigned long lpcr = 0, senc;
2198 unsigned long psize, porder;
2201 mutex_lock(&kvm->lock);
2202 if (kvm->arch.rma_setup_done)
2203 goto out; /* another vcpu beat us to it */
2205 /* Allocate hashed page table (if not done already) and reset it */
2206 if (!kvm->arch.hpt_virt) {
2207 err = kvmppc_alloc_hpt(kvm, NULL);
2209 pr_err("KVM: Couldn't alloc HPT\n");
2214 /* Look up the memslot for guest physical address 0 */
2215 srcu_idx = srcu_read_lock(&kvm->srcu);
2216 memslot = gfn_to_memslot(kvm, 0);
2218 /* We must have some memory at 0 by now */
2220 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2223 /* Look up the VMA for the start of this memory slot */
2224 hva = memslot->userspace_addr;
2225 down_read(¤t->mm->mmap_sem);
2226 vma = find_vma(current->mm, hva);
2227 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2230 psize = vma_kernel_pagesize(vma);
2231 porder = __ilog2(psize);
2233 up_read(¤t->mm->mmap_sem);
2235 /* We can handle 4k, 64k or 16M pages in the VRMA */
2237 if (!(psize == 0x1000 || psize == 0x10000 ||
2238 psize == 0x1000000))
2241 /* Update VRMASD field in the LPCR */
2242 senc = slb_pgsize_encoding(psize);
2243 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2244 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2245 /* the -4 is to account for senc values starting at 0x10 */
2246 lpcr = senc << (LPCR_VRMASD_SH - 4);
2248 /* Create HPTEs in the hash page table for the VRMA */
2249 kvmppc_map_vrma(vcpu, memslot, porder);
2251 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
2253 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2255 kvm->arch.rma_setup_done = 1;
2258 srcu_read_unlock(&kvm->srcu, srcu_idx);
2260 mutex_unlock(&kvm->lock);
2264 up_read(¤t->mm->mmap_sem);
2268 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2270 unsigned long lpcr, lpid;
2272 /* Allocate the guest's logical partition ID */
2274 lpid = kvmppc_alloc_lpid();
2277 kvm->arch.lpid = lpid;
2280 * Since we don't flush the TLB when tearing down a VM,
2281 * and this lpid might have previously been used,
2282 * make sure we flush on each core before running the new VM.
2284 cpumask_setall(&kvm->arch.need_tlb_flush);
2286 /* Start out with the default set of hcalls enabled */
2287 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2288 sizeof(kvm->arch.enabled_hcalls));
2290 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2292 /* Init LPCR for virtual RMA mode */
2293 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2294 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2295 lpcr &= LPCR_PECE | LPCR_LPES;
2296 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2297 LPCR_VPM0 | LPCR_VPM1;
2298 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2299 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2300 /* On POWER8 turn on online bit to enable PURR/SPURR */
2301 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2303 kvm->arch.lpcr = lpcr;
2306 * Track that we now have a HV mode VM active. This blocks secondary
2307 * CPU threads from coming online.
2309 kvm_hv_vm_activated();
2314 static void kvmppc_free_vcores(struct kvm *kvm)
2318 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2319 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2320 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2321 free_pages((unsigned long)vc->mpp_buffer,
2324 kfree(kvm->arch.vcores[i]);
2326 kvm->arch.online_vcores = 0;
2329 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2331 kvm_hv_vm_deactivated();
2333 kvmppc_free_vcores(kvm);
2335 kvmppc_free_hpt(kvm);
2338 /* We don't need to emulate any privileged instructions or dcbz */
2339 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2340 unsigned int inst, int *advance)
2342 return EMULATE_FAIL;
2345 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2348 return EMULATE_FAIL;
2351 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2354 return EMULATE_FAIL;
2357 static int kvmppc_core_check_processor_compat_hv(void)
2359 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2360 !cpu_has_feature(CPU_FTR_ARCH_206))
2365 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2366 unsigned int ioctl, unsigned long arg)
2368 struct kvm *kvm __maybe_unused = filp->private_data;
2369 void __user *argp = (void __user *)arg;
2374 case KVM_PPC_ALLOCATE_HTAB: {
2378 if (get_user(htab_order, (u32 __user *)argp))
2380 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2384 if (put_user(htab_order, (u32 __user *)argp))
2390 case KVM_PPC_GET_HTAB_FD: {
2391 struct kvm_get_htab_fd ghf;
2394 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2396 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2408 * List of hcall numbers to enable by default.
2409 * For compatibility with old userspace, we enable by default
2410 * all hcalls that were implemented before the hcall-enabling
2411 * facility was added. Note this list should not include H_RTAS.
2413 static unsigned int default_hcall_list[] = {
2427 #ifdef CONFIG_KVM_XICS
2438 static void init_default_hcalls(void)
2443 for (i = 0; default_hcall_list[i]; ++i) {
2444 hcall = default_hcall_list[i];
2445 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2446 __set_bit(hcall / 4, default_enabled_hcalls);
2450 static struct kvmppc_ops kvm_ops_hv = {
2451 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2452 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2453 .get_one_reg = kvmppc_get_one_reg_hv,
2454 .set_one_reg = kvmppc_set_one_reg_hv,
2455 .vcpu_load = kvmppc_core_vcpu_load_hv,
2456 .vcpu_put = kvmppc_core_vcpu_put_hv,
2457 .set_msr = kvmppc_set_msr_hv,
2458 .vcpu_run = kvmppc_vcpu_run_hv,
2459 .vcpu_create = kvmppc_core_vcpu_create_hv,
2460 .vcpu_free = kvmppc_core_vcpu_free_hv,
2461 .check_requests = kvmppc_core_check_requests_hv,
2462 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2463 .flush_memslot = kvmppc_core_flush_memslot_hv,
2464 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2465 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2466 .unmap_hva = kvm_unmap_hva_hv,
2467 .unmap_hva_range = kvm_unmap_hva_range_hv,
2468 .age_hva = kvm_age_hva_hv,
2469 .test_age_hva = kvm_test_age_hva_hv,
2470 .set_spte_hva = kvm_set_spte_hva_hv,
2471 .mmu_destroy = kvmppc_mmu_destroy_hv,
2472 .free_memslot = kvmppc_core_free_memslot_hv,
2473 .create_memslot = kvmppc_core_create_memslot_hv,
2474 .init_vm = kvmppc_core_init_vm_hv,
2475 .destroy_vm = kvmppc_core_destroy_vm_hv,
2476 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2477 .emulate_op = kvmppc_core_emulate_op_hv,
2478 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2479 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2480 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2481 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2482 .hcall_implemented = kvmppc_hcall_impl_hv,
2485 static int kvmppc_book3s_init_hv(void)
2489 * FIXME!! Do we need to check on all cpus ?
2491 r = kvmppc_core_check_processor_compat_hv();
2495 kvm_ops_hv.owner = THIS_MODULE;
2496 kvmppc_hv_ops = &kvm_ops_hv;
2498 init_default_hcalls();
2500 r = kvmppc_mmu_hv_init();
2504 static void kvmppc_book3s_exit_hv(void)
2506 kvmppc_hv_ops = NULL;
2509 module_init(kvmppc_book3s_init_hv);
2510 module_exit(kvmppc_book3s_exit_hv);
2511 MODULE_LICENSE("GPL");
2512 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2513 MODULE_ALIAS("devname:kvm");