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 case BOOK3S_INTERRUPT_PERFMON:
775 case BOOK3S_INTERRUPT_MACHINE_CHECK:
777 * Deliver a machine check interrupt to the guest.
778 * We have to do this, even if the host has handled the
779 * machine check, because machine checks use SRR0/1 and
780 * the interrupt might have trashed guest state in them.
782 kvmppc_book3s_queue_irqprio(vcpu,
783 BOOK3S_INTERRUPT_MACHINE_CHECK);
786 case BOOK3S_INTERRUPT_PROGRAM:
790 * Normally program interrupts are delivered directly
791 * to the guest by the hardware, but we can get here
792 * as a result of a hypervisor emulation interrupt
793 * (e40) getting turned into a 700 by BML RTAS.
795 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
796 kvmppc_core_queue_program(vcpu, flags);
800 case BOOK3S_INTERRUPT_SYSCALL:
802 /* hcall - punt to userspace */
805 /* hypercall with MSR_PR has already been handled in rmode,
806 * and never reaches here.
809 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
810 for (i = 0; i < 9; ++i)
811 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
812 run->exit_reason = KVM_EXIT_PAPR_HCALL;
813 vcpu->arch.hcall_needed = 1;
818 * We get these next two if the guest accesses a page which it thinks
819 * it has mapped but which is not actually present, either because
820 * it is for an emulated I/O device or because the corresonding
821 * host page has been paged out. Any other HDSI/HISI interrupts
822 * have been handled already.
824 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
825 r = RESUME_PAGE_FAULT;
827 case BOOK3S_INTERRUPT_H_INST_STORAGE:
828 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
829 vcpu->arch.fault_dsisr = 0;
830 r = RESUME_PAGE_FAULT;
833 * This occurs if the guest executes an illegal instruction.
834 * If the guest debug is disabled, generate a program interrupt
835 * to the guest. If guest debug is enabled, we need to check
836 * whether the instruction is a software breakpoint instruction.
837 * Accordingly return to Guest or Host.
839 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
840 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
841 r = kvmppc_emulate_debug_inst(run, vcpu);
843 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
848 * This occurs if the guest (kernel or userspace), does something that
849 * is prohibited by HFSCR. We just generate a program interrupt to
852 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
853 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
857 kvmppc_dump_regs(vcpu);
858 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
859 vcpu->arch.trap, kvmppc_get_pc(vcpu),
860 vcpu->arch.shregs.msr);
861 run->hw.hardware_exit_reason = vcpu->arch.trap;
869 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
870 struct kvm_sregs *sregs)
874 memset(sregs, 0, sizeof(struct kvm_sregs));
875 sregs->pvr = vcpu->arch.pvr;
876 for (i = 0; i < vcpu->arch.slb_max; i++) {
877 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
878 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
884 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
885 struct kvm_sregs *sregs)
889 /* Only accept the same PVR as the host's, since we can't spoof it */
890 if (sregs->pvr != vcpu->arch.pvr)
894 for (i = 0; i < vcpu->arch.slb_nr; i++) {
895 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
896 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
897 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
901 vcpu->arch.slb_max = j;
906 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
909 struct kvmppc_vcore *vc = vcpu->arch.vcore;
912 spin_lock(&vc->lock);
914 * If ILE (interrupt little-endian) has changed, update the
915 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
917 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
918 struct kvm *kvm = vcpu->kvm;
919 struct kvm_vcpu *vcpu;
922 mutex_lock(&kvm->lock);
923 kvm_for_each_vcpu(i, vcpu, kvm) {
924 if (vcpu->arch.vcore != vc)
926 if (new_lpcr & LPCR_ILE)
927 vcpu->arch.intr_msr |= MSR_LE;
929 vcpu->arch.intr_msr &= ~MSR_LE;
931 mutex_unlock(&kvm->lock);
935 * Userspace can only modify DPFD (default prefetch depth),
936 * ILE (interrupt little-endian) and TC (translation control).
937 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
939 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
940 if (cpu_has_feature(CPU_FTR_ARCH_207S))
943 /* Broken 32-bit version of LPCR must not clear top bits */
946 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
947 spin_unlock(&vc->lock);
950 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
951 union kvmppc_one_reg *val)
957 case KVM_REG_PPC_DEBUG_INST:
958 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
960 case KVM_REG_PPC_HIOR:
961 *val = get_reg_val(id, 0);
963 case KVM_REG_PPC_DABR:
964 *val = get_reg_val(id, vcpu->arch.dabr);
966 case KVM_REG_PPC_DABRX:
967 *val = get_reg_val(id, vcpu->arch.dabrx);
969 case KVM_REG_PPC_DSCR:
970 *val = get_reg_val(id, vcpu->arch.dscr);
972 case KVM_REG_PPC_PURR:
973 *val = get_reg_val(id, vcpu->arch.purr);
975 case KVM_REG_PPC_SPURR:
976 *val = get_reg_val(id, vcpu->arch.spurr);
978 case KVM_REG_PPC_AMR:
979 *val = get_reg_val(id, vcpu->arch.amr);
981 case KVM_REG_PPC_UAMOR:
982 *val = get_reg_val(id, vcpu->arch.uamor);
984 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
985 i = id - KVM_REG_PPC_MMCR0;
986 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
988 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
989 i = id - KVM_REG_PPC_PMC1;
990 *val = get_reg_val(id, vcpu->arch.pmc[i]);
992 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
993 i = id - KVM_REG_PPC_SPMC1;
994 *val = get_reg_val(id, vcpu->arch.spmc[i]);
996 case KVM_REG_PPC_SIAR:
997 *val = get_reg_val(id, vcpu->arch.siar);
999 case KVM_REG_PPC_SDAR:
1000 *val = get_reg_val(id, vcpu->arch.sdar);
1002 case KVM_REG_PPC_SIER:
1003 *val = get_reg_val(id, vcpu->arch.sier);
1005 case KVM_REG_PPC_IAMR:
1006 *val = get_reg_val(id, vcpu->arch.iamr);
1008 case KVM_REG_PPC_PSPB:
1009 *val = get_reg_val(id, vcpu->arch.pspb);
1011 case KVM_REG_PPC_DPDES:
1012 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1014 case KVM_REG_PPC_DAWR:
1015 *val = get_reg_val(id, vcpu->arch.dawr);
1017 case KVM_REG_PPC_DAWRX:
1018 *val = get_reg_val(id, vcpu->arch.dawrx);
1020 case KVM_REG_PPC_CIABR:
1021 *val = get_reg_val(id, vcpu->arch.ciabr);
1023 case KVM_REG_PPC_CSIGR:
1024 *val = get_reg_val(id, vcpu->arch.csigr);
1026 case KVM_REG_PPC_TACR:
1027 *val = get_reg_val(id, vcpu->arch.tacr);
1029 case KVM_REG_PPC_TCSCR:
1030 *val = get_reg_val(id, vcpu->arch.tcscr);
1032 case KVM_REG_PPC_PID:
1033 *val = get_reg_val(id, vcpu->arch.pid);
1035 case KVM_REG_PPC_ACOP:
1036 *val = get_reg_val(id, vcpu->arch.acop);
1038 case KVM_REG_PPC_WORT:
1039 *val = get_reg_val(id, vcpu->arch.wort);
1041 case KVM_REG_PPC_VPA_ADDR:
1042 spin_lock(&vcpu->arch.vpa_update_lock);
1043 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1044 spin_unlock(&vcpu->arch.vpa_update_lock);
1046 case KVM_REG_PPC_VPA_SLB:
1047 spin_lock(&vcpu->arch.vpa_update_lock);
1048 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1049 val->vpaval.length = vcpu->arch.slb_shadow.len;
1050 spin_unlock(&vcpu->arch.vpa_update_lock);
1052 case KVM_REG_PPC_VPA_DTL:
1053 spin_lock(&vcpu->arch.vpa_update_lock);
1054 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1055 val->vpaval.length = vcpu->arch.dtl.len;
1056 spin_unlock(&vcpu->arch.vpa_update_lock);
1058 case KVM_REG_PPC_TB_OFFSET:
1059 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1061 case KVM_REG_PPC_LPCR:
1062 case KVM_REG_PPC_LPCR_64:
1063 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1065 case KVM_REG_PPC_PPR:
1066 *val = get_reg_val(id, vcpu->arch.ppr);
1068 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1069 case KVM_REG_PPC_TFHAR:
1070 *val = get_reg_val(id, vcpu->arch.tfhar);
1072 case KVM_REG_PPC_TFIAR:
1073 *val = get_reg_val(id, vcpu->arch.tfiar);
1075 case KVM_REG_PPC_TEXASR:
1076 *val = get_reg_val(id, vcpu->arch.texasr);
1078 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1079 i = id - KVM_REG_PPC_TM_GPR0;
1080 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1082 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1085 i = id - KVM_REG_PPC_TM_VSR0;
1087 for (j = 0; j < TS_FPRWIDTH; j++)
1088 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1090 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1091 val->vval = vcpu->arch.vr_tm.vr[i-32];
1097 case KVM_REG_PPC_TM_CR:
1098 *val = get_reg_val(id, vcpu->arch.cr_tm);
1100 case KVM_REG_PPC_TM_LR:
1101 *val = get_reg_val(id, vcpu->arch.lr_tm);
1103 case KVM_REG_PPC_TM_CTR:
1104 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1106 case KVM_REG_PPC_TM_FPSCR:
1107 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1109 case KVM_REG_PPC_TM_AMR:
1110 *val = get_reg_val(id, vcpu->arch.amr_tm);
1112 case KVM_REG_PPC_TM_PPR:
1113 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1115 case KVM_REG_PPC_TM_VRSAVE:
1116 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1118 case KVM_REG_PPC_TM_VSCR:
1119 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1120 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1124 case KVM_REG_PPC_TM_DSCR:
1125 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1127 case KVM_REG_PPC_TM_TAR:
1128 *val = get_reg_val(id, vcpu->arch.tar_tm);
1131 case KVM_REG_PPC_ARCH_COMPAT:
1132 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1142 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1143 union kvmppc_one_reg *val)
1147 unsigned long addr, len;
1150 case KVM_REG_PPC_HIOR:
1151 /* Only allow this to be set to zero */
1152 if (set_reg_val(id, *val))
1155 case KVM_REG_PPC_DABR:
1156 vcpu->arch.dabr = set_reg_val(id, *val);
1158 case KVM_REG_PPC_DABRX:
1159 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1161 case KVM_REG_PPC_DSCR:
1162 vcpu->arch.dscr = set_reg_val(id, *val);
1164 case KVM_REG_PPC_PURR:
1165 vcpu->arch.purr = set_reg_val(id, *val);
1167 case KVM_REG_PPC_SPURR:
1168 vcpu->arch.spurr = set_reg_val(id, *val);
1170 case KVM_REG_PPC_AMR:
1171 vcpu->arch.amr = set_reg_val(id, *val);
1173 case KVM_REG_PPC_UAMOR:
1174 vcpu->arch.uamor = set_reg_val(id, *val);
1176 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1177 i = id - KVM_REG_PPC_MMCR0;
1178 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1180 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1181 i = id - KVM_REG_PPC_PMC1;
1182 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1184 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1185 i = id - KVM_REG_PPC_SPMC1;
1186 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1188 case KVM_REG_PPC_SIAR:
1189 vcpu->arch.siar = set_reg_val(id, *val);
1191 case KVM_REG_PPC_SDAR:
1192 vcpu->arch.sdar = set_reg_val(id, *val);
1194 case KVM_REG_PPC_SIER:
1195 vcpu->arch.sier = set_reg_val(id, *val);
1197 case KVM_REG_PPC_IAMR:
1198 vcpu->arch.iamr = set_reg_val(id, *val);
1200 case KVM_REG_PPC_PSPB:
1201 vcpu->arch.pspb = set_reg_val(id, *val);
1203 case KVM_REG_PPC_DPDES:
1204 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1206 case KVM_REG_PPC_DAWR:
1207 vcpu->arch.dawr = set_reg_val(id, *val);
1209 case KVM_REG_PPC_DAWRX:
1210 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1212 case KVM_REG_PPC_CIABR:
1213 vcpu->arch.ciabr = set_reg_val(id, *val);
1214 /* Don't allow setting breakpoints in hypervisor code */
1215 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1216 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1218 case KVM_REG_PPC_CSIGR:
1219 vcpu->arch.csigr = set_reg_val(id, *val);
1221 case KVM_REG_PPC_TACR:
1222 vcpu->arch.tacr = set_reg_val(id, *val);
1224 case KVM_REG_PPC_TCSCR:
1225 vcpu->arch.tcscr = set_reg_val(id, *val);
1227 case KVM_REG_PPC_PID:
1228 vcpu->arch.pid = set_reg_val(id, *val);
1230 case KVM_REG_PPC_ACOP:
1231 vcpu->arch.acop = set_reg_val(id, *val);
1233 case KVM_REG_PPC_WORT:
1234 vcpu->arch.wort = set_reg_val(id, *val);
1236 case KVM_REG_PPC_VPA_ADDR:
1237 addr = set_reg_val(id, *val);
1239 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1240 vcpu->arch.dtl.next_gpa))
1242 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1244 case KVM_REG_PPC_VPA_SLB:
1245 addr = val->vpaval.addr;
1246 len = val->vpaval.length;
1248 if (addr && !vcpu->arch.vpa.next_gpa)
1250 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1252 case KVM_REG_PPC_VPA_DTL:
1253 addr = val->vpaval.addr;
1254 len = val->vpaval.length;
1256 if (addr && (len < sizeof(struct dtl_entry) ||
1257 !vcpu->arch.vpa.next_gpa))
1259 len -= len % sizeof(struct dtl_entry);
1260 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1262 case KVM_REG_PPC_TB_OFFSET:
1263 /* round up to multiple of 2^24 */
1264 vcpu->arch.vcore->tb_offset =
1265 ALIGN(set_reg_val(id, *val), 1UL << 24);
1267 case KVM_REG_PPC_LPCR:
1268 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1270 case KVM_REG_PPC_LPCR_64:
1271 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
1273 case KVM_REG_PPC_PPR:
1274 vcpu->arch.ppr = set_reg_val(id, *val);
1276 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1277 case KVM_REG_PPC_TFHAR:
1278 vcpu->arch.tfhar = set_reg_val(id, *val);
1280 case KVM_REG_PPC_TFIAR:
1281 vcpu->arch.tfiar = set_reg_val(id, *val);
1283 case KVM_REG_PPC_TEXASR:
1284 vcpu->arch.texasr = set_reg_val(id, *val);
1286 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1287 i = id - KVM_REG_PPC_TM_GPR0;
1288 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1290 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1293 i = id - KVM_REG_PPC_TM_VSR0;
1295 for (j = 0; j < TS_FPRWIDTH; j++)
1296 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1298 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1299 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1304 case KVM_REG_PPC_TM_CR:
1305 vcpu->arch.cr_tm = set_reg_val(id, *val);
1307 case KVM_REG_PPC_TM_LR:
1308 vcpu->arch.lr_tm = set_reg_val(id, *val);
1310 case KVM_REG_PPC_TM_CTR:
1311 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1313 case KVM_REG_PPC_TM_FPSCR:
1314 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1316 case KVM_REG_PPC_TM_AMR:
1317 vcpu->arch.amr_tm = set_reg_val(id, *val);
1319 case KVM_REG_PPC_TM_PPR:
1320 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1322 case KVM_REG_PPC_TM_VRSAVE:
1323 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1325 case KVM_REG_PPC_TM_VSCR:
1326 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1327 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1331 case KVM_REG_PPC_TM_DSCR:
1332 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1334 case KVM_REG_PPC_TM_TAR:
1335 vcpu->arch.tar_tm = set_reg_val(id, *val);
1338 case KVM_REG_PPC_ARCH_COMPAT:
1339 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1349 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1351 struct kvmppc_vcore *vcore;
1353 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1358 INIT_LIST_HEAD(&vcore->runnable_threads);
1359 spin_lock_init(&vcore->lock);
1360 init_waitqueue_head(&vcore->wq);
1361 vcore->preempt_tb = TB_NIL;
1362 vcore->lpcr = kvm->arch.lpcr;
1363 vcore->first_vcpuid = core * threads_per_subcore;
1366 vcore->mpp_buffer_is_valid = false;
1368 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1369 vcore->mpp_buffer = (void *)__get_free_pages(
1370 GFP_KERNEL|__GFP_ZERO,
1376 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1379 struct kvm_vcpu *vcpu;
1382 struct kvmppc_vcore *vcore;
1384 core = id / threads_per_subcore;
1385 if (core >= KVM_MAX_VCORES)
1389 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1393 err = kvm_vcpu_init(vcpu, kvm, id);
1397 vcpu->arch.shared = &vcpu->arch.shregs;
1398 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1400 * The shared struct is never shared on HV,
1401 * so we can always use host endianness
1403 #ifdef __BIG_ENDIAN__
1404 vcpu->arch.shared_big_endian = true;
1406 vcpu->arch.shared_big_endian = false;
1409 vcpu->arch.mmcr[0] = MMCR0_FC;
1410 vcpu->arch.ctrl = CTRL_RUNLATCH;
1411 /* default to host PVR, since we can't spoof it */
1412 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1413 spin_lock_init(&vcpu->arch.vpa_update_lock);
1414 spin_lock_init(&vcpu->arch.tbacct_lock);
1415 vcpu->arch.busy_preempt = TB_NIL;
1416 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1418 kvmppc_mmu_book3s_hv_init(vcpu);
1420 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1422 init_waitqueue_head(&vcpu->arch.cpu_run);
1424 mutex_lock(&kvm->lock);
1425 vcore = kvm->arch.vcores[core];
1427 vcore = kvmppc_vcore_create(kvm, core);
1428 kvm->arch.vcores[core] = vcore;
1429 kvm->arch.online_vcores++;
1431 mutex_unlock(&kvm->lock);
1436 spin_lock(&vcore->lock);
1437 ++vcore->num_threads;
1438 spin_unlock(&vcore->lock);
1439 vcpu->arch.vcore = vcore;
1440 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1442 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1443 kvmppc_sanity_check(vcpu);
1448 kmem_cache_free(kvm_vcpu_cache, vcpu);
1450 return ERR_PTR(err);
1453 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1455 if (vpa->pinned_addr)
1456 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1460 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1462 spin_lock(&vcpu->arch.vpa_update_lock);
1463 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1464 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1465 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1466 spin_unlock(&vcpu->arch.vpa_update_lock);
1467 kvm_vcpu_uninit(vcpu);
1468 kmem_cache_free(kvm_vcpu_cache, vcpu);
1471 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1473 /* Indicate we want to get back into the guest */
1477 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1479 unsigned long dec_nsec, now;
1482 if (now > vcpu->arch.dec_expires) {
1483 /* decrementer has already gone negative */
1484 kvmppc_core_queue_dec(vcpu);
1485 kvmppc_core_prepare_to_enter(vcpu);
1488 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1490 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1492 vcpu->arch.timer_running = 1;
1495 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1497 vcpu->arch.ceded = 0;
1498 if (vcpu->arch.timer_running) {
1499 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1500 vcpu->arch.timer_running = 0;
1504 extern void __kvmppc_vcore_entry(void);
1506 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1507 struct kvm_vcpu *vcpu)
1511 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1513 spin_lock_irq(&vcpu->arch.tbacct_lock);
1515 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1516 vcpu->arch.stolen_logged;
1517 vcpu->arch.busy_preempt = now;
1518 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1519 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1521 list_del(&vcpu->arch.run_list);
1524 static int kvmppc_grab_hwthread(int cpu)
1526 struct paca_struct *tpaca;
1527 long timeout = 10000;
1531 /* Ensure the thread won't go into the kernel if it wakes */
1532 tpaca->kvm_hstate.hwthread_req = 1;
1533 tpaca->kvm_hstate.kvm_vcpu = NULL;
1536 * If the thread is already executing in the kernel (e.g. handling
1537 * a stray interrupt), wait for it to get back to nap mode.
1538 * The smp_mb() is to ensure that our setting of hwthread_req
1539 * is visible before we look at hwthread_state, so if this
1540 * races with the code at system_reset_pSeries and the thread
1541 * misses our setting of hwthread_req, we are sure to see its
1542 * setting of hwthread_state, and vice versa.
1545 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1546 if (--timeout <= 0) {
1547 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1555 static void kvmppc_release_hwthread(int cpu)
1557 struct paca_struct *tpaca;
1560 tpaca->kvm_hstate.hwthread_req = 0;
1561 tpaca->kvm_hstate.kvm_vcpu = NULL;
1564 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1567 struct paca_struct *tpaca;
1568 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1570 if (vcpu->arch.timer_running) {
1571 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1572 vcpu->arch.timer_running = 0;
1574 cpu = vc->pcpu + vcpu->arch.ptid;
1576 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1577 tpaca->kvm_hstate.kvm_vcore = vc;
1578 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1579 vcpu->cpu = vc->pcpu;
1581 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1582 if (cpu != smp_processor_id()) {
1584 if (vcpu->arch.ptid)
1590 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1596 while (vc->nap_count < vc->n_woken) {
1597 if (++i >= 1000000) {
1598 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1599 vc->nap_count, vc->n_woken);
1608 * Check that we are on thread 0 and that any other threads in
1609 * this core are off-line. Then grab the threads so they can't
1612 static int on_primary_thread(void)
1614 int cpu = smp_processor_id();
1617 /* Are we on a primary subcore? */
1618 if (cpu_thread_in_subcore(cpu))
1622 while (++thr < threads_per_subcore)
1623 if (cpu_online(cpu + thr))
1626 /* Grab all hw threads so they can't go into the kernel */
1627 for (thr = 1; thr < threads_per_subcore; ++thr) {
1628 if (kvmppc_grab_hwthread(cpu + thr)) {
1629 /* Couldn't grab one; let the others go */
1631 kvmppc_release_hwthread(cpu + thr);
1632 } while (--thr > 0);
1639 static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1641 phys_addr_t phy_addr, mpp_addr;
1643 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1644 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1646 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1647 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1649 vc->mpp_buffer_is_valid = true;
1652 static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1654 phys_addr_t phy_addr, mpp_addr;
1656 phy_addr = virt_to_phys(vc->mpp_buffer);
1657 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1659 /* We must abort any in-progress save operations to ensure
1660 * the table is valid so that prefetch engine knows when to
1661 * stop prefetching. */
1662 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1663 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1667 * Run a set of guest threads on a physical core.
1668 * Called with vc->lock held.
1670 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1672 struct kvm_vcpu *vcpu, *vnext;
1675 int i, need_vpa_update;
1677 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1679 /* don't start if any threads have a signal pending */
1680 need_vpa_update = 0;
1681 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1682 if (signal_pending(vcpu->arch.run_task))
1684 if (vcpu->arch.vpa.update_pending ||
1685 vcpu->arch.slb_shadow.update_pending ||
1686 vcpu->arch.dtl.update_pending)
1687 vcpus_to_update[need_vpa_update++] = vcpu;
1691 * Initialize *vc, in particular vc->vcore_state, so we can
1692 * drop the vcore lock if necessary.
1696 vc->entry_exit_count = 0;
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);
1731 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1732 get_paca()->kvm_hstate.kvm_vcore = vc;
1733 get_paca()->kvm_hstate.ptid = 0;
1735 vc->vcore_state = VCORE_RUNNING;
1737 spin_unlock(&vc->lock);
1741 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1743 if (vc->mpp_buffer_is_valid)
1744 kvmppc_start_restoring_l2_cache(vc);
1746 __kvmppc_vcore_entry();
1748 spin_lock(&vc->lock);
1751 kvmppc_start_saving_l2_cache(vc);
1753 /* disable sending of IPIs on virtual external irqs */
1754 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1756 /* wait for secondary threads to finish writing their state to memory */
1757 if (vc->nap_count < vc->n_woken)
1758 kvmppc_wait_for_nap(vc);
1759 for (i = 0; i < threads_per_subcore; ++i)
1760 kvmppc_release_hwthread(vc->pcpu + i);
1761 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1762 vc->vcore_state = VCORE_EXITING;
1763 spin_unlock(&vc->lock);
1765 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1767 /* make sure updates to secondary vcpu structs are visible now */
1774 spin_lock(&vc->lock);
1776 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1777 /* cancel pending dec exception if dec is positive */
1778 if (now < vcpu->arch.dec_expires &&
1779 kvmppc_core_pending_dec(vcpu))
1780 kvmppc_core_dequeue_dec(vcpu);
1783 if (vcpu->arch.trap)
1784 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1785 vcpu->arch.run_task);
1787 vcpu->arch.ret = ret;
1788 vcpu->arch.trap = 0;
1790 if (vcpu->arch.ceded) {
1791 if (!is_kvmppc_resume_guest(ret))
1792 kvmppc_end_cede(vcpu);
1794 kvmppc_set_timer(vcpu);
1799 vc->vcore_state = VCORE_INACTIVE;
1800 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1802 if (!is_kvmppc_resume_guest(vcpu->arch.ret)) {
1803 kvmppc_remove_runnable(vc, vcpu);
1804 wake_up(&vcpu->arch.cpu_run);
1810 * Wait for some other vcpu thread to execute us, and
1811 * wake us up when we need to handle something in the host.
1813 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1817 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1818 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1820 finish_wait(&vcpu->arch.cpu_run, &wait);
1824 * All the vcpus in this vcore are idle, so wait for a decrementer
1825 * or external interrupt to one of the vcpus. vc->lock is held.
1827 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1831 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1832 vc->vcore_state = VCORE_SLEEPING;
1833 spin_unlock(&vc->lock);
1835 finish_wait(&vc->wq, &wait);
1836 spin_lock(&vc->lock);
1837 vc->vcore_state = VCORE_INACTIVE;
1840 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1843 struct kvmppc_vcore *vc;
1844 struct kvm_vcpu *v, *vn;
1846 kvm_run->exit_reason = 0;
1847 vcpu->arch.ret = RESUME_GUEST;
1848 vcpu->arch.trap = 0;
1849 kvmppc_update_vpas(vcpu);
1852 * Synchronize with other threads in this virtual core
1854 vc = vcpu->arch.vcore;
1855 spin_lock(&vc->lock);
1856 vcpu->arch.ceded = 0;
1857 vcpu->arch.run_task = current;
1858 vcpu->arch.kvm_run = kvm_run;
1859 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1860 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1861 vcpu->arch.busy_preempt = TB_NIL;
1862 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1866 * This happens the first time this is called for a vcpu.
1867 * If the vcore is already running, we may be able to start
1868 * this thread straight away and have it join in.
1870 if (!signal_pending(current)) {
1871 if (vc->vcore_state == VCORE_RUNNING &&
1872 VCORE_EXIT_COUNT(vc) == 0) {
1873 kvmppc_create_dtl_entry(vcpu, vc);
1874 kvmppc_start_thread(vcpu);
1875 } else if (vc->vcore_state == VCORE_SLEEPING) {
1881 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1882 !signal_pending(current)) {
1883 if (vc->vcore_state != VCORE_INACTIVE) {
1884 spin_unlock(&vc->lock);
1885 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1886 spin_lock(&vc->lock);
1889 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1891 kvmppc_core_prepare_to_enter(v);
1892 if (signal_pending(v->arch.run_task)) {
1893 kvmppc_remove_runnable(vc, v);
1894 v->stat.signal_exits++;
1895 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1896 v->arch.ret = -EINTR;
1897 wake_up(&v->arch.cpu_run);
1900 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1904 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1905 if (!v->arch.pending_exceptions)
1906 n_ceded += v->arch.ceded;
1910 if (n_ceded == vc->n_runnable)
1911 kvmppc_vcore_blocked(vc);
1913 kvmppc_run_core(vc);
1917 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1918 (vc->vcore_state == VCORE_RUNNING ||
1919 vc->vcore_state == VCORE_EXITING)) {
1920 spin_unlock(&vc->lock);
1921 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1922 spin_lock(&vc->lock);
1925 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1926 kvmppc_remove_runnable(vc, vcpu);
1927 vcpu->stat.signal_exits++;
1928 kvm_run->exit_reason = KVM_EXIT_INTR;
1929 vcpu->arch.ret = -EINTR;
1932 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1933 /* Wake up some vcpu to run the core */
1934 v = list_first_entry(&vc->runnable_threads,
1935 struct kvm_vcpu, arch.run_list);
1936 wake_up(&v->arch.cpu_run);
1939 spin_unlock(&vc->lock);
1940 return vcpu->arch.ret;
1943 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1948 if (!vcpu->arch.sane) {
1949 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1953 kvmppc_core_prepare_to_enter(vcpu);
1955 /* No need to go into the guest when all we'll do is come back out */
1956 if (signal_pending(current)) {
1957 run->exit_reason = KVM_EXIT_INTR;
1961 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1962 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1965 /* On the first time here, set up HTAB and VRMA or RMA */
1966 if (!vcpu->kvm->arch.rma_setup_done) {
1967 r = kvmppc_hv_setup_htab_rma(vcpu);
1972 flush_fp_to_thread(current);
1973 flush_altivec_to_thread(current);
1974 flush_vsx_to_thread(current);
1975 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1976 vcpu->arch.pgdir = current->mm->pgd;
1977 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1980 r = kvmppc_run_vcpu(run, vcpu);
1982 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1983 !(vcpu->arch.shregs.msr & MSR_PR)) {
1984 r = kvmppc_pseries_do_hcall(vcpu);
1985 kvmppc_core_prepare_to_enter(vcpu);
1986 } else if (r == RESUME_PAGE_FAULT) {
1987 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1988 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1989 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1990 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1992 } while (is_kvmppc_resume_guest(r));
1995 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1996 atomic_dec(&vcpu->kvm->arch.vcpus_running);
2001 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
2002 Assumes POWER7 or PPC970. */
2003 static inline int lpcr_rmls(unsigned long rma_size)
2006 case 32ul << 20: /* 32 MB */
2007 if (cpu_has_feature(CPU_FTR_ARCH_206))
2008 return 8; /* only supported on POWER7 */
2010 case 64ul << 20: /* 64 MB */
2012 case 128ul << 20: /* 128 MB */
2014 case 256ul << 20: /* 256 MB */
2016 case 1ul << 30: /* 1 GB */
2018 case 16ul << 30: /* 16 GB */
2020 case 256ul << 30: /* 256 GB */
2027 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
2030 struct kvm_rma_info *ri = vma->vm_file->private_data;
2032 if (vmf->pgoff >= kvm_rma_pages)
2033 return VM_FAULT_SIGBUS;
2035 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
2041 static const struct vm_operations_struct kvm_rma_vm_ops = {
2042 .fault = kvm_rma_fault,
2045 static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
2047 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
2048 vma->vm_ops = &kvm_rma_vm_ops;
2052 static int kvm_rma_release(struct inode *inode, struct file *filp)
2054 struct kvm_rma_info *ri = filp->private_data;
2056 kvm_release_rma(ri);
2060 static const struct file_operations kvm_rma_fops = {
2061 .mmap = kvm_rma_mmap,
2062 .release = kvm_rma_release,
2065 static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
2066 struct kvm_allocate_rma *ret)
2069 struct kvm_rma_info *ri;
2071 * Only do this on PPC970 in HV mode
2073 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2074 !cpu_has_feature(CPU_FTR_ARCH_201))
2080 ri = kvm_alloc_rma();
2084 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
2086 kvm_release_rma(ri);
2088 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
2092 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2095 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2099 (*sps)->page_shift = def->shift;
2100 (*sps)->slb_enc = def->sllp;
2101 (*sps)->enc[0].page_shift = def->shift;
2102 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
2104 * Add 16MB MPSS support if host supports it
2106 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2107 (*sps)->enc[1].page_shift = 24;
2108 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2113 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2114 struct kvm_ppc_smmu_info *info)
2116 struct kvm_ppc_one_seg_page_size *sps;
2118 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2119 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2120 info->flags |= KVM_PPC_1T_SEGMENTS;
2121 info->slb_size = mmu_slb_size;
2123 /* We only support these sizes for now, and no muti-size segments */
2124 sps = &info->sps[0];
2125 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2126 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2127 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2133 * Get (and clear) the dirty memory log for a memory slot.
2135 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2136 struct kvm_dirty_log *log)
2138 struct kvm_memory_slot *memslot;
2142 mutex_lock(&kvm->slots_lock);
2145 if (log->slot >= KVM_USER_MEM_SLOTS)
2148 memslot = id_to_memslot(kvm->memslots, log->slot);
2150 if (!memslot->dirty_bitmap)
2153 n = kvm_dirty_bitmap_bytes(memslot);
2154 memset(memslot->dirty_bitmap, 0, n);
2156 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
2161 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2166 mutex_unlock(&kvm->slots_lock);
2170 static void unpin_slot(struct kvm_memory_slot *memslot)
2172 unsigned long *physp;
2173 unsigned long j, npages, pfn;
2176 physp = memslot->arch.slot_phys;
2177 npages = memslot->npages;
2180 for (j = 0; j < npages; j++) {
2181 if (!(physp[j] & KVMPPC_GOT_PAGE))
2183 pfn = physp[j] >> PAGE_SHIFT;
2184 page = pfn_to_page(pfn);
2190 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2191 struct kvm_memory_slot *dont)
2193 if (!dont || free->arch.rmap != dont->arch.rmap) {
2194 vfree(free->arch.rmap);
2195 free->arch.rmap = NULL;
2197 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
2199 vfree(free->arch.slot_phys);
2200 free->arch.slot_phys = NULL;
2204 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2205 unsigned long npages)
2207 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2208 if (!slot->arch.rmap)
2210 slot->arch.slot_phys = NULL;
2215 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2216 struct kvm_memory_slot *memslot,
2217 struct kvm_userspace_memory_region *mem)
2219 unsigned long *phys;
2221 /* Allocate a slot_phys array if needed */
2222 phys = memslot->arch.slot_phys;
2223 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
2224 phys = vzalloc(memslot->npages * sizeof(unsigned long));
2227 memslot->arch.slot_phys = phys;
2233 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2234 struct kvm_userspace_memory_region *mem,
2235 const struct kvm_memory_slot *old)
2237 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2238 struct kvm_memory_slot *memslot;
2240 if (npages && old->npages) {
2242 * If modifying a memslot, reset all the rmap dirty bits.
2243 * If this is a new memslot, we don't need to do anything
2244 * since the rmap array starts out as all zeroes,
2245 * i.e. no pages are dirty.
2247 memslot = id_to_memslot(kvm->memslots, mem->slot);
2248 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2253 * Update LPCR values in kvm->arch and in vcores.
2254 * Caller must hold kvm->lock.
2256 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2261 if ((kvm->arch.lpcr & mask) == lpcr)
2264 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2266 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2267 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2270 spin_lock(&vc->lock);
2271 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2272 spin_unlock(&vc->lock);
2273 if (++cores_done >= kvm->arch.online_vcores)
2278 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2283 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2286 struct kvm *kvm = vcpu->kvm;
2287 struct kvm_rma_info *ri = NULL;
2289 struct kvm_memory_slot *memslot;
2290 struct vm_area_struct *vma;
2291 unsigned long lpcr = 0, senc;
2292 unsigned long lpcr_mask = 0;
2293 unsigned long psize, porder;
2294 unsigned long rma_size;
2296 unsigned long *physp;
2297 unsigned long i, npages;
2300 mutex_lock(&kvm->lock);
2301 if (kvm->arch.rma_setup_done)
2302 goto out; /* another vcpu beat us to it */
2304 /* Allocate hashed page table (if not done already) and reset it */
2305 if (!kvm->arch.hpt_virt) {
2306 err = kvmppc_alloc_hpt(kvm, NULL);
2308 pr_err("KVM: Couldn't alloc HPT\n");
2313 /* Look up the memslot for guest physical address 0 */
2314 srcu_idx = srcu_read_lock(&kvm->srcu);
2315 memslot = gfn_to_memslot(kvm, 0);
2317 /* We must have some memory at 0 by now */
2319 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2322 /* Look up the VMA for the start of this memory slot */
2323 hva = memslot->userspace_addr;
2324 down_read(¤t->mm->mmap_sem);
2325 vma = find_vma(current->mm, hva);
2326 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2329 psize = vma_kernel_pagesize(vma);
2330 porder = __ilog2(psize);
2332 /* Is this one of our preallocated RMAs? */
2333 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2334 hva == vma->vm_start)
2335 ri = vma->vm_file->private_data;
2337 up_read(¤t->mm->mmap_sem);
2340 /* On POWER7, use VRMA; on PPC970, give up */
2342 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2343 pr_err("KVM: CPU requires an RMO\n");
2347 /* We can handle 4k, 64k or 16M pages in the VRMA */
2349 if (!(psize == 0x1000 || psize == 0x10000 ||
2350 psize == 0x1000000))
2353 /* Update VRMASD field in the LPCR */
2354 senc = slb_pgsize_encoding(psize);
2355 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2356 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2357 lpcr_mask = LPCR_VRMASD;
2358 /* the -4 is to account for senc values starting at 0x10 */
2359 lpcr = senc << (LPCR_VRMASD_SH - 4);
2361 /* Create HPTEs in the hash page table for the VRMA */
2362 kvmppc_map_vrma(vcpu, memslot, porder);
2365 /* Set up to use an RMO region */
2366 rma_size = kvm_rma_pages;
2367 if (rma_size > memslot->npages)
2368 rma_size = memslot->npages;
2369 rma_size <<= PAGE_SHIFT;
2370 rmls = lpcr_rmls(rma_size);
2372 if ((long)rmls < 0) {
2373 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
2376 atomic_inc(&ri->use_count);
2379 /* Update LPCR and RMOR */
2380 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2381 /* PPC970; insert RMLS value (split field) in HID4 */
2382 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2383 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2384 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
2385 ((rmls & 3) << HID4_RMLS2_SH);
2386 /* RMOR is also in HID4 */
2387 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2391 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2392 lpcr = rmls << LPCR_RMLS_SH;
2393 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
2395 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2396 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
2398 /* Initialize phys addrs of pages in RMO */
2399 npages = kvm_rma_pages;
2400 porder = __ilog2(npages);
2401 physp = memslot->arch.slot_phys;
2403 if (npages > memslot->npages)
2404 npages = memslot->npages;
2405 spin_lock(&kvm->arch.slot_phys_lock);
2406 for (i = 0; i < npages; ++i)
2407 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2409 spin_unlock(&kvm->arch.slot_phys_lock);
2413 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2415 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2417 kvm->arch.rma_setup_done = 1;
2420 srcu_read_unlock(&kvm->srcu, srcu_idx);
2422 mutex_unlock(&kvm->lock);
2426 up_read(¤t->mm->mmap_sem);
2430 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2432 unsigned long lpcr, lpid;
2434 /* Allocate the guest's logical partition ID */
2436 lpid = kvmppc_alloc_lpid();
2439 kvm->arch.lpid = lpid;
2442 * Since we don't flush the TLB when tearing down a VM,
2443 * and this lpid might have previously been used,
2444 * make sure we flush on each core before running the new VM.
2446 cpumask_setall(&kvm->arch.need_tlb_flush);
2448 /* Start out with the default set of hcalls enabled */
2449 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2450 sizeof(kvm->arch.enabled_hcalls));
2452 kvm->arch.rma = NULL;
2454 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2456 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2457 /* PPC970; HID4 is effectively the LPCR */
2458 kvm->arch.host_lpid = 0;
2459 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2460 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2461 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2462 ((lpid & 0xf) << HID4_LPID5_SH);
2464 /* POWER7; init LPCR for virtual RMA mode */
2465 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2466 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2467 lpcr &= LPCR_PECE | LPCR_LPES;
2468 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2469 LPCR_VPM0 | LPCR_VPM1;
2470 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2471 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2472 /* On POWER8 turn on online bit to enable PURR/SPURR */
2473 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2476 kvm->arch.lpcr = lpcr;
2478 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
2479 spin_lock_init(&kvm->arch.slot_phys_lock);
2482 * Track that we now have a HV mode VM active. This blocks secondary
2483 * CPU threads from coming online.
2485 kvm_hv_vm_activated();
2490 static void kvmppc_free_vcores(struct kvm *kvm)
2494 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2495 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2496 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2497 free_pages((unsigned long)vc->mpp_buffer,
2500 kfree(kvm->arch.vcores[i]);
2502 kvm->arch.online_vcores = 0;
2505 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2507 kvm_hv_vm_deactivated();
2509 kvmppc_free_vcores(kvm);
2510 if (kvm->arch.rma) {
2511 kvm_release_rma(kvm->arch.rma);
2512 kvm->arch.rma = NULL;
2515 kvmppc_free_hpt(kvm);
2518 /* We don't need to emulate any privileged instructions or dcbz */
2519 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2520 unsigned int inst, int *advance)
2522 return EMULATE_FAIL;
2525 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2528 return EMULATE_FAIL;
2531 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2534 return EMULATE_FAIL;
2537 static int kvmppc_core_check_processor_compat_hv(void)
2539 if (!cpu_has_feature(CPU_FTR_HVMODE))
2544 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2545 unsigned int ioctl, unsigned long arg)
2547 struct kvm *kvm __maybe_unused = filp->private_data;
2548 void __user *argp = (void __user *)arg;
2553 case KVM_ALLOCATE_RMA: {
2554 struct kvm_allocate_rma rma;
2555 struct kvm *kvm = filp->private_data;
2557 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2558 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2563 case KVM_PPC_ALLOCATE_HTAB: {
2567 if (get_user(htab_order, (u32 __user *)argp))
2569 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2573 if (put_user(htab_order, (u32 __user *)argp))
2579 case KVM_PPC_GET_HTAB_FD: {
2580 struct kvm_get_htab_fd ghf;
2583 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2585 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2597 * List of hcall numbers to enable by default.
2598 * For compatibility with old userspace, we enable by default
2599 * all hcalls that were implemented before the hcall-enabling
2600 * facility was added. Note this list should not include H_RTAS.
2602 static unsigned int default_hcall_list[] = {
2616 #ifdef CONFIG_KVM_XICS
2627 static void init_default_hcalls(void)
2632 for (i = 0; default_hcall_list[i]; ++i) {
2633 hcall = default_hcall_list[i];
2634 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2635 __set_bit(hcall / 4, default_enabled_hcalls);
2639 static struct kvmppc_ops kvm_ops_hv = {
2640 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2641 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2642 .get_one_reg = kvmppc_get_one_reg_hv,
2643 .set_one_reg = kvmppc_set_one_reg_hv,
2644 .vcpu_load = kvmppc_core_vcpu_load_hv,
2645 .vcpu_put = kvmppc_core_vcpu_put_hv,
2646 .set_msr = kvmppc_set_msr_hv,
2647 .vcpu_run = kvmppc_vcpu_run_hv,
2648 .vcpu_create = kvmppc_core_vcpu_create_hv,
2649 .vcpu_free = kvmppc_core_vcpu_free_hv,
2650 .check_requests = kvmppc_core_check_requests_hv,
2651 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2652 .flush_memslot = kvmppc_core_flush_memslot_hv,
2653 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2654 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2655 .unmap_hva = kvm_unmap_hva_hv,
2656 .unmap_hva_range = kvm_unmap_hva_range_hv,
2657 .age_hva = kvm_age_hva_hv,
2658 .test_age_hva = kvm_test_age_hva_hv,
2659 .set_spte_hva = kvm_set_spte_hva_hv,
2660 .mmu_destroy = kvmppc_mmu_destroy_hv,
2661 .free_memslot = kvmppc_core_free_memslot_hv,
2662 .create_memslot = kvmppc_core_create_memslot_hv,
2663 .init_vm = kvmppc_core_init_vm_hv,
2664 .destroy_vm = kvmppc_core_destroy_vm_hv,
2665 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2666 .emulate_op = kvmppc_core_emulate_op_hv,
2667 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2668 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2669 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2670 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2671 .hcall_implemented = kvmppc_hcall_impl_hv,
2674 static int kvmppc_book3s_init_hv(void)
2678 * FIXME!! Do we need to check on all cpus ?
2680 r = kvmppc_core_check_processor_compat_hv();
2684 kvm_ops_hv.owner = THIS_MODULE;
2685 kvmppc_hv_ops = &kvm_ops_hv;
2687 init_default_hcalls();
2689 r = kvmppc_mmu_hv_init();
2693 static void kvmppc_book3s_exit_hv(void)
2695 kvmppc_hv_ops = NULL;
2698 module_init(kvmppc_book3s_init_hv);
2699 module_exit(kvmppc_book3s_exit_hv);
2700 MODULE_LICENSE("GPL");
2701 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2702 MODULE_ALIAS("devname:kvm");