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/cacheflush.h>
39 #include <asm/tlbflush.h>
40 #include <asm/uaccess.h>
42 #include <asm/kvm_ppc.h>
43 #include <asm/kvm_book3s.h>
44 #include <asm/mmu_context.h>
45 #include <asm/lppaca.h>
46 #include <asm/processor.h>
47 #include <asm/cputhreads.h>
49 #include <asm/hvcall.h>
50 #include <asm/switch_to.h>
52 #include <linux/gfp.h>
53 #include <linux/vmalloc.h>
54 #include <linux/highmem.h>
55 #include <linux/hugetlb.h>
56 #include <linux/module.h>
60 /* #define EXIT_DEBUG */
61 /* #define EXIT_DEBUG_SIMPLE */
62 /* #define EXIT_DEBUG_INT */
64 /* Used to indicate that a guest page fault needs to be handled */
65 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
67 /* Used as a "null" value for timebase values */
68 #define TB_NIL (~(u64)0)
70 static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
71 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
73 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
77 wait_queue_head_t *wqp;
79 wqp = kvm_arch_vcpu_wq(vcpu);
80 if (waitqueue_active(wqp)) {
81 wake_up_interruptible(wqp);
82 ++vcpu->stat.halt_wakeup;
87 /* CPU points to the first thread of the core */
88 if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) {
89 #ifdef CONFIG_KVM_XICS
90 int real_cpu = cpu + vcpu->arch.ptid;
91 if (paca[real_cpu].kvm_hstate.xics_phys)
92 xics_wake_cpu(real_cpu);
96 smp_send_reschedule(cpu);
102 * We use the vcpu_load/put functions to measure stolen time.
103 * Stolen time is counted as time when either the vcpu is able to
104 * run as part of a virtual core, but the task running the vcore
105 * is preempted or sleeping, or when the vcpu needs something done
106 * in the kernel by the task running the vcpu, but that task is
107 * preempted or sleeping. Those two things have to be counted
108 * separately, since one of the vcpu tasks will take on the job
109 * of running the core, and the other vcpu tasks in the vcore will
110 * sleep waiting for it to do that, but that sleep shouldn't count
113 * Hence we accumulate stolen time when the vcpu can run as part of
114 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
115 * needs its task to do other things in the kernel (for example,
116 * service a page fault) in busy_stolen. We don't accumulate
117 * stolen time for a vcore when it is inactive, or for a vcpu
118 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
119 * a misnomer; it means that the vcpu task is not executing in
120 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
121 * the kernel. We don't have any way of dividing up that time
122 * between time that the vcpu is genuinely stopped, time that
123 * the task is actively working on behalf of the vcpu, and time
124 * that the task is preempted, so we don't count any of it as
127 * Updates to busy_stolen are protected by arch.tbacct_lock;
128 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
129 * of the vcpu that has taken responsibility for running the vcore
130 * (i.e. vc->runner). The stolen times are measured in units of
131 * timebase ticks. (Note that the != TB_NIL checks below are
132 * purely defensive; they should never fail.)
135 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
137 struct kvmppc_vcore *vc = vcpu->arch.vcore;
139 spin_lock(&vcpu->arch.tbacct_lock);
140 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE &&
141 vc->preempt_tb != TB_NIL) {
142 vc->stolen_tb += mftb() - vc->preempt_tb;
143 vc->preempt_tb = TB_NIL;
145 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
146 vcpu->arch.busy_preempt != TB_NIL) {
147 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
148 vcpu->arch.busy_preempt = TB_NIL;
150 spin_unlock(&vcpu->arch.tbacct_lock);
153 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
155 struct kvmppc_vcore *vc = vcpu->arch.vcore;
157 spin_lock(&vcpu->arch.tbacct_lock);
158 if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE)
159 vc->preempt_tb = mftb();
160 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
161 vcpu->arch.busy_preempt = mftb();
162 spin_unlock(&vcpu->arch.tbacct_lock);
165 static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
167 vcpu->arch.shregs.msr = msr;
168 kvmppc_end_cede(vcpu);
171 void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
173 vcpu->arch.pvr = pvr;
176 int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
178 unsigned long pcr = 0;
179 struct kvmppc_vcore *vc = vcpu->arch.vcore;
182 if (!cpu_has_feature(CPU_FTR_ARCH_206))
183 return -EINVAL; /* 970 has no compat mode support */
185 switch (arch_compat) {
197 spin_lock(&vc->lock);
198 vc->arch_compat = arch_compat;
200 spin_unlock(&vc->lock);
205 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
209 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
210 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
211 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
212 for (r = 0; r < 16; ++r)
213 pr_err("r%2d = %.16lx r%d = %.16lx\n",
214 r, kvmppc_get_gpr(vcpu, r),
215 r+16, kvmppc_get_gpr(vcpu, r+16));
216 pr_err("ctr = %.16lx lr = %.16lx\n",
217 vcpu->arch.ctr, vcpu->arch.lr);
218 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
219 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
220 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
221 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
222 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
223 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
224 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
225 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
226 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
227 pr_err("fault dar = %.16lx dsisr = %.8x\n",
228 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
229 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
230 for (r = 0; r < vcpu->arch.slb_max; ++r)
231 pr_err(" ESID = %.16llx VSID = %.16llx\n",
232 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
233 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
234 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
235 vcpu->arch.last_inst);
238 struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
241 struct kvm_vcpu *v, *ret = NULL;
243 mutex_lock(&kvm->lock);
244 kvm_for_each_vcpu(r, v, kvm) {
245 if (v->vcpu_id == id) {
250 mutex_unlock(&kvm->lock);
254 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
256 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
257 vpa->yield_count = 1;
260 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
261 unsigned long addr, unsigned long len)
263 /* check address is cacheline aligned */
264 if (addr & (L1_CACHE_BYTES - 1))
266 spin_lock(&vcpu->arch.vpa_update_lock);
267 if (v->next_gpa != addr || v->len != len) {
269 v->len = addr ? len : 0;
270 v->update_pending = 1;
272 spin_unlock(&vcpu->arch.vpa_update_lock);
276 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
285 static int vpa_is_registered(struct kvmppc_vpa *vpap)
287 if (vpap->update_pending)
288 return vpap->next_gpa != 0;
289 return vpap->pinned_addr != NULL;
292 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
294 unsigned long vcpuid, unsigned long vpa)
296 struct kvm *kvm = vcpu->kvm;
297 unsigned long len, nb;
299 struct kvm_vcpu *tvcpu;
302 struct kvmppc_vpa *vpap;
304 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
308 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
309 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
310 subfunc == H_VPA_REG_SLB) {
311 /* Registering new area - address must be cache-line aligned */
312 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
315 /* convert logical addr to kernel addr and read length */
316 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
319 if (subfunc == H_VPA_REG_VPA)
320 len = ((struct reg_vpa *)va)->length.hword;
322 len = ((struct reg_vpa *)va)->length.word;
323 kvmppc_unpin_guest_page(kvm, va, vpa, false);
326 if (len > nb || len < sizeof(struct reg_vpa))
335 spin_lock(&tvcpu->arch.vpa_update_lock);
338 case H_VPA_REG_VPA: /* register VPA */
339 if (len < sizeof(struct lppaca))
341 vpap = &tvcpu->arch.vpa;
345 case H_VPA_REG_DTL: /* register DTL */
346 if (len < sizeof(struct dtl_entry))
348 len -= len % sizeof(struct dtl_entry);
350 /* Check that they have previously registered a VPA */
352 if (!vpa_is_registered(&tvcpu->arch.vpa))
355 vpap = &tvcpu->arch.dtl;
359 case H_VPA_REG_SLB: /* register SLB shadow buffer */
360 /* Check that they have previously registered a VPA */
362 if (!vpa_is_registered(&tvcpu->arch.vpa))
365 vpap = &tvcpu->arch.slb_shadow;
369 case H_VPA_DEREG_VPA: /* deregister VPA */
370 /* Check they don't still have a DTL or SLB buf registered */
372 if (vpa_is_registered(&tvcpu->arch.dtl) ||
373 vpa_is_registered(&tvcpu->arch.slb_shadow))
376 vpap = &tvcpu->arch.vpa;
380 case H_VPA_DEREG_DTL: /* deregister DTL */
381 vpap = &tvcpu->arch.dtl;
385 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
386 vpap = &tvcpu->arch.slb_shadow;
392 vpap->next_gpa = vpa;
394 vpap->update_pending = 1;
397 spin_unlock(&tvcpu->arch.vpa_update_lock);
402 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
404 struct kvm *kvm = vcpu->kvm;
410 * We need to pin the page pointed to by vpap->next_gpa,
411 * but we can't call kvmppc_pin_guest_page under the lock
412 * as it does get_user_pages() and down_read(). So we
413 * have to drop the lock, pin the page, then get the lock
414 * again and check that a new area didn't get registered
418 gpa = vpap->next_gpa;
419 spin_unlock(&vcpu->arch.vpa_update_lock);
423 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
424 spin_lock(&vcpu->arch.vpa_update_lock);
425 if (gpa == vpap->next_gpa)
427 /* sigh... unpin that one and try again */
429 kvmppc_unpin_guest_page(kvm, va, gpa, false);
432 vpap->update_pending = 0;
433 if (va && nb < vpap->len) {
435 * If it's now too short, it must be that userspace
436 * has changed the mappings underlying guest memory,
437 * so unregister the region.
439 kvmppc_unpin_guest_page(kvm, va, gpa, false);
442 if (vpap->pinned_addr)
443 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
446 vpap->pinned_addr = va;
449 vpap->pinned_end = va + vpap->len;
452 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
454 if (!(vcpu->arch.vpa.update_pending ||
455 vcpu->arch.slb_shadow.update_pending ||
456 vcpu->arch.dtl.update_pending))
459 spin_lock(&vcpu->arch.vpa_update_lock);
460 if (vcpu->arch.vpa.update_pending) {
461 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
462 if (vcpu->arch.vpa.pinned_addr)
463 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
465 if (vcpu->arch.dtl.update_pending) {
466 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
467 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
468 vcpu->arch.dtl_index = 0;
470 if (vcpu->arch.slb_shadow.update_pending)
471 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
472 spin_unlock(&vcpu->arch.vpa_update_lock);
476 * Return the accumulated stolen time for the vcore up until `now'.
477 * The caller should hold the vcore lock.
479 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
484 * If we are the task running the vcore, then since we hold
485 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
486 * can't be updated, so we don't need the tbacct_lock.
487 * If the vcore is inactive, it can't become active (since we
488 * hold the vcore lock), so the vcpu load/put functions won't
489 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
491 if (vc->vcore_state != VCORE_INACTIVE &&
492 vc->runner->arch.run_task != current) {
493 spin_lock(&vc->runner->arch.tbacct_lock);
495 if (vc->preempt_tb != TB_NIL)
496 p += now - vc->preempt_tb;
497 spin_unlock(&vc->runner->arch.tbacct_lock);
504 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
505 struct kvmppc_vcore *vc)
507 struct dtl_entry *dt;
509 unsigned long stolen;
510 unsigned long core_stolen;
513 dt = vcpu->arch.dtl_ptr;
514 vpa = vcpu->arch.vpa.pinned_addr;
516 core_stolen = vcore_stolen_time(vc, now);
517 stolen = core_stolen - vcpu->arch.stolen_logged;
518 vcpu->arch.stolen_logged = core_stolen;
519 spin_lock(&vcpu->arch.tbacct_lock);
520 stolen += vcpu->arch.busy_stolen;
521 vcpu->arch.busy_stolen = 0;
522 spin_unlock(&vcpu->arch.tbacct_lock);
525 memset(dt, 0, sizeof(struct dtl_entry));
526 dt->dispatch_reason = 7;
527 dt->processor_id = vc->pcpu + vcpu->arch.ptid;
528 dt->timebase = now + vc->tb_offset;
529 dt->enqueue_to_dispatch_time = stolen;
530 dt->srr0 = kvmppc_get_pc(vcpu);
531 dt->srr1 = vcpu->arch.shregs.msr;
533 if (dt == vcpu->arch.dtl.pinned_end)
534 dt = vcpu->arch.dtl.pinned_addr;
535 vcpu->arch.dtl_ptr = dt;
536 /* order writing *dt vs. writing vpa->dtl_idx */
538 vpa->dtl_idx = ++vcpu->arch.dtl_index;
539 vcpu->arch.dtl.dirty = true;
542 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
544 unsigned long req = kvmppc_get_gpr(vcpu, 3);
545 unsigned long target, ret = H_SUCCESS;
546 struct kvm_vcpu *tvcpu;
551 idx = srcu_read_lock(&vcpu->kvm->srcu);
552 ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
553 kvmppc_get_gpr(vcpu, 5),
554 kvmppc_get_gpr(vcpu, 6),
555 kvmppc_get_gpr(vcpu, 7));
556 srcu_read_unlock(&vcpu->kvm->srcu, idx);
561 target = kvmppc_get_gpr(vcpu, 4);
562 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
567 tvcpu->arch.prodded = 1;
569 if (vcpu->arch.ceded) {
570 if (waitqueue_active(&vcpu->wq)) {
571 wake_up_interruptible(&vcpu->wq);
572 vcpu->stat.halt_wakeup++;
577 target = kvmppc_get_gpr(vcpu, 4);
580 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
585 kvm_vcpu_yield_to(tvcpu);
588 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
589 kvmppc_get_gpr(vcpu, 5),
590 kvmppc_get_gpr(vcpu, 6));
593 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
596 rc = kvmppc_rtas_hcall(vcpu);
603 /* Send the error out to userspace via KVM_RUN */
612 if (kvmppc_xics_enabled(vcpu)) {
613 ret = kvmppc_xics_hcall(vcpu, req);
619 kvmppc_set_gpr(vcpu, 3, ret);
620 vcpu->arch.hcall_needed = 0;
624 static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
625 struct task_struct *tsk)
629 vcpu->stat.sum_exits++;
631 run->exit_reason = KVM_EXIT_UNKNOWN;
632 run->ready_for_interrupt_injection = 1;
633 switch (vcpu->arch.trap) {
634 /* We're good on these - the host merely wanted to get our attention */
635 case BOOK3S_INTERRUPT_HV_DECREMENTER:
636 vcpu->stat.dec_exits++;
639 case BOOK3S_INTERRUPT_EXTERNAL:
640 vcpu->stat.ext_intr_exits++;
643 case BOOK3S_INTERRUPT_PERFMON:
646 case BOOK3S_INTERRUPT_MACHINE_CHECK:
648 * Deliver a machine check interrupt to the guest.
649 * We have to do this, even if the host has handled the
650 * machine check, because machine checks use SRR0/1 and
651 * the interrupt might have trashed guest state in them.
653 kvmppc_book3s_queue_irqprio(vcpu,
654 BOOK3S_INTERRUPT_MACHINE_CHECK);
657 case BOOK3S_INTERRUPT_PROGRAM:
661 * Normally program interrupts are delivered directly
662 * to the guest by the hardware, but we can get here
663 * as a result of a hypervisor emulation interrupt
664 * (e40) getting turned into a 700 by BML RTAS.
666 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
667 kvmppc_core_queue_program(vcpu, flags);
671 case BOOK3S_INTERRUPT_SYSCALL:
673 /* hcall - punt to userspace */
676 /* hypercall with MSR_PR has already been handled in rmode,
677 * and never reaches here.
680 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
681 for (i = 0; i < 9; ++i)
682 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
683 run->exit_reason = KVM_EXIT_PAPR_HCALL;
684 vcpu->arch.hcall_needed = 1;
689 * We get these next two if the guest accesses a page which it thinks
690 * it has mapped but which is not actually present, either because
691 * it is for an emulated I/O device or because the corresonding
692 * host page has been paged out. Any other HDSI/HISI interrupts
693 * have been handled already.
695 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
696 r = RESUME_PAGE_FAULT;
698 case BOOK3S_INTERRUPT_H_INST_STORAGE:
699 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
700 vcpu->arch.fault_dsisr = 0;
701 r = RESUME_PAGE_FAULT;
704 * This occurs if the guest executes an illegal instruction.
705 * We just generate a program interrupt to the guest, since
706 * we don't emulate any guest instructions at this stage.
708 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
709 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
713 * This occurs if the guest (kernel or userspace), does something that
714 * is prohibited by HFSCR. We just generate a program interrupt to
717 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
718 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
722 kvmppc_dump_regs(vcpu);
723 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
724 vcpu->arch.trap, kvmppc_get_pc(vcpu),
725 vcpu->arch.shregs.msr);
726 run->hw.hardware_exit_reason = vcpu->arch.trap;
734 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
735 struct kvm_sregs *sregs)
739 memset(sregs, 0, sizeof(struct kvm_sregs));
740 sregs->pvr = vcpu->arch.pvr;
741 for (i = 0; i < vcpu->arch.slb_max; i++) {
742 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
743 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
749 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
750 struct kvm_sregs *sregs)
754 kvmppc_set_pvr_hv(vcpu, sregs->pvr);
757 for (i = 0; i < vcpu->arch.slb_nr; i++) {
758 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
759 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
760 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
764 vcpu->arch.slb_max = j;
769 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr)
771 struct kvmppc_vcore *vc = vcpu->arch.vcore;
774 spin_lock(&vc->lock);
776 * Userspace can only modify DPFD (default prefetch depth),
777 * ILE (interrupt little-endian) and TC (translation control).
779 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
780 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
781 spin_unlock(&vc->lock);
784 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
785 union kvmppc_one_reg *val)
791 case KVM_REG_PPC_HIOR:
792 *val = get_reg_val(id, 0);
794 case KVM_REG_PPC_DABR:
795 *val = get_reg_val(id, vcpu->arch.dabr);
797 case KVM_REG_PPC_DSCR:
798 *val = get_reg_val(id, vcpu->arch.dscr);
800 case KVM_REG_PPC_PURR:
801 *val = get_reg_val(id, vcpu->arch.purr);
803 case KVM_REG_PPC_SPURR:
804 *val = get_reg_val(id, vcpu->arch.spurr);
806 case KVM_REG_PPC_AMR:
807 *val = get_reg_val(id, vcpu->arch.amr);
809 case KVM_REG_PPC_UAMOR:
810 *val = get_reg_val(id, vcpu->arch.uamor);
812 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
813 i = id - KVM_REG_PPC_MMCR0;
814 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
816 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
817 i = id - KVM_REG_PPC_PMC1;
818 *val = get_reg_val(id, vcpu->arch.pmc[i]);
820 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
821 i = id - KVM_REG_PPC_SPMC1;
822 *val = get_reg_val(id, vcpu->arch.spmc[i]);
824 case KVM_REG_PPC_SIAR:
825 *val = get_reg_val(id, vcpu->arch.siar);
827 case KVM_REG_PPC_SDAR:
828 *val = get_reg_val(id, vcpu->arch.sdar);
830 case KVM_REG_PPC_SIER:
831 *val = get_reg_val(id, vcpu->arch.sier);
833 case KVM_REG_PPC_IAMR:
834 *val = get_reg_val(id, vcpu->arch.iamr);
836 case KVM_REG_PPC_TFHAR:
837 *val = get_reg_val(id, vcpu->arch.tfhar);
839 case KVM_REG_PPC_TFIAR:
840 *val = get_reg_val(id, vcpu->arch.tfiar);
842 case KVM_REG_PPC_TEXASR:
843 *val = get_reg_val(id, vcpu->arch.texasr);
845 case KVM_REG_PPC_FSCR:
846 *val = get_reg_val(id, vcpu->arch.fscr);
848 case KVM_REG_PPC_PSPB:
849 *val = get_reg_val(id, vcpu->arch.pspb);
851 case KVM_REG_PPC_EBBHR:
852 *val = get_reg_val(id, vcpu->arch.ebbhr);
854 case KVM_REG_PPC_EBBRR:
855 *val = get_reg_val(id, vcpu->arch.ebbrr);
857 case KVM_REG_PPC_BESCR:
858 *val = get_reg_val(id, vcpu->arch.bescr);
860 case KVM_REG_PPC_TAR:
861 *val = get_reg_val(id, vcpu->arch.tar);
863 case KVM_REG_PPC_DPDES:
864 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
866 case KVM_REG_PPC_DAWR:
867 *val = get_reg_val(id, vcpu->arch.dawr);
869 case KVM_REG_PPC_DAWRX:
870 *val = get_reg_val(id, vcpu->arch.dawrx);
872 case KVM_REG_PPC_CIABR:
873 *val = get_reg_val(id, vcpu->arch.ciabr);
876 *val = get_reg_val(id, vcpu->arch.ic);
878 case KVM_REG_PPC_VTB:
879 *val = get_reg_val(id, vcpu->arch.vtb);
881 case KVM_REG_PPC_CSIGR:
882 *val = get_reg_val(id, vcpu->arch.csigr);
884 case KVM_REG_PPC_TACR:
885 *val = get_reg_val(id, vcpu->arch.tacr);
887 case KVM_REG_PPC_TCSCR:
888 *val = get_reg_val(id, vcpu->arch.tcscr);
890 case KVM_REG_PPC_PID:
891 *val = get_reg_val(id, vcpu->arch.pid);
893 case KVM_REG_PPC_ACOP:
894 *val = get_reg_val(id, vcpu->arch.acop);
896 case KVM_REG_PPC_WORT:
897 *val = get_reg_val(id, vcpu->arch.wort);
899 case KVM_REG_PPC_VPA_ADDR:
900 spin_lock(&vcpu->arch.vpa_update_lock);
901 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
902 spin_unlock(&vcpu->arch.vpa_update_lock);
904 case KVM_REG_PPC_VPA_SLB:
905 spin_lock(&vcpu->arch.vpa_update_lock);
906 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
907 val->vpaval.length = vcpu->arch.slb_shadow.len;
908 spin_unlock(&vcpu->arch.vpa_update_lock);
910 case KVM_REG_PPC_VPA_DTL:
911 spin_lock(&vcpu->arch.vpa_update_lock);
912 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
913 val->vpaval.length = vcpu->arch.dtl.len;
914 spin_unlock(&vcpu->arch.vpa_update_lock);
916 case KVM_REG_PPC_TB_OFFSET:
917 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
919 case KVM_REG_PPC_LPCR:
920 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
922 case KVM_REG_PPC_PPR:
923 *val = get_reg_val(id, vcpu->arch.ppr);
925 case KVM_REG_PPC_ARCH_COMPAT:
926 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
936 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
937 union kvmppc_one_reg *val)
941 unsigned long addr, len;
944 case KVM_REG_PPC_HIOR:
945 /* Only allow this to be set to zero */
946 if (set_reg_val(id, *val))
949 case KVM_REG_PPC_DABR:
950 vcpu->arch.dabr = set_reg_val(id, *val);
952 case KVM_REG_PPC_DSCR:
953 vcpu->arch.dscr = set_reg_val(id, *val);
955 case KVM_REG_PPC_PURR:
956 vcpu->arch.purr = set_reg_val(id, *val);
958 case KVM_REG_PPC_SPURR:
959 vcpu->arch.spurr = set_reg_val(id, *val);
961 case KVM_REG_PPC_AMR:
962 vcpu->arch.amr = set_reg_val(id, *val);
964 case KVM_REG_PPC_UAMOR:
965 vcpu->arch.uamor = set_reg_val(id, *val);
967 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
968 i = id - KVM_REG_PPC_MMCR0;
969 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
971 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
972 i = id - KVM_REG_PPC_PMC1;
973 vcpu->arch.pmc[i] = set_reg_val(id, *val);
975 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
976 i = id - KVM_REG_PPC_SPMC1;
977 vcpu->arch.spmc[i] = set_reg_val(id, *val);
979 case KVM_REG_PPC_SIAR:
980 vcpu->arch.siar = set_reg_val(id, *val);
982 case KVM_REG_PPC_SDAR:
983 vcpu->arch.sdar = set_reg_val(id, *val);
985 case KVM_REG_PPC_SIER:
986 vcpu->arch.sier = set_reg_val(id, *val);
988 case KVM_REG_PPC_IAMR:
989 vcpu->arch.iamr = set_reg_val(id, *val);
991 case KVM_REG_PPC_TFHAR:
992 vcpu->arch.tfhar = set_reg_val(id, *val);
994 case KVM_REG_PPC_TFIAR:
995 vcpu->arch.tfiar = set_reg_val(id, *val);
997 case KVM_REG_PPC_TEXASR:
998 vcpu->arch.texasr = set_reg_val(id, *val);
1000 case KVM_REG_PPC_FSCR:
1001 vcpu->arch.fscr = set_reg_val(id, *val);
1003 case KVM_REG_PPC_PSPB:
1004 vcpu->arch.pspb = set_reg_val(id, *val);
1006 case KVM_REG_PPC_EBBHR:
1007 vcpu->arch.ebbhr = set_reg_val(id, *val);
1009 case KVM_REG_PPC_EBBRR:
1010 vcpu->arch.ebbrr = set_reg_val(id, *val);
1012 case KVM_REG_PPC_BESCR:
1013 vcpu->arch.bescr = set_reg_val(id, *val);
1015 case KVM_REG_PPC_TAR:
1016 vcpu->arch.tar = set_reg_val(id, *val);
1018 case KVM_REG_PPC_DPDES:
1019 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1021 case KVM_REG_PPC_DAWR:
1022 vcpu->arch.dawr = set_reg_val(id, *val);
1024 case KVM_REG_PPC_DAWRX:
1025 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1027 case KVM_REG_PPC_CIABR:
1028 vcpu->arch.ciabr = set_reg_val(id, *val);
1029 /* Don't allow setting breakpoints in hypervisor code */
1030 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1031 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1033 case KVM_REG_PPC_IC:
1034 vcpu->arch.ic = set_reg_val(id, *val);
1036 case KVM_REG_PPC_VTB:
1037 vcpu->arch.vtb = set_reg_val(id, *val);
1039 case KVM_REG_PPC_CSIGR:
1040 vcpu->arch.csigr = set_reg_val(id, *val);
1042 case KVM_REG_PPC_TACR:
1043 vcpu->arch.tacr = set_reg_val(id, *val);
1045 case KVM_REG_PPC_TCSCR:
1046 vcpu->arch.tcscr = set_reg_val(id, *val);
1048 case KVM_REG_PPC_PID:
1049 vcpu->arch.pid = set_reg_val(id, *val);
1051 case KVM_REG_PPC_ACOP:
1052 vcpu->arch.acop = set_reg_val(id, *val);
1054 case KVM_REG_PPC_WORT:
1055 vcpu->arch.wort = set_reg_val(id, *val);
1057 case KVM_REG_PPC_VPA_ADDR:
1058 addr = set_reg_val(id, *val);
1060 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1061 vcpu->arch.dtl.next_gpa))
1063 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1065 case KVM_REG_PPC_VPA_SLB:
1066 addr = val->vpaval.addr;
1067 len = val->vpaval.length;
1069 if (addr && !vcpu->arch.vpa.next_gpa)
1071 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1073 case KVM_REG_PPC_VPA_DTL:
1074 addr = val->vpaval.addr;
1075 len = val->vpaval.length;
1077 if (addr && (len < sizeof(struct dtl_entry) ||
1078 !vcpu->arch.vpa.next_gpa))
1080 len -= len % sizeof(struct dtl_entry);
1081 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1083 case KVM_REG_PPC_TB_OFFSET:
1084 /* round up to multiple of 2^24 */
1085 vcpu->arch.vcore->tb_offset =
1086 ALIGN(set_reg_val(id, *val), 1UL << 24);
1088 case KVM_REG_PPC_LPCR:
1089 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val));
1091 case KVM_REG_PPC_PPR:
1092 vcpu->arch.ppr = set_reg_val(id, *val);
1094 case KVM_REG_PPC_ARCH_COMPAT:
1095 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1105 static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1108 struct kvm_vcpu *vcpu;
1111 struct kvmppc_vcore *vcore;
1113 core = id / threads_per_core;
1114 if (core >= KVM_MAX_VCORES)
1118 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
1122 err = kvm_vcpu_init(vcpu, kvm, id);
1126 vcpu->arch.shared = &vcpu->arch.shregs;
1127 vcpu->arch.mmcr[0] = MMCR0_FC;
1128 vcpu->arch.ctrl = CTRL_RUNLATCH;
1129 /* default to host PVR, since we can't spoof it */
1130 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
1131 spin_lock_init(&vcpu->arch.vpa_update_lock);
1132 spin_lock_init(&vcpu->arch.tbacct_lock);
1133 vcpu->arch.busy_preempt = TB_NIL;
1135 kvmppc_mmu_book3s_hv_init(vcpu);
1137 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1139 init_waitqueue_head(&vcpu->arch.cpu_run);
1141 mutex_lock(&kvm->lock);
1142 vcore = kvm->arch.vcores[core];
1144 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1146 INIT_LIST_HEAD(&vcore->runnable_threads);
1147 spin_lock_init(&vcore->lock);
1148 init_waitqueue_head(&vcore->wq);
1149 vcore->preempt_tb = TB_NIL;
1150 vcore->lpcr = kvm->arch.lpcr;
1151 vcore->first_vcpuid = core * threads_per_core;
1154 kvm->arch.vcores[core] = vcore;
1155 kvm->arch.online_vcores++;
1157 mutex_unlock(&kvm->lock);
1162 spin_lock(&vcore->lock);
1163 ++vcore->num_threads;
1164 spin_unlock(&vcore->lock);
1165 vcpu->arch.vcore = vcore;
1166 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
1168 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1169 kvmppc_sanity_check(vcpu);
1174 kmem_cache_free(kvm_vcpu_cache, vcpu);
1176 return ERR_PTR(err);
1179 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1181 if (vpa->pinned_addr)
1182 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1186 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
1188 spin_lock(&vcpu->arch.vpa_update_lock);
1189 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1190 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1191 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
1192 spin_unlock(&vcpu->arch.vpa_update_lock);
1193 kvm_vcpu_uninit(vcpu);
1194 kmem_cache_free(kvm_vcpu_cache, vcpu);
1197 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1199 /* Indicate we want to get back into the guest */
1203 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
1205 unsigned long dec_nsec, now;
1208 if (now > vcpu->arch.dec_expires) {
1209 /* decrementer has already gone negative */
1210 kvmppc_core_queue_dec(vcpu);
1211 kvmppc_core_prepare_to_enter(vcpu);
1214 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1216 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1218 vcpu->arch.timer_running = 1;
1221 static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
1223 vcpu->arch.ceded = 0;
1224 if (vcpu->arch.timer_running) {
1225 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1226 vcpu->arch.timer_running = 0;
1230 extern void __kvmppc_vcore_entry(void);
1232 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1233 struct kvm_vcpu *vcpu)
1237 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1239 spin_lock(&vcpu->arch.tbacct_lock);
1241 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1242 vcpu->arch.stolen_logged;
1243 vcpu->arch.busy_preempt = now;
1244 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1245 spin_unlock(&vcpu->arch.tbacct_lock);
1247 list_del(&vcpu->arch.run_list);
1250 static int kvmppc_grab_hwthread(int cpu)
1252 struct paca_struct *tpaca;
1253 long timeout = 1000;
1257 /* Ensure the thread won't go into the kernel if it wakes */
1258 tpaca->kvm_hstate.hwthread_req = 1;
1259 tpaca->kvm_hstate.kvm_vcpu = NULL;
1262 * If the thread is already executing in the kernel (e.g. handling
1263 * a stray interrupt), wait for it to get back to nap mode.
1264 * The smp_mb() is to ensure that our setting of hwthread_req
1265 * is visible before we look at hwthread_state, so if this
1266 * races with the code at system_reset_pSeries and the thread
1267 * misses our setting of hwthread_req, we are sure to see its
1268 * setting of hwthread_state, and vice versa.
1271 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1272 if (--timeout <= 0) {
1273 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1281 static void kvmppc_release_hwthread(int cpu)
1283 struct paca_struct *tpaca;
1286 tpaca->kvm_hstate.hwthread_req = 0;
1287 tpaca->kvm_hstate.kvm_vcpu = NULL;
1290 static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1293 struct paca_struct *tpaca;
1294 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1296 if (vcpu->arch.timer_running) {
1297 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1298 vcpu->arch.timer_running = 0;
1300 cpu = vc->pcpu + vcpu->arch.ptid;
1302 tpaca->kvm_hstate.kvm_vcpu = vcpu;
1303 tpaca->kvm_hstate.kvm_vcore = vc;
1304 tpaca->kvm_hstate.ptid = vcpu->arch.ptid;
1305 vcpu->cpu = vc->pcpu;
1307 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1308 if (cpu != smp_processor_id()) {
1309 #ifdef CONFIG_KVM_XICS
1312 if (vcpu->arch.ptid)
1318 static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc)
1324 while (vc->nap_count < vc->n_woken) {
1325 if (++i >= 1000000) {
1326 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1327 vc->nap_count, vc->n_woken);
1336 * Check that we are on thread 0 and that any other threads in
1337 * this core are off-line. Then grab the threads so they can't
1340 static int on_primary_thread(void)
1342 int cpu = smp_processor_id();
1343 int thr = cpu_thread_in_core(cpu);
1347 while (++thr < threads_per_core)
1348 if (cpu_online(cpu + thr))
1351 /* Grab all hw threads so they can't go into the kernel */
1352 for (thr = 1; thr < threads_per_core; ++thr) {
1353 if (kvmppc_grab_hwthread(cpu + thr)) {
1354 /* Couldn't grab one; let the others go */
1356 kvmppc_release_hwthread(cpu + thr);
1357 } while (--thr > 0);
1365 * Run a set of guest threads on a physical core.
1366 * Called with vc->lock held.
1368 static void kvmppc_run_core(struct kvmppc_vcore *vc)
1370 struct kvm_vcpu *vcpu, *vnext;
1373 int i, need_vpa_update;
1375 struct kvm_vcpu *vcpus_to_update[threads_per_core];
1377 /* don't start if any threads have a signal pending */
1378 need_vpa_update = 0;
1379 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1380 if (signal_pending(vcpu->arch.run_task))
1382 if (vcpu->arch.vpa.update_pending ||
1383 vcpu->arch.slb_shadow.update_pending ||
1384 vcpu->arch.dtl.update_pending)
1385 vcpus_to_update[need_vpa_update++] = vcpu;
1389 * Initialize *vc, in particular vc->vcore_state, so we can
1390 * drop the vcore lock if necessary.
1394 vc->entry_exit_count = 0;
1395 vc->vcore_state = VCORE_STARTING;
1397 vc->napping_threads = 0;
1400 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1401 * which can't be called with any spinlocks held.
1403 if (need_vpa_update) {
1404 spin_unlock(&vc->lock);
1405 for (i = 0; i < need_vpa_update; ++i)
1406 kvmppc_update_vpas(vcpus_to_update[i]);
1407 spin_lock(&vc->lock);
1411 * Make sure we are running on thread 0, and that
1412 * secondary threads are offline.
1414 if (threads_per_core > 1 && !on_primary_thread()) {
1415 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1416 vcpu->arch.ret = -EBUSY;
1420 vc->pcpu = smp_processor_id();
1421 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1422 kvmppc_start_thread(vcpu);
1423 kvmppc_create_dtl_entry(vcpu, vc);
1426 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1427 get_paca()->kvm_hstate.kvm_vcore = vc;
1428 get_paca()->kvm_hstate.ptid = 0;
1430 vc->vcore_state = VCORE_RUNNING;
1432 spin_unlock(&vc->lock);
1436 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
1438 __kvmppc_vcore_entry();
1440 spin_lock(&vc->lock);
1441 /* disable sending of IPIs on virtual external irqs */
1442 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list)
1444 /* wait for secondary threads to finish writing their state to memory */
1445 if (vc->nap_count < vc->n_woken)
1446 kvmppc_wait_for_nap(vc);
1447 for (i = 0; i < threads_per_core; ++i)
1448 kvmppc_release_hwthread(vc->pcpu + i);
1449 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1450 vc->vcore_state = VCORE_EXITING;
1451 spin_unlock(&vc->lock);
1453 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
1455 /* make sure updates to secondary vcpu structs are visible now */
1462 spin_lock(&vc->lock);
1464 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
1465 /* cancel pending dec exception if dec is positive */
1466 if (now < vcpu->arch.dec_expires &&
1467 kvmppc_core_pending_dec(vcpu))
1468 kvmppc_core_dequeue_dec(vcpu);
1471 if (vcpu->arch.trap)
1472 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
1473 vcpu->arch.run_task);
1475 vcpu->arch.ret = ret;
1476 vcpu->arch.trap = 0;
1478 if (vcpu->arch.ceded) {
1479 if (ret != RESUME_GUEST)
1480 kvmppc_end_cede(vcpu);
1482 kvmppc_set_timer(vcpu);
1487 vc->vcore_state = VCORE_INACTIVE;
1488 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
1490 if (vcpu->arch.ret != RESUME_GUEST) {
1491 kvmppc_remove_runnable(vc, vcpu);
1492 wake_up(&vcpu->arch.cpu_run);
1498 * Wait for some other vcpu thread to execute us, and
1499 * wake us up when we need to handle something in the host.
1501 static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state)
1505 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
1506 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE)
1508 finish_wait(&vcpu->arch.cpu_run, &wait);
1512 * All the vcpus in this vcore are idle, so wait for a decrementer
1513 * or external interrupt to one of the vcpus. vc->lock is held.
1515 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
1519 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
1520 vc->vcore_state = VCORE_SLEEPING;
1521 spin_unlock(&vc->lock);
1523 finish_wait(&vc->wq, &wait);
1524 spin_lock(&vc->lock);
1525 vc->vcore_state = VCORE_INACTIVE;
1528 static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
1531 struct kvmppc_vcore *vc;
1532 struct kvm_vcpu *v, *vn;
1534 kvm_run->exit_reason = 0;
1535 vcpu->arch.ret = RESUME_GUEST;
1536 vcpu->arch.trap = 0;
1537 kvmppc_update_vpas(vcpu);
1540 * Synchronize with other threads in this virtual core
1542 vc = vcpu->arch.vcore;
1543 spin_lock(&vc->lock);
1544 vcpu->arch.ceded = 0;
1545 vcpu->arch.run_task = current;
1546 vcpu->arch.kvm_run = kvm_run;
1547 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
1548 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
1549 vcpu->arch.busy_preempt = TB_NIL;
1550 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
1554 * This happens the first time this is called for a vcpu.
1555 * If the vcore is already running, we may be able to start
1556 * this thread straight away and have it join in.
1558 if (!signal_pending(current)) {
1559 if (vc->vcore_state == VCORE_RUNNING &&
1560 VCORE_EXIT_COUNT(vc) == 0) {
1561 kvmppc_create_dtl_entry(vcpu, vc);
1562 kvmppc_start_thread(vcpu);
1563 } else if (vc->vcore_state == VCORE_SLEEPING) {
1569 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1570 !signal_pending(current)) {
1571 if (vc->vcore_state != VCORE_INACTIVE) {
1572 spin_unlock(&vc->lock);
1573 kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE);
1574 spin_lock(&vc->lock);
1577 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
1579 kvmppc_core_prepare_to_enter(v);
1580 if (signal_pending(v->arch.run_task)) {
1581 kvmppc_remove_runnable(vc, v);
1582 v->stat.signal_exits++;
1583 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
1584 v->arch.ret = -EINTR;
1585 wake_up(&v->arch.cpu_run);
1588 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1592 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
1593 if (!v->arch.pending_exceptions)
1594 n_ceded += v->arch.ceded;
1598 if (n_ceded == vc->n_runnable)
1599 kvmppc_vcore_blocked(vc);
1601 kvmppc_run_core(vc);
1605 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
1606 (vc->vcore_state == VCORE_RUNNING ||
1607 vc->vcore_state == VCORE_EXITING)) {
1608 spin_unlock(&vc->lock);
1609 kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE);
1610 spin_lock(&vc->lock);
1613 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
1614 kvmppc_remove_runnable(vc, vcpu);
1615 vcpu->stat.signal_exits++;
1616 kvm_run->exit_reason = KVM_EXIT_INTR;
1617 vcpu->arch.ret = -EINTR;
1620 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
1621 /* Wake up some vcpu to run the core */
1622 v = list_first_entry(&vc->runnable_threads,
1623 struct kvm_vcpu, arch.run_list);
1624 wake_up(&v->arch.cpu_run);
1627 spin_unlock(&vc->lock);
1628 return vcpu->arch.ret;
1631 static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
1636 if (!vcpu->arch.sane) {
1637 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1641 kvmppc_core_prepare_to_enter(vcpu);
1643 /* No need to go into the guest when all we'll do is come back out */
1644 if (signal_pending(current)) {
1645 run->exit_reason = KVM_EXIT_INTR;
1649 atomic_inc(&vcpu->kvm->arch.vcpus_running);
1650 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1653 /* On the first time here, set up HTAB and VRMA or RMA */
1654 if (!vcpu->kvm->arch.rma_setup_done) {
1655 r = kvmppc_hv_setup_htab_rma(vcpu);
1660 flush_fp_to_thread(current);
1661 flush_altivec_to_thread(current);
1662 flush_vsx_to_thread(current);
1663 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
1664 vcpu->arch.pgdir = current->mm->pgd;
1665 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
1668 r = kvmppc_run_vcpu(run, vcpu);
1670 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
1671 !(vcpu->arch.shregs.msr & MSR_PR)) {
1672 r = kvmppc_pseries_do_hcall(vcpu);
1673 kvmppc_core_prepare_to_enter(vcpu);
1674 } else if (r == RESUME_PAGE_FAULT) {
1675 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1676 r = kvmppc_book3s_hv_page_fault(run, vcpu,
1677 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1678 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1680 } while (r == RESUME_GUEST);
1683 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
1684 atomic_dec(&vcpu->kvm->arch.vcpus_running);
1689 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1690 Assumes POWER7 or PPC970. */
1691 static inline int lpcr_rmls(unsigned long rma_size)
1694 case 32ul << 20: /* 32 MB */
1695 if (cpu_has_feature(CPU_FTR_ARCH_206))
1696 return 8; /* only supported on POWER7 */
1698 case 64ul << 20: /* 64 MB */
1700 case 128ul << 20: /* 128 MB */
1702 case 256ul << 20: /* 256 MB */
1704 case 1ul << 30: /* 1 GB */
1706 case 16ul << 30: /* 16 GB */
1708 case 256ul << 30: /* 256 GB */
1715 static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1718 struct kvm_rma_info *ri = vma->vm_file->private_data;
1720 if (vmf->pgoff >= kvm_rma_pages)
1721 return VM_FAULT_SIGBUS;
1723 page = pfn_to_page(ri->base_pfn + vmf->pgoff);
1729 static const struct vm_operations_struct kvm_rma_vm_ops = {
1730 .fault = kvm_rma_fault,
1733 static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma)
1735 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
1736 vma->vm_ops = &kvm_rma_vm_ops;
1740 static int kvm_rma_release(struct inode *inode, struct file *filp)
1742 struct kvm_rma_info *ri = filp->private_data;
1744 kvm_release_rma(ri);
1748 static const struct file_operations kvm_rma_fops = {
1749 .mmap = kvm_rma_mmap,
1750 .release = kvm_rma_release,
1753 static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm,
1754 struct kvm_allocate_rma *ret)
1757 struct kvm_rma_info *ri;
1759 * Only do this on PPC970 in HV mode
1761 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
1762 !cpu_has_feature(CPU_FTR_ARCH_201))
1768 ri = kvm_alloc_rma();
1772 fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC);
1774 kvm_release_rma(ri);
1776 ret->rma_size = kvm_rma_pages << PAGE_SHIFT;
1780 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
1783 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
1787 (*sps)->page_shift = def->shift;
1788 (*sps)->slb_enc = def->sllp;
1789 (*sps)->enc[0].page_shift = def->shift;
1791 * Only return base page encoding. We don't want to return
1792 * all the supporting pte_enc, because our H_ENTER doesn't
1793 * support MPSS yet. Once they do, we can start passing all
1794 * support pte_enc here
1796 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
1800 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
1801 struct kvm_ppc_smmu_info *info)
1803 struct kvm_ppc_one_seg_page_size *sps;
1805 info->flags = KVM_PPC_PAGE_SIZES_REAL;
1806 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
1807 info->flags |= KVM_PPC_1T_SEGMENTS;
1808 info->slb_size = mmu_slb_size;
1810 /* We only support these sizes for now, and no muti-size segments */
1811 sps = &info->sps[0];
1812 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
1813 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
1814 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
1820 * Get (and clear) the dirty memory log for a memory slot.
1822 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
1823 struct kvm_dirty_log *log)
1825 struct kvm_memory_slot *memslot;
1829 mutex_lock(&kvm->slots_lock);
1832 if (log->slot >= KVM_USER_MEM_SLOTS)
1835 memslot = id_to_memslot(kvm->memslots, log->slot);
1837 if (!memslot->dirty_bitmap)
1840 n = kvm_dirty_bitmap_bytes(memslot);
1841 memset(memslot->dirty_bitmap, 0, n);
1843 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
1848 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
1853 mutex_unlock(&kvm->slots_lock);
1857 static void unpin_slot(struct kvm_memory_slot *memslot)
1859 unsigned long *physp;
1860 unsigned long j, npages, pfn;
1863 physp = memslot->arch.slot_phys;
1864 npages = memslot->npages;
1867 for (j = 0; j < npages; j++) {
1868 if (!(physp[j] & KVMPPC_GOT_PAGE))
1870 pfn = physp[j] >> PAGE_SHIFT;
1871 page = pfn_to_page(pfn);
1877 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
1878 struct kvm_memory_slot *dont)
1880 if (!dont || free->arch.rmap != dont->arch.rmap) {
1881 vfree(free->arch.rmap);
1882 free->arch.rmap = NULL;
1884 if (!dont || free->arch.slot_phys != dont->arch.slot_phys) {
1886 vfree(free->arch.slot_phys);
1887 free->arch.slot_phys = NULL;
1891 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
1892 unsigned long npages)
1894 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
1895 if (!slot->arch.rmap)
1897 slot->arch.slot_phys = NULL;
1902 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
1903 struct kvm_memory_slot *memslot,
1904 struct kvm_userspace_memory_region *mem)
1906 unsigned long *phys;
1908 /* Allocate a slot_phys array if needed */
1909 phys = memslot->arch.slot_phys;
1910 if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) {
1911 phys = vzalloc(memslot->npages * sizeof(unsigned long));
1914 memslot->arch.slot_phys = phys;
1920 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
1921 struct kvm_userspace_memory_region *mem,
1922 const struct kvm_memory_slot *old)
1924 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
1925 struct kvm_memory_slot *memslot;
1927 if (npages && old->npages) {
1929 * If modifying a memslot, reset all the rmap dirty bits.
1930 * If this is a new memslot, we don't need to do anything
1931 * since the rmap array starts out as all zeroes,
1932 * i.e. no pages are dirty.
1934 memslot = id_to_memslot(kvm->memslots, mem->slot);
1935 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
1940 * Update LPCR values in kvm->arch and in vcores.
1941 * Caller must hold kvm->lock.
1943 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
1948 if ((kvm->arch.lpcr & mask) == lpcr)
1951 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
1953 for (i = 0; i < KVM_MAX_VCORES; ++i) {
1954 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
1957 spin_lock(&vc->lock);
1958 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
1959 spin_unlock(&vc->lock);
1960 if (++cores_done >= kvm->arch.online_vcores)
1965 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
1970 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
1973 struct kvm *kvm = vcpu->kvm;
1974 struct kvm_rma_info *ri = NULL;
1976 struct kvm_memory_slot *memslot;
1977 struct vm_area_struct *vma;
1978 unsigned long lpcr = 0, senc;
1979 unsigned long lpcr_mask = 0;
1980 unsigned long psize, porder;
1981 unsigned long rma_size;
1983 unsigned long *physp;
1984 unsigned long i, npages;
1987 mutex_lock(&kvm->lock);
1988 if (kvm->arch.rma_setup_done)
1989 goto out; /* another vcpu beat us to it */
1991 /* Allocate hashed page table (if not done already) and reset it */
1992 if (!kvm->arch.hpt_virt) {
1993 err = kvmppc_alloc_hpt(kvm, NULL);
1995 pr_err("KVM: Couldn't alloc HPT\n");
2000 /* Look up the memslot for guest physical address 0 */
2001 srcu_idx = srcu_read_lock(&kvm->srcu);
2002 memslot = gfn_to_memslot(kvm, 0);
2004 /* We must have some memory at 0 by now */
2006 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
2009 /* Look up the VMA for the start of this memory slot */
2010 hva = memslot->userspace_addr;
2011 down_read(¤t->mm->mmap_sem);
2012 vma = find_vma(current->mm, hva);
2013 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2016 psize = vma_kernel_pagesize(vma);
2017 porder = __ilog2(psize);
2019 /* Is this one of our preallocated RMAs? */
2020 if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops &&
2021 hva == vma->vm_start)
2022 ri = vma->vm_file->private_data;
2024 up_read(¤t->mm->mmap_sem);
2027 /* On POWER7, use VRMA; on PPC970, give up */
2029 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2030 pr_err("KVM: CPU requires an RMO\n");
2034 /* We can handle 4k, 64k or 16M pages in the VRMA */
2036 if (!(psize == 0x1000 || psize == 0x10000 ||
2037 psize == 0x1000000))
2040 /* Update VRMASD field in the LPCR */
2041 senc = slb_pgsize_encoding(psize);
2042 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2043 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2044 lpcr_mask = LPCR_VRMASD;
2045 /* the -4 is to account for senc values starting at 0x10 */
2046 lpcr = senc << (LPCR_VRMASD_SH - 4);
2048 /* Create HPTEs in the hash page table for the VRMA */
2049 kvmppc_map_vrma(vcpu, memslot, porder);
2052 /* Set up to use an RMO region */
2053 rma_size = kvm_rma_pages;
2054 if (rma_size > memslot->npages)
2055 rma_size = memslot->npages;
2056 rma_size <<= PAGE_SHIFT;
2057 rmls = lpcr_rmls(rma_size);
2059 if ((long)rmls < 0) {
2060 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size);
2063 atomic_inc(&ri->use_count);
2066 /* Update LPCR and RMOR */
2067 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2068 /* PPC970; insert RMLS value (split field) in HID4 */
2069 lpcr_mask = (1ul << HID4_RMLS0_SH) |
2070 (3ul << HID4_RMLS2_SH) | HID4_RMOR;
2071 lpcr = ((rmls >> 2) << HID4_RMLS0_SH) |
2072 ((rmls & 3) << HID4_RMLS2_SH);
2073 /* RMOR is also in HID4 */
2074 lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff)
2078 lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS;
2079 lpcr = rmls << LPCR_RMLS_SH;
2080 kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT;
2082 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2083 ri->base_pfn << PAGE_SHIFT, rma_size, lpcr);
2085 /* Initialize phys addrs of pages in RMO */
2086 npages = kvm_rma_pages;
2087 porder = __ilog2(npages);
2088 physp = memslot->arch.slot_phys;
2090 if (npages > memslot->npages)
2091 npages = memslot->npages;
2092 spin_lock(&kvm->arch.slot_phys_lock);
2093 for (i = 0; i < npages; ++i)
2094 physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) +
2096 spin_unlock(&kvm->arch.slot_phys_lock);
2100 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
2102 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2104 kvm->arch.rma_setup_done = 1;
2107 srcu_read_unlock(&kvm->srcu, srcu_idx);
2109 mutex_unlock(&kvm->lock);
2113 up_read(¤t->mm->mmap_sem);
2117 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
2119 unsigned long lpcr, lpid;
2121 /* Allocate the guest's logical partition ID */
2123 lpid = kvmppc_alloc_lpid();
2126 kvm->arch.lpid = lpid;
2129 * Since we don't flush the TLB when tearing down a VM,
2130 * and this lpid might have previously been used,
2131 * make sure we flush on each core before running the new VM.
2133 cpumask_setall(&kvm->arch.need_tlb_flush);
2135 kvm->arch.rma = NULL;
2137 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
2139 if (cpu_has_feature(CPU_FTR_ARCH_201)) {
2140 /* PPC970; HID4 is effectively the LPCR */
2141 kvm->arch.host_lpid = 0;
2142 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4);
2143 lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH));
2144 lpcr |= ((lpid >> 4) << HID4_LPID1_SH) |
2145 ((lpid & 0xf) << HID4_LPID5_SH);
2147 /* POWER7; init LPCR for virtual RMA mode */
2148 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2149 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2150 lpcr &= LPCR_PECE | LPCR_LPES;
2151 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2152 LPCR_VPM0 | LPCR_VPM1;
2153 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2154 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2156 kvm->arch.lpcr = lpcr;
2158 kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206);
2159 spin_lock_init(&kvm->arch.slot_phys_lock);
2162 * Don't allow secondary CPU threads to come online
2163 * while any KVM VMs exist.
2165 inhibit_secondary_onlining();
2170 static void kvmppc_free_vcores(struct kvm *kvm)
2174 for (i = 0; i < KVM_MAX_VCORES; ++i)
2175 kfree(kvm->arch.vcores[i]);
2176 kvm->arch.online_vcores = 0;
2179 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
2181 uninhibit_secondary_onlining();
2183 kvmppc_free_vcores(kvm);
2184 if (kvm->arch.rma) {
2185 kvm_release_rma(kvm->arch.rma);
2186 kvm->arch.rma = NULL;
2189 kvmppc_free_hpt(kvm);
2192 /* We don't need to emulate any privileged instructions or dcbz */
2193 static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2194 unsigned int inst, int *advance)
2196 return EMULATE_FAIL;
2199 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2202 return EMULATE_FAIL;
2205 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2208 return EMULATE_FAIL;
2211 static int kvmppc_core_check_processor_compat_hv(void)
2213 if (!cpu_has_feature(CPU_FTR_HVMODE))
2218 static long kvm_arch_vm_ioctl_hv(struct file *filp,
2219 unsigned int ioctl, unsigned long arg)
2221 struct kvm *kvm __maybe_unused = filp->private_data;
2222 void __user *argp = (void __user *)arg;
2227 case KVM_ALLOCATE_RMA: {
2228 struct kvm_allocate_rma rma;
2229 struct kvm *kvm = filp->private_data;
2231 r = kvm_vm_ioctl_allocate_rma(kvm, &rma);
2232 if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma)))
2237 case KVM_PPC_ALLOCATE_HTAB: {
2241 if (get_user(htab_order, (u32 __user *)argp))
2243 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2247 if (put_user(htab_order, (u32 __user *)argp))
2253 case KVM_PPC_GET_HTAB_FD: {
2254 struct kvm_get_htab_fd ghf;
2257 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2259 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2270 static struct kvmppc_ops kvm_ops_hv = {
2271 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2272 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2273 .get_one_reg = kvmppc_get_one_reg_hv,
2274 .set_one_reg = kvmppc_set_one_reg_hv,
2275 .vcpu_load = kvmppc_core_vcpu_load_hv,
2276 .vcpu_put = kvmppc_core_vcpu_put_hv,
2277 .set_msr = kvmppc_set_msr_hv,
2278 .vcpu_run = kvmppc_vcpu_run_hv,
2279 .vcpu_create = kvmppc_core_vcpu_create_hv,
2280 .vcpu_free = kvmppc_core_vcpu_free_hv,
2281 .check_requests = kvmppc_core_check_requests_hv,
2282 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2283 .flush_memslot = kvmppc_core_flush_memslot_hv,
2284 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2285 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2286 .unmap_hva = kvm_unmap_hva_hv,
2287 .unmap_hva_range = kvm_unmap_hva_range_hv,
2288 .age_hva = kvm_age_hva_hv,
2289 .test_age_hva = kvm_test_age_hva_hv,
2290 .set_spte_hva = kvm_set_spte_hva_hv,
2291 .mmu_destroy = kvmppc_mmu_destroy_hv,
2292 .free_memslot = kvmppc_core_free_memslot_hv,
2293 .create_memslot = kvmppc_core_create_memslot_hv,
2294 .init_vm = kvmppc_core_init_vm_hv,
2295 .destroy_vm = kvmppc_core_destroy_vm_hv,
2296 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2297 .emulate_op = kvmppc_core_emulate_op_hv,
2298 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2299 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2300 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2301 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
2304 static int kvmppc_book3s_init_hv(void)
2308 * FIXME!! Do we need to check on all cpus ?
2310 r = kvmppc_core_check_processor_compat_hv();
2314 kvm_ops_hv.owner = THIS_MODULE;
2315 kvmppc_hv_ops = &kvm_ops_hv;
2317 r = kvmppc_mmu_hv_init();
2321 static void kvmppc_book3s_exit_hv(void)
2323 kvmppc_hv_ops = NULL;
2326 module_init(kvmppc_book3s_init_hv);
2327 module_exit(kvmppc_book3s_exit_hv);
2328 MODULE_LICENSE("GPL");
2329 MODULE_ALIAS_MISCDEV(KVM_MINOR);
2330 MODULE_ALIAS("devname:kvm");