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>
36 #include <asm/cputable.h>
37 #include <asm/cacheflush.h>
38 #include <asm/tlbflush.h>
39 #include <asm/uaccess.h>
41 #include <asm/kvm_ppc.h>
42 #include <asm/kvm_book3s.h>
43 #include <asm/mmu_context.h>
44 #include <asm/lppaca.h>
45 #include <asm/processor.h>
46 #include <asm/cputhreads.h>
48 #include <asm/hvcall.h>
49 #include <asm/switch_to.h>
51 #include <linux/gfp.h>
52 #include <linux/vmalloc.h>
53 #include <linux/highmem.h>
54 #include <linux/hugetlb.h>
56 /* #define EXIT_DEBUG */
57 /* #define EXIT_DEBUG_SIMPLE */
58 /* #define EXIT_DEBUG_INT */
60 /* Used to indicate that a guest page fault needs to be handled */
61 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
63 /* Used as a "null" value for timebase values */
64 #define TB_NIL (~(u64)0)
66 static void kvmppc_end_cede(struct kvm_vcpu
*vcpu
);
67 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu
*vcpu
);
69 void kvmppc_fast_vcpu_kick(struct kvm_vcpu
*vcpu
)
73 wait_queue_head_t
*wqp
;
75 wqp
= kvm_arch_vcpu_wq(vcpu
);
76 if (waitqueue_active(wqp
)) {
77 wake_up_interruptible(wqp
);
78 ++vcpu
->stat
.halt_wakeup
;
83 /* CPU points to the first thread of the core */
84 if (cpu
!= me
&& cpu
>= 0 && cpu
< nr_cpu_ids
) {
85 #ifdef CONFIG_PPC_ICP_NATIVE
86 int real_cpu
= cpu
+ vcpu
->arch
.ptid
;
87 if (paca
[real_cpu
].kvm_hstate
.xics_phys
)
88 xics_wake_cpu(real_cpu
);
92 smp_send_reschedule(cpu
);
98 * We use the vcpu_load/put functions to measure stolen time.
99 * Stolen time is counted as time when either the vcpu is able to
100 * run as part of a virtual core, but the task running the vcore
101 * is preempted or sleeping, or when the vcpu needs something done
102 * in the kernel by the task running the vcpu, but that task is
103 * preempted or sleeping. Those two things have to be counted
104 * separately, since one of the vcpu tasks will take on the job
105 * of running the core, and the other vcpu tasks in the vcore will
106 * sleep waiting for it to do that, but that sleep shouldn't count
109 * Hence we accumulate stolen time when the vcpu can run as part of
110 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
111 * needs its task to do other things in the kernel (for example,
112 * service a page fault) in busy_stolen. We don't accumulate
113 * stolen time for a vcore when it is inactive, or for a vcpu
114 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
115 * a misnomer; it means that the vcpu task is not executing in
116 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
117 * the kernel. We don't have any way of dividing up that time
118 * between time that the vcpu is genuinely stopped, time that
119 * the task is actively working on behalf of the vcpu, and time
120 * that the task is preempted, so we don't count any of it as
123 * Updates to busy_stolen are protected by arch.tbacct_lock;
124 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
125 * of the vcpu that has taken responsibility for running the vcore
126 * (i.e. vc->runner). The stolen times are measured in units of
127 * timebase ticks. (Note that the != TB_NIL checks below are
128 * purely defensive; they should never fail.)
131 void kvmppc_core_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
133 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
135 spin_lock(&vcpu
->arch
.tbacct_lock
);
136 if (vc
->runner
== vcpu
&& vc
->vcore_state
!= VCORE_INACTIVE
&&
137 vc
->preempt_tb
!= TB_NIL
) {
138 vc
->stolen_tb
+= mftb() - vc
->preempt_tb
;
139 vc
->preempt_tb
= TB_NIL
;
141 if (vcpu
->arch
.state
== KVMPPC_VCPU_BUSY_IN_HOST
&&
142 vcpu
->arch
.busy_preempt
!= TB_NIL
) {
143 vcpu
->arch
.busy_stolen
+= mftb() - vcpu
->arch
.busy_preempt
;
144 vcpu
->arch
.busy_preempt
= TB_NIL
;
146 spin_unlock(&vcpu
->arch
.tbacct_lock
);
149 void kvmppc_core_vcpu_put(struct kvm_vcpu
*vcpu
)
151 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
153 spin_lock(&vcpu
->arch
.tbacct_lock
);
154 if (vc
->runner
== vcpu
&& vc
->vcore_state
!= VCORE_INACTIVE
)
155 vc
->preempt_tb
= mftb();
156 if (vcpu
->arch
.state
== KVMPPC_VCPU_BUSY_IN_HOST
)
157 vcpu
->arch
.busy_preempt
= mftb();
158 spin_unlock(&vcpu
->arch
.tbacct_lock
);
161 void kvmppc_set_msr(struct kvm_vcpu
*vcpu
, u64 msr
)
163 vcpu
->arch
.shregs
.msr
= msr
;
164 kvmppc_end_cede(vcpu
);
167 void kvmppc_set_pvr(struct kvm_vcpu
*vcpu
, u32 pvr
)
169 vcpu
->arch
.pvr
= pvr
;
172 void kvmppc_dump_regs(struct kvm_vcpu
*vcpu
)
176 pr_err("vcpu %p (%d):\n", vcpu
, vcpu
->vcpu_id
);
177 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
178 vcpu
->arch
.pc
, vcpu
->arch
.shregs
.msr
, vcpu
->arch
.trap
);
179 for (r
= 0; r
< 16; ++r
)
180 pr_err("r%2d = %.16lx r%d = %.16lx\n",
181 r
, kvmppc_get_gpr(vcpu
, r
),
182 r
+16, kvmppc_get_gpr(vcpu
, r
+16));
183 pr_err("ctr = %.16lx lr = %.16lx\n",
184 vcpu
->arch
.ctr
, vcpu
->arch
.lr
);
185 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
186 vcpu
->arch
.shregs
.srr0
, vcpu
->arch
.shregs
.srr1
);
187 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
188 vcpu
->arch
.shregs
.sprg0
, vcpu
->arch
.shregs
.sprg1
);
189 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
190 vcpu
->arch
.shregs
.sprg2
, vcpu
->arch
.shregs
.sprg3
);
191 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
192 vcpu
->arch
.cr
, vcpu
->arch
.xer
, vcpu
->arch
.shregs
.dsisr
);
193 pr_err("dar = %.16llx\n", vcpu
->arch
.shregs
.dar
);
194 pr_err("fault dar = %.16lx dsisr = %.8x\n",
195 vcpu
->arch
.fault_dar
, vcpu
->arch
.fault_dsisr
);
196 pr_err("SLB (%d entries):\n", vcpu
->arch
.slb_max
);
197 for (r
= 0; r
< vcpu
->arch
.slb_max
; ++r
)
198 pr_err(" ESID = %.16llx VSID = %.16llx\n",
199 vcpu
->arch
.slb
[r
].orige
, vcpu
->arch
.slb
[r
].origv
);
200 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
201 vcpu
->kvm
->arch
.lpcr
, vcpu
->kvm
->arch
.sdr1
,
202 vcpu
->arch
.last_inst
);
205 struct kvm_vcpu
*kvmppc_find_vcpu(struct kvm
*kvm
, int id
)
208 struct kvm_vcpu
*v
, *ret
= NULL
;
210 mutex_lock(&kvm
->lock
);
211 kvm_for_each_vcpu(r
, v
, kvm
) {
212 if (v
->vcpu_id
== id
) {
217 mutex_unlock(&kvm
->lock
);
221 static void init_vpa(struct kvm_vcpu
*vcpu
, struct lppaca
*vpa
)
223 vpa
->__old_status
|= LPPACA_OLD_SHARED_PROC
;
224 vpa
->yield_count
= 1;
227 static int set_vpa(struct kvm_vcpu
*vcpu
, struct kvmppc_vpa
*v
,
228 unsigned long addr
, unsigned long len
)
230 /* check address is cacheline aligned */
231 if (addr
& (L1_CACHE_BYTES
- 1))
233 spin_lock(&vcpu
->arch
.vpa_update_lock
);
234 if (v
->next_gpa
!= addr
|| v
->len
!= len
) {
236 v
->len
= addr
? len
: 0;
237 v
->update_pending
= 1;
239 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
243 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
252 static int vpa_is_registered(struct kvmppc_vpa
*vpap
)
254 if (vpap
->update_pending
)
255 return vpap
->next_gpa
!= 0;
256 return vpap
->pinned_addr
!= NULL
;
259 static unsigned long do_h_register_vpa(struct kvm_vcpu
*vcpu
,
261 unsigned long vcpuid
, unsigned long vpa
)
263 struct kvm
*kvm
= vcpu
->kvm
;
264 unsigned long len
, nb
;
266 struct kvm_vcpu
*tvcpu
;
269 struct kvmppc_vpa
*vpap
;
271 tvcpu
= kvmppc_find_vcpu(kvm
, vcpuid
);
275 subfunc
= (flags
>> H_VPA_FUNC_SHIFT
) & H_VPA_FUNC_MASK
;
276 if (subfunc
== H_VPA_REG_VPA
|| subfunc
== H_VPA_REG_DTL
||
277 subfunc
== H_VPA_REG_SLB
) {
278 /* Registering new area - address must be cache-line aligned */
279 if ((vpa
& (L1_CACHE_BYTES
- 1)) || !vpa
)
282 /* convert logical addr to kernel addr and read length */
283 va
= kvmppc_pin_guest_page(kvm
, vpa
, &nb
);
286 if (subfunc
== H_VPA_REG_VPA
)
287 len
= ((struct reg_vpa
*)va
)->length
.hword
;
289 len
= ((struct reg_vpa
*)va
)->length
.word
;
290 kvmppc_unpin_guest_page(kvm
, va
, vpa
, false);
293 if (len
> nb
|| len
< sizeof(struct reg_vpa
))
302 spin_lock(&tvcpu
->arch
.vpa_update_lock
);
305 case H_VPA_REG_VPA
: /* register VPA */
306 if (len
< sizeof(struct lppaca
))
308 vpap
= &tvcpu
->arch
.vpa
;
312 case H_VPA_REG_DTL
: /* register DTL */
313 if (len
< sizeof(struct dtl_entry
))
315 len
-= len
% sizeof(struct dtl_entry
);
317 /* Check that they have previously registered a VPA */
319 if (!vpa_is_registered(&tvcpu
->arch
.vpa
))
322 vpap
= &tvcpu
->arch
.dtl
;
326 case H_VPA_REG_SLB
: /* register SLB shadow buffer */
327 /* Check that they have previously registered a VPA */
329 if (!vpa_is_registered(&tvcpu
->arch
.vpa
))
332 vpap
= &tvcpu
->arch
.slb_shadow
;
336 case H_VPA_DEREG_VPA
: /* deregister VPA */
337 /* Check they don't still have a DTL or SLB buf registered */
339 if (vpa_is_registered(&tvcpu
->arch
.dtl
) ||
340 vpa_is_registered(&tvcpu
->arch
.slb_shadow
))
343 vpap
= &tvcpu
->arch
.vpa
;
347 case H_VPA_DEREG_DTL
: /* deregister DTL */
348 vpap
= &tvcpu
->arch
.dtl
;
352 case H_VPA_DEREG_SLB
: /* deregister SLB shadow buffer */
353 vpap
= &tvcpu
->arch
.slb_shadow
;
359 vpap
->next_gpa
= vpa
;
361 vpap
->update_pending
= 1;
364 spin_unlock(&tvcpu
->arch
.vpa_update_lock
);
369 static void kvmppc_update_vpa(struct kvm_vcpu
*vcpu
, struct kvmppc_vpa
*vpap
)
371 struct kvm
*kvm
= vcpu
->kvm
;
377 * We need to pin the page pointed to by vpap->next_gpa,
378 * but we can't call kvmppc_pin_guest_page under the lock
379 * as it does get_user_pages() and down_read(). So we
380 * have to drop the lock, pin the page, then get the lock
381 * again and check that a new area didn't get registered
385 gpa
= vpap
->next_gpa
;
386 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
390 va
= kvmppc_pin_guest_page(kvm
, gpa
, &nb
);
391 spin_lock(&vcpu
->arch
.vpa_update_lock
);
392 if (gpa
== vpap
->next_gpa
)
394 /* sigh... unpin that one and try again */
396 kvmppc_unpin_guest_page(kvm
, va
, gpa
, false);
399 vpap
->update_pending
= 0;
400 if (va
&& nb
< vpap
->len
) {
402 * If it's now too short, it must be that userspace
403 * has changed the mappings underlying guest memory,
404 * so unregister the region.
406 kvmppc_unpin_guest_page(kvm
, va
, gpa
, false);
409 if (vpap
->pinned_addr
)
410 kvmppc_unpin_guest_page(kvm
, vpap
->pinned_addr
, vpap
->gpa
,
413 vpap
->pinned_addr
= va
;
416 vpap
->pinned_end
= va
+ vpap
->len
;
419 static void kvmppc_update_vpas(struct kvm_vcpu
*vcpu
)
421 if (!(vcpu
->arch
.vpa
.update_pending
||
422 vcpu
->arch
.slb_shadow
.update_pending
||
423 vcpu
->arch
.dtl
.update_pending
))
426 spin_lock(&vcpu
->arch
.vpa_update_lock
);
427 if (vcpu
->arch
.vpa
.update_pending
) {
428 kvmppc_update_vpa(vcpu
, &vcpu
->arch
.vpa
);
429 if (vcpu
->arch
.vpa
.pinned_addr
)
430 init_vpa(vcpu
, vcpu
->arch
.vpa
.pinned_addr
);
432 if (vcpu
->arch
.dtl
.update_pending
) {
433 kvmppc_update_vpa(vcpu
, &vcpu
->arch
.dtl
);
434 vcpu
->arch
.dtl_ptr
= vcpu
->arch
.dtl
.pinned_addr
;
435 vcpu
->arch
.dtl_index
= 0;
437 if (vcpu
->arch
.slb_shadow
.update_pending
)
438 kvmppc_update_vpa(vcpu
, &vcpu
->arch
.slb_shadow
);
439 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
443 * Return the accumulated stolen time for the vcore up until `now'.
444 * The caller should hold the vcore lock.
446 static u64
vcore_stolen_time(struct kvmppc_vcore
*vc
, u64 now
)
451 * If we are the task running the vcore, then since we hold
452 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
453 * can't be updated, so we don't need the tbacct_lock.
454 * If the vcore is inactive, it can't become active (since we
455 * hold the vcore lock), so the vcpu load/put functions won't
456 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
458 if (vc
->vcore_state
!= VCORE_INACTIVE
&&
459 vc
->runner
->arch
.run_task
!= current
) {
460 spin_lock(&vc
->runner
->arch
.tbacct_lock
);
462 if (vc
->preempt_tb
!= TB_NIL
)
463 p
+= now
- vc
->preempt_tb
;
464 spin_unlock(&vc
->runner
->arch
.tbacct_lock
);
471 static void kvmppc_create_dtl_entry(struct kvm_vcpu
*vcpu
,
472 struct kvmppc_vcore
*vc
)
474 struct dtl_entry
*dt
;
476 unsigned long stolen
;
477 unsigned long core_stolen
;
480 dt
= vcpu
->arch
.dtl_ptr
;
481 vpa
= vcpu
->arch
.vpa
.pinned_addr
;
483 core_stolen
= vcore_stolen_time(vc
, now
);
484 stolen
= core_stolen
- vcpu
->arch
.stolen_logged
;
485 vcpu
->arch
.stolen_logged
= core_stolen
;
486 spin_lock(&vcpu
->arch
.tbacct_lock
);
487 stolen
+= vcpu
->arch
.busy_stolen
;
488 vcpu
->arch
.busy_stolen
= 0;
489 spin_unlock(&vcpu
->arch
.tbacct_lock
);
492 memset(dt
, 0, sizeof(struct dtl_entry
));
493 dt
->dispatch_reason
= 7;
494 dt
->processor_id
= vc
->pcpu
+ vcpu
->arch
.ptid
;
496 dt
->enqueue_to_dispatch_time
= stolen
;
497 dt
->srr0
= kvmppc_get_pc(vcpu
);
498 dt
->srr1
= vcpu
->arch
.shregs
.msr
;
500 if (dt
== vcpu
->arch
.dtl
.pinned_end
)
501 dt
= vcpu
->arch
.dtl
.pinned_addr
;
502 vcpu
->arch
.dtl_ptr
= dt
;
503 /* order writing *dt vs. writing vpa->dtl_idx */
505 vpa
->dtl_idx
= ++vcpu
->arch
.dtl_index
;
506 vcpu
->arch
.dtl
.dirty
= true;
509 int kvmppc_pseries_do_hcall(struct kvm_vcpu
*vcpu
)
511 unsigned long req
= kvmppc_get_gpr(vcpu
, 3);
512 unsigned long target
, ret
= H_SUCCESS
;
513 struct kvm_vcpu
*tvcpu
;
518 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
519 ret
= kvmppc_virtmode_h_enter(vcpu
, kvmppc_get_gpr(vcpu
, 4),
520 kvmppc_get_gpr(vcpu
, 5),
521 kvmppc_get_gpr(vcpu
, 6),
522 kvmppc_get_gpr(vcpu
, 7));
523 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
528 target
= kvmppc_get_gpr(vcpu
, 4);
529 tvcpu
= kvmppc_find_vcpu(vcpu
->kvm
, target
);
534 tvcpu
->arch
.prodded
= 1;
536 if (vcpu
->arch
.ceded
) {
537 if (waitqueue_active(&vcpu
->wq
)) {
538 wake_up_interruptible(&vcpu
->wq
);
539 vcpu
->stat
.halt_wakeup
++;
546 ret
= do_h_register_vpa(vcpu
, kvmppc_get_gpr(vcpu
, 4),
547 kvmppc_get_gpr(vcpu
, 5),
548 kvmppc_get_gpr(vcpu
, 6));
551 if (list_empty(&vcpu
->kvm
->arch
.rtas_tokens
))
554 rc
= kvmppc_rtas_hcall(vcpu
);
561 /* Send the error out to userspace via KVM_RUN */
570 if (kvmppc_xics_enabled(vcpu
)) {
571 ret
= kvmppc_xics_hcall(vcpu
, req
);
577 kvmppc_set_gpr(vcpu
, 3, ret
);
578 vcpu
->arch
.hcall_needed
= 0;
582 static int kvmppc_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
583 struct task_struct
*tsk
)
587 vcpu
->stat
.sum_exits
++;
589 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
590 run
->ready_for_interrupt_injection
= 1;
591 switch (vcpu
->arch
.trap
) {
592 /* We're good on these - the host merely wanted to get our attention */
593 case BOOK3S_INTERRUPT_HV_DECREMENTER
:
594 vcpu
->stat
.dec_exits
++;
597 case BOOK3S_INTERRUPT_EXTERNAL
:
598 vcpu
->stat
.ext_intr_exits
++;
601 case BOOK3S_INTERRUPT_PERFMON
:
604 case BOOK3S_INTERRUPT_MACHINE_CHECK
:
606 * Deliver a machine check interrupt to the guest.
607 * We have to do this, even if the host has handled the
608 * machine check, because machine checks use SRR0/1 and
609 * the interrupt might have trashed guest state in them.
611 kvmppc_book3s_queue_irqprio(vcpu
,
612 BOOK3S_INTERRUPT_MACHINE_CHECK
);
615 case BOOK3S_INTERRUPT_PROGRAM
:
619 * Normally program interrupts are delivered directly
620 * to the guest by the hardware, but we can get here
621 * as a result of a hypervisor emulation interrupt
622 * (e40) getting turned into a 700 by BML RTAS.
624 flags
= vcpu
->arch
.shregs
.msr
& 0x1f0000ull
;
625 kvmppc_core_queue_program(vcpu
, flags
);
629 case BOOK3S_INTERRUPT_SYSCALL
:
631 /* hcall - punt to userspace */
634 if (vcpu
->arch
.shregs
.msr
& MSR_PR
) {
635 /* sc 1 from userspace - reflect to guest syscall */
636 kvmppc_book3s_queue_irqprio(vcpu
, BOOK3S_INTERRUPT_SYSCALL
);
640 run
->papr_hcall
.nr
= kvmppc_get_gpr(vcpu
, 3);
641 for (i
= 0; i
< 9; ++i
)
642 run
->papr_hcall
.args
[i
] = kvmppc_get_gpr(vcpu
, 4 + i
);
643 run
->exit_reason
= KVM_EXIT_PAPR_HCALL
;
644 vcpu
->arch
.hcall_needed
= 1;
649 * We get these next two if the guest accesses a page which it thinks
650 * it has mapped but which is not actually present, either because
651 * it is for an emulated I/O device or because the corresonding
652 * host page has been paged out. Any other HDSI/HISI interrupts
653 * have been handled already.
655 case BOOK3S_INTERRUPT_H_DATA_STORAGE
:
656 r
= RESUME_PAGE_FAULT
;
658 case BOOK3S_INTERRUPT_H_INST_STORAGE
:
659 vcpu
->arch
.fault_dar
= kvmppc_get_pc(vcpu
);
660 vcpu
->arch
.fault_dsisr
= 0;
661 r
= RESUME_PAGE_FAULT
;
664 * This occurs if the guest executes an illegal instruction.
665 * We just generate a program interrupt to the guest, since
666 * we don't emulate any guest instructions at this stage.
668 case BOOK3S_INTERRUPT_H_EMUL_ASSIST
:
669 kvmppc_core_queue_program(vcpu
, 0x80000);
673 kvmppc_dump_regs(vcpu
);
674 printk(KERN_EMERG
"trap=0x%x | pc=0x%lx | msr=0x%llx\n",
675 vcpu
->arch
.trap
, kvmppc_get_pc(vcpu
),
676 vcpu
->arch
.shregs
.msr
);
685 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
686 struct kvm_sregs
*sregs
)
690 memset(sregs
, 0, sizeof(struct kvm_sregs
));
691 sregs
->pvr
= vcpu
->arch
.pvr
;
692 for (i
= 0; i
< vcpu
->arch
.slb_max
; i
++) {
693 sregs
->u
.s
.ppc64
.slb
[i
].slbe
= vcpu
->arch
.slb
[i
].orige
;
694 sregs
->u
.s
.ppc64
.slb
[i
].slbv
= vcpu
->arch
.slb
[i
].origv
;
700 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
701 struct kvm_sregs
*sregs
)
705 kvmppc_set_pvr(vcpu
, sregs
->pvr
);
708 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
709 if (sregs
->u
.s
.ppc64
.slb
[i
].slbe
& SLB_ESID_V
) {
710 vcpu
->arch
.slb
[j
].orige
= sregs
->u
.s
.ppc64
.slb
[i
].slbe
;
711 vcpu
->arch
.slb
[j
].origv
= sregs
->u
.s
.ppc64
.slb
[i
].slbv
;
715 vcpu
->arch
.slb_max
= j
;
720 int kvmppc_get_one_reg(struct kvm_vcpu
*vcpu
, u64 id
, union kvmppc_one_reg
*val
)
726 case KVM_REG_PPC_HIOR
:
727 *val
= get_reg_val(id
, 0);
729 case KVM_REG_PPC_DABR
:
730 *val
= get_reg_val(id
, vcpu
->arch
.dabr
);
732 case KVM_REG_PPC_DSCR
:
733 *val
= get_reg_val(id
, vcpu
->arch
.dscr
);
735 case KVM_REG_PPC_PURR
:
736 *val
= get_reg_val(id
, vcpu
->arch
.purr
);
738 case KVM_REG_PPC_SPURR
:
739 *val
= get_reg_val(id
, vcpu
->arch
.spurr
);
741 case KVM_REG_PPC_AMR
:
742 *val
= get_reg_val(id
, vcpu
->arch
.amr
);
744 case KVM_REG_PPC_UAMOR
:
745 *val
= get_reg_val(id
, vcpu
->arch
.uamor
);
747 case KVM_REG_PPC_MMCR0
... KVM_REG_PPC_MMCRA
:
748 i
= id
- KVM_REG_PPC_MMCR0
;
749 *val
= get_reg_val(id
, vcpu
->arch
.mmcr
[i
]);
751 case KVM_REG_PPC_PMC1
... KVM_REG_PPC_PMC8
:
752 i
= id
- KVM_REG_PPC_PMC1
;
753 *val
= get_reg_val(id
, vcpu
->arch
.pmc
[i
]);
756 case KVM_REG_PPC_FPR0
... KVM_REG_PPC_FPR31
:
757 if (cpu_has_feature(CPU_FTR_VSX
)) {
758 /* VSX => FP reg i is stored in arch.vsr[2*i] */
759 long int i
= id
- KVM_REG_PPC_FPR0
;
760 *val
= get_reg_val(id
, vcpu
->arch
.vsr
[2 * i
]);
762 /* let generic code handle it */
766 case KVM_REG_PPC_VSR0
... KVM_REG_PPC_VSR31
:
767 if (cpu_has_feature(CPU_FTR_VSX
)) {
768 long int i
= id
- KVM_REG_PPC_VSR0
;
769 val
->vsxval
[0] = vcpu
->arch
.vsr
[2 * i
];
770 val
->vsxval
[1] = vcpu
->arch
.vsr
[2 * i
+ 1];
775 #endif /* CONFIG_VSX */
776 case KVM_REG_PPC_VPA_ADDR
:
777 spin_lock(&vcpu
->arch
.vpa_update_lock
);
778 *val
= get_reg_val(id
, vcpu
->arch
.vpa
.next_gpa
);
779 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
781 case KVM_REG_PPC_VPA_SLB
:
782 spin_lock(&vcpu
->arch
.vpa_update_lock
);
783 val
->vpaval
.addr
= vcpu
->arch
.slb_shadow
.next_gpa
;
784 val
->vpaval
.length
= vcpu
->arch
.slb_shadow
.len
;
785 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
787 case KVM_REG_PPC_VPA_DTL
:
788 spin_lock(&vcpu
->arch
.vpa_update_lock
);
789 val
->vpaval
.addr
= vcpu
->arch
.dtl
.next_gpa
;
790 val
->vpaval
.length
= vcpu
->arch
.dtl
.len
;
791 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
801 int kvmppc_set_one_reg(struct kvm_vcpu
*vcpu
, u64 id
, union kvmppc_one_reg
*val
)
805 unsigned long addr
, len
;
808 case KVM_REG_PPC_HIOR
:
809 /* Only allow this to be set to zero */
810 if (set_reg_val(id
, *val
))
813 case KVM_REG_PPC_DABR
:
814 vcpu
->arch
.dabr
= set_reg_val(id
, *val
);
816 case KVM_REG_PPC_DSCR
:
817 vcpu
->arch
.dscr
= set_reg_val(id
, *val
);
819 case KVM_REG_PPC_PURR
:
820 vcpu
->arch
.purr
= set_reg_val(id
, *val
);
822 case KVM_REG_PPC_SPURR
:
823 vcpu
->arch
.spurr
= set_reg_val(id
, *val
);
825 case KVM_REG_PPC_AMR
:
826 vcpu
->arch
.amr
= set_reg_val(id
, *val
);
828 case KVM_REG_PPC_UAMOR
:
829 vcpu
->arch
.uamor
= set_reg_val(id
, *val
);
831 case KVM_REG_PPC_MMCR0
... KVM_REG_PPC_MMCRA
:
832 i
= id
- KVM_REG_PPC_MMCR0
;
833 vcpu
->arch
.mmcr
[i
] = set_reg_val(id
, *val
);
835 case KVM_REG_PPC_PMC1
... KVM_REG_PPC_PMC8
:
836 i
= id
- KVM_REG_PPC_PMC1
;
837 vcpu
->arch
.pmc
[i
] = set_reg_val(id
, *val
);
840 case KVM_REG_PPC_FPR0
... KVM_REG_PPC_FPR31
:
841 if (cpu_has_feature(CPU_FTR_VSX
)) {
842 /* VSX => FP reg i is stored in arch.vsr[2*i] */
843 long int i
= id
- KVM_REG_PPC_FPR0
;
844 vcpu
->arch
.vsr
[2 * i
] = set_reg_val(id
, *val
);
846 /* let generic code handle it */
850 case KVM_REG_PPC_VSR0
... KVM_REG_PPC_VSR31
:
851 if (cpu_has_feature(CPU_FTR_VSX
)) {
852 long int i
= id
- KVM_REG_PPC_VSR0
;
853 vcpu
->arch
.vsr
[2 * i
] = val
->vsxval
[0];
854 vcpu
->arch
.vsr
[2 * i
+ 1] = val
->vsxval
[1];
859 #endif /* CONFIG_VSX */
860 case KVM_REG_PPC_VPA_ADDR
:
861 addr
= set_reg_val(id
, *val
);
863 if (!addr
&& (vcpu
->arch
.slb_shadow
.next_gpa
||
864 vcpu
->arch
.dtl
.next_gpa
))
866 r
= set_vpa(vcpu
, &vcpu
->arch
.vpa
, addr
, sizeof(struct lppaca
));
868 case KVM_REG_PPC_VPA_SLB
:
869 addr
= val
->vpaval
.addr
;
870 len
= val
->vpaval
.length
;
872 if (addr
&& !vcpu
->arch
.vpa
.next_gpa
)
874 r
= set_vpa(vcpu
, &vcpu
->arch
.slb_shadow
, addr
, len
);
876 case KVM_REG_PPC_VPA_DTL
:
877 addr
= val
->vpaval
.addr
;
878 len
= val
->vpaval
.length
;
880 if (addr
&& (len
< sizeof(struct dtl_entry
) ||
881 !vcpu
->arch
.vpa
.next_gpa
))
883 len
-= len
% sizeof(struct dtl_entry
);
884 r
= set_vpa(vcpu
, &vcpu
->arch
.dtl
, addr
, len
);
894 int kvmppc_core_check_processor_compat(void)
896 if (cpu_has_feature(CPU_FTR_HVMODE
))
901 struct kvm_vcpu
*kvmppc_core_vcpu_create(struct kvm
*kvm
, unsigned int id
)
903 struct kvm_vcpu
*vcpu
;
906 struct kvmppc_vcore
*vcore
;
908 core
= id
/ threads_per_core
;
909 if (core
>= KVM_MAX_VCORES
)
913 vcpu
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
917 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
921 vcpu
->arch
.shared
= &vcpu
->arch
.shregs
;
922 vcpu
->arch
.mmcr
[0] = MMCR0_FC
;
923 vcpu
->arch
.ctrl
= CTRL_RUNLATCH
;
924 /* default to host PVR, since we can't spoof it */
925 vcpu
->arch
.pvr
= mfspr(SPRN_PVR
);
926 kvmppc_set_pvr(vcpu
, vcpu
->arch
.pvr
);
927 spin_lock_init(&vcpu
->arch
.vpa_update_lock
);
928 spin_lock_init(&vcpu
->arch
.tbacct_lock
);
929 vcpu
->arch
.busy_preempt
= TB_NIL
;
931 kvmppc_mmu_book3s_hv_init(vcpu
);
933 vcpu
->arch
.state
= KVMPPC_VCPU_NOTREADY
;
935 init_waitqueue_head(&vcpu
->arch
.cpu_run
);
937 mutex_lock(&kvm
->lock
);
938 vcore
= kvm
->arch
.vcores
[core
];
940 vcore
= kzalloc(sizeof(struct kvmppc_vcore
), GFP_KERNEL
);
942 INIT_LIST_HEAD(&vcore
->runnable_threads
);
943 spin_lock_init(&vcore
->lock
);
944 init_waitqueue_head(&vcore
->wq
);
945 vcore
->preempt_tb
= TB_NIL
;
947 kvm
->arch
.vcores
[core
] = vcore
;
948 kvm
->arch
.online_vcores
++;
950 mutex_unlock(&kvm
->lock
);
955 spin_lock(&vcore
->lock
);
956 ++vcore
->num_threads
;
957 spin_unlock(&vcore
->lock
);
958 vcpu
->arch
.vcore
= vcore
;
960 vcpu
->arch
.cpu_type
= KVM_CPU_3S_64
;
961 kvmppc_sanity_check(vcpu
);
966 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
971 static void unpin_vpa(struct kvm
*kvm
, struct kvmppc_vpa
*vpa
)
973 if (vpa
->pinned_addr
)
974 kvmppc_unpin_guest_page(kvm
, vpa
->pinned_addr
, vpa
->gpa
,
978 void kvmppc_core_vcpu_free(struct kvm_vcpu
*vcpu
)
980 spin_lock(&vcpu
->arch
.vpa_update_lock
);
981 unpin_vpa(vcpu
->kvm
, &vcpu
->arch
.dtl
);
982 unpin_vpa(vcpu
->kvm
, &vcpu
->arch
.slb_shadow
);
983 unpin_vpa(vcpu
->kvm
, &vcpu
->arch
.vpa
);
984 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
985 kvm_vcpu_uninit(vcpu
);
986 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
989 static void kvmppc_set_timer(struct kvm_vcpu
*vcpu
)
991 unsigned long dec_nsec
, now
;
994 if (now
> vcpu
->arch
.dec_expires
) {
995 /* decrementer has already gone negative */
996 kvmppc_core_queue_dec(vcpu
);
997 kvmppc_core_prepare_to_enter(vcpu
);
1000 dec_nsec
= (vcpu
->arch
.dec_expires
- now
) * NSEC_PER_SEC
1002 hrtimer_start(&vcpu
->arch
.dec_timer
, ktime_set(0, dec_nsec
),
1004 vcpu
->arch
.timer_running
= 1;
1007 static void kvmppc_end_cede(struct kvm_vcpu
*vcpu
)
1009 vcpu
->arch
.ceded
= 0;
1010 if (vcpu
->arch
.timer_running
) {
1011 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
1012 vcpu
->arch
.timer_running
= 0;
1016 extern int __kvmppc_vcore_entry(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
);
1018 static void kvmppc_remove_runnable(struct kvmppc_vcore
*vc
,
1019 struct kvm_vcpu
*vcpu
)
1023 if (vcpu
->arch
.state
!= KVMPPC_VCPU_RUNNABLE
)
1025 spin_lock(&vcpu
->arch
.tbacct_lock
);
1027 vcpu
->arch
.busy_stolen
+= vcore_stolen_time(vc
, now
) -
1028 vcpu
->arch
.stolen_logged
;
1029 vcpu
->arch
.busy_preempt
= now
;
1030 vcpu
->arch
.state
= KVMPPC_VCPU_BUSY_IN_HOST
;
1031 spin_unlock(&vcpu
->arch
.tbacct_lock
);
1033 list_del(&vcpu
->arch
.run_list
);
1036 static int kvmppc_grab_hwthread(int cpu
)
1038 struct paca_struct
*tpaca
;
1039 long timeout
= 1000;
1043 /* Ensure the thread won't go into the kernel if it wakes */
1044 tpaca
->kvm_hstate
.hwthread_req
= 1;
1045 tpaca
->kvm_hstate
.kvm_vcpu
= NULL
;
1048 * If the thread is already executing in the kernel (e.g. handling
1049 * a stray interrupt), wait for it to get back to nap mode.
1050 * The smp_mb() is to ensure that our setting of hwthread_req
1051 * is visible before we look at hwthread_state, so if this
1052 * races with the code at system_reset_pSeries and the thread
1053 * misses our setting of hwthread_req, we are sure to see its
1054 * setting of hwthread_state, and vice versa.
1057 while (tpaca
->kvm_hstate
.hwthread_state
== KVM_HWTHREAD_IN_KERNEL
) {
1058 if (--timeout
<= 0) {
1059 pr_err("KVM: couldn't grab cpu %d\n", cpu
);
1067 static void kvmppc_release_hwthread(int cpu
)
1069 struct paca_struct
*tpaca
;
1072 tpaca
->kvm_hstate
.hwthread_req
= 0;
1073 tpaca
->kvm_hstate
.kvm_vcpu
= NULL
;
1076 static void kvmppc_start_thread(struct kvm_vcpu
*vcpu
)
1079 struct paca_struct
*tpaca
;
1080 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
1082 if (vcpu
->arch
.timer_running
) {
1083 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
1084 vcpu
->arch
.timer_running
= 0;
1086 cpu
= vc
->pcpu
+ vcpu
->arch
.ptid
;
1088 tpaca
->kvm_hstate
.kvm_vcpu
= vcpu
;
1089 tpaca
->kvm_hstate
.kvm_vcore
= vc
;
1090 tpaca
->kvm_hstate
.napping
= 0;
1091 vcpu
->cpu
= vc
->pcpu
;
1093 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1094 if (vcpu
->arch
.ptid
) {
1101 static void kvmppc_wait_for_nap(struct kvmppc_vcore
*vc
)
1107 while (vc
->nap_count
< vc
->n_woken
) {
1108 if (++i
>= 1000000) {
1109 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1110 vc
->nap_count
, vc
->n_woken
);
1119 * Check that we are on thread 0 and that any other threads in
1120 * this core are off-line. Then grab the threads so they can't
1123 static int on_primary_thread(void)
1125 int cpu
= smp_processor_id();
1126 int thr
= cpu_thread_in_core(cpu
);
1130 while (++thr
< threads_per_core
)
1131 if (cpu_online(cpu
+ thr
))
1134 /* Grab all hw threads so they can't go into the kernel */
1135 for (thr
= 1; thr
< threads_per_core
; ++thr
) {
1136 if (kvmppc_grab_hwthread(cpu
+ thr
)) {
1137 /* Couldn't grab one; let the others go */
1139 kvmppc_release_hwthread(cpu
+ thr
);
1140 } while (--thr
> 0);
1148 * Run a set of guest threads on a physical core.
1149 * Called with vc->lock held.
1151 static void kvmppc_run_core(struct kvmppc_vcore
*vc
)
1153 struct kvm_vcpu
*vcpu
, *vcpu0
, *vnext
;
1156 int ptid
, i
, need_vpa_update
;
1158 struct kvm_vcpu
*vcpus_to_update
[threads_per_core
];
1160 /* don't start if any threads have a signal pending */
1161 need_vpa_update
= 0;
1162 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1163 if (signal_pending(vcpu
->arch
.run_task
))
1165 if (vcpu
->arch
.vpa
.update_pending
||
1166 vcpu
->arch
.slb_shadow
.update_pending
||
1167 vcpu
->arch
.dtl
.update_pending
)
1168 vcpus_to_update
[need_vpa_update
++] = vcpu
;
1172 * Initialize *vc, in particular vc->vcore_state, so we can
1173 * drop the vcore lock if necessary.
1177 vc
->entry_exit_count
= 0;
1178 vc
->vcore_state
= VCORE_STARTING
;
1180 vc
->napping_threads
= 0;
1183 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1184 * which can't be called with any spinlocks held.
1186 if (need_vpa_update
) {
1187 spin_unlock(&vc
->lock
);
1188 for (i
= 0; i
< need_vpa_update
; ++i
)
1189 kvmppc_update_vpas(vcpus_to_update
[i
]);
1190 spin_lock(&vc
->lock
);
1194 * Assign physical thread IDs, first to non-ceded vcpus
1195 * and then to ceded ones.
1199 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1200 if (!vcpu
->arch
.ceded
) {
1203 vcpu
->arch
.ptid
= ptid
++;
1207 goto out
; /* nothing to run; should never happen */
1208 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
)
1209 if (vcpu
->arch
.ceded
)
1210 vcpu
->arch
.ptid
= ptid
++;
1213 * Make sure we are running on thread 0, and that
1214 * secondary threads are offline.
1216 if (threads_per_core
> 1 && !on_primary_thread()) {
1217 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
)
1218 vcpu
->arch
.ret
= -EBUSY
;
1222 vc
->pcpu
= smp_processor_id();
1223 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1224 kvmppc_start_thread(vcpu
);
1225 kvmppc_create_dtl_entry(vcpu
, vc
);
1228 vc
->vcore_state
= VCORE_RUNNING
;
1230 spin_unlock(&vc
->lock
);
1234 srcu_idx
= srcu_read_lock(&vcpu0
->kvm
->srcu
);
1236 __kvmppc_vcore_entry(NULL
, vcpu0
);
1238 spin_lock(&vc
->lock
);
1239 /* disable sending of IPIs on virtual external irqs */
1240 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
)
1242 /* wait for secondary threads to finish writing their state to memory */
1243 if (vc
->nap_count
< vc
->n_woken
)
1244 kvmppc_wait_for_nap(vc
);
1245 for (i
= 0; i
< threads_per_core
; ++i
)
1246 kvmppc_release_hwthread(vc
->pcpu
+ i
);
1247 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1248 vc
->vcore_state
= VCORE_EXITING
;
1249 spin_unlock(&vc
->lock
);
1251 srcu_read_unlock(&vcpu0
->kvm
->srcu
, srcu_idx
);
1253 /* make sure updates to secondary vcpu structs are visible now */
1260 spin_lock(&vc
->lock
);
1262 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1263 /* cancel pending dec exception if dec is positive */
1264 if (now
< vcpu
->arch
.dec_expires
&&
1265 kvmppc_core_pending_dec(vcpu
))
1266 kvmppc_core_dequeue_dec(vcpu
);
1269 if (vcpu
->arch
.trap
)
1270 ret
= kvmppc_handle_exit(vcpu
->arch
.kvm_run
, vcpu
,
1271 vcpu
->arch
.run_task
);
1273 vcpu
->arch
.ret
= ret
;
1274 vcpu
->arch
.trap
= 0;
1276 if (vcpu
->arch
.ceded
) {
1277 if (ret
!= RESUME_GUEST
)
1278 kvmppc_end_cede(vcpu
);
1280 kvmppc_set_timer(vcpu
);
1285 vc
->vcore_state
= VCORE_INACTIVE
;
1286 list_for_each_entry_safe(vcpu
, vnext
, &vc
->runnable_threads
,
1288 if (vcpu
->arch
.ret
!= RESUME_GUEST
) {
1289 kvmppc_remove_runnable(vc
, vcpu
);
1290 wake_up(&vcpu
->arch
.cpu_run
);
1296 * Wait for some other vcpu thread to execute us, and
1297 * wake us up when we need to handle something in the host.
1299 static void kvmppc_wait_for_exec(struct kvm_vcpu
*vcpu
, int wait_state
)
1303 prepare_to_wait(&vcpu
->arch
.cpu_run
, &wait
, wait_state
);
1304 if (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
)
1306 finish_wait(&vcpu
->arch
.cpu_run
, &wait
);
1310 * All the vcpus in this vcore are idle, so wait for a decrementer
1311 * or external interrupt to one of the vcpus. vc->lock is held.
1313 static void kvmppc_vcore_blocked(struct kvmppc_vcore
*vc
)
1317 prepare_to_wait(&vc
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1318 vc
->vcore_state
= VCORE_SLEEPING
;
1319 spin_unlock(&vc
->lock
);
1321 finish_wait(&vc
->wq
, &wait
);
1322 spin_lock(&vc
->lock
);
1323 vc
->vcore_state
= VCORE_INACTIVE
;
1326 static int kvmppc_run_vcpu(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
1329 struct kvmppc_vcore
*vc
;
1330 struct kvm_vcpu
*v
, *vn
;
1332 kvm_run
->exit_reason
= 0;
1333 vcpu
->arch
.ret
= RESUME_GUEST
;
1334 vcpu
->arch
.trap
= 0;
1335 kvmppc_update_vpas(vcpu
);
1338 * Synchronize with other threads in this virtual core
1340 vc
= vcpu
->arch
.vcore
;
1341 spin_lock(&vc
->lock
);
1342 vcpu
->arch
.ceded
= 0;
1343 vcpu
->arch
.run_task
= current
;
1344 vcpu
->arch
.kvm_run
= kvm_run
;
1345 vcpu
->arch
.stolen_logged
= vcore_stolen_time(vc
, mftb());
1346 vcpu
->arch
.state
= KVMPPC_VCPU_RUNNABLE
;
1347 vcpu
->arch
.busy_preempt
= TB_NIL
;
1348 list_add_tail(&vcpu
->arch
.run_list
, &vc
->runnable_threads
);
1352 * This happens the first time this is called for a vcpu.
1353 * If the vcore is already running, we may be able to start
1354 * this thread straight away and have it join in.
1356 if (!signal_pending(current
)) {
1357 if (vc
->vcore_state
== VCORE_RUNNING
&&
1358 VCORE_EXIT_COUNT(vc
) == 0) {
1359 vcpu
->arch
.ptid
= vc
->n_runnable
- 1;
1360 kvmppc_create_dtl_entry(vcpu
, vc
);
1361 kvmppc_start_thread(vcpu
);
1362 } else if (vc
->vcore_state
== VCORE_SLEEPING
) {
1368 while (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
&&
1369 !signal_pending(current
)) {
1370 if (vc
->vcore_state
!= VCORE_INACTIVE
) {
1371 spin_unlock(&vc
->lock
);
1372 kvmppc_wait_for_exec(vcpu
, TASK_INTERRUPTIBLE
);
1373 spin_lock(&vc
->lock
);
1376 list_for_each_entry_safe(v
, vn
, &vc
->runnable_threads
,
1378 kvmppc_core_prepare_to_enter(v
);
1379 if (signal_pending(v
->arch
.run_task
)) {
1380 kvmppc_remove_runnable(vc
, v
);
1381 v
->stat
.signal_exits
++;
1382 v
->arch
.kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1383 v
->arch
.ret
= -EINTR
;
1384 wake_up(&v
->arch
.cpu_run
);
1387 if (!vc
->n_runnable
|| vcpu
->arch
.state
!= KVMPPC_VCPU_RUNNABLE
)
1391 list_for_each_entry(v
, &vc
->runnable_threads
, arch
.run_list
) {
1392 if (!v
->arch
.pending_exceptions
)
1393 n_ceded
+= v
->arch
.ceded
;
1397 if (n_ceded
== vc
->n_runnable
)
1398 kvmppc_vcore_blocked(vc
);
1400 kvmppc_run_core(vc
);
1404 while (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
&&
1405 (vc
->vcore_state
== VCORE_RUNNING
||
1406 vc
->vcore_state
== VCORE_EXITING
)) {
1407 spin_unlock(&vc
->lock
);
1408 kvmppc_wait_for_exec(vcpu
, TASK_UNINTERRUPTIBLE
);
1409 spin_lock(&vc
->lock
);
1412 if (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
) {
1413 kvmppc_remove_runnable(vc
, vcpu
);
1414 vcpu
->stat
.signal_exits
++;
1415 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1416 vcpu
->arch
.ret
= -EINTR
;
1419 if (vc
->n_runnable
&& vc
->vcore_state
== VCORE_INACTIVE
) {
1420 /* Wake up some vcpu to run the core */
1421 v
= list_first_entry(&vc
->runnable_threads
,
1422 struct kvm_vcpu
, arch
.run_list
);
1423 wake_up(&v
->arch
.cpu_run
);
1426 spin_unlock(&vc
->lock
);
1427 return vcpu
->arch
.ret
;
1430 int kvmppc_vcpu_run(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
1435 if (!vcpu
->arch
.sane
) {
1436 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1440 kvmppc_core_prepare_to_enter(vcpu
);
1442 /* No need to go into the guest when all we'll do is come back out */
1443 if (signal_pending(current
)) {
1444 run
->exit_reason
= KVM_EXIT_INTR
;
1448 atomic_inc(&vcpu
->kvm
->arch
.vcpus_running
);
1449 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1452 /* On the first time here, set up HTAB and VRMA or RMA */
1453 if (!vcpu
->kvm
->arch
.rma_setup_done
) {
1454 r
= kvmppc_hv_setup_htab_rma(vcpu
);
1459 flush_fp_to_thread(current
);
1460 flush_altivec_to_thread(current
);
1461 flush_vsx_to_thread(current
);
1462 vcpu
->arch
.wqp
= &vcpu
->arch
.vcore
->wq
;
1463 vcpu
->arch
.pgdir
= current
->mm
->pgd
;
1464 vcpu
->arch
.state
= KVMPPC_VCPU_BUSY_IN_HOST
;
1467 r
= kvmppc_run_vcpu(run
, vcpu
);
1469 if (run
->exit_reason
== KVM_EXIT_PAPR_HCALL
&&
1470 !(vcpu
->arch
.shregs
.msr
& MSR_PR
)) {
1471 r
= kvmppc_pseries_do_hcall(vcpu
);
1472 kvmppc_core_prepare_to_enter(vcpu
);
1473 } else if (r
== RESUME_PAGE_FAULT
) {
1474 srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1475 r
= kvmppc_book3s_hv_page_fault(run
, vcpu
,
1476 vcpu
->arch
.fault_dar
, vcpu
->arch
.fault_dsisr
);
1477 srcu_read_unlock(&vcpu
->kvm
->srcu
, srcu_idx
);
1479 } while (r
== RESUME_GUEST
);
1482 vcpu
->arch
.state
= KVMPPC_VCPU_NOTREADY
;
1483 atomic_dec(&vcpu
->kvm
->arch
.vcpus_running
);
1488 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1489 Assumes POWER7 or PPC970. */
1490 static inline int lpcr_rmls(unsigned long rma_size
)
1493 case 32ul << 20: /* 32 MB */
1494 if (cpu_has_feature(CPU_FTR_ARCH_206
))
1495 return 8; /* only supported on POWER7 */
1497 case 64ul << 20: /* 64 MB */
1499 case 128ul << 20: /* 128 MB */
1501 case 256ul << 20: /* 256 MB */
1503 case 1ul << 30: /* 1 GB */
1505 case 16ul << 30: /* 16 GB */
1507 case 256ul << 30: /* 256 GB */
1514 static int kvm_rma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1517 struct kvm_rma_info
*ri
= vma
->vm_file
->private_data
;
1519 if (vmf
->pgoff
>= kvm_rma_pages
)
1520 return VM_FAULT_SIGBUS
;
1522 page
= pfn_to_page(ri
->base_pfn
+ vmf
->pgoff
);
1528 static const struct vm_operations_struct kvm_rma_vm_ops
= {
1529 .fault
= kvm_rma_fault
,
1532 static int kvm_rma_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1534 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
1535 vma
->vm_ops
= &kvm_rma_vm_ops
;
1539 static int kvm_rma_release(struct inode
*inode
, struct file
*filp
)
1541 struct kvm_rma_info
*ri
= filp
->private_data
;
1543 kvm_release_rma(ri
);
1547 static const struct file_operations kvm_rma_fops
= {
1548 .mmap
= kvm_rma_mmap
,
1549 .release
= kvm_rma_release
,
1552 long kvm_vm_ioctl_allocate_rma(struct kvm
*kvm
, struct kvm_allocate_rma
*ret
)
1555 struct kvm_rma_info
*ri
;
1557 * Only do this on PPC970 in HV mode
1559 if (!cpu_has_feature(CPU_FTR_HVMODE
) ||
1560 !cpu_has_feature(CPU_FTR_ARCH_201
))
1566 ri
= kvm_alloc_rma();
1570 fd
= anon_inode_getfd("kvm-rma", &kvm_rma_fops
, ri
, O_RDWR
| O_CLOEXEC
);
1572 kvm_release_rma(ri
);
1574 ret
->rma_size
= kvm_rma_pages
<< PAGE_SHIFT
;
1578 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size
**sps
,
1581 struct mmu_psize_def
*def
= &mmu_psize_defs
[linux_psize
];
1585 (*sps
)->page_shift
= def
->shift
;
1586 (*sps
)->slb_enc
= def
->sllp
;
1587 (*sps
)->enc
[0].page_shift
= def
->shift
;
1589 * Only return base page encoding. We don't want to return
1590 * all the supporting pte_enc, because our H_ENTER doesn't
1591 * support MPSS yet. Once they do, we can start passing all
1592 * support pte_enc here
1594 (*sps
)->enc
[0].pte_enc
= def
->penc
[linux_psize
];
1598 int kvm_vm_ioctl_get_smmu_info(struct kvm
*kvm
, struct kvm_ppc_smmu_info
*info
)
1600 struct kvm_ppc_one_seg_page_size
*sps
;
1602 info
->flags
= KVM_PPC_PAGE_SIZES_REAL
;
1603 if (mmu_has_feature(MMU_FTR_1T_SEGMENT
))
1604 info
->flags
|= KVM_PPC_1T_SEGMENTS
;
1605 info
->slb_size
= mmu_slb_size
;
1607 /* We only support these sizes for now, and no muti-size segments */
1608 sps
= &info
->sps
[0];
1609 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_4K
);
1610 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_64K
);
1611 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_16M
);
1617 * Get (and clear) the dirty memory log for a memory slot.
1619 int kvm_vm_ioctl_get_dirty_log(struct kvm
*kvm
, struct kvm_dirty_log
*log
)
1621 struct kvm_memory_slot
*memslot
;
1625 mutex_lock(&kvm
->slots_lock
);
1628 if (log
->slot
>= KVM_USER_MEM_SLOTS
)
1631 memslot
= id_to_memslot(kvm
->memslots
, log
->slot
);
1633 if (!memslot
->dirty_bitmap
)
1636 n
= kvm_dirty_bitmap_bytes(memslot
);
1637 memset(memslot
->dirty_bitmap
, 0, n
);
1639 r
= kvmppc_hv_get_dirty_log(kvm
, memslot
, memslot
->dirty_bitmap
);
1644 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1649 mutex_unlock(&kvm
->slots_lock
);
1653 static void unpin_slot(struct kvm_memory_slot
*memslot
)
1655 unsigned long *physp
;
1656 unsigned long j
, npages
, pfn
;
1659 physp
= memslot
->arch
.slot_phys
;
1660 npages
= memslot
->npages
;
1663 for (j
= 0; j
< npages
; j
++) {
1664 if (!(physp
[j
] & KVMPPC_GOT_PAGE
))
1666 pfn
= physp
[j
] >> PAGE_SHIFT
;
1667 page
= pfn_to_page(pfn
);
1673 void kvmppc_core_free_memslot(struct kvm_memory_slot
*free
,
1674 struct kvm_memory_slot
*dont
)
1676 if (!dont
|| free
->arch
.rmap
!= dont
->arch
.rmap
) {
1677 vfree(free
->arch
.rmap
);
1678 free
->arch
.rmap
= NULL
;
1680 if (!dont
|| free
->arch
.slot_phys
!= dont
->arch
.slot_phys
) {
1682 vfree(free
->arch
.slot_phys
);
1683 free
->arch
.slot_phys
= NULL
;
1687 int kvmppc_core_create_memslot(struct kvm_memory_slot
*slot
,
1688 unsigned long npages
)
1690 slot
->arch
.rmap
= vzalloc(npages
* sizeof(*slot
->arch
.rmap
));
1691 if (!slot
->arch
.rmap
)
1693 slot
->arch
.slot_phys
= NULL
;
1698 int kvmppc_core_prepare_memory_region(struct kvm
*kvm
,
1699 struct kvm_memory_slot
*memslot
,
1700 struct kvm_userspace_memory_region
*mem
)
1702 unsigned long *phys
;
1704 /* Allocate a slot_phys array if needed */
1705 phys
= memslot
->arch
.slot_phys
;
1706 if (!kvm
->arch
.using_mmu_notifiers
&& !phys
&& memslot
->npages
) {
1707 phys
= vzalloc(memslot
->npages
* sizeof(unsigned long));
1710 memslot
->arch
.slot_phys
= phys
;
1716 void kvmppc_core_commit_memory_region(struct kvm
*kvm
,
1717 struct kvm_userspace_memory_region
*mem
,
1718 const struct kvm_memory_slot
*old
)
1720 unsigned long npages
= mem
->memory_size
>> PAGE_SHIFT
;
1721 struct kvm_memory_slot
*memslot
;
1723 if (npages
&& old
->npages
) {
1725 * If modifying a memslot, reset all the rmap dirty bits.
1726 * If this is a new memslot, we don't need to do anything
1727 * since the rmap array starts out as all zeroes,
1728 * i.e. no pages are dirty.
1730 memslot
= id_to_memslot(kvm
->memslots
, mem
->slot
);
1731 kvmppc_hv_get_dirty_log(kvm
, memslot
, NULL
);
1735 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu
*vcpu
)
1738 struct kvm
*kvm
= vcpu
->kvm
;
1739 struct kvm_rma_info
*ri
= NULL
;
1741 struct kvm_memory_slot
*memslot
;
1742 struct vm_area_struct
*vma
;
1743 unsigned long lpcr
, senc
;
1744 unsigned long psize
, porder
;
1745 unsigned long rma_size
;
1747 unsigned long *physp
;
1748 unsigned long i
, npages
;
1751 mutex_lock(&kvm
->lock
);
1752 if (kvm
->arch
.rma_setup_done
)
1753 goto out
; /* another vcpu beat us to it */
1755 /* Allocate hashed page table (if not done already) and reset it */
1756 if (!kvm
->arch
.hpt_virt
) {
1757 err
= kvmppc_alloc_hpt(kvm
, NULL
);
1759 pr_err("KVM: Couldn't alloc HPT\n");
1764 /* Look up the memslot for guest physical address 0 */
1765 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
1766 memslot
= gfn_to_memslot(kvm
, 0);
1768 /* We must have some memory at 0 by now */
1770 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
1773 /* Look up the VMA for the start of this memory slot */
1774 hva
= memslot
->userspace_addr
;
1775 down_read(¤t
->mm
->mmap_sem
);
1776 vma
= find_vma(current
->mm
, hva
);
1777 if (!vma
|| vma
->vm_start
> hva
|| (vma
->vm_flags
& VM_IO
))
1780 psize
= vma_kernel_pagesize(vma
);
1781 porder
= __ilog2(psize
);
1783 /* Is this one of our preallocated RMAs? */
1784 if (vma
->vm_file
&& vma
->vm_file
->f_op
== &kvm_rma_fops
&&
1785 hva
== vma
->vm_start
)
1786 ri
= vma
->vm_file
->private_data
;
1788 up_read(¤t
->mm
->mmap_sem
);
1791 /* On POWER7, use VRMA; on PPC970, give up */
1793 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
1794 pr_err("KVM: CPU requires an RMO\n");
1798 /* We can handle 4k, 64k or 16M pages in the VRMA */
1800 if (!(psize
== 0x1000 || psize
== 0x10000 ||
1801 psize
== 0x1000000))
1804 /* Update VRMASD field in the LPCR */
1805 senc
= slb_pgsize_encoding(psize
);
1806 kvm
->arch
.vrma_slb_v
= senc
| SLB_VSID_B_1T
|
1807 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
1808 lpcr
= kvm
->arch
.lpcr
& ~LPCR_VRMASD
;
1809 lpcr
|= senc
<< (LPCR_VRMASD_SH
- 4);
1810 kvm
->arch
.lpcr
= lpcr
;
1812 /* Create HPTEs in the hash page table for the VRMA */
1813 kvmppc_map_vrma(vcpu
, memslot
, porder
);
1816 /* Set up to use an RMO region */
1817 rma_size
= kvm_rma_pages
;
1818 if (rma_size
> memslot
->npages
)
1819 rma_size
= memslot
->npages
;
1820 rma_size
<<= PAGE_SHIFT
;
1821 rmls
= lpcr_rmls(rma_size
);
1823 if ((long)rmls
< 0) {
1824 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size
);
1827 atomic_inc(&ri
->use_count
);
1830 /* Update LPCR and RMOR */
1831 lpcr
= kvm
->arch
.lpcr
;
1832 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
1833 /* PPC970; insert RMLS value (split field) in HID4 */
1834 lpcr
&= ~((1ul << HID4_RMLS0_SH
) |
1835 (3ul << HID4_RMLS2_SH
));
1836 lpcr
|= ((rmls
>> 2) << HID4_RMLS0_SH
) |
1837 ((rmls
& 3) << HID4_RMLS2_SH
);
1838 /* RMOR is also in HID4 */
1839 lpcr
|= ((ri
->base_pfn
>> (26 - PAGE_SHIFT
)) & 0xffff)
1843 lpcr
&= ~(LPCR_VPM0
| LPCR_VRMA_L
);
1844 lpcr
|= rmls
<< LPCR_RMLS_SH
;
1845 kvm
->arch
.rmor
= ri
->base_pfn
<< PAGE_SHIFT
;
1847 kvm
->arch
.lpcr
= lpcr
;
1848 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
1849 ri
->base_pfn
<< PAGE_SHIFT
, rma_size
, lpcr
);
1851 /* Initialize phys addrs of pages in RMO */
1852 npages
= kvm_rma_pages
;
1853 porder
= __ilog2(npages
);
1854 physp
= memslot
->arch
.slot_phys
;
1856 if (npages
> memslot
->npages
)
1857 npages
= memslot
->npages
;
1858 spin_lock(&kvm
->arch
.slot_phys_lock
);
1859 for (i
= 0; i
< npages
; ++i
)
1860 physp
[i
] = ((ri
->base_pfn
+ i
) << PAGE_SHIFT
) +
1862 spin_unlock(&kvm
->arch
.slot_phys_lock
);
1866 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
1868 kvm
->arch
.rma_setup_done
= 1;
1871 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
1873 mutex_unlock(&kvm
->lock
);
1877 up_read(¤t
->mm
->mmap_sem
);
1881 int kvmppc_core_init_vm(struct kvm
*kvm
)
1883 unsigned long lpcr
, lpid
;
1885 /* Allocate the guest's logical partition ID */
1887 lpid
= kvmppc_alloc_lpid();
1890 kvm
->arch
.lpid
= lpid
;
1893 * Since we don't flush the TLB when tearing down a VM,
1894 * and this lpid might have previously been used,
1895 * make sure we flush on each core before running the new VM.
1897 cpumask_setall(&kvm
->arch
.need_tlb_flush
);
1899 INIT_LIST_HEAD(&kvm
->arch
.spapr_tce_tables
);
1900 INIT_LIST_HEAD(&kvm
->arch
.rtas_tokens
);
1902 kvm
->arch
.rma
= NULL
;
1904 kvm
->arch
.host_sdr1
= mfspr(SPRN_SDR1
);
1906 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
1907 /* PPC970; HID4 is effectively the LPCR */
1908 kvm
->arch
.host_lpid
= 0;
1909 kvm
->arch
.host_lpcr
= lpcr
= mfspr(SPRN_HID4
);
1910 lpcr
&= ~((3 << HID4_LPID1_SH
) | (0xful
<< HID4_LPID5_SH
));
1911 lpcr
|= ((lpid
>> 4) << HID4_LPID1_SH
) |
1912 ((lpid
& 0xf) << HID4_LPID5_SH
);
1914 /* POWER7; init LPCR for virtual RMA mode */
1915 kvm
->arch
.host_lpid
= mfspr(SPRN_LPID
);
1916 kvm
->arch
.host_lpcr
= lpcr
= mfspr(SPRN_LPCR
);
1917 lpcr
&= LPCR_PECE
| LPCR_LPES
;
1918 lpcr
|= (4UL << LPCR_DPFD_SH
) | LPCR_HDICE
|
1919 LPCR_VPM0
| LPCR_VPM1
;
1920 kvm
->arch
.vrma_slb_v
= SLB_VSID_B_1T
|
1921 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
1923 kvm
->arch
.lpcr
= lpcr
;
1925 kvm
->arch
.using_mmu_notifiers
= !!cpu_has_feature(CPU_FTR_ARCH_206
);
1926 spin_lock_init(&kvm
->arch
.slot_phys_lock
);
1929 * Don't allow secondary CPU threads to come online
1930 * while any KVM VMs exist.
1932 inhibit_secondary_onlining();
1937 void kvmppc_core_destroy_vm(struct kvm
*kvm
)
1939 uninhibit_secondary_onlining();
1941 if (kvm
->arch
.rma
) {
1942 kvm_release_rma(kvm
->arch
.rma
);
1943 kvm
->arch
.rma
= NULL
;
1946 kvmppc_rtas_tokens_free(kvm
);
1948 kvmppc_free_hpt(kvm
);
1949 WARN_ON(!list_empty(&kvm
->arch
.spapr_tce_tables
));
1952 /* These are stubs for now */
1953 void kvmppc_mmu_pte_pflush(struct kvm_vcpu
*vcpu
, ulong pa_start
, ulong pa_end
)
1957 /* We don't need to emulate any privileged instructions or dcbz */
1958 int kvmppc_core_emulate_op(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
1959 unsigned int inst
, int *advance
)
1961 return EMULATE_FAIL
;
1964 int kvmppc_core_emulate_mtspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong spr_val
)
1966 return EMULATE_FAIL
;
1969 int kvmppc_core_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, ulong
*spr_val
)
1971 return EMULATE_FAIL
;
1974 static int kvmppc_book3s_hv_init(void)
1978 r
= kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
1983 r
= kvmppc_mmu_hv_init();
1988 static void kvmppc_book3s_hv_exit(void)
1993 module_init(kvmppc_book3s_hv_init
);
1994 module_exit(kvmppc_book3s_hv_exit
);