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>
35 #include <linux/debugfs.h>
38 #include <asm/cputable.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 <asm/dbell.h>
54 #include <linux/gfp.h>
55 #include <linux/vmalloc.h>
56 #include <linux/highmem.h>
57 #include <linux/hugetlb.h>
58 #include <linux/module.h>
62 #define CREATE_TRACE_POINTS
65 /* #define EXIT_DEBUG */
66 /* #define EXIT_DEBUG_SIMPLE */
67 /* #define EXIT_DEBUG_INT */
69 /* Used to indicate that a guest page fault needs to be handled */
70 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
72 /* Used as a "null" value for timebase values */
73 #define TB_NIL (~(u64)0)
75 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
77 static int dynamic_mt_modes = 6;
78 module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
79 MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
80 static int target_smt_mode;
81 module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
82 MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
84 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
85 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
87 static bool kvmppc_ipi_thread(int cpu)
89 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
90 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
92 if (cpu_first_thread_sibling(cpu) ==
93 cpu_first_thread_sibling(smp_processor_id())) {
94 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
95 msg |= cpu_thread_in_core(cpu);
97 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
104 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
105 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
114 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
117 wait_queue_head_t *wqp;
119 wqp = kvm_arch_vcpu_wq(vcpu);
120 if (waitqueue_active(wqp)) {
121 wake_up_interruptible(wqp);
122 ++vcpu->stat.halt_wakeup;
125 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
128 /* CPU points to the first thread of the core */
130 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
131 smp_send_reschedule(cpu);
135 * We use the vcpu_load/put functions to measure stolen time.
136 * Stolen time is counted as time when either the vcpu is able to
137 * run as part of a virtual core, but the task running the vcore
138 * is preempted or sleeping, or when the vcpu needs something done
139 * in the kernel by the task running the vcpu, but that task is
140 * preempted or sleeping. Those two things have to be counted
141 * separately, since one of the vcpu tasks will take on the job
142 * of running the core, and the other vcpu tasks in the vcore will
143 * sleep waiting for it to do that, but that sleep shouldn't count
146 * Hence we accumulate stolen time when the vcpu can run as part of
147 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
148 * needs its task to do other things in the kernel (for example,
149 * service a page fault) in busy_stolen. We don't accumulate
150 * stolen time for a vcore when it is inactive, or for a vcpu
151 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
152 * a misnomer; it means that the vcpu task is not executing in
153 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
154 * the kernel. We don't have any way of dividing up that time
155 * between time that the vcpu is genuinely stopped, time that
156 * the task is actively working on behalf of the vcpu, and time
157 * that the task is preempted, so we don't count any of it as
160 * Updates to busy_stolen are protected by arch.tbacct_lock;
161 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
162 * lock. The stolen times are measured in units of timebase ticks.
163 * (Note that the != TB_NIL checks below are purely defensive;
164 * they should never fail.)
167 static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
171 spin_lock_irqsave(&vc->stoltb_lock, flags);
172 vc->preempt_tb = mftb();
173 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
176 static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
180 spin_lock_irqsave(&vc->stoltb_lock, flags);
181 if (vc->preempt_tb != TB_NIL) {
182 vc->stolen_tb += mftb() - vc->preempt_tb;
183 vc->preempt_tb = TB_NIL;
185 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
188 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
190 struct kvmppc_vcore *vc = vcpu->arch.vcore;
194 * We can test vc->runner without taking the vcore lock,
195 * because only this task ever sets vc->runner to this
196 * vcpu, and once it is set to this vcpu, only this task
197 * ever sets it to NULL.
199 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
200 kvmppc_core_end_stolen(vc);
202 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
203 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
204 vcpu->arch.busy_preempt != TB_NIL) {
205 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
206 vcpu->arch.busy_preempt = TB_NIL;
208 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
211 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
213 struct kvmppc_vcore *vc = vcpu->arch.vcore;
216 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
217 kvmppc_core_start_stolen(vc);
219 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
220 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
221 vcpu->arch.busy_preempt = mftb();
222 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
225 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
228 * Check for illegal transactional state bit combination
229 * and if we find it, force the TS field to a safe state.
231 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
233 vcpu->arch.shregs.msr = msr;
234 kvmppc_end_cede(vcpu);
237 static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
239 vcpu->arch.pvr = pvr;
242 static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
244 unsigned long pcr = 0;
245 struct kvmppc_vcore *vc = vcpu->arch.vcore;
248 switch (arch_compat) {
251 * If an arch bit is set in PCR, all the defined
252 * higher-order arch bits also have to be set.
254 pcr = PCR_ARCH_206 | PCR_ARCH_205;
266 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
267 /* POWER7 can't emulate POWER8 */
268 if (!(pcr & PCR_ARCH_206))
270 pcr &= ~PCR_ARCH_206;
274 spin_lock(&vc->lock);
275 vc->arch_compat = arch_compat;
277 spin_unlock(&vc->lock);
282 static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
286 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
287 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
288 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
289 for (r = 0; r < 16; ++r)
290 pr_err("r%2d = %.16lx r%d = %.16lx\n",
291 r, kvmppc_get_gpr(vcpu, r),
292 r+16, kvmppc_get_gpr(vcpu, r+16));
293 pr_err("ctr = %.16lx lr = %.16lx\n",
294 vcpu->arch.ctr, vcpu->arch.lr);
295 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
296 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
297 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
298 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
299 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
300 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
301 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
302 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
303 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
304 pr_err("fault dar = %.16lx dsisr = %.8x\n",
305 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
306 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
307 for (r = 0; r < vcpu->arch.slb_max; ++r)
308 pr_err(" ESID = %.16llx VSID = %.16llx\n",
309 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
310 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
311 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
312 vcpu->arch.last_inst);
315 static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
317 struct kvm_vcpu *ret;
319 mutex_lock(&kvm->lock);
320 ret = kvm_get_vcpu_by_id(kvm, id);
321 mutex_unlock(&kvm->lock);
325 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
327 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
328 vpa->yield_count = cpu_to_be32(1);
331 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
332 unsigned long addr, unsigned long len)
334 /* check address is cacheline aligned */
335 if (addr & (L1_CACHE_BYTES - 1))
337 spin_lock(&vcpu->arch.vpa_update_lock);
338 if (v->next_gpa != addr || v->len != len) {
340 v->len = addr ? len : 0;
341 v->update_pending = 1;
343 spin_unlock(&vcpu->arch.vpa_update_lock);
347 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
356 static int vpa_is_registered(struct kvmppc_vpa *vpap)
358 if (vpap->update_pending)
359 return vpap->next_gpa != 0;
360 return vpap->pinned_addr != NULL;
363 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
365 unsigned long vcpuid, unsigned long vpa)
367 struct kvm *kvm = vcpu->kvm;
368 unsigned long len, nb;
370 struct kvm_vcpu *tvcpu;
373 struct kvmppc_vpa *vpap;
375 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
379 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
380 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
381 subfunc == H_VPA_REG_SLB) {
382 /* Registering new area - address must be cache-line aligned */
383 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
386 /* convert logical addr to kernel addr and read length */
387 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
390 if (subfunc == H_VPA_REG_VPA)
391 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
393 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
394 kvmppc_unpin_guest_page(kvm, va, vpa, false);
397 if (len > nb || len < sizeof(struct reg_vpa))
406 spin_lock(&tvcpu->arch.vpa_update_lock);
409 case H_VPA_REG_VPA: /* register VPA */
410 if (len < sizeof(struct lppaca))
412 vpap = &tvcpu->arch.vpa;
416 case H_VPA_REG_DTL: /* register DTL */
417 if (len < sizeof(struct dtl_entry))
419 len -= len % sizeof(struct dtl_entry);
421 /* Check that they have previously registered a VPA */
423 if (!vpa_is_registered(&tvcpu->arch.vpa))
426 vpap = &tvcpu->arch.dtl;
430 case H_VPA_REG_SLB: /* register SLB shadow buffer */
431 /* Check that they have previously registered a VPA */
433 if (!vpa_is_registered(&tvcpu->arch.vpa))
436 vpap = &tvcpu->arch.slb_shadow;
440 case H_VPA_DEREG_VPA: /* deregister VPA */
441 /* Check they don't still have a DTL or SLB buf registered */
443 if (vpa_is_registered(&tvcpu->arch.dtl) ||
444 vpa_is_registered(&tvcpu->arch.slb_shadow))
447 vpap = &tvcpu->arch.vpa;
451 case H_VPA_DEREG_DTL: /* deregister DTL */
452 vpap = &tvcpu->arch.dtl;
456 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
457 vpap = &tvcpu->arch.slb_shadow;
463 vpap->next_gpa = vpa;
465 vpap->update_pending = 1;
468 spin_unlock(&tvcpu->arch.vpa_update_lock);
473 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
475 struct kvm *kvm = vcpu->kvm;
481 * We need to pin the page pointed to by vpap->next_gpa,
482 * but we can't call kvmppc_pin_guest_page under the lock
483 * as it does get_user_pages() and down_read(). So we
484 * have to drop the lock, pin the page, then get the lock
485 * again and check that a new area didn't get registered
489 gpa = vpap->next_gpa;
490 spin_unlock(&vcpu->arch.vpa_update_lock);
494 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
495 spin_lock(&vcpu->arch.vpa_update_lock);
496 if (gpa == vpap->next_gpa)
498 /* sigh... unpin that one and try again */
500 kvmppc_unpin_guest_page(kvm, va, gpa, false);
503 vpap->update_pending = 0;
504 if (va && nb < vpap->len) {
506 * If it's now too short, it must be that userspace
507 * has changed the mappings underlying guest memory,
508 * so unregister the region.
510 kvmppc_unpin_guest_page(kvm, va, gpa, false);
513 if (vpap->pinned_addr)
514 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
517 vpap->pinned_addr = va;
520 vpap->pinned_end = va + vpap->len;
523 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
525 if (!(vcpu->arch.vpa.update_pending ||
526 vcpu->arch.slb_shadow.update_pending ||
527 vcpu->arch.dtl.update_pending))
530 spin_lock(&vcpu->arch.vpa_update_lock);
531 if (vcpu->arch.vpa.update_pending) {
532 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
533 if (vcpu->arch.vpa.pinned_addr)
534 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
536 if (vcpu->arch.dtl.update_pending) {
537 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
538 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
539 vcpu->arch.dtl_index = 0;
541 if (vcpu->arch.slb_shadow.update_pending)
542 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
543 spin_unlock(&vcpu->arch.vpa_update_lock);
547 * Return the accumulated stolen time for the vcore up until `now'.
548 * The caller should hold the vcore lock.
550 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
555 spin_lock_irqsave(&vc->stoltb_lock, flags);
557 if (vc->vcore_state != VCORE_INACTIVE &&
558 vc->preempt_tb != TB_NIL)
559 p += now - vc->preempt_tb;
560 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
564 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
565 struct kvmppc_vcore *vc)
567 struct dtl_entry *dt;
569 unsigned long stolen;
570 unsigned long core_stolen;
573 dt = vcpu->arch.dtl_ptr;
574 vpa = vcpu->arch.vpa.pinned_addr;
576 core_stolen = vcore_stolen_time(vc, now);
577 stolen = core_stolen - vcpu->arch.stolen_logged;
578 vcpu->arch.stolen_logged = core_stolen;
579 spin_lock_irq(&vcpu->arch.tbacct_lock);
580 stolen += vcpu->arch.busy_stolen;
581 vcpu->arch.busy_stolen = 0;
582 spin_unlock_irq(&vcpu->arch.tbacct_lock);
585 memset(dt, 0, sizeof(struct dtl_entry));
586 dt->dispatch_reason = 7;
587 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
588 dt->timebase = cpu_to_be64(now + vc->tb_offset);
589 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
590 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
591 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
593 if (dt == vcpu->arch.dtl.pinned_end)
594 dt = vcpu->arch.dtl.pinned_addr;
595 vcpu->arch.dtl_ptr = dt;
596 /* order writing *dt vs. writing vpa->dtl_idx */
598 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
599 vcpu->arch.dtl.dirty = true;
602 static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
604 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
606 if ((!vcpu->arch.vcore->arch_compat) &&
607 cpu_has_feature(CPU_FTR_ARCH_207S))
612 static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
613 unsigned long resource, unsigned long value1,
614 unsigned long value2)
617 case H_SET_MODE_RESOURCE_SET_CIABR:
618 if (!kvmppc_power8_compatible(vcpu))
623 return H_UNSUPPORTED_FLAG_START;
624 /* Guests can't breakpoint the hypervisor */
625 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
627 vcpu->arch.ciabr = value1;
629 case H_SET_MODE_RESOURCE_SET_DAWR:
630 if (!kvmppc_power8_compatible(vcpu))
633 return H_UNSUPPORTED_FLAG_START;
634 if (value2 & DABRX_HYP)
636 vcpu->arch.dawr = value1;
637 vcpu->arch.dawrx = value2;
644 static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
646 struct kvmppc_vcore *vcore = target->arch.vcore;
649 * We expect to have been called by the real mode handler
650 * (kvmppc_rm_h_confer()) which would have directly returned
651 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
652 * have useful work to do and should not confer) so we don't
656 spin_lock(&vcore->lock);
657 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
658 vcore->vcore_state != VCORE_INACTIVE &&
660 target = vcore->runner;
661 spin_unlock(&vcore->lock);
663 return kvm_vcpu_yield_to(target);
666 static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
669 struct lppaca *lppaca;
671 spin_lock(&vcpu->arch.vpa_update_lock);
672 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
674 yield_count = be32_to_cpu(lppaca->yield_count);
675 spin_unlock(&vcpu->arch.vpa_update_lock);
679 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
681 unsigned long req = kvmppc_get_gpr(vcpu, 3);
682 unsigned long target, ret = H_SUCCESS;
684 struct kvm_vcpu *tvcpu;
687 if (req <= MAX_HCALL_OPCODE &&
688 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
695 target = kvmppc_get_gpr(vcpu, 4);
696 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
701 tvcpu->arch.prodded = 1;
703 if (vcpu->arch.ceded) {
704 if (waitqueue_active(&vcpu->wq)) {
705 wake_up_interruptible(&vcpu->wq);
706 vcpu->stat.halt_wakeup++;
711 target = kvmppc_get_gpr(vcpu, 4);
714 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
719 yield_count = kvmppc_get_gpr(vcpu, 5);
720 if (kvmppc_get_yield_count(tvcpu) != yield_count)
722 kvm_arch_vcpu_yield_to(tvcpu);
725 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
726 kvmppc_get_gpr(vcpu, 5),
727 kvmppc_get_gpr(vcpu, 6));
730 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
733 idx = srcu_read_lock(&vcpu->kvm->srcu);
734 rc = kvmppc_rtas_hcall(vcpu);
735 srcu_read_unlock(&vcpu->kvm->srcu, idx);
742 /* Send the error out to userspace via KVM_RUN */
744 case H_LOGICAL_CI_LOAD:
745 ret = kvmppc_h_logical_ci_load(vcpu);
746 if (ret == H_TOO_HARD)
749 case H_LOGICAL_CI_STORE:
750 ret = kvmppc_h_logical_ci_store(vcpu);
751 if (ret == H_TOO_HARD)
755 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
756 kvmppc_get_gpr(vcpu, 5),
757 kvmppc_get_gpr(vcpu, 6),
758 kvmppc_get_gpr(vcpu, 7));
759 if (ret == H_TOO_HARD)
768 if (kvmppc_xics_enabled(vcpu)) {
769 ret = kvmppc_xics_hcall(vcpu, req);
774 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
775 kvmppc_get_gpr(vcpu, 5),
776 kvmppc_get_gpr(vcpu, 6));
777 if (ret == H_TOO_HARD)
780 case H_PUT_TCE_INDIRECT:
781 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
782 kvmppc_get_gpr(vcpu, 5),
783 kvmppc_get_gpr(vcpu, 6),
784 kvmppc_get_gpr(vcpu, 7));
785 if (ret == H_TOO_HARD)
789 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
790 kvmppc_get_gpr(vcpu, 5),
791 kvmppc_get_gpr(vcpu, 6),
792 kvmppc_get_gpr(vcpu, 7));
793 if (ret == H_TOO_HARD)
799 kvmppc_set_gpr(vcpu, 3, ret);
800 vcpu->arch.hcall_needed = 0;
804 static int kvmppc_hcall_impl_hv(unsigned long cmd)
812 case H_LOGICAL_CI_LOAD:
813 case H_LOGICAL_CI_STORE:
814 #ifdef CONFIG_KVM_XICS
825 /* See if it's in the real-mode table */
826 return kvmppc_hcall_impl_hv_realmode(cmd);
829 static int kvmppc_emulate_debug_inst(struct kvm_run *run,
830 struct kvm_vcpu *vcpu)
834 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
837 * Fetch failed, so return to guest and
838 * try executing it again.
843 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
844 run->exit_reason = KVM_EXIT_DEBUG;
845 run->debug.arch.address = kvmppc_get_pc(vcpu);
848 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
853 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
854 struct task_struct *tsk)
858 vcpu->stat.sum_exits++;
861 * This can happen if an interrupt occurs in the last stages
862 * of guest entry or the first stages of guest exit (i.e. after
863 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
864 * and before setting it to KVM_GUEST_MODE_HOST_HV).
865 * That can happen due to a bug, or due to a machine check
866 * occurring at just the wrong time.
868 if (vcpu->arch.shregs.msr & MSR_HV) {
869 printk(KERN_EMERG "KVM trap in HV mode!\n");
870 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
871 vcpu->arch.trap, kvmppc_get_pc(vcpu),
872 vcpu->arch.shregs.msr);
873 kvmppc_dump_regs(vcpu);
874 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
875 run->hw.hardware_exit_reason = vcpu->arch.trap;
878 run->exit_reason = KVM_EXIT_UNKNOWN;
879 run->ready_for_interrupt_injection = 1;
880 switch (vcpu->arch.trap) {
881 /* We're good on these - the host merely wanted to get our attention */
882 case BOOK3S_INTERRUPT_HV_DECREMENTER:
883 vcpu->stat.dec_exits++;
886 case BOOK3S_INTERRUPT_EXTERNAL:
887 case BOOK3S_INTERRUPT_H_DOORBELL:
888 vcpu->stat.ext_intr_exits++;
891 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
892 case BOOK3S_INTERRUPT_HMI:
893 case BOOK3S_INTERRUPT_PERFMON:
896 case BOOK3S_INTERRUPT_MACHINE_CHECK:
898 * Deliver a machine check interrupt to the guest.
899 * We have to do this, even if the host has handled the
900 * machine check, because machine checks use SRR0/1 and
901 * the interrupt might have trashed guest state in them.
903 kvmppc_book3s_queue_irqprio(vcpu,
904 BOOK3S_INTERRUPT_MACHINE_CHECK);
907 case BOOK3S_INTERRUPT_PROGRAM:
911 * Normally program interrupts are delivered directly
912 * to the guest by the hardware, but we can get here
913 * as a result of a hypervisor emulation interrupt
914 * (e40) getting turned into a 700 by BML RTAS.
916 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
917 kvmppc_core_queue_program(vcpu, flags);
921 case BOOK3S_INTERRUPT_SYSCALL:
923 /* hcall - punt to userspace */
926 /* hypercall with MSR_PR has already been handled in rmode,
927 * and never reaches here.
930 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
931 for (i = 0; i < 9; ++i)
932 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
933 run->exit_reason = KVM_EXIT_PAPR_HCALL;
934 vcpu->arch.hcall_needed = 1;
939 * We get these next two if the guest accesses a page which it thinks
940 * it has mapped but which is not actually present, either because
941 * it is for an emulated I/O device or because the corresonding
942 * host page has been paged out. Any other HDSI/HISI interrupts
943 * have been handled already.
945 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
946 r = RESUME_PAGE_FAULT;
948 case BOOK3S_INTERRUPT_H_INST_STORAGE:
949 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
950 vcpu->arch.fault_dsisr = 0;
951 r = RESUME_PAGE_FAULT;
954 * This occurs if the guest executes an illegal instruction.
955 * If the guest debug is disabled, generate a program interrupt
956 * to the guest. If guest debug is enabled, we need to check
957 * whether the instruction is a software breakpoint instruction.
958 * Accordingly return to Guest or Host.
960 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
961 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
962 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
963 swab32(vcpu->arch.emul_inst) :
964 vcpu->arch.emul_inst;
965 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
966 r = kvmppc_emulate_debug_inst(run, vcpu);
968 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
973 * This occurs if the guest (kernel or userspace), does something that
974 * is prohibited by HFSCR. We just generate a program interrupt to
977 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
978 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
982 kvmppc_dump_regs(vcpu);
983 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
984 vcpu->arch.trap, kvmppc_get_pc(vcpu),
985 vcpu->arch.shregs.msr);
986 run->hw.hardware_exit_reason = vcpu->arch.trap;
994 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
995 struct kvm_sregs *sregs)
999 memset(sregs, 0, sizeof(struct kvm_sregs));
1000 sregs->pvr = vcpu->arch.pvr;
1001 for (i = 0; i < vcpu->arch.slb_max; i++) {
1002 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1003 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1009 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1010 struct kvm_sregs *sregs)
1014 /* Only accept the same PVR as the host's, since we can't spoof it */
1015 if (sregs->pvr != vcpu->arch.pvr)
1019 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1020 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1021 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1022 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1026 vcpu->arch.slb_max = j;
1031 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1032 bool preserve_top32)
1034 struct kvm *kvm = vcpu->kvm;
1035 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1038 mutex_lock(&kvm->lock);
1039 spin_lock(&vc->lock);
1041 * If ILE (interrupt little-endian) has changed, update the
1042 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1044 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
1045 struct kvm_vcpu *vcpu;
1048 kvm_for_each_vcpu(i, vcpu, kvm) {
1049 if (vcpu->arch.vcore != vc)
1051 if (new_lpcr & LPCR_ILE)
1052 vcpu->arch.intr_msr |= MSR_LE;
1054 vcpu->arch.intr_msr &= ~MSR_LE;
1059 * Userspace can only modify DPFD (default prefetch depth),
1060 * ILE (interrupt little-endian) and TC (translation control).
1061 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
1063 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
1064 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1067 /* Broken 32-bit version of LPCR must not clear top bits */
1070 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1071 spin_unlock(&vc->lock);
1072 mutex_unlock(&kvm->lock);
1075 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1076 union kvmppc_one_reg *val)
1082 case KVM_REG_PPC_DEBUG_INST:
1083 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1085 case KVM_REG_PPC_HIOR:
1086 *val = get_reg_val(id, 0);
1088 case KVM_REG_PPC_DABR:
1089 *val = get_reg_val(id, vcpu->arch.dabr);
1091 case KVM_REG_PPC_DABRX:
1092 *val = get_reg_val(id, vcpu->arch.dabrx);
1094 case KVM_REG_PPC_DSCR:
1095 *val = get_reg_val(id, vcpu->arch.dscr);
1097 case KVM_REG_PPC_PURR:
1098 *val = get_reg_val(id, vcpu->arch.purr);
1100 case KVM_REG_PPC_SPURR:
1101 *val = get_reg_val(id, vcpu->arch.spurr);
1103 case KVM_REG_PPC_AMR:
1104 *val = get_reg_val(id, vcpu->arch.amr);
1106 case KVM_REG_PPC_UAMOR:
1107 *val = get_reg_val(id, vcpu->arch.uamor);
1109 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1110 i = id - KVM_REG_PPC_MMCR0;
1111 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1113 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1114 i = id - KVM_REG_PPC_PMC1;
1115 *val = get_reg_val(id, vcpu->arch.pmc[i]);
1117 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1118 i = id - KVM_REG_PPC_SPMC1;
1119 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1121 case KVM_REG_PPC_SIAR:
1122 *val = get_reg_val(id, vcpu->arch.siar);
1124 case KVM_REG_PPC_SDAR:
1125 *val = get_reg_val(id, vcpu->arch.sdar);
1127 case KVM_REG_PPC_SIER:
1128 *val = get_reg_val(id, vcpu->arch.sier);
1130 case KVM_REG_PPC_IAMR:
1131 *val = get_reg_val(id, vcpu->arch.iamr);
1133 case KVM_REG_PPC_PSPB:
1134 *val = get_reg_val(id, vcpu->arch.pspb);
1136 case KVM_REG_PPC_DPDES:
1137 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1139 case KVM_REG_PPC_DAWR:
1140 *val = get_reg_val(id, vcpu->arch.dawr);
1142 case KVM_REG_PPC_DAWRX:
1143 *val = get_reg_val(id, vcpu->arch.dawrx);
1145 case KVM_REG_PPC_CIABR:
1146 *val = get_reg_val(id, vcpu->arch.ciabr);
1148 case KVM_REG_PPC_CSIGR:
1149 *val = get_reg_val(id, vcpu->arch.csigr);
1151 case KVM_REG_PPC_TACR:
1152 *val = get_reg_val(id, vcpu->arch.tacr);
1154 case KVM_REG_PPC_TCSCR:
1155 *val = get_reg_val(id, vcpu->arch.tcscr);
1157 case KVM_REG_PPC_PID:
1158 *val = get_reg_val(id, vcpu->arch.pid);
1160 case KVM_REG_PPC_ACOP:
1161 *val = get_reg_val(id, vcpu->arch.acop);
1163 case KVM_REG_PPC_WORT:
1164 *val = get_reg_val(id, vcpu->arch.wort);
1166 case KVM_REG_PPC_VPA_ADDR:
1167 spin_lock(&vcpu->arch.vpa_update_lock);
1168 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1169 spin_unlock(&vcpu->arch.vpa_update_lock);
1171 case KVM_REG_PPC_VPA_SLB:
1172 spin_lock(&vcpu->arch.vpa_update_lock);
1173 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1174 val->vpaval.length = vcpu->arch.slb_shadow.len;
1175 spin_unlock(&vcpu->arch.vpa_update_lock);
1177 case KVM_REG_PPC_VPA_DTL:
1178 spin_lock(&vcpu->arch.vpa_update_lock);
1179 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1180 val->vpaval.length = vcpu->arch.dtl.len;
1181 spin_unlock(&vcpu->arch.vpa_update_lock);
1183 case KVM_REG_PPC_TB_OFFSET:
1184 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1186 case KVM_REG_PPC_LPCR:
1187 case KVM_REG_PPC_LPCR_64:
1188 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1190 case KVM_REG_PPC_PPR:
1191 *val = get_reg_val(id, vcpu->arch.ppr);
1193 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1194 case KVM_REG_PPC_TFHAR:
1195 *val = get_reg_val(id, vcpu->arch.tfhar);
1197 case KVM_REG_PPC_TFIAR:
1198 *val = get_reg_val(id, vcpu->arch.tfiar);
1200 case KVM_REG_PPC_TEXASR:
1201 *val = get_reg_val(id, vcpu->arch.texasr);
1203 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1204 i = id - KVM_REG_PPC_TM_GPR0;
1205 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1207 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1210 i = id - KVM_REG_PPC_TM_VSR0;
1212 for (j = 0; j < TS_FPRWIDTH; j++)
1213 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1215 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1216 val->vval = vcpu->arch.vr_tm.vr[i-32];
1222 case KVM_REG_PPC_TM_CR:
1223 *val = get_reg_val(id, vcpu->arch.cr_tm);
1225 case KVM_REG_PPC_TM_LR:
1226 *val = get_reg_val(id, vcpu->arch.lr_tm);
1228 case KVM_REG_PPC_TM_CTR:
1229 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1231 case KVM_REG_PPC_TM_FPSCR:
1232 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1234 case KVM_REG_PPC_TM_AMR:
1235 *val = get_reg_val(id, vcpu->arch.amr_tm);
1237 case KVM_REG_PPC_TM_PPR:
1238 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1240 case KVM_REG_PPC_TM_VRSAVE:
1241 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1243 case KVM_REG_PPC_TM_VSCR:
1244 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1245 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1249 case KVM_REG_PPC_TM_DSCR:
1250 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1252 case KVM_REG_PPC_TM_TAR:
1253 *val = get_reg_val(id, vcpu->arch.tar_tm);
1256 case KVM_REG_PPC_ARCH_COMPAT:
1257 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1267 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1268 union kvmppc_one_reg *val)
1272 unsigned long addr, len;
1275 case KVM_REG_PPC_HIOR:
1276 /* Only allow this to be set to zero */
1277 if (set_reg_val(id, *val))
1280 case KVM_REG_PPC_DABR:
1281 vcpu->arch.dabr = set_reg_val(id, *val);
1283 case KVM_REG_PPC_DABRX:
1284 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1286 case KVM_REG_PPC_DSCR:
1287 vcpu->arch.dscr = set_reg_val(id, *val);
1289 case KVM_REG_PPC_PURR:
1290 vcpu->arch.purr = set_reg_val(id, *val);
1292 case KVM_REG_PPC_SPURR:
1293 vcpu->arch.spurr = set_reg_val(id, *val);
1295 case KVM_REG_PPC_AMR:
1296 vcpu->arch.amr = set_reg_val(id, *val);
1298 case KVM_REG_PPC_UAMOR:
1299 vcpu->arch.uamor = set_reg_val(id, *val);
1301 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
1302 i = id - KVM_REG_PPC_MMCR0;
1303 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1305 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1306 i = id - KVM_REG_PPC_PMC1;
1307 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1309 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1310 i = id - KVM_REG_PPC_SPMC1;
1311 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1313 case KVM_REG_PPC_SIAR:
1314 vcpu->arch.siar = set_reg_val(id, *val);
1316 case KVM_REG_PPC_SDAR:
1317 vcpu->arch.sdar = set_reg_val(id, *val);
1319 case KVM_REG_PPC_SIER:
1320 vcpu->arch.sier = set_reg_val(id, *val);
1322 case KVM_REG_PPC_IAMR:
1323 vcpu->arch.iamr = set_reg_val(id, *val);
1325 case KVM_REG_PPC_PSPB:
1326 vcpu->arch.pspb = set_reg_val(id, *val);
1328 case KVM_REG_PPC_DPDES:
1329 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1331 case KVM_REG_PPC_DAWR:
1332 vcpu->arch.dawr = set_reg_val(id, *val);
1334 case KVM_REG_PPC_DAWRX:
1335 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1337 case KVM_REG_PPC_CIABR:
1338 vcpu->arch.ciabr = set_reg_val(id, *val);
1339 /* Don't allow setting breakpoints in hypervisor code */
1340 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1341 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1343 case KVM_REG_PPC_CSIGR:
1344 vcpu->arch.csigr = set_reg_val(id, *val);
1346 case KVM_REG_PPC_TACR:
1347 vcpu->arch.tacr = set_reg_val(id, *val);
1349 case KVM_REG_PPC_TCSCR:
1350 vcpu->arch.tcscr = set_reg_val(id, *val);
1352 case KVM_REG_PPC_PID:
1353 vcpu->arch.pid = set_reg_val(id, *val);
1355 case KVM_REG_PPC_ACOP:
1356 vcpu->arch.acop = set_reg_val(id, *val);
1358 case KVM_REG_PPC_WORT:
1359 vcpu->arch.wort = set_reg_val(id, *val);
1361 case KVM_REG_PPC_VPA_ADDR:
1362 addr = set_reg_val(id, *val);
1364 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1365 vcpu->arch.dtl.next_gpa))
1367 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1369 case KVM_REG_PPC_VPA_SLB:
1370 addr = val->vpaval.addr;
1371 len = val->vpaval.length;
1373 if (addr && !vcpu->arch.vpa.next_gpa)
1375 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1377 case KVM_REG_PPC_VPA_DTL:
1378 addr = val->vpaval.addr;
1379 len = val->vpaval.length;
1381 if (addr && (len < sizeof(struct dtl_entry) ||
1382 !vcpu->arch.vpa.next_gpa))
1384 len -= len % sizeof(struct dtl_entry);
1385 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1387 case KVM_REG_PPC_TB_OFFSET:
1388 /* round up to multiple of 2^24 */
1389 vcpu->arch.vcore->tb_offset =
1390 ALIGN(set_reg_val(id, *val), 1UL << 24);
1392 case KVM_REG_PPC_LPCR:
1393 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1395 case KVM_REG_PPC_LPCR_64:
1396 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
1398 case KVM_REG_PPC_PPR:
1399 vcpu->arch.ppr = set_reg_val(id, *val);
1401 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1402 case KVM_REG_PPC_TFHAR:
1403 vcpu->arch.tfhar = set_reg_val(id, *val);
1405 case KVM_REG_PPC_TFIAR:
1406 vcpu->arch.tfiar = set_reg_val(id, *val);
1408 case KVM_REG_PPC_TEXASR:
1409 vcpu->arch.texasr = set_reg_val(id, *val);
1411 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1412 i = id - KVM_REG_PPC_TM_GPR0;
1413 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1415 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1418 i = id - KVM_REG_PPC_TM_VSR0;
1420 for (j = 0; j < TS_FPRWIDTH; j++)
1421 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1423 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1424 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1429 case KVM_REG_PPC_TM_CR:
1430 vcpu->arch.cr_tm = set_reg_val(id, *val);
1432 case KVM_REG_PPC_TM_LR:
1433 vcpu->arch.lr_tm = set_reg_val(id, *val);
1435 case KVM_REG_PPC_TM_CTR:
1436 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1438 case KVM_REG_PPC_TM_FPSCR:
1439 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1441 case KVM_REG_PPC_TM_AMR:
1442 vcpu->arch.amr_tm = set_reg_val(id, *val);
1444 case KVM_REG_PPC_TM_PPR:
1445 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1447 case KVM_REG_PPC_TM_VRSAVE:
1448 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1450 case KVM_REG_PPC_TM_VSCR:
1451 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1452 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1456 case KVM_REG_PPC_TM_DSCR:
1457 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1459 case KVM_REG_PPC_TM_TAR:
1460 vcpu->arch.tar_tm = set_reg_val(id, *val);
1463 case KVM_REG_PPC_ARCH_COMPAT:
1464 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1474 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1476 struct kvmppc_vcore *vcore;
1478 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1483 INIT_LIST_HEAD(&vcore->runnable_threads);
1484 spin_lock_init(&vcore->lock);
1485 spin_lock_init(&vcore->stoltb_lock);
1486 init_waitqueue_head(&vcore->wq);
1487 vcore->preempt_tb = TB_NIL;
1488 vcore->lpcr = kvm->arch.lpcr;
1489 vcore->first_vcpuid = core * threads_per_subcore;
1491 INIT_LIST_HEAD(&vcore->preempt_list);
1496 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1497 static struct debugfs_timings_element {
1501 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1502 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1503 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1504 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1505 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1508 #define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1510 struct debugfs_timings_state {
1511 struct kvm_vcpu *vcpu;
1512 unsigned int buflen;
1513 char buf[N_TIMINGS * 100];
1516 static int debugfs_timings_open(struct inode *inode, struct file *file)
1518 struct kvm_vcpu *vcpu = inode->i_private;
1519 struct debugfs_timings_state *p;
1521 p = kzalloc(sizeof(*p), GFP_KERNEL);
1525 kvm_get_kvm(vcpu->kvm);
1527 file->private_data = p;
1529 return nonseekable_open(inode, file);
1532 static int debugfs_timings_release(struct inode *inode, struct file *file)
1534 struct debugfs_timings_state *p = file->private_data;
1536 kvm_put_kvm(p->vcpu->kvm);
1541 static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1542 size_t len, loff_t *ppos)
1544 struct debugfs_timings_state *p = file->private_data;
1545 struct kvm_vcpu *vcpu = p->vcpu;
1547 struct kvmhv_tb_accumulator tb;
1556 buf_end = s + sizeof(p->buf);
1557 for (i = 0; i < N_TIMINGS; ++i) {
1558 struct kvmhv_tb_accumulator *acc;
1560 acc = (struct kvmhv_tb_accumulator *)
1561 ((unsigned long)vcpu + timings[i].offset);
1563 for (loops = 0; loops < 1000; ++loops) {
1564 count = acc->seqcount;
1569 if (count == acc->seqcount) {
1577 snprintf(s, buf_end - s, "%s: stuck\n",
1580 snprintf(s, buf_end - s,
1581 "%s: %llu %llu %llu %llu\n",
1582 timings[i].name, count / 2,
1583 tb_to_ns(tb.tb_total),
1584 tb_to_ns(tb.tb_min),
1585 tb_to_ns(tb.tb_max));
1588 p->buflen = s - p->buf;
1592 if (pos >= p->buflen)
1594 if (len > p->buflen - pos)
1595 len = p->buflen - pos;
1596 n = copy_to_user(buf, p->buf + pos, len);
1606 static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1607 size_t len, loff_t *ppos)
1612 static const struct file_operations debugfs_timings_ops = {
1613 .owner = THIS_MODULE,
1614 .open = debugfs_timings_open,
1615 .release = debugfs_timings_release,
1616 .read = debugfs_timings_read,
1617 .write = debugfs_timings_write,
1618 .llseek = generic_file_llseek,
1621 /* Create a debugfs directory for the vcpu */
1622 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1625 struct kvm *kvm = vcpu->kvm;
1627 snprintf(buf, sizeof(buf), "vcpu%u", id);
1628 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1630 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1631 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1633 vcpu->arch.debugfs_timings =
1634 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1635 vcpu, &debugfs_timings_ops);
1638 #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1639 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1642 #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1644 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1647 struct kvm_vcpu *vcpu;
1650 struct kvmppc_vcore *vcore;
1652 core = id / threads_per_subcore;
1653 if (core >= KVM_MAX_VCORES)
1657 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1661 err = kvm_vcpu_init(vcpu, kvm, id);
1665 vcpu->arch.shared = &vcpu->arch.shregs;
1666 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1668 * The shared struct is never shared on HV,
1669 * so we can always use host endianness
1671 #ifdef __BIG_ENDIAN__
1672 vcpu->arch.shared_big_endian = true;
1674 vcpu->arch.shared_big_endian = false;
1677 vcpu->arch.mmcr[0] = MMCR0_FC;
1678 vcpu->arch.ctrl = CTRL_RUNLATCH;
1679 /* default to host PVR, since we can't spoof it */
1680 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1681 spin_lock_init(&vcpu->arch.vpa_update_lock);
1682 spin_lock_init(&vcpu->arch.tbacct_lock);
1683 vcpu->arch.busy_preempt = TB_NIL;
1684 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
1686 kvmppc_mmu_book3s_hv_init(vcpu);
1688 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1690 init_waitqueue_head(&vcpu->arch.cpu_run);
1692 mutex_lock(&kvm->lock);
1693 vcore = kvm->arch.vcores[core];
1695 vcore = kvmppc_vcore_create(kvm, core);
1696 kvm->arch.vcores[core] = vcore;
1697 kvm->arch.online_vcores++;
1699 mutex_unlock(&kvm->lock);
1704 spin_lock(&vcore->lock);
1705 ++vcore->num_threads;
1706 spin_unlock(&vcore->lock);
1707 vcpu->arch.vcore = vcore;
1708 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1709 vcpu->arch.thread_cpu = -1;
1711 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1712 kvmppc_sanity_check(vcpu);
1714 debugfs_vcpu_init(vcpu, id);
1719 kmem_cache_free(kvm_vcpu_cache, vcpu);
1721 return ERR_PTR(err);
1724 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1726 if (vpa->pinned_addr)
1727 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1731 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1733 spin_lock(&vcpu->arch.vpa_update_lock);
1734 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1735 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1736 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1737 spin_unlock(&vcpu->arch.vpa_update_lock);
1738 kvm_vcpu_uninit(vcpu);
1739 kmem_cache_free(kvm_vcpu_cache, vcpu);
1742 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1744 /* Indicate we want to get back into the guest */
1748 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1750 unsigned long dec_nsec, now;
1753 if (now > vcpu->arch.dec_expires) {
1754 /* decrementer has already gone negative */
1755 kvmppc_core_queue_dec(vcpu);
1756 kvmppc_core_prepare_to_enter(vcpu);
1759 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1761 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1763 vcpu->arch.timer_running = 1;
1766 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1768 vcpu->arch.ceded = 0;
1769 if (vcpu->arch.timer_running) {
1770 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1771 vcpu->arch.timer_running = 0;
1775 extern void __kvmppc_vcore_entry(void);
1777 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1778 struct kvm_vcpu *vcpu)
1782 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1784 spin_lock_irq(&vcpu->arch.tbacct_lock);
1786 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1787 vcpu->arch.stolen_logged;
1788 vcpu->arch.busy_preempt = now;
1789 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1790 spin_unlock_irq(&vcpu->arch.tbacct_lock);
1792 list_del(&vcpu->arch.run_list);
1795 static int kvmppc_grab_hwthread(int cpu)
1797 struct paca_struct *tpaca;
1798 long timeout = 10000;
1802 /* Ensure the thread won't go into the kernel if it wakes */
1803 tpaca->kvm_hstate.kvm_vcpu = NULL;
1804 tpaca->kvm_hstate.kvm_vcore = NULL;
1805 tpaca->kvm_hstate.napping = 0;
1807 tpaca->kvm_hstate.hwthread_req = 1;
1810 * If the thread is already executing in the kernel (e.g. handling
1811 * a stray interrupt), wait for it to get back to nap mode.
1812 * The smp_mb() is to ensure that our setting of hwthread_req
1813 * is visible before we look at hwthread_state, so if this
1814 * races with the code at system_reset_pSeries and the thread
1815 * misses our setting of hwthread_req, we are sure to see its
1816 * setting of hwthread_state, and vice versa.
1819 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1820 if (--timeout <= 0) {
1821 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1829 static void kvmppc_release_hwthread(int cpu)
1831 struct paca_struct *tpaca;
1834 tpaca->kvm_hstate.hwthread_req = 0;
1835 tpaca->kvm_hstate.kvm_vcpu = NULL;
1836 tpaca->kvm_hstate.kvm_vcore = NULL;
1837 tpaca->kvm_hstate.kvm_split_mode = NULL;
1840 static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
1843 struct paca_struct *tpaca;
1844 struct kvmppc_vcore *mvc = vc->master_vcore;
1848 if (vcpu->arch.timer_running) {
1849 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1850 vcpu->arch.timer_running = 0;
1852 cpu += vcpu->arch.ptid;
1853 vcpu->cpu = mvc->pcpu;
1854 vcpu->arch.thread_cpu = cpu;
1857 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1858 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
1859 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
1861 tpaca->kvm_hstate.kvm_vcore = mvc;
1862 if (cpu != smp_processor_id())
1863 kvmppc_ipi_thread(cpu);
1866 static void kvmppc_wait_for_nap(void)
1868 int cpu = smp_processor_id();
1871 for (loops = 0; loops < 1000000; ++loops) {
1873 * Check if all threads are finished.
1874 * We set the vcore pointer when starting a thread
1875 * and the thread clears it when finished, so we look
1876 * for any threads that still have a non-NULL vcore ptr.
1878 for (i = 1; i < threads_per_subcore; ++i)
1879 if (paca[cpu + i].kvm_hstate.kvm_vcore)
1881 if (i == threads_per_subcore) {
1888 for (i = 1; i < threads_per_subcore; ++i)
1889 if (paca[cpu + i].kvm_hstate.kvm_vcore)
1890 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
1894 * Check that we are on thread 0 and that any other threads in
1895 * this core are off-line. Then grab the threads so they can't
1898 static int on_primary_thread(void)
1900 int cpu = smp_processor_id();
1903 /* Are we on a primary subcore? */
1904 if (cpu_thread_in_subcore(cpu))
1908 while (++thr < threads_per_subcore)
1909 if (cpu_online(cpu + thr))
1912 /* Grab all hw threads so they can't go into the kernel */
1913 for (thr = 1; thr < threads_per_subcore; ++thr) {
1914 if (kvmppc_grab_hwthread(cpu + thr)) {
1915 /* Couldn't grab one; let the others go */
1917 kvmppc_release_hwthread(cpu + thr);
1918 } while (--thr > 0);
1926 * A list of virtual cores for each physical CPU.
1927 * These are vcores that could run but their runner VCPU tasks are
1928 * (or may be) preempted.
1930 struct preempted_vcore_list {
1931 struct list_head list;
1935 static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
1937 static void init_vcore_lists(void)
1941 for_each_possible_cpu(cpu) {
1942 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
1943 spin_lock_init(&lp->lock);
1944 INIT_LIST_HEAD(&lp->list);
1948 static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
1950 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
1952 vc->vcore_state = VCORE_PREEMPT;
1953 vc->pcpu = smp_processor_id();
1954 if (vc->num_threads < threads_per_subcore) {
1955 spin_lock(&lp->lock);
1956 list_add_tail(&vc->preempt_list, &lp->list);
1957 spin_unlock(&lp->lock);
1960 /* Start accumulating stolen time */
1961 kvmppc_core_start_stolen(vc);
1964 static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
1966 struct preempted_vcore_list *lp;
1968 kvmppc_core_end_stolen(vc);
1969 if (!list_empty(&vc->preempt_list)) {
1970 lp = &per_cpu(preempted_vcores, vc->pcpu);
1971 spin_lock(&lp->lock);
1972 list_del_init(&vc->preempt_list);
1973 spin_unlock(&lp->lock);
1975 vc->vcore_state = VCORE_INACTIVE;
1979 * This stores information about the virtual cores currently
1980 * assigned to a physical core.
1984 int max_subcore_threads;
1986 int subcore_threads[MAX_SUBCORES];
1987 struct kvm *subcore_vm[MAX_SUBCORES];
1988 struct list_head vcs[MAX_SUBCORES];
1992 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
1993 * respectively in 2-way micro-threading (split-core) mode.
1995 static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
1997 static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
2001 memset(cip, 0, sizeof(*cip));
2002 cip->n_subcores = 1;
2003 cip->max_subcore_threads = vc->num_threads;
2004 cip->total_threads = vc->num_threads;
2005 cip->subcore_threads[0] = vc->num_threads;
2006 cip->subcore_vm[0] = vc->kvm;
2007 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2008 INIT_LIST_HEAD(&cip->vcs[sub]);
2009 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2012 static bool subcore_config_ok(int n_subcores, int n_threads)
2014 /* Can only dynamically split if unsplit to begin with */
2015 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2017 if (n_subcores > MAX_SUBCORES)
2019 if (n_subcores > 1) {
2020 if (!(dynamic_mt_modes & 2))
2022 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2026 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
2029 static void init_master_vcore(struct kvmppc_vcore *vc)
2031 vc->master_vcore = vc;
2032 vc->entry_exit_map = 0;
2034 vc->napping_threads = 0;
2035 vc->conferring_threads = 0;
2039 * See if the existing subcores can be split into 3 (or fewer) subcores
2040 * of at most two threads each, so we can fit in another vcore. This
2041 * assumes there are at most two subcores and at most 6 threads in total.
2043 static bool can_split_piggybacked_subcores(struct core_info *cip)
2048 int n_subcores = cip->n_subcores;
2049 struct kvmppc_vcore *vc, *vcnext;
2050 struct kvmppc_vcore *master_vc = NULL;
2052 for (sub = 0; sub < cip->n_subcores; ++sub) {
2053 if (cip->subcore_threads[sub] <= 2)
2058 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2060 if (vc->num_threads > 2)
2062 n_subcores += (cip->subcore_threads[sub] - 1) >> 1;
2064 if (large_sub < 0 || !subcore_config_ok(n_subcores + 1, 2))
2068 * Seems feasible, so go through and move vcores to new subcores.
2069 * Note that when we have two or more vcores in one subcore,
2070 * all those vcores must have only one thread each.
2072 new_sub = cip->n_subcores;
2075 list_for_each_entry_safe(vc, vcnext, &cip->vcs[sub], preempt_list) {
2077 list_del(&vc->preempt_list);
2078 list_add_tail(&vc->preempt_list, &cip->vcs[new_sub]);
2079 /* vc->num_threads must be 1 */
2080 if (++cip->subcore_threads[new_sub] == 1) {
2081 cip->subcore_vm[new_sub] = vc->kvm;
2082 init_master_vcore(vc);
2086 vc->master_vcore = master_vc;
2090 thr += vc->num_threads;
2092 cip->subcore_threads[large_sub] = 2;
2093 cip->max_subcore_threads = 2;
2098 static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2100 int n_threads = vc->num_threads;
2103 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2106 if (n_threads < cip->max_subcore_threads)
2107 n_threads = cip->max_subcore_threads;
2108 if (subcore_config_ok(cip->n_subcores + 1, n_threads)) {
2109 cip->max_subcore_threads = n_threads;
2110 } else if (cip->n_subcores <= 2 && cip->total_threads <= 6 &&
2111 vc->num_threads <= 2) {
2113 * We may be able to fit another subcore in by
2114 * splitting an existing subcore with 3 or 4
2115 * threads into two 2-thread subcores, or one
2116 * with 5 or 6 threads into three subcores.
2117 * We can only do this if those subcores have
2118 * piggybacked virtual cores.
2120 if (!can_split_piggybacked_subcores(cip))
2126 sub = cip->n_subcores;
2128 cip->total_threads += vc->num_threads;
2129 cip->subcore_threads[sub] = vc->num_threads;
2130 cip->subcore_vm[sub] = vc->kvm;
2131 init_master_vcore(vc);
2132 list_del(&vc->preempt_list);
2133 list_add_tail(&vc->preempt_list, &cip->vcs[sub]);
2138 static bool can_piggyback_subcore(struct kvmppc_vcore *pvc,
2139 struct core_info *cip, int sub)
2141 struct kvmppc_vcore *vc;
2144 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2147 /* require same VM and same per-core reg values */
2148 if (pvc->kvm != vc->kvm ||
2149 pvc->tb_offset != vc->tb_offset ||
2150 pvc->pcr != vc->pcr ||
2151 pvc->lpcr != vc->lpcr)
2154 /* P8 guest with > 1 thread per core would see wrong TIR value */
2155 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
2156 (vc->num_threads > 1 || pvc->num_threads > 1))
2159 n_thr = cip->subcore_threads[sub] + pvc->num_threads;
2160 if (n_thr > cip->max_subcore_threads) {
2161 if (!subcore_config_ok(cip->n_subcores, n_thr))
2163 cip->max_subcore_threads = n_thr;
2166 cip->total_threads += pvc->num_threads;
2167 cip->subcore_threads[sub] = n_thr;
2168 pvc->master_vcore = vc;
2169 list_del(&pvc->preempt_list);
2170 list_add_tail(&pvc->preempt_list, &cip->vcs[sub]);
2176 * Work out whether it is possible to piggyback the execution of
2177 * vcore *pvc onto the execution of the other vcores described in *cip.
2179 static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2184 if (cip->total_threads + pvc->num_threads > target_threads)
2186 for (sub = 0; sub < cip->n_subcores; ++sub)
2187 if (cip->subcore_threads[sub] &&
2188 can_piggyback_subcore(pvc, cip, sub))
2191 if (can_dynamic_split(pvc, cip))
2197 static void prepare_threads(struct kvmppc_vcore *vc)
2199 struct kvm_vcpu *vcpu, *vnext;
2201 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2203 if (signal_pending(vcpu->arch.run_task))
2204 vcpu->arch.ret = -EINTR;
2205 else if (vcpu->arch.vpa.update_pending ||
2206 vcpu->arch.slb_shadow.update_pending ||
2207 vcpu->arch.dtl.update_pending)
2208 vcpu->arch.ret = RESUME_GUEST;
2211 kvmppc_remove_runnable(vc, vcpu);
2212 wake_up(&vcpu->arch.cpu_run);
2216 static void collect_piggybacks(struct core_info *cip, int target_threads)
2218 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2219 struct kvmppc_vcore *pvc, *vcnext;
2221 spin_lock(&lp->lock);
2222 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2223 if (!spin_trylock(&pvc->lock))
2225 prepare_threads(pvc);
2226 if (!pvc->n_runnable) {
2227 list_del_init(&pvc->preempt_list);
2228 if (pvc->runner == NULL) {
2229 pvc->vcore_state = VCORE_INACTIVE;
2230 kvmppc_core_end_stolen(pvc);
2232 spin_unlock(&pvc->lock);
2235 if (!can_piggyback(pvc, cip, target_threads)) {
2236 spin_unlock(&pvc->lock);
2239 kvmppc_core_end_stolen(pvc);
2240 pvc->vcore_state = VCORE_PIGGYBACK;
2241 if (cip->total_threads >= target_threads)
2244 spin_unlock(&lp->lock);
2247 static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2249 int still_running = 0;
2252 struct kvm_vcpu *vcpu, *vnext;
2254 spin_lock(&vc->lock);
2256 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2258 /* cancel pending dec exception if dec is positive */
2259 if (now < vcpu->arch.dec_expires &&
2260 kvmppc_core_pending_dec(vcpu))
2261 kvmppc_core_dequeue_dec(vcpu);
2263 trace_kvm_guest_exit(vcpu);
2266 if (vcpu->arch.trap)
2267 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2268 vcpu->arch.run_task);
2270 vcpu->arch.ret = ret;
2271 vcpu->arch.trap = 0;
2273 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2274 if (vcpu->arch.pending_exceptions)
2275 kvmppc_core_prepare_to_enter(vcpu);
2276 if (vcpu->arch.ceded)
2277 kvmppc_set_timer(vcpu);
2281 kvmppc_remove_runnable(vc, vcpu);
2282 wake_up(&vcpu->arch.cpu_run);
2285 list_del_init(&vc->preempt_list);
2287 if (still_running > 0) {
2288 kvmppc_vcore_preempt(vc);
2289 } else if (vc->runner) {
2290 vc->vcore_state = VCORE_PREEMPT;
2291 kvmppc_core_start_stolen(vc);
2293 vc->vcore_state = VCORE_INACTIVE;
2295 if (vc->n_runnable > 0 && vc->runner == NULL) {
2296 /* make sure there's a candidate runner awake */
2297 vcpu = list_first_entry(&vc->runnable_threads,
2298 struct kvm_vcpu, arch.run_list);
2299 wake_up(&vcpu->arch.cpu_run);
2302 spin_unlock(&vc->lock);
2306 * Run a set of guest threads on a physical core.
2307 * Called with vc->lock held.
2309 static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
2311 struct kvm_vcpu *vcpu, *vnext;
2314 struct core_info core_info;
2315 struct kvmppc_vcore *pvc, *vcnext;
2316 struct kvm_split_mode split_info, *sip;
2317 int split, subcore_size, active;
2320 unsigned long cmd_bit, stat_bit;
2325 * Remove from the list any threads that have a signal pending
2326 * or need a VPA update done
2328 prepare_threads(vc);
2330 /* if the runner is no longer runnable, let the caller pick a new one */
2331 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2337 init_master_vcore(vc);
2338 vc->preempt_tb = TB_NIL;
2341 * Make sure we are running on primary threads, and that secondary
2342 * threads are offline. Also check if the number of threads in this
2343 * guest are greater than the current system threads per guest.
2345 if ((threads_per_core > 1) &&
2346 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
2347 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2349 vcpu->arch.ret = -EBUSY;
2350 kvmppc_remove_runnable(vc, vcpu);
2351 wake_up(&vcpu->arch.cpu_run);
2357 * See if we could run any other vcores on the physical core
2358 * along with this one.
2360 init_core_info(&core_info, vc);
2361 pcpu = smp_processor_id();
2362 target_threads = threads_per_subcore;
2363 if (target_smt_mode && target_smt_mode < target_threads)
2364 target_threads = target_smt_mode;
2365 if (vc->num_threads < target_threads)
2366 collect_piggybacks(&core_info, target_threads);
2368 /* Decide on micro-threading (split-core) mode */
2369 subcore_size = threads_per_subcore;
2370 cmd_bit = stat_bit = 0;
2371 split = core_info.n_subcores;
2374 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2375 if (split == 2 && (dynamic_mt_modes & 2)) {
2376 cmd_bit = HID0_POWER8_1TO2LPAR;
2377 stat_bit = HID0_POWER8_2LPARMODE;
2380 cmd_bit = HID0_POWER8_1TO4LPAR;
2381 stat_bit = HID0_POWER8_4LPARMODE;
2383 subcore_size = MAX_SMT_THREADS / split;
2385 memset(&split_info, 0, sizeof(split_info));
2386 split_info.rpr = mfspr(SPRN_RPR);
2387 split_info.pmmar = mfspr(SPRN_PMMAR);
2388 split_info.ldbar = mfspr(SPRN_LDBAR);
2389 split_info.subcore_size = subcore_size;
2390 for (sub = 0; sub < core_info.n_subcores; ++sub)
2391 split_info.master_vcs[sub] =
2392 list_first_entry(&core_info.vcs[sub],
2393 struct kvmppc_vcore, preempt_list);
2394 /* order writes to split_info before kvm_split_mode pointer */
2397 pcpu = smp_processor_id();
2398 for (thr = 0; thr < threads_per_subcore; ++thr)
2399 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2401 /* Initiate micro-threading (split-core) if required */
2403 unsigned long hid0 = mfspr(SPRN_HID0);
2405 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2407 mtspr(SPRN_HID0, hid0);
2410 hid0 = mfspr(SPRN_HID0);
2411 if (hid0 & stat_bit)
2417 /* Start all the threads */
2419 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2420 thr = subcore_thread_map[sub];
2423 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2424 pvc->pcpu = pcpu + thr;
2425 list_for_each_entry(vcpu, &pvc->runnable_threads,
2427 kvmppc_start_thread(vcpu, pvc);
2428 kvmppc_create_dtl_entry(vcpu, pvc);
2429 trace_kvm_guest_enter(vcpu);
2430 if (!vcpu->arch.ptid)
2432 active |= 1 << (thr + vcpu->arch.ptid);
2435 * We need to start the first thread of each subcore
2436 * even if it doesn't have a vcpu.
2438 if (pvc->master_vcore == pvc && !thr0_done)
2439 kvmppc_start_thread(NULL, pvc);
2440 thr += pvc->num_threads;
2445 * Ensure that split_info.do_nap is set after setting
2446 * the vcore pointer in the PACA of the secondaries.
2450 split_info.do_nap = 1; /* ask secondaries to nap when done */
2453 * When doing micro-threading, poke the inactive threads as well.
2454 * This gets them to the nap instruction after kvm_do_nap,
2455 * which reduces the time taken to unsplit later.
2458 for (thr = 1; thr < threads_per_subcore; ++thr)
2459 if (!(active & (1 << thr)))
2460 kvmppc_ipi_thread(pcpu + thr);
2462 vc->vcore_state = VCORE_RUNNING;
2465 trace_kvmppc_run_core(vc, 0);
2467 for (sub = 0; sub < core_info.n_subcores; ++sub)
2468 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2469 spin_unlock(&pvc->lock);
2473 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
2475 __kvmppc_vcore_entry();
2477 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2479 spin_lock(&vc->lock);
2480 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
2481 vc->vcore_state = VCORE_EXITING;
2483 /* wait for secondary threads to finish writing their state to memory */
2484 kvmppc_wait_for_nap();
2486 /* Return to whole-core mode if we split the core earlier */
2488 unsigned long hid0 = mfspr(SPRN_HID0);
2489 unsigned long loops = 0;
2491 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2492 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2494 mtspr(SPRN_HID0, hid0);
2497 hid0 = mfspr(SPRN_HID0);
2498 if (!(hid0 & stat_bit))
2503 split_info.do_nap = 0;
2506 /* Let secondaries go back to the offline loop */
2507 for (i = 0; i < threads_per_subcore; ++i) {
2508 kvmppc_release_hwthread(pcpu + i);
2509 if (sip && sip->napped[i])
2510 kvmppc_ipi_thread(pcpu + i);
2513 spin_unlock(&vc->lock);
2515 /* make sure updates to secondary vcpu structs are visible now */
2519 for (sub = 0; sub < core_info.n_subcores; ++sub)
2520 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2522 post_guest_process(pvc, pvc == vc);
2524 spin_lock(&vc->lock);
2528 vc->vcore_state = VCORE_INACTIVE;
2529 trace_kvmppc_run_core(vc, 1);
2533 * Wait for some other vcpu thread to execute us, and
2534 * wake us up when we need to handle something in the host.
2536 static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2537 struct kvm_vcpu *vcpu, int wait_state)
2541 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
2542 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2543 spin_unlock(&vc->lock);
2545 spin_lock(&vc->lock);
2547 finish_wait(&vcpu->arch.cpu_run, &wait);
2551 * All the vcpus in this vcore are idle, so wait for a decrementer
2552 * or external interrupt to one of the vcpus. vc->lock is held.
2554 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2556 struct kvm_vcpu *vcpu;
2561 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
2564 * Check one last time for pending exceptions and ceded state after
2565 * we put ourselves on the wait queue
2567 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
2568 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
2575 finish_wait(&vc->wq, &wait);
2579 vc->vcore_state = VCORE_SLEEPING;
2580 trace_kvmppc_vcore_blocked(vc, 0);
2581 spin_unlock(&vc->lock);
2583 finish_wait(&vc->wq, &wait);
2584 spin_lock(&vc->lock);
2585 vc->vcore_state = VCORE_INACTIVE;
2586 trace_kvmppc_vcore_blocked(vc, 1);
2589 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2592 struct kvmppc_vcore *vc;
2593 struct kvm_vcpu *v, *vn;
2595 trace_kvmppc_run_vcpu_enter(vcpu);
2597 kvm_run->exit_reason = 0;
2598 vcpu->arch.ret = RESUME_GUEST;
2599 vcpu->arch.trap = 0;
2600 kvmppc_update_vpas(vcpu);
2603 * Synchronize with other threads in this virtual core
2605 vc = vcpu->arch.vcore;
2606 spin_lock(&vc->lock);
2607 vcpu->arch.ceded = 0;
2608 vcpu->arch.run_task = current;
2609 vcpu->arch.kvm_run = kvm_run;
2610 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
2611 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
2612 vcpu->arch.busy_preempt = TB_NIL;
2613 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
2617 * This happens the first time this is called for a vcpu.
2618 * If the vcore is already running, we may be able to start
2619 * this thread straight away and have it join in.
2621 if (!signal_pending(current)) {
2622 if (vc->vcore_state == VCORE_PIGGYBACK) {
2623 struct kvmppc_vcore *mvc = vc->master_vcore;
2624 if (spin_trylock(&mvc->lock)) {
2625 if (mvc->vcore_state == VCORE_RUNNING &&
2626 !VCORE_IS_EXITING(mvc)) {
2627 kvmppc_create_dtl_entry(vcpu, vc);
2628 kvmppc_start_thread(vcpu, vc);
2629 trace_kvm_guest_enter(vcpu);
2631 spin_unlock(&mvc->lock);
2633 } else if (vc->vcore_state == VCORE_RUNNING &&
2634 !VCORE_IS_EXITING(vc)) {
2635 kvmppc_create_dtl_entry(vcpu, vc);
2636 kvmppc_start_thread(vcpu, vc);
2637 trace_kvm_guest_enter(vcpu);
2638 } else if (vc->vcore_state == VCORE_SLEEPING) {
2644 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2645 !signal_pending(current)) {
2646 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2647 kvmppc_vcore_end_preempt(vc);
2649 if (vc->vcore_state != VCORE_INACTIVE) {
2650 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
2653 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
2655 kvmppc_core_prepare_to_enter(v);
2656 if (signal_pending(v->arch.run_task)) {
2657 kvmppc_remove_runnable(vc, v);
2658 v->stat.signal_exits++;
2659 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2660 v->arch.ret = -EINTR;
2661 wake_up(&v->arch.cpu_run);
2664 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2667 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
2668 if (!v->arch.pending_exceptions)
2669 n_ceded += v->arch.ceded;
2674 if (n_ceded == vc->n_runnable) {
2675 kvmppc_vcore_blocked(vc);
2676 } else if (need_resched()) {
2677 kvmppc_vcore_preempt(vc);
2678 /* Let something else run */
2679 cond_resched_lock(&vc->lock);
2680 if (vc->vcore_state == VCORE_PREEMPT)
2681 kvmppc_vcore_end_preempt(vc);
2683 kvmppc_run_core(vc);
2688 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2689 (vc->vcore_state == VCORE_RUNNING ||
2690 vc->vcore_state == VCORE_EXITING ||
2691 vc->vcore_state == VCORE_PIGGYBACK))
2692 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
2694 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2695 kvmppc_vcore_end_preempt(vc);
2697 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2698 kvmppc_remove_runnable(vc, vcpu);
2699 vcpu->stat.signal_exits++;
2700 kvm_run->exit_reason = KVM_EXIT_INTR;
2701 vcpu->arch.ret = -EINTR;
2704 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2705 /* Wake up some vcpu to run the core */
2706 v = list_first_entry(&vc->runnable_threads,
2707 struct kvm_vcpu, arch.run_list);
2708 wake_up(&v->arch.cpu_run);
2711 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
2712 spin_unlock(&vc->lock);
2713 return vcpu->arch.ret;
2716 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
2721 if (!vcpu->arch.sane) {
2722 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2726 kvmppc_core_prepare_to_enter(vcpu);
2728 /* No need to go into the guest when all we'll do is come back out */
2729 if (signal_pending(current)) {
2730 run->exit_reason = KVM_EXIT_INTR;
2734 atomic_inc(&vcpu->kvm->arch.vcpus_running);
2735 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
2738 /* On the first time here, set up HTAB and VRMA */
2739 if (!vcpu->kvm->arch.hpte_setup_done) {
2740 r = kvmppc_hv_setup_htab_rma(vcpu);
2745 flush_all_to_thread(current);
2747 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
2748 vcpu->arch.pgdir = current->mm->pgd;
2749 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
2752 r = kvmppc_run_vcpu(run, vcpu);
2754 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2755 !(vcpu->arch.shregs.msr & MSR_PR)) {
2756 trace_kvm_hcall_enter(vcpu);
2757 r = kvmppc_pseries_do_hcall(vcpu);
2758 trace_kvm_hcall_exit(vcpu, r);
2759 kvmppc_core_prepare_to_enter(vcpu);
2760 } else if (r == RESUME_PAGE_FAULT) {
2761 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2762 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2763 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2764 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
2766 } while (is_kvmppc_resume_guest(r));
2769 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
2770 atomic_dec(&vcpu->kvm->arch.vcpus_running);
2774 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2777 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2781 (*sps)->page_shift = def->shift;
2782 (*sps)->slb_enc = def->sllp;
2783 (*sps)->enc[0].page_shift = def->shift;
2784 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
2786 * Add 16MB MPSS support if host supports it
2788 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2789 (*sps)->enc[1].page_shift = 24;
2790 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2795 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2796 struct kvm_ppc_smmu_info *info)
2798 struct kvm_ppc_one_seg_page_size *sps;
2800 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2801 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2802 info->flags |= KVM_PPC_1T_SEGMENTS;
2803 info->slb_size = mmu_slb_size;
2805 /* We only support these sizes for now, and no muti-size segments */
2806 sps = &info->sps[0];
2807 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2808 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2809 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2815 * Get (and clear) the dirty memory log for a memory slot.
2817 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2818 struct kvm_dirty_log *log)
2820 struct kvm_memslots *slots;
2821 struct kvm_memory_slot *memslot;
2825 mutex_lock(&kvm->slots_lock);
2828 if (log->slot >= KVM_USER_MEM_SLOTS)
2831 slots = kvm_memslots(kvm);
2832 memslot = id_to_memslot(slots, log->slot);
2834 if (!memslot->dirty_bitmap)
2837 n = kvm_dirty_bitmap_bytes(memslot);
2838 memset(memslot->dirty_bitmap, 0, n);
2840 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
2845 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2850 mutex_unlock(&kvm->slots_lock);
2854 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2855 struct kvm_memory_slot *dont)
2857 if (!dont || free->arch.rmap != dont->arch.rmap) {
2858 vfree(free->arch.rmap);
2859 free->arch.rmap = NULL;
2863 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2864 unsigned long npages)
2866 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2867 if (!slot->arch.rmap)
2873 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2874 struct kvm_memory_slot *memslot,
2875 const struct kvm_userspace_memory_region *mem)
2880 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
2881 const struct kvm_userspace_memory_region *mem,
2882 const struct kvm_memory_slot *old,
2883 const struct kvm_memory_slot *new)
2885 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
2886 struct kvm_memslots *slots;
2887 struct kvm_memory_slot *memslot;
2889 if (npages && old->npages) {
2891 * If modifying a memslot, reset all the rmap dirty bits.
2892 * If this is a new memslot, we don't need to do anything
2893 * since the rmap array starts out as all zeroes,
2894 * i.e. no pages are dirty.
2896 slots = kvm_memslots(kvm);
2897 memslot = id_to_memslot(slots, mem->slot);
2898 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2903 * Update LPCR values in kvm->arch and in vcores.
2904 * Caller must hold kvm->lock.
2906 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2911 if ((kvm->arch.lpcr & mask) == lpcr)
2914 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2916 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2917 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2920 spin_lock(&vc->lock);
2921 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2922 spin_unlock(&vc->lock);
2923 if (++cores_done >= kvm->arch.online_vcores)
2928 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2933 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
2936 struct kvm *kvm = vcpu->kvm;
2938 struct kvm_memory_slot *memslot;
2939 struct vm_area_struct *vma;
2940 unsigned long lpcr = 0, senc;
2941 unsigned long psize, porder;
2944 mutex_lock(&kvm->lock);
2945 if (kvm->arch.hpte_setup_done)
2946 goto out; /* another vcpu beat us to it */
2948 /* Allocate hashed page table (if not done already) and reset it */
2949 if (!kvm->arch.hpt_virt) {
2950 err = kvmppc_alloc_hpt(kvm, NULL);
2952 pr_err("KVM: Couldn't alloc HPT\n");
2957 /* Look up the memslot for guest physical address 0 */
2958 srcu_idx = srcu_read_lock(&kvm->srcu);
2959 memslot = gfn_to_memslot(kvm, 0);
2961 /* We must have some memory at 0 by now */
2963 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2966 /* Look up the VMA for the start of this memory slot */
2967 hva = memslot->userspace_addr;
2968 down_read(¤t->mm->mmap_sem);
2969 vma = find_vma(current->mm, hva);
2970 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2973 psize = vma_kernel_pagesize(vma);
2974 porder = __ilog2(psize);
2976 up_read(¤t->mm->mmap_sem);
2978 /* We can handle 4k, 64k or 16M pages in the VRMA */
2980 if (!(psize == 0x1000 || psize == 0x10000 ||
2981 psize == 0x1000000))
2984 /* Update VRMASD field in the LPCR */
2985 senc = slb_pgsize_encoding(psize);
2986 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2987 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2988 /* the -4 is to account for senc values starting at 0x10 */
2989 lpcr = senc << (LPCR_VRMASD_SH - 4);
2991 /* Create HPTEs in the hash page table for the VRMA */
2992 kvmppc_map_vrma(vcpu, memslot, porder);
2994 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
2996 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
2998 kvm->arch.hpte_setup_done = 1;
3001 srcu_read_unlock(&kvm->srcu, srcu_idx);
3003 mutex_unlock(&kvm->lock);
3007 up_read(¤t->mm->mmap_sem);
3011 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
3013 unsigned long lpcr, lpid;
3016 /* Allocate the guest's logical partition ID */
3018 lpid = kvmppc_alloc_lpid();
3021 kvm->arch.lpid = lpid;
3024 * Since we don't flush the TLB when tearing down a VM,
3025 * and this lpid might have previously been used,
3026 * make sure we flush on each core before running the new VM.
3028 cpumask_setall(&kvm->arch.need_tlb_flush);
3030 /* Start out with the default set of hcalls enabled */
3031 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3032 sizeof(kvm->arch.enabled_hcalls));
3034 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
3036 /* Init LPCR for virtual RMA mode */
3037 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3038 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3039 lpcr &= LPCR_PECE | LPCR_LPES;
3040 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3041 LPCR_VPM0 | LPCR_VPM1;
3042 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3043 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3044 /* On POWER8 turn on online bit to enable PURR/SPURR */
3045 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3047 kvm->arch.lpcr = lpcr;
3050 * Track that we now have a HV mode VM active. This blocks secondary
3051 * CPU threads from coming online.
3053 kvm_hv_vm_activated();
3056 * Create a debugfs directory for the VM
3058 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3059 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3060 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3061 kvmppc_mmu_debugfs_init(kvm);
3066 static void kvmppc_free_vcores(struct kvm *kvm)
3070 for (i = 0; i < KVM_MAX_VCORES; ++i)
3071 kfree(kvm->arch.vcores[i]);
3072 kvm->arch.online_vcores = 0;
3075 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
3077 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3079 kvm_hv_vm_deactivated();
3081 kvmppc_free_vcores(kvm);
3083 kvmppc_free_hpt(kvm);
3086 /* We don't need to emulate any privileged instructions or dcbz */
3087 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3088 unsigned int inst, int *advance)
3090 return EMULATE_FAIL;
3093 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3096 return EMULATE_FAIL;
3099 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3102 return EMULATE_FAIL;
3105 static int kvmppc_core_check_processor_compat_hv(void)
3107 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3108 !cpu_has_feature(CPU_FTR_ARCH_206))
3113 static long kvm_arch_vm_ioctl_hv(struct file *filp,
3114 unsigned int ioctl, unsigned long arg)
3116 struct kvm *kvm __maybe_unused = filp->private_data;
3117 void __user *argp = (void __user *)arg;
3122 case KVM_PPC_ALLOCATE_HTAB: {
3126 if (get_user(htab_order, (u32 __user *)argp))
3128 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
3132 if (put_user(htab_order, (u32 __user *)argp))
3138 case KVM_PPC_GET_HTAB_FD: {
3139 struct kvm_get_htab_fd ghf;
3142 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3144 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3156 * List of hcall numbers to enable by default.
3157 * For compatibility with old userspace, we enable by default
3158 * all hcalls that were implemented before the hcall-enabling
3159 * facility was added. Note this list should not include H_RTAS.
3161 static unsigned int default_hcall_list[] = {
3175 #ifdef CONFIG_KVM_XICS
3186 static void init_default_hcalls(void)
3191 for (i = 0; default_hcall_list[i]; ++i) {
3192 hcall = default_hcall_list[i];
3193 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3194 __set_bit(hcall / 4, default_enabled_hcalls);
3198 static struct kvmppc_ops kvm_ops_hv = {
3199 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3200 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3201 .get_one_reg = kvmppc_get_one_reg_hv,
3202 .set_one_reg = kvmppc_set_one_reg_hv,
3203 .vcpu_load = kvmppc_core_vcpu_load_hv,
3204 .vcpu_put = kvmppc_core_vcpu_put_hv,
3205 .set_msr = kvmppc_set_msr_hv,
3206 .vcpu_run = kvmppc_vcpu_run_hv,
3207 .vcpu_create = kvmppc_core_vcpu_create_hv,
3208 .vcpu_free = kvmppc_core_vcpu_free_hv,
3209 .check_requests = kvmppc_core_check_requests_hv,
3210 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3211 .flush_memslot = kvmppc_core_flush_memslot_hv,
3212 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3213 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3214 .unmap_hva = kvm_unmap_hva_hv,
3215 .unmap_hva_range = kvm_unmap_hva_range_hv,
3216 .age_hva = kvm_age_hva_hv,
3217 .test_age_hva = kvm_test_age_hva_hv,
3218 .set_spte_hva = kvm_set_spte_hva_hv,
3219 .mmu_destroy = kvmppc_mmu_destroy_hv,
3220 .free_memslot = kvmppc_core_free_memslot_hv,
3221 .create_memslot = kvmppc_core_create_memslot_hv,
3222 .init_vm = kvmppc_core_init_vm_hv,
3223 .destroy_vm = kvmppc_core_destroy_vm_hv,
3224 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3225 .emulate_op = kvmppc_core_emulate_op_hv,
3226 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3227 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3228 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3229 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
3230 .hcall_implemented = kvmppc_hcall_impl_hv,
3233 static int kvmppc_book3s_init_hv(void)
3237 * FIXME!! Do we need to check on all cpus ?
3239 r = kvmppc_core_check_processor_compat_hv();
3243 kvm_ops_hv.owner = THIS_MODULE;
3244 kvmppc_hv_ops = &kvm_ops_hv;
3246 init_default_hcalls();
3250 r = kvmppc_mmu_hv_init();
3254 static void kvmppc_book3s_exit_hv(void)
3256 kvmppc_hv_ops = NULL;
3259 module_init(kvmppc_book3s_init_hv);
3260 module_exit(kvmppc_book3s_exit_hv);
3261 MODULE_LICENSE("GPL");
3262 MODULE_ALIAS_MISCDEV(KVM_MINOR);
3263 MODULE_ALIAS("devname:kvm");