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 EXIT_DEBUG */
62 /* #define EXIT_DEBUG_SIMPLE */
63 /* #define EXIT_DEBUG_INT */
65 /* Used to indicate that a guest page fault needs to be handled */
66 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
68 /* Used as a "null" value for timebase values */
69 #define TB_NIL (~(u64)0)
71 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
73 #if defined(CONFIG_PPC_64K_PAGES)
74 #define MPP_BUFFER_ORDER 0
75 #elif defined(CONFIG_PPC_4K_PAGES)
76 #define MPP_BUFFER_ORDER 3
80 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
81 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
83 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
87 wait_queue_head_t *wqp;
89 wqp = kvm_arch_vcpu_wq(vcpu);
90 if (waitqueue_active(wqp)) {
91 wake_up_interruptible(wqp);
92 ++vcpu->stat.halt_wakeup;
97 /* CPU points to the first thread of the core */
98 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
99 #ifdef CONFIG_PPC_ICP_NATIVE
100 int real_cpu = cpu + vcpu->arch.ptid;
101 if (paca[real_cpu].kvm_hstate.xics_phys)
102 xics_wake_cpu(real_cpu);
106 smp_send_reschedule(cpu);
112 * We use the vcpu_load/put functions to measure stolen time.
113 * Stolen time is counted as time when either the vcpu is able to
114 * run as part of a virtual core, but the task running the vcore
115 * is preempted or sleeping, or when the vcpu needs something done
116 * in the kernel by the task running the vcpu, but that task is
117 * preempted or sleeping. Those two things have to be counted
118 * separately, since one of the vcpu tasks will take on the job
119 * of running the core, and the other vcpu tasks in the vcore will
120 * sleep waiting for it to do that, but that sleep shouldn't count
123 * Hence we accumulate stolen time when the vcpu can run as part of
124 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
125 * needs its task to do other things in the kernel (for example,
126 * service a page fault) in busy_stolen. We don't accumulate
127 * stolen time for a vcore when it is inactive, or for a vcpu
128 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
129 * a misnomer; it means that the vcpu task is not executing in
130 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
131 * the kernel. We don't have any way of dividing up that time
132 * between time that the vcpu is genuinely stopped, time that
133 * the task is actively working on behalf of the vcpu, and time
134 * that the task is preempted, so we don't count any of it as
137 * Updates to busy_stolen are protected by arch.tbacct_lock;
138 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
139 * of the vcpu that has taken responsibility for running the vcore
140 * (i.e. vc->runner). The stolen times are measured in units of
141 * timebase ticks. (Note that the != TB_NIL checks below are
142 * purely defensive; they should never fail.)
145 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
147 struct kvmppc_vcore *vc = vcpu->arch.vcore;
150 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
151 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
152 vc->preempt_tb != TB_NIL) {
153 vc->stolen_tb += mftb() - vc->preempt_tb;
154 vc->preempt_tb = TB_NIL;
156 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
157 vcpu->arch.busy_preempt != TB_NIL) {
158 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
159 vcpu->arch.busy_preempt = TB_NIL;
161 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
164 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
166 struct kvmppc_vcore *vc = vcpu->arch.vcore;
169 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
170 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
171 vc->preempt_tb = mftb();
172 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
173 vcpu->arch.busy_preempt = mftb();
174 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
177 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
179 vcpu->arch.shregs.msr = msr;
180 kvmppc_end_cede(vcpu);
183 void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
185 vcpu->arch.pvr = pvr;
188 int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
190 unsigned long pcr = 0;
191 struct kvmppc_vcore *vc = vcpu->arch.vcore;
194 if (!cpu_has_feature(CPU_FTR_ARCH_206))
195 return -EINVAL; /* 970 has no compat mode support */
197 switch (arch_compat) {
200 * If an arch bit is set in PCR, all the defined
201 * higher-order arch bits also have to be set.
203 pcr = PCR_ARCH_206 | PCR_ARCH_205;
215 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
216 /* POWER7 can't emulate POWER8 */
217 if (!(pcr & PCR_ARCH_206))
219 pcr &= ~PCR_ARCH_206;
223 spin_lock(&vc->lock);
224 vc->arch_compat = arch_compat;
226 spin_unlock(&vc->lock);
231 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
235 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
236 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
237 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
238 for (r = 0; r < 16; ++r)
239 pr_err("r%2d = %.16lx r%d = %.16lx\n",
240 r, kvmppc_get_gpr(vcpu, r),
241 r+16, kvmppc_get_gpr(vcpu, r+16));
242 pr_err("ctr = %.16lx lr = %.16lx\n",
243 vcpu->arch.ctr, vcpu->arch.lr);
244 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
245 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
246 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
247 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
248 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
249 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
250 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
251 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
252 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
253 pr_err("fault dar = %.16lx dsisr = %.8x\n",
254 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
255 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
256 for (r = 0; r < vcpu->arch.slb_max; ++r)
257 pr_err(" ESID = %.16llx VSID = %.16llx\n",
258 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
259 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
260 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
261 vcpu->arch.last_inst);
264 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
267 struct kvm_vcpu *v, *ret = NULL;
269 mutex_lock(&kvm->lock);
270 kvm_for_each_vcpu(r, v, kvm) {
271 if (v->vcpu_id == id) {
276 mutex_unlock(&kvm->lock);
280 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
282 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
283 vpa->yield_count = cpu_to_be32(1);
286 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
287 unsigned long addr, unsigned long len)
289 /* check address is cacheline aligned */
290 if (addr & (L1_CACHE_BYTES - 1))
292 spin_lock(&vcpu->arch.vpa_update_lock);
293 if (v->next_gpa != addr || v->len != len) {
295 v->len = addr ? len : 0;
296 v->update_pending = 1;
298 spin_unlock(&vcpu->arch.vpa_update_lock);
302 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
311 static int vpa_is_registered(struct kvmppc_vpa *vpap)
313 if (vpap->update_pending)
314 return vpap->next_gpa != 0;
315 return vpap->pinned_addr != NULL;
318 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
320 unsigned long vcpuid, unsigned long vpa)
322 struct kvm *kvm = vcpu->kvm;
323 unsigned long len, nb;
325 struct kvm_vcpu *tvcpu;
328 struct kvmppc_vpa *vpap;
330 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
334 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
335 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
336 subfunc == H_VPA_REG_SLB) {
337 /* Registering new area - address must be cache-line aligned */
338 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
341 /* convert logical addr to kernel addr and read length */
342 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
345 if (subfunc == H_VPA_REG_VPA)
346 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
348 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
349 kvmppc_unpin_guest_page(kvm, va, vpa, false);
352 if (len > nb || len < sizeof(struct reg_vpa))
361 spin_lock(&tvcpu->arch.vpa_update_lock);
364 case H_VPA_REG_VPA: /* register VPA */
365 if (len < sizeof(struct lppaca))
367 vpap = &tvcpu->arch.vpa;
371 case H_VPA_REG_DTL: /* register DTL */
372 if (len < sizeof(struct dtl_entry))
374 len -= len % sizeof(struct dtl_entry);
376 /* Check that they have previously registered a VPA */
378 if (!vpa_is_registered(&tvcpu->arch.vpa))
381 vpap = &tvcpu->arch.dtl;
385 case H_VPA_REG_SLB: /* register SLB shadow buffer */
386 /* Check that they have previously registered a VPA */
388 if (!vpa_is_registered(&tvcpu->arch.vpa))
391 vpap = &tvcpu->arch.slb_shadow;
395 case H_VPA_DEREG_VPA: /* deregister VPA */
396 /* Check they don't still have a DTL or SLB buf registered */
398 if (vpa_is_registered(&tvcpu->arch.dtl) ||
399 vpa_is_registered(&tvcpu->arch.slb_shadow))
402 vpap = &tvcpu->arch.vpa;
406 case H_VPA_DEREG_DTL: /* deregister DTL */
407 vpap = &tvcpu->arch.dtl;
411 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
412 vpap = &tvcpu->arch.slb_shadow;
418 vpap->next_gpa = vpa;
420 vpap->update_pending = 1;
423 spin_unlock(&tvcpu->arch.vpa_update_lock);
428 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
430 struct kvm *kvm = vcpu->kvm;
436 * We need to pin the page pointed to by vpap->next_gpa,
437 * but we can't call kvmppc_pin_guest_page under the lock
438 * as it does get_user_pages() and down_read(). So we
439 * have to drop the lock, pin the page, then get the lock
440 * again and check that a new area didn't get registered
444 gpa = vpap->next_gpa;
445 spin_unlock(&vcpu->arch.vpa_update_lock);
449 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
450 spin_lock(&vcpu->arch.vpa_update_lock);
451 if (gpa == vpap->next_gpa)
453 /* sigh... unpin that one and try again */
455 kvmppc_unpin_guest_page(kvm, va, gpa, false);
458 vpap->update_pending = 0;
459 if (va && nb < vpap->len) {
461 * If it's now too short, it must be that userspace
462 * has changed the mappings underlying guest memory,
463 * so unregister the region.
465 kvmppc_unpin_guest_page(kvm, va, gpa, false);
468 if (vpap->pinned_addr)
469 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
472 vpap->pinned_addr = va;
475 vpap->pinned_end = va + vpap->len;
478 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
480 if (!(vcpu->arch.vpa.update_pending ||
481 vcpu->arch.slb_shadow.update_pending ||
482 vcpu->arch.dtl.update_pending))
485 spin_lock(&vcpu->arch.vpa_update_lock);
486 if (vcpu->arch.vpa.update_pending) {
487 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
488 if (vcpu->arch.vpa.pinned_addr)
489 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
491 if (vcpu->arch.dtl.update_pending) {
492 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
493 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
494 vcpu->arch.dtl_index = 0;
496 if (vcpu->arch.slb_shadow.update_pending)
497 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
498 spin_unlock(&vcpu->arch.vpa_update_lock);
502 * Return the accumulated stolen time for the vcore up until `now'.
503 * The caller should hold the vcore lock.
505 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
510 * If we are the task running the vcore, then since we hold
511 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
512 * can't be updated, so we don't need the tbacct_lock.
513 * If the vcore is inactive, it can't become active (since we
514 * hold the vcore lock), so the vcpu load/put functions won't
515 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
517 if (vc->vcore_state != VCORE_INACTIVE &&
518 vc->runner->arch.run_task != current) {
519 spin_lock_irq(&vc->runner->arch.tbacct_lock);
521 if (vc->preempt_tb != TB_NIL)
522 p += now - vc->preempt_tb;
523 spin_unlock_irq(&vc->runner->arch.tbacct_lock);
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))
623 idx = srcu_read_lock(&vcpu->kvm->srcu);
624 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
625 kvmppc_get_gpr(vcpu, 5),
626 kvmppc_get_gpr(vcpu, 6),
627 kvmppc_get_gpr(vcpu, 7));
628 srcu_read_unlock(&vcpu->kvm->srcu, idx);
633 target = kvmppc_get_gpr(vcpu, 4);
634 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
639 tvcpu->arch.prodded = 1;
641 if (vcpu->arch.ceded) {
642 if (waitqueue_active(&vcpu->wq)) {
643 wake_up_interruptible(&vcpu->wq);
644 vcpu->stat.halt_wakeup++;
649 target = kvmppc_get_gpr(vcpu, 4);
652 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
657 kvm_vcpu_yield_to(tvcpu);
660 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
661 kvmppc_get_gpr(vcpu, 5),
662 kvmppc_get_gpr(vcpu, 6));
665 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
668 idx = srcu_read_lock(&vcpu->kvm->srcu);
669 rc = kvmppc_rtas_hcall(vcpu);
670 srcu_read_unlock(&vcpu->kvm->srcu, idx);
677 /* Send the error out to userspace via KVM_RUN */
680 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
681 kvmppc_get_gpr(vcpu, 5),
682 kvmppc_get_gpr(vcpu, 6),
683 kvmppc_get_gpr(vcpu, 7));
684 if (ret == H_TOO_HARD)
693 if (kvmppc_xics_enabled(vcpu)) {
694 ret = kvmppc_xics_hcall(vcpu, req);
700 kvmppc_set_gpr(vcpu, 3, ret);
701 vcpu->arch.hcall_needed = 0;
705 static int kvmppc_hcall_impl_hv(unsigned long cmd)
713 #ifdef CONFIG_KVM_XICS
724 /* See if it's in the real-mode table */
725 return kvmppc_hcall_impl_hv_realmode(cmd);
728 static int kvmppc_emulate_debug_inst(struct kvm_run *run,
729 struct kvm_vcpu *vcpu)
733 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
736 * Fetch failed, so return to guest and
737 * try executing it again.
742 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
743 run->exit_reason = KVM_EXIT_DEBUG;
744 run->debug.arch.address = kvmppc_get_pc(vcpu);
747 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
752 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
753 struct task_struct *tsk)
757 vcpu->stat.sum_exits++;
759 run->exit_reason = KVM_EXIT_UNKNOWN;
760 run->ready_for_interrupt_injection = 1;
761 switch (vcpu->arch.trap) {
762 /* We're good on these - the host merely wanted to get our attention */
763 case BOOK3S_INTERRUPT_HV_DECREMENTER:
764 vcpu->stat.dec_exits++;
767 case BOOK3S_INTERRUPT_EXTERNAL:
768 case BOOK3S_INTERRUPT_H_DOORBELL:
769 vcpu->stat.ext_intr_exits++;
772 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
773 case BOOK3S_INTERRUPT_HMI:
774 case BOOK3S_INTERRUPT_PERFMON:
777 case BOOK3S_INTERRUPT_MACHINE_CHECK:
779 * Deliver a machine check interrupt to the guest.
780 * We have to do this, even if the host has handled the
781 * machine check, because machine checks use SRR0/1 and
782 * the interrupt might have trashed guest state in them.
784 kvmppc_book3s_queue_irqprio(vcpu,
785 BOOK3S_INTERRUPT_MACHINE_CHECK);
788 case BOOK3S_INTERRUPT_PROGRAM:
792 * Normally program interrupts are delivered directly
793 * to the guest by the hardware, but we can get here
794 * as a result of a hypervisor emulation interrupt
795 * (e40) getting turned into a 700 by BML RTAS.
797 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
798 kvmppc_core_queue_program(vcpu, flags);
802 case BOOK3S_INTERRUPT_SYSCALL:
804 /* hcall - punt to userspace */
807 /* hypercall with MSR_PR has already been handled in rmode,
808 * and never reaches here.
811 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
812 for (i = 0; i < 9; ++i)
813 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
814 run->exit_reason = KVM_EXIT_PAPR_HCALL;
815 vcpu->arch.hcall_needed = 1;
820 * We get these next two if the guest accesses a page which it thinks
821 * it has mapped but which is not actually present, either because
822 * it is for an emulated I/O device or because the corresonding
823 * host page has been paged out. Any other HDSI/HISI interrupts
824 * have been handled already.
826 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
827 r = RESUME_PAGE_FAULT;
829 case BOOK3S_INTERRUPT_H_INST_STORAGE:
830 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
831 vcpu->arch.fault_dsisr = 0;
832 r = RESUME_PAGE_FAULT;
835 * This occurs if the guest executes an illegal instruction.
836 * If the guest debug is disabled, generate a program interrupt
837 * to the guest. If guest debug is enabled, we need to check
838 * whether the instruction is a software breakpoint instruction.
839 * Accordingly return to Guest or Host.
841 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
842 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
843 r = kvmppc_emulate_debug_inst(run, vcpu);
845 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
850 * This occurs if the guest (kernel or userspace), does something that
851 * is prohibited by HFSCR. We just generate a program interrupt to
854 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
855 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
859 kvmppc_dump_regs(vcpu);
860 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
861 vcpu->arch.trap, kvmppc_get_pc(vcpu),
862 vcpu->arch.shregs.msr);
863 run->hw.hardware_exit_reason = vcpu->arch.trap;
871 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
872 struct kvm_sregs *sregs)
876 memset(sregs, 0, sizeof(struct kvm_sregs));
877 sregs->pvr = vcpu->arch.pvr;
878 for (i = 0; i < vcpu->arch.slb_max; i++) {
879 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
880 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
886 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
887 struct kvm_sregs *sregs)
891 /* Only accept the same PVR as the host's, since we can't spoof it */
892 if (sregs->pvr != vcpu->arch.pvr)
896 for (i = 0; i < vcpu->arch.slb_nr; i++) {
897 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
898 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
899 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
903 vcpu->arch.slb_max = j;
908 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
911 struct kvmppc_vcore *vc = vcpu->arch.vcore;
914 spin_lock(&vc->lock);
916 * If ILE (interrupt little-endian) has changed, update the
917 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
919 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
920 struct kvm *kvm = vcpu->kvm;
921 struct kvm_vcpu *vcpu;
924 mutex_lock(&kvm->lock);
925 kvm_for_each_vcpu(i, vcpu, kvm) {
926 if (vcpu->arch.vcore != vc)
928 if (new_lpcr & LPCR_ILE)
929 vcpu->arch.intr_msr |= MSR_LE;
931 vcpu->arch.intr_msr &= ~MSR_LE;
933 mutex_unlock(&kvm->lock);
937 * Userspace can only modify DPFD (default prefetch depth),
938 * ILE (interrupt little-endian) and TC (translation control).
939 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
941 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
942 if (cpu_has_feature(CPU_FTR_ARCH_207S))
945 /* Broken 32-bit version of LPCR must not clear top bits */
948 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
949 spin_unlock(&vc->lock);
952 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
953 union kvmppc_one_reg *val)
959 case KVM_REG_PPC_DEBUG_INST:
960 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
962 case KVM_REG_PPC_HIOR:
963 *val = get_reg_val(id, 0);
965 case KVM_REG_PPC_DABR:
966 *val = get_reg_val(id, vcpu->arch.dabr);
968 case KVM_REG_PPC_DABRX:
969 *val = get_reg_val(id, vcpu->arch.dabrx);
971 case KVM_REG_PPC_DSCR:
972 *val = get_reg_val(id, vcpu->arch.dscr);
974 case KVM_REG_PPC_PURR:
975 *val = get_reg_val(id, vcpu->arch.purr);
977 case KVM_REG_PPC_SPURR:
978 *val = get_reg_val(id, vcpu->arch.spurr);
980 case KVM_REG_PPC_AMR:
981 *val = get_reg_val(id, vcpu->arch.amr);
983 case KVM_REG_PPC_UAMOR:
984 *val = get_reg_val(id, vcpu->arch.uamor);
986 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
987 i = id - KVM_REG_PPC_MMCR0;
988 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
990 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
991 i = id - KVM_REG_PPC_PMC1;
992 *val = get_reg_val(id, vcpu->arch.pmc[i]);
994 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
995 i = id - KVM_REG_PPC_SPMC1;
996 *val = get_reg_val(id, vcpu->arch.spmc[i]);
998 case KVM_REG_PPC_SIAR:
999 *val = get_reg_val(id, vcpu->arch.siar);
1001 case KVM_REG_PPC_SDAR:
1002 *val = get_reg_val(id, vcpu->arch.sdar);
1004 case KVM_REG_PPC_SIER:
1005 *val = get_reg_val(id, vcpu->arch.sier);
1007 case KVM_REG_PPC_IAMR:
1008 *val = get_reg_val(id, vcpu->arch.iamr);
1010 case KVM_REG_PPC_PSPB:
1011 *val = get_reg_val(id, vcpu->arch.pspb);
1013 case KVM_REG_PPC_DPDES:
1014 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1016 case KVM_REG_PPC_DAWR:
1017 *val = get_reg_val(id, vcpu->arch.dawr);
1019 case KVM_REG_PPC_DAWRX:
1020 *val = get_reg_val(id, vcpu->arch.dawrx);
1022 case KVM_REG_PPC_CIABR:
1023 *val = get_reg_val(id, vcpu->arch.ciabr);
1025 case KVM_REG_PPC_CSIGR:
1026 *val = get_reg_val(id, vcpu->arch.csigr);
1028 case KVM_REG_PPC_TACR:
1029 *val = get_reg_val(id, vcpu->arch.tacr);
1031 case KVM_REG_PPC_TCSCR:
1032 *val = get_reg_val(id, vcpu->arch.tcscr);
1034 case KVM_REG_PPC_PID:
1035 *val = get_reg_val(id, vcpu->arch.pid);
1037 case KVM_REG_PPC_ACOP:
1038 *val = get_reg_val(id, vcpu->arch.acop);
1040 case KVM_REG_PPC_WORT:
1041 *val = get_reg_val(id, vcpu->arch.wort);
1043 case KVM_REG_PPC_VPA_ADDR:
1044 spin_lock(&vcpu->arch.vpa_update_lock);
1045 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1046 spin_unlock(&vcpu->arch.vpa_update_lock);
1048 case KVM_REG_PPC_VPA_SLB:
1049 spin_lock(&vcpu->arch.vpa_update_lock);
1050 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1051 val->vpaval.length = vcpu->arch.slb_shadow.len;
1052 spin_unlock(&vcpu->arch.vpa_update_lock);
1054 case KVM_REG_PPC_VPA_DTL:
1055 spin_lock(&vcpu->arch.vpa_update_lock);
1056 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1057 val->vpaval.length = vcpu->arch.dtl.len;
1058 spin_unlock(&vcpu->arch.vpa_update_lock);
1060 case KVM_REG_PPC_TB_OFFSET:
1061 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1063 case KVM_REG_PPC_LPCR:
1064 case KVM_REG_PPC_LPCR_64:
1065 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1067 case KVM_REG_PPC_PPR:
1068 *val = get_reg_val(id, vcpu->arch.ppr);
1070 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1071 case KVM_REG_PPC_TFHAR:
1072 *val = get_reg_val(id, vcpu->arch.tfhar);
1074 case KVM_REG_PPC_TFIAR:
1075 *val = get_reg_val(id, vcpu->arch.tfiar);
1077 case KVM_REG_PPC_TEXASR:
1078 *val = get_reg_val(id, vcpu->arch.texasr);
1080 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1081 i = id - KVM_REG_PPC_TM_GPR0;
1082 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1084 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1087 i = id - KVM_REG_PPC_TM_VSR0;
1089 for (j = 0; j < TS_FPRWIDTH; j++)
1090 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1092 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1093 val->vval = vcpu->arch.vr_tm.vr[i-32];
1099 case KVM_REG_PPC_TM_CR:
1100 *val = get_reg_val(id, vcpu->arch.cr_tm);
1102 case KVM_REG_PPC_TM_LR:
1103 *val = get_reg_val(id, vcpu->arch.lr_tm);
1105 case KVM_REG_PPC_TM_CTR:
1106 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1108 case KVM_REG_PPC_TM_FPSCR:
1109 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1111 case KVM_REG_PPC_TM_AMR:
1112 *val = get_reg_val(id, vcpu->arch.amr_tm);
1114 case KVM_REG_PPC_TM_PPR:
1115 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1117 case KVM_REG_PPC_TM_VRSAVE:
1118 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1120 case KVM_REG_PPC_TM_VSCR:
1121 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1122 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1126 case KVM_REG_PPC_TM_DSCR:
1127 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1129 case KVM_REG_PPC_TM_TAR:
1130 *val = get_reg_val(id, vcpu->arch.tar_tm);
1133 case KVM_REG_PPC_ARCH_COMPAT:
1134 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1144 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1145 union kvmppc_one_reg *val)
1149 unsigned long addr, len;
1152 case KVM_REG_PPC_HIOR:
1153 /* Only allow this to be set to zero */
1154 if (set_reg_val(id, *val))
1157 case KVM_REG_PPC_DABR:
1158 vcpu->arch.dabr = set_reg_val(id, *val);
1160 case KVM_REG_PPC_DABRX:
1161 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1163 case KVM_REG_PPC_DSCR:
1164 vcpu->arch.dscr = set_reg_val(id, *val);
1166 case KVM_REG_PPC_PURR:
1167 vcpu->arch.purr = set_reg_val(id, *val);
1169 case KVM_REG_PPC_SPURR:
1170 vcpu->arch.spurr = set_reg_val(id, *val);
1172 case KVM_REG_PPC_AMR:
1173 vcpu->arch.amr = set_reg_val(id, *val);
1175 case KVM_REG_PPC_UAMOR:
1176 vcpu->arch.uamor = set_reg_val(id, *val);
1178 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1179 i = id - KVM_REG_PPC_MMCR0;
1180 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1182 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1183 i = id - KVM_REG_PPC_PMC1;
1184 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1186 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1187 i = id - KVM_REG_PPC_SPMC1;
1188 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1190 case KVM_REG_PPC_SIAR:
1191 vcpu->arch.siar = set_reg_val(id, *val);
1193 case KVM_REG_PPC_SDAR:
1194 vcpu->arch.sdar = set_reg_val(id, *val);
1196 case KVM_REG_PPC_SIER:
1197 vcpu->arch.sier = set_reg_val(id, *val);
1199 case KVM_REG_PPC_IAMR:
1200 vcpu->arch.iamr = set_reg_val(id, *val);
1202 case KVM_REG_PPC_PSPB:
1203 vcpu->arch.pspb = set_reg_val(id, *val);
1205 case KVM_REG_PPC_DPDES:
1206 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1208 case KVM_REG_PPC_DAWR:
1209 vcpu->arch.dawr = set_reg_val(id, *val);
1211 case KVM_REG_PPC_DAWRX:
1212 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1214 case KVM_REG_PPC_CIABR:
1215 vcpu->arch.ciabr = set_reg_val(id, *val);
1216 /* Don't allow setting breakpoints in hypervisor code */
1217 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1218 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1220 case KVM_REG_PPC_CSIGR:
1221 vcpu->arch.csigr = set_reg_val(id, *val);
1223 case KVM_REG_PPC_TACR:
1224 vcpu->arch.tacr = set_reg_val(id, *val);
1226 case KVM_REG_PPC_TCSCR:
1227 vcpu->arch.tcscr = set_reg_val(id, *val);
1229 case KVM_REG_PPC_PID:
1230 vcpu->arch.pid = set_reg_val(id, *val);
1232 case KVM_REG_PPC_ACOP:
1233 vcpu->arch.acop = set_reg_val(id, *val);
1235 case KVM_REG_PPC_WORT:
1236 vcpu->arch.wort = set_reg_val(id, *val);
1238 case KVM_REG_PPC_VPA_ADDR:
1239 addr = set_reg_val(id, *val);
1241 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1242 vcpu->arch.dtl.next_gpa))
1244 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1246 case KVM_REG_PPC_VPA_SLB:
1247 addr = val->vpaval.addr;
1248 len = val->vpaval.length;
1250 if (addr && !vcpu->arch.vpa.next_gpa)
1252 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1254 case KVM_REG_PPC_VPA_DTL:
1255 addr = val->vpaval.addr;
1256 len = val->vpaval.length;
1258 if (addr && (len < sizeof(struct dtl_entry) ||
1259 !vcpu->arch.vpa.next_gpa))
1261 len -= len % sizeof(struct dtl_entry);
1262 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1264 case KVM_REG_PPC_TB_OFFSET:
1265 /* round up to multiple of 2^24 */
1266 vcpu->arch.vcore->tb_offset =
1267 ALIGN(set_reg_val(id, *val), 1UL << 24);
1269 case KVM_REG_PPC_LPCR:
1270 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1272 case KVM_REG_PPC_LPCR_64:
1273 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
1275 case KVM_REG_PPC_PPR:
1276 vcpu->arch.ppr = set_reg_val(id, *val);
1278 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1279 case KVM_REG_PPC_TFHAR:
1280 vcpu->arch.tfhar = set_reg_val(id, *val);
1282 case KVM_REG_PPC_TFIAR:
1283 vcpu->arch.tfiar = set_reg_val(id, *val);
1285 case KVM_REG_PPC_TEXASR:
1286 vcpu->arch.texasr = set_reg_val(id, *val);
1288 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1289 i = id - KVM_REG_PPC_TM_GPR0;
1290 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1292 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1295 i = id - KVM_REG_PPC_TM_VSR0;
1297 for (j = 0; j < TS_FPRWIDTH; j++)
1298 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1300 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1301 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1306 case KVM_REG_PPC_TM_CR:
1307 vcpu->arch.cr_tm = set_reg_val(id, *val);
1309 case KVM_REG_PPC_TM_LR:
1310 vcpu->arch.lr_tm = set_reg_val(id, *val);
1312 case KVM_REG_PPC_TM_CTR:
1313 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1315 case KVM_REG_PPC_TM_FPSCR:
1316 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1318 case KVM_REG_PPC_TM_AMR:
1319 vcpu->arch.amr_tm = set_reg_val(id, *val);
1321 case KVM_REG_PPC_TM_PPR:
1322 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1324 case KVM_REG_PPC_TM_VRSAVE:
1325 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1327 case KVM_REG_PPC_TM_VSCR:
1328 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1329 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1333 case KVM_REG_PPC_TM_DSCR:
1334 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1336 case KVM_REG_PPC_TM_TAR:
1337 vcpu->arch.tar_tm = set_reg_val(id, *val);
1340 case KVM_REG_PPC_ARCH_COMPAT:
1341 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1351 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1353 struct kvmppc_vcore *vcore;
1355 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1360 INIT_LIST_HEAD(&vcore->runnable_threads);
1361 spin_lock_init(&vcore->lock);
1362 init_waitqueue_head(&vcore->wq);
1363 vcore->preempt_tb = TB_NIL;
1364 vcore->lpcr = kvm->arch.lpcr;
1365 vcore->first_vcpuid = core * threads_per_subcore;
1368 vcore->mpp_buffer_is_valid = false;
1370 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1371 vcore->mpp_buffer = (void *)__get_free_pages(
1372 GFP_KERNEL|__GFP_ZERO,
1378 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1381 struct kvm_vcpu *vcpu;
1384 struct kvmppc_vcore *vcore;
1386 core = id / threads_per_subcore;
1387 if (core >= KVM_MAX_VCORES)
1391 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1395 err = kvm_vcpu_init(vcpu, kvm, id);
1399 vcpu->arch.shared = &vcpu->arch.shregs;
1400 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1402 * The shared struct is never shared on HV,
1403 * so we can always use host endianness
1405 #ifdef __BIG_ENDIAN__
1406 vcpu->arch.shared_big_endian = true;
1408 vcpu->arch.shared_big_endian = false;
1411 vcpu->arch.mmcr[0] = MMCR0_FC;
1412 vcpu->arch.ctrl = CTRL_RUNLATCH;
1413 /* default to host PVR, since we can't spoof it */
1414 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1415 spin_lock_init(&vcpu->arch.vpa_update_lock);
1416 spin_lock_init(&vcpu->arch.tbacct_lock);
1417 vcpu->arch.busy_preempt = TB_NIL;
1418 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1420 kvmppc_mmu_book3s_hv_init(vcpu);
1422 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1424 init_waitqueue_head(&vcpu->arch.cpu_run);
1426 mutex_lock(&kvm->lock);
1427 vcore = kvm->arch.vcores[core];
1429 vcore = kvmppc_vcore_create(kvm, core);
1430 kvm->arch.vcores[core] = vcore;
1431 kvm->arch.online_vcores++;
1433 mutex_unlock(&kvm->lock);
1438 spin_lock(&vcore->lock);
1439 ++vcore->num_threads;
1440 spin_unlock(&vcore->lock);
1441 vcpu->arch.vcore = vcore;
1442 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1444 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1445 kvmppc_sanity_check(vcpu);
1450 kmem_cache_free(kvm_vcpu_cache, vcpu);
1452 return ERR_PTR(err);
1455 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1457 if (vpa->pinned_addr)
1458 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1462 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1464 spin_lock(&vcpu->arch.vpa_update_lock);
1465 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1466 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1467 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1468 spin_unlock(&vcpu->arch.vpa_update_lock);
1469 kvm_vcpu_uninit(vcpu);
1470 kmem_cache_free(kvm_vcpu_cache, vcpu);
1473 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1475 /* Indicate we want to get back into the guest */
1479 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1481 unsigned long dec_nsec, now;
1484 if (now > vcpu->arch.dec_expires) {
1485 /* decrementer has already gone negative */
1486 kvmppc_core_queue_dec(vcpu);
1487 kvmppc_core_prepare_to_enter(vcpu);
1490 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1492 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1494 vcpu->arch.timer_running = 1;
1497 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1499 vcpu->arch.ceded = 0;
1500 if (vcpu->arch.timer_running) {
1501 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1502 vcpu->arch.timer_running = 0;
1506 extern void __kvmppc_vcore_entry(void);
1508 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1509 struct kvm_vcpu *vcpu)
1513 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1515 spin_lock_irq(&vcpu->arch.tbacct_lock);
1517 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1518 vcpu->arch.stolen_logged;
1519 vcpu->arch.busy_preempt = now;
1520 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1521 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1523 list_del(&vcpu->arch.run_list);
1526 static int kvmppc_grab_hwthread(int cpu)
1528 struct paca_struct *tpaca;
1529 long timeout = 10000;
1533 /* Ensure the thread won't go into the kernel if it wakes */
1534 tpaca->kvm_hstate.hwthread_req = 1;
1535 tpaca->kvm_hstate.kvm_vcpu = NULL;
1538 * If the thread is already executing in the kernel (e.g. handling
1539 * a stray interrupt), wait for it to get back to nap mode.
1540 * The smp_mb() is to ensure that our setting of hwthread_req
1541 * is visible before we look at hwthread_state, so if this
1542 * races with the code at system_reset_pSeries and the thread
1543 * misses our setting of hwthread_req, we are sure to see its
1544 * setting of hwthread_state, and vice versa.
1547 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1548 if (--timeout <= 0) {
1549 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1557 static void kvmppc_release_hwthread(int cpu)
1559 struct paca_struct *tpaca;
1562 tpaca->kvm_hstate.hwthread_req = 0;
1563 tpaca->kvm_hstate.kvm_vcpu = NULL;
1566 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1569 struct paca_struct *tpaca;
1570 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1572 if (vcpu->arch.timer_running) {
1573 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1574 vcpu->arch.timer_running = 0;
1576 cpu = vc->pcpu + vcpu->arch.ptid;
1578 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1579 tpaca->kvm_hstate.kvm_vcore = vc;
1580 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1581 vcpu->cpu = vc->pcpu;
1583 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1584 if (cpu != smp_processor_id()) {
1586 if (vcpu->arch.ptid)
1592 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1598 while (vc->nap_count < vc->n_woken) {
1599 if (++i >= 1000000) {
1600 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1601 vc->nap_count, vc->n_woken);
1610 * Check that we are on thread 0 and that any other threads in
1611 * this core are off-line. Then grab the threads so they can't
1614 static int on_primary_thread(void)
1616 int cpu = smp_processor_id();
1619 /* Are we on a primary subcore? */
1620 if (cpu_thread_in_subcore(cpu))
1624 while (++thr < threads_per_subcore)
1625 if (cpu_online(cpu + thr))
1628 /* Grab all hw threads so they can't go into the kernel */
1629 for (thr = 1; thr < threads_per_subcore; ++thr) {
1630 if (kvmppc_grab_hwthread(cpu + thr)) {
1631 /* Couldn't grab one; let the others go */
1633 kvmppc_release_hwthread(cpu + thr);
1634 } while (--thr > 0);
1641 static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1643 phys_addr_t phy_addr, mpp_addr;
1645 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1646 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1648 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1649 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1651 vc->mpp_buffer_is_valid = true;
1654 static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1656 phys_addr_t phy_addr, mpp_addr;
1658 phy_addr = virt_to_phys(vc->mpp_buffer);
1659 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1661 /* We must abort any in-progress save operations to ensure
1662 * the table is valid so that prefetch engine knows when to
1663 * stop prefetching. */
1664 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1665 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1669 * Run a set of guest threads on a physical core.
1670 * Called with vc->lock held.
1672 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1674 struct kvm_vcpu *vcpu, *vnext;
1677 int i, need_vpa_update;
1679 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1681 /* don't start if any threads have a signal pending */
1682 need_vpa_update = 0;
1683 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1684 if (signal_pending(vcpu->arch.run_task))
1686 if (vcpu->arch.vpa.update_pending ||
1687 vcpu->arch.slb_shadow.update_pending ||
1688 vcpu->arch.dtl.update_pending)
1689 vcpus_to_update[need_vpa_update++] = vcpu;
1693 * Initialize *vc, in particular vc->vcore_state, so we can
1694 * drop the vcore lock if necessary.
1698 vc->entry_exit_count = 0;
1699 vc->vcore_state = VCORE_STARTING;
1701 vc->napping_threads = 0;
1704 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1705 * which can't be called with any spinlocks held.
1707 if (need_vpa_update) {
1708 spin_unlock(&vc->lock);
1709 for (i = 0; i < need_vpa_update; ++i)
1710 kvmppc_update_vpas(vcpus_to_update[i]);
1711 spin_lock(&vc->lock);
1715 * Make sure we are running on primary threads, and that secondary
1716 * threads are offline. Also check if the number of threads in this
1717 * guest are greater than the current system threads per guest.
1719 if ((threads_per_core > 1) &&
1720 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
1721 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1722 vcpu->arch.ret = -EBUSY;
1727 vc->pcpu = smp_processor_id();
1728 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1729 kvmppc_start_thread(vcpu);
1730 kvmppc_create_dtl_entry(vcpu, vc);
1733 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1734 get_paca()->kvm_hstate.kvm_vcore = vc;
1735 get_paca()->kvm_hstate.ptid = 0;
1737 vc->vcore_state = VCORE_RUNNING;
1739 spin_unlock(&vc->lock);
1743 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1745 if (vc->mpp_buffer_is_valid)
1746 kvmppc_start_restoring_l2_cache(vc);
1748 __kvmppc_vcore_entry();
1750 spin_lock(&vc->lock);
1753 kvmppc_start_saving_l2_cache(vc);
1755 /* disable sending of IPIs on virtual external irqs */
1756 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1758 /* wait for secondary threads to finish writing their state to memory */
1759 if (vc->nap_count < vc->n_woken)
1760 kvmppc_wait_for_nap(vc);
1761 for (i = 0; i < threads_per_subcore; ++i)
1762 kvmppc_release_hwthread(vc->pcpu + i);
1763 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1764 vc->vcore_state = VCORE_EXITING;
1765 spin_unlock(&vc->lock);
1767 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1769 /* make sure updates to secondary vcpu structs are visible now */
1776 spin_lock(&vc->lock);
1778 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1779 /* cancel pending dec exception if dec is positive */
1780 if (now < vcpu->arch.dec_expires &&
1781 kvmppc_core_pending_dec(vcpu))
1782 kvmppc_core_dequeue_dec(vcpu);
1785 if (vcpu->arch.trap)
1786 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1787 vcpu->arch.run_task);
1789 vcpu->arch.ret = ret;
1790 vcpu->arch.trap = 0;
1792 if (vcpu->arch.ceded) {
1793 if (!is_kvmppc_resume_guest(ret))
1794 kvmppc_end_cede(vcpu);
1796 kvmppc_set_timer(vcpu);
1801 vc->vcore_state = VCORE_INACTIVE;
1802 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1804 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
1805 kvmppc_remove_runnable(vc, vcpu);
1806 wake_up(&vcpu->arch.cpu_run);
1812 * Wait for some other vcpu thread to execute us, and
1813 * wake us up when we need to handle something in the host.
1815 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1819 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1820 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1822 finish_wait(&vcpu->arch.cpu_run, &wait);
1826 * All the vcpus in this vcore are idle, so wait for a decrementer
1827 * or external interrupt to one of the vcpus. vc->lock is held.
1829 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1831 struct kvm_vcpu *vcpu;
1836 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1839 * Check one last time for pending exceptions and ceded state after
1840 * we put ourselves on the wait queue
1842 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1843 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
1850 finish_wait(&vc->wq, &wait);
1854 vc->vcore_state = VCORE_SLEEPING;
1855 spin_unlock(&vc->lock);
1857 finish_wait(&vc->wq, &wait);
1858 spin_lock(&vc->lock);
1859 vc->vcore_state = VCORE_INACTIVE;
1862 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1865 struct kvmppc_vcore *vc;
1866 struct kvm_vcpu *v, *vn;
1868 kvm_run->exit_reason = 0;
1869 vcpu->arch.ret = RESUME_GUEST;
1870 vcpu->arch.trap = 0;
1871 kvmppc_update_vpas(vcpu);
1874 * Synchronize with other threads in this virtual core
1876 vc = vcpu->arch.vcore;
1877 spin_lock(&vc->lock);
1878 vcpu->arch.ceded = 0;
1879 vcpu->arch.run_task = current;
1880 vcpu->arch.kvm_run = kvm_run;
1881 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1882 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1883 vcpu->arch.busy_preempt = TB_NIL;
1884 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1888 * This happens the first time this is called for a vcpu.
1889 * If the vcore is already running, we may be able to start
1890 * this thread straight away and have it join in.
1892 if (!signal_pending(current)) {
1893 if (vc->vcore_state == VCORE_RUNNING &&
1894 VCORE_EXIT_COUNT(vc) == 0) {
1895 kvmppc_create_dtl_entry(vcpu, vc);
1896 kvmppc_start_thread(vcpu);
1897 } else if (vc->vcore_state == VCORE_SLEEPING) {
1903 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1904 !signal_pending(current)) {
1905 if (vc->vcore_state != VCORE_INACTIVE) {
1906 spin_unlock(&vc->lock);
1907 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1908 spin_lock(&vc->lock);
1911 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1913 kvmppc_core_prepare_to_enter(v);
1914 if (signal_pending(v->arch.run_task)) {
1915 kvmppc_remove_runnable(vc, v);
1916 v->stat.signal_exits++;
1917 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1918 v->arch.ret = -EINTR;
1919 wake_up(&v->arch.cpu_run);
1922 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1926 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1927 if (!v->arch.pending_exceptions)
1928 n_ceded += v->arch.ceded;
1932 if (n_ceded == vc->n_runnable)
1933 kvmppc_vcore_blocked(vc);
1935 kvmppc_run_core(vc);
1939 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1940 (vc->vcore_state == VCORE_RUNNING ||
1941 vc->vcore_state == VCORE_EXITING)) {
1942 spin_unlock(&vc->lock);
1943 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1944 spin_lock(&vc->lock);
1947 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1948 kvmppc_remove_runnable(vc, vcpu);
1949 vcpu->stat.signal_exits++;
1950 kvm_run->exit_reason = KVM_EXIT_INTR;
1951 vcpu->arch.ret = -EINTR;
1954 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1955 /* Wake up some vcpu to run the core */
1956 v = list_first_entry(&vc->runnable_threads,
1957 struct kvm_vcpu, arch.run_list);
1958 wake_up(&v->arch.cpu_run);
1961 spin_unlock(&vc->lock);
1962 return vcpu->arch.ret;
1965 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1970 if (!vcpu->arch.sane) {
1971 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1975 kvmppc_core_prepare_to_enter(vcpu);
1977 /* No need to go into the guest when all we'll do is come back out */
1978 if (signal_pending(current)) {
1979 run->exit_reason = KVM_EXIT_INTR;
1983 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1984 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1987 /* On the first time here, set up HTAB and VRMA or RMA */
1988 if (!vcpu->kvm->arch.rma_setup_done) {
1989 r = kvmppc_hv_setup_htab_rma(vcpu);
1994 flush_fp_to_thread(current);
1995 flush_altivec_to_thread(current);
1996 flush_vsx_to_thread(current);
1997 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1998 vcpu->arch.pgdir = current->mm->pgd;
1999 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
2002 r = kvmppc_run_vcpu(run, vcpu);
2004 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2005 !(vcpu->arch.shregs.msr & MSR_PR)) {
2006 r = kvmppc_pseries_do_hcall(vcpu);
2007 kvmppc_core_prepare_to_enter(vcpu);
2008 } else if (r == RESUME_PAGE_FAULT) {
2009 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2010 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2011 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2012 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2014 } while (is_kvmppc_resume_guest(r));
2017 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
2018 atomic_dec(&vcpu->kvm->arch.vcpus_running);
2023 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
2024 Assumes POWER7 or PPC970. */
2025 static inline int lpcr_rmls(unsigned long rma_size)
2028 case 32ul << 20: /* 32 MB */
2029 if (cpu_has_feature(CPU_FTR_ARCH_206))
2030 return 8; /* only supported on POWER7 */
2032 case 64ul << 20: /* 64 MB */
2034 case 128ul << 20: /* 128 MB */
2036 case 256ul << 20: /* 256 MB */
2038 case 1ul << 30: /* 1 GB */
2040 case 16ul << 30: /* 16 GB */
2042 case 256ul << 30: /* 256 GB */
2049 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2052 struct kvm_rma_info *ri = vma->vm_file->private_data;
2054 if (vmf->pgoff >= kvm_rma_pages)
2055 return VM_FAULT_SIGBUS;
2057 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
2063 static const struct vm_operations_struct kvm_rma_vm_ops = {
2064 .fault = kvm_rma_fault,
2067 static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
2069 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2070 vma->vm_ops = &kvm_rma_vm_ops;
2074 static int kvm_rma_release(struct inode *inode, struct file *filp)
2076 struct kvm_rma_info *ri = filp->private_data;
2078 kvm_release_rma(ri);
2082 static const struct file_operations kvm_rma_fops = {
2083 .mmap = kvm_rma_mmap,
2084 .release = kvm_rma_release,
2087 static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
2088 struct kvm_allocate_rma *ret)
2091 struct kvm_rma_info *ri;
2093 * Only do this on PPC970 in HV mode
2095 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2096 !cpu_has_feature(CPU_FTR_ARCH_201))
2102 ri = kvm_alloc_rma();
2106 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
2108 kvm_release_rma(ri);
2110 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
2114 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2117 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2121 (*sps)->page_shift = def->shift;
2122 (*sps)->slb_enc = def->sllp;
2123 (*sps)->enc[0].page_shift = def->shift;
2124 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
2126 * Add 16MB MPSS support if host supports it
2128 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2129 (*sps)->enc[1].page_shift = 24;
2130 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2135 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2136 struct kvm_ppc_smmu_info *info)
2138 struct kvm_ppc_one_seg_page_size *sps;
2140 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2141 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2142 info->flags |= KVM_PPC_1T_SEGMENTS;
2143 info->slb_size = mmu_slb_size;
2145 /* We only support these sizes for now, and no muti-size segments */
2146 sps = &info->sps[0];
2147 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2148 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2149 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2155 * Get (and clear) the dirty memory log for a memory slot.
2157 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2158 struct kvm_dirty_log *log)
2160 struct kvm_memory_slot *memslot;
2164 mutex_lock(&kvm->slots_lock);
2167 if (log->slot >= KVM_USER_MEM_SLOTS)
2170 memslot = id_to_memslot(kvm->memslots, log->slot);
2172 if (!memslot->dirty_bitmap)
2175 n = kvm_dirty_bitmap_bytes(memslot);
2176 memset(memslot->dirty_bitmap, 0, n);
2178 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
2183 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2188 mutex_unlock(&kvm->slots_lock);
2192 static void unpin_slot(struct kvm_memory_slot *memslot)
2194 unsigned long *physp;
2195 unsigned long j, npages, pfn;
2198 physp = memslot->arch.slot_phys;
2199 npages = memslot->npages;
2202 for (j = 0; j < npages; j++) {
2203 if (!(physp[j] & KVMPPC_GOT_PAGE))
2205 pfn = physp[j] >> PAGE_SHIFT;
2206 page = pfn_to_page(pfn);
2212 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2213 struct kvm_memory_slot *dont)
2215 if (!dont || free->arch.rmap != dont->arch.rmap) {
2216 vfree(free->arch.rmap);
2217 free->arch.rmap = NULL;
2219 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
2221 vfree(free->arch.slot_phys);
2222 free->arch.slot_phys = NULL;
2226 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2227 unsigned long npages)
2229 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2230 if (!slot->arch.rmap)
2232 slot->arch.slot_phys = NULL;
2237 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2238 struct kvm_memory_slot *memslot,
2239 struct kvm_userspace_memory_region *mem)
2241 unsigned long *phys;
2243 /* Allocate a slot_phys array if needed */
2244 phys = memslot->arch.slot_phys;
2245 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
2246 phys = vzalloc(memslot->npages * sizeof(unsigned long));
2249 memslot->arch.slot_phys = phys;
2255 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2256 struct kvm_userspace_memory_region *mem,
2257 const struct kvm_memory_slot *old)
2259 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2260 struct kvm_memory_slot *memslot;
2262 if (npages && old->npages) {
2264 * If modifying a memslot, reset all the rmap dirty bits.
2265 * If this is a new memslot, we don't need to do anything
2266 * since the rmap array starts out as all zeroes,
2267 * i.e. no pages are dirty.
2269 memslot = id_to_memslot(kvm->memslots, mem->slot);
2270 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2275 * Update LPCR values in kvm->arch and in vcores.
2276 * Caller must hold kvm->lock.
2278 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2283 if ((kvm->arch.lpcr & mask) == lpcr)
2286 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2288 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2289 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2292 spin_lock(&vc->lock);
2293 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2294 spin_unlock(&vc->lock);
2295 if (++cores_done >= kvm->arch.online_vcores)
2300 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2305 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2308 struct kvm *kvm = vcpu->kvm;
2309 struct kvm_rma_info *ri = NULL;
2311 struct kvm_memory_slot *memslot;
2312 struct vm_area_struct *vma;
2313 unsigned long lpcr = 0, senc;
2314 unsigned long lpcr_mask = 0;
2315 unsigned long psize, porder;
2316 unsigned long rma_size;
2318 unsigned long *physp;
2319 unsigned long i, npages;
2322 mutex_lock(&kvm->lock);
2323 if (kvm->arch.rma_setup_done)
2324 goto out; /* another vcpu beat us to it */
2326 /* Allocate hashed page table (if not done already) and reset it */
2327 if (!kvm->arch.hpt_virt) {
2328 err = kvmppc_alloc_hpt(kvm, NULL);
2330 pr_err("KVM: Couldn't alloc HPT\n");
2335 /* Look up the memslot for guest physical address 0 */
2336 srcu_idx = srcu_read_lock(&kvm->srcu);
2337 memslot = gfn_to_memslot(kvm, 0);
2339 /* We must have some memory at 0 by now */
2341 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2344 /* Look up the VMA for the start of this memory slot */
2345 hva = memslot->userspace_addr;
2346 down_read(¤t->mm->mmap_sem);
2347 vma = find_vma(current->mm, hva);
2348 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2351 psize = vma_kernel_pagesize(vma);
2352 porder = __ilog2(psize);
2354 /* Is this one of our preallocated RMAs? */
2355 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2356 hva == vma->vm_start)
2357 ri = vma->vm_file->private_data;
2359 up_read(¤t->mm->mmap_sem);
2362 /* On POWER7, use VRMA; on PPC970, give up */
2364 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2365 pr_err("KVM: CPU requires an RMO\n");
2369 /* We can handle 4k, 64k or 16M pages in the VRMA */
2371 if (!(psize == 0x1000 || psize == 0x10000 ||
2372 psize == 0x1000000))
2375 /* Update VRMASD field in the LPCR */
2376 senc = slb_pgsize_encoding(psize);
2377 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2378 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2379 lpcr_mask = LPCR_VRMASD;
2380 /* the -4 is to account for senc values starting at 0x10 */
2381 lpcr = senc << (LPCR_VRMASD_SH - 4);
2383 /* Create HPTEs in the hash page table for the VRMA */
2384 kvmppc_map_vrma(vcpu, memslot, porder);
2387 /* Set up to use an RMO region */
2388 rma_size = kvm_rma_pages;
2389 if (rma_size > memslot->npages)
2390 rma_size = memslot->npages;
2391 rma_size <<= PAGE_SHIFT;
2392 rmls = lpcr_rmls(rma_size);
2394 if ((long)rmls < 0) {
2395 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
2398 atomic_inc(&ri->use_count);
2401 /* Update LPCR and RMOR */
2402 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2403 /* PPC970; insert RMLS value (split field) in HID4 */
2404 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2405 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2406 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
2407 ((rmls & 3) << HID4_RMLS2_SH);
2408 /* RMOR is also in HID4 */
2409 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2413 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2414 lpcr = rmls << LPCR_RMLS_SH;
2415 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
2417 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2418 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
2420 /* Initialize phys addrs of pages in RMO */
2421 npages = kvm_rma_pages;
2422 porder = __ilog2(npages);
2423 physp = memslot->arch.slot_phys;
2425 if (npages > memslot->npages)
2426 npages = memslot->npages;
2427 spin_lock(&kvm->arch.slot_phys_lock);
2428 for (i = 0; i < npages; ++i)
2429 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2431 spin_unlock(&kvm->arch.slot_phys_lock);
2435 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2437 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2439 kvm->arch.rma_setup_done = 1;
2442 srcu_read_unlock(&kvm->srcu, srcu_idx);
2444 mutex_unlock(&kvm->lock);
2448 up_read(¤t->mm->mmap_sem);
2452 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2454 unsigned long lpcr, lpid;
2456 /* Allocate the guest's logical partition ID */
2458 lpid = kvmppc_alloc_lpid();
2461 kvm->arch.lpid = lpid;
2464 * Since we don't flush the TLB when tearing down a VM,
2465 * and this lpid might have previously been used,
2466 * make sure we flush on each core before running the new VM.
2468 cpumask_setall(&kvm->arch.need_tlb_flush);
2470 /* Start out with the default set of hcalls enabled */
2471 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2472 sizeof(kvm->arch.enabled_hcalls));
2474 kvm->arch.rma = NULL;
2476 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2478 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2479 /* PPC970; HID4 is effectively the LPCR */
2480 kvm->arch.host_lpid = 0;
2481 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2482 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2483 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2484 ((lpid & 0xf) << HID4_LPID5_SH);
2486 /* POWER7; init LPCR for virtual RMA mode */
2487 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2488 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2489 lpcr &= LPCR_PECE | LPCR_LPES;
2490 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2491 LPCR_VPM0 | LPCR_VPM1;
2492 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2493 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2494 /* On POWER8 turn on online bit to enable PURR/SPURR */
2495 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2498 kvm->arch.lpcr = lpcr;
2500 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
2501 spin_lock_init(&kvm->arch.slot_phys_lock);
2504 * Track that we now have a HV mode VM active. This blocks secondary
2505 * CPU threads from coming online.
2507 kvm_hv_vm_activated();
2512 static void kvmppc_free_vcores(struct kvm *kvm)
2516 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2517 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2518 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2519 free_pages((unsigned long)vc->mpp_buffer,
2522 kfree(kvm->arch.vcores[i]);
2524 kvm->arch.online_vcores = 0;
2527 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2529 kvm_hv_vm_deactivated();
2531 kvmppc_free_vcores(kvm);
2532 if (kvm->arch.rma) {
2533 kvm_release_rma(kvm->arch.rma);
2534 kvm->arch.rma = NULL;
2537 kvmppc_free_hpt(kvm);
2540 /* We don't need to emulate any privileged instructions or dcbz */
2541 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2542 unsigned int inst, int *advance)
2544 return EMULATE_FAIL;
2547 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2550 return EMULATE_FAIL;
2553 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2556 return EMULATE_FAIL;
2559 static int kvmppc_core_check_processor_compat_hv(void)
2561 if (!cpu_has_feature(CPU_FTR_HVMODE))
2566 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2567 unsigned int ioctl, unsigned long arg)
2569 struct kvm *kvm __maybe_unused = filp->private_data;
2570 void __user *argp = (void __user *)arg;
2575 case KVM_ALLOCATE_RMA: {
2576 struct kvm_allocate_rma rma;
2577 struct kvm *kvm = filp->private_data;
2579 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2580 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2585 case KVM_PPC_ALLOCATE_HTAB: {
2589 if (get_user(htab_order, (u32 __user *)argp))
2591 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2595 if (put_user(htab_order, (u32 __user *)argp))
2601 case KVM_PPC_GET_HTAB_FD: {
2602 struct kvm_get_htab_fd ghf;
2605 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2607 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2619 * List of hcall numbers to enable by default.
2620 * For compatibility with old userspace, we enable by default
2621 * all hcalls that were implemented before the hcall-enabling
2622 * facility was added. Note this list should not include H_RTAS.
2624 static unsigned int default_hcall_list[] = {
2638 #ifdef CONFIG_KVM_XICS
2649 static void init_default_hcalls(void)
2654 for (i = 0; default_hcall_list[i]; ++i) {
2655 hcall = default_hcall_list[i];
2656 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2657 __set_bit(hcall / 4, default_enabled_hcalls);
2661 static struct kvmppc_ops kvm_ops_hv = {
2662 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2663 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2664 .get_one_reg = kvmppc_get_one_reg_hv,
2665 .set_one_reg = kvmppc_set_one_reg_hv,
2666 .vcpu_load = kvmppc_core_vcpu_load_hv,
2667 .vcpu_put = kvmppc_core_vcpu_put_hv,
2668 .set_msr = kvmppc_set_msr_hv,
2669 .vcpu_run = kvmppc_vcpu_run_hv,
2670 .vcpu_create = kvmppc_core_vcpu_create_hv,
2671 .vcpu_free = kvmppc_core_vcpu_free_hv,
2672 .check_requests = kvmppc_core_check_requests_hv,
2673 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2674 .flush_memslot = kvmppc_core_flush_memslot_hv,
2675 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2676 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2677 .unmap_hva = kvm_unmap_hva_hv,
2678 .unmap_hva_range = kvm_unmap_hva_range_hv,
2679 .age_hva = kvm_age_hva_hv,
2680 .test_age_hva = kvm_test_age_hva_hv,
2681 .set_spte_hva = kvm_set_spte_hva_hv,
2682 .mmu_destroy = kvmppc_mmu_destroy_hv,
2683 .free_memslot = kvmppc_core_free_memslot_hv,
2684 .create_memslot = kvmppc_core_create_memslot_hv,
2685 .init_vm = kvmppc_core_init_vm_hv,
2686 .destroy_vm = kvmppc_core_destroy_vm_hv,
2687 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2688 .emulate_op = kvmppc_core_emulate_op_hv,
2689 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2690 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2691 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2692 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2693 .hcall_implemented = kvmppc_hcall_impl_hv,
2696 static int kvmppc_book3s_init_hv(void)
2700 * FIXME!! Do we need to check on all cpus ?
2702 r = kvmppc_core_check_processor_compat_hv();
2706 kvm_ops_hv.owner = THIS_MODULE;
2707 kvmppc_hv_ops = &kvm_ops_hv;
2709 init_default_hcalls();
2711 r = kvmppc_mmu_hv_init();
2715 static void kvmppc_book3s_exit_hv(void)
2717 kvmppc_hv_ops = NULL;
2720 module_init(kvmppc_book3s_init_hv);
2721 module_exit(kvmppc_book3s_exit_hv);
2722 MODULE_LICENSE("GPL");
2723 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2724 MODULE_ALIAS("devname:kvm");