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 struct swait_head
*wqp
;
79 wqp
= kvm_arch_vcpu_wq(vcpu
);
80 if (swaitqueue_active(wqp
)) {
81 swait_wake_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_PPC_ICP_NATIVE
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
;
140 spin_lock_irqsave(&vcpu
->arch
.tbacct_lock
, flags
);
141 if (vc
->runner
== vcpu
&& vc
->vcore_state
!= VCORE_INACTIVE
&&
142 vc
->preempt_tb
!= TB_NIL
) {
143 vc
->stolen_tb
+= mftb() - vc
->preempt_tb
;
144 vc
->preempt_tb
= TB_NIL
;
146 if (vcpu
->arch
.state
== KVMPPC_VCPU_BUSY_IN_HOST
&&
147 vcpu
->arch
.busy_preempt
!= TB_NIL
) {
148 vcpu
->arch
.busy_stolen
+= mftb() - vcpu
->arch
.busy_preempt
;
149 vcpu
->arch
.busy_preempt
= TB_NIL
;
151 spin_unlock_irqrestore(&vcpu
->arch
.tbacct_lock
, flags
);
154 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu
*vcpu
)
156 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
159 spin_lock_irqsave(&vcpu
->arch
.tbacct_lock
, flags
);
160 if (vc
->runner
== vcpu
&& vc
->vcore_state
!= VCORE_INACTIVE
)
161 vc
->preempt_tb
= mftb();
162 if (vcpu
->arch
.state
== KVMPPC_VCPU_BUSY_IN_HOST
)
163 vcpu
->arch
.busy_preempt
= mftb();
164 spin_unlock_irqrestore(&vcpu
->arch
.tbacct_lock
, flags
);
167 static void kvmppc_set_msr_hv(struct kvm_vcpu
*vcpu
, u64 msr
)
169 vcpu
->arch
.shregs
.msr
= msr
;
170 kvmppc_end_cede(vcpu
);
173 void kvmppc_set_pvr_hv(struct kvm_vcpu
*vcpu
, u32 pvr
)
175 vcpu
->arch
.pvr
= pvr
;
178 int kvmppc_set_arch_compat(struct kvm_vcpu
*vcpu
, u32 arch_compat
)
180 unsigned long pcr
= 0;
181 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
184 if (!cpu_has_feature(CPU_FTR_ARCH_206
))
185 return -EINVAL
; /* 970 has no compat mode support */
187 switch (arch_compat
) {
190 * If an arch bit is set in PCR, all the defined
191 * higher-order arch bits also have to be set.
193 pcr
= PCR_ARCH_206
| PCR_ARCH_205
;
205 if (!cpu_has_feature(CPU_FTR_ARCH_207S
)) {
206 /* POWER7 can't emulate POWER8 */
207 if (!(pcr
& PCR_ARCH_206
))
209 pcr
&= ~PCR_ARCH_206
;
213 spin_lock(&vc
->lock
);
214 vc
->arch_compat
= arch_compat
;
216 spin_unlock(&vc
->lock
);
221 void kvmppc_dump_regs(struct kvm_vcpu
*vcpu
)
225 pr_err("vcpu %p (%d):\n", vcpu
, vcpu
->vcpu_id
);
226 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
227 vcpu
->arch
.pc
, vcpu
->arch
.shregs
.msr
, vcpu
->arch
.trap
);
228 for (r
= 0; r
< 16; ++r
)
229 pr_err("r%2d = %.16lx r%d = %.16lx\n",
230 r
, kvmppc_get_gpr(vcpu
, r
),
231 r
+16, kvmppc_get_gpr(vcpu
, r
+16));
232 pr_err("ctr = %.16lx lr = %.16lx\n",
233 vcpu
->arch
.ctr
, vcpu
->arch
.lr
);
234 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
235 vcpu
->arch
.shregs
.srr0
, vcpu
->arch
.shregs
.srr1
);
236 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
237 vcpu
->arch
.shregs
.sprg0
, vcpu
->arch
.shregs
.sprg1
);
238 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
239 vcpu
->arch
.shregs
.sprg2
, vcpu
->arch
.shregs
.sprg3
);
240 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
241 vcpu
->arch
.cr
, vcpu
->arch
.xer
, vcpu
->arch
.shregs
.dsisr
);
242 pr_err("dar = %.16llx\n", vcpu
->arch
.shregs
.dar
);
243 pr_err("fault dar = %.16lx dsisr = %.8x\n",
244 vcpu
->arch
.fault_dar
, vcpu
->arch
.fault_dsisr
);
245 pr_err("SLB (%d entries):\n", vcpu
->arch
.slb_max
);
246 for (r
= 0; r
< vcpu
->arch
.slb_max
; ++r
)
247 pr_err(" ESID = %.16llx VSID = %.16llx\n",
248 vcpu
->arch
.slb
[r
].orige
, vcpu
->arch
.slb
[r
].origv
);
249 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
250 vcpu
->arch
.vcore
->lpcr
, vcpu
->kvm
->arch
.sdr1
,
251 vcpu
->arch
.last_inst
);
254 struct kvm_vcpu
*kvmppc_find_vcpu(struct kvm
*kvm
, int id
)
257 struct kvm_vcpu
*v
, *ret
= NULL
;
259 mutex_lock(&kvm
->lock
);
260 kvm_for_each_vcpu(r
, v
, kvm
) {
261 if (v
->vcpu_id
== id
) {
266 mutex_unlock(&kvm
->lock
);
270 static void init_vpa(struct kvm_vcpu
*vcpu
, struct lppaca
*vpa
)
272 vpa
->__old_status
|= LPPACA_OLD_SHARED_PROC
;
273 vpa
->yield_count
= 1;
276 static int set_vpa(struct kvm_vcpu
*vcpu
, struct kvmppc_vpa
*v
,
277 unsigned long addr
, unsigned long len
)
279 /* check address is cacheline aligned */
280 if (addr
& (L1_CACHE_BYTES
- 1))
282 spin_lock(&vcpu
->arch
.vpa_update_lock
);
283 if (v
->next_gpa
!= addr
|| v
->len
!= len
) {
285 v
->len
= addr
? len
: 0;
286 v
->update_pending
= 1;
288 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
292 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
301 static int vpa_is_registered(struct kvmppc_vpa
*vpap
)
303 if (vpap
->update_pending
)
304 return vpap
->next_gpa
!= 0;
305 return vpap
->pinned_addr
!= NULL
;
308 static unsigned long do_h_register_vpa(struct kvm_vcpu
*vcpu
,
310 unsigned long vcpuid
, unsigned long vpa
)
312 struct kvm
*kvm
= vcpu
->kvm
;
313 unsigned long len
, nb
;
315 struct kvm_vcpu
*tvcpu
;
318 struct kvmppc_vpa
*vpap
;
320 tvcpu
= kvmppc_find_vcpu(kvm
, vcpuid
);
324 subfunc
= (flags
>> H_VPA_FUNC_SHIFT
) & H_VPA_FUNC_MASK
;
325 if (subfunc
== H_VPA_REG_VPA
|| subfunc
== H_VPA_REG_DTL
||
326 subfunc
== H_VPA_REG_SLB
) {
327 /* Registering new area - address must be cache-line aligned */
328 if ((vpa
& (L1_CACHE_BYTES
- 1)) || !vpa
)
331 /* convert logical addr to kernel addr and read length */
332 va
= kvmppc_pin_guest_page(kvm
, vpa
, &nb
);
335 if (subfunc
== H_VPA_REG_VPA
)
336 len
= ((struct reg_vpa
*)va
)->length
.hword
;
338 len
= ((struct reg_vpa
*)va
)->length
.word
;
339 kvmppc_unpin_guest_page(kvm
, va
, vpa
, false);
342 if (len
> nb
|| len
< sizeof(struct reg_vpa
))
351 spin_lock(&tvcpu
->arch
.vpa_update_lock
);
354 case H_VPA_REG_VPA
: /* register VPA */
355 if (len
< sizeof(struct lppaca
))
357 vpap
= &tvcpu
->arch
.vpa
;
361 case H_VPA_REG_DTL
: /* register DTL */
362 if (len
< sizeof(struct dtl_entry
))
364 len
-= len
% sizeof(struct dtl_entry
);
366 /* Check that they have previously registered a VPA */
368 if (!vpa_is_registered(&tvcpu
->arch
.vpa
))
371 vpap
= &tvcpu
->arch
.dtl
;
375 case H_VPA_REG_SLB
: /* register SLB shadow buffer */
376 /* Check that they have previously registered a VPA */
378 if (!vpa_is_registered(&tvcpu
->arch
.vpa
))
381 vpap
= &tvcpu
->arch
.slb_shadow
;
385 case H_VPA_DEREG_VPA
: /* deregister VPA */
386 /* Check they don't still have a DTL or SLB buf registered */
388 if (vpa_is_registered(&tvcpu
->arch
.dtl
) ||
389 vpa_is_registered(&tvcpu
->arch
.slb_shadow
))
392 vpap
= &tvcpu
->arch
.vpa
;
396 case H_VPA_DEREG_DTL
: /* deregister DTL */
397 vpap
= &tvcpu
->arch
.dtl
;
401 case H_VPA_DEREG_SLB
: /* deregister SLB shadow buffer */
402 vpap
= &tvcpu
->arch
.slb_shadow
;
408 vpap
->next_gpa
= vpa
;
410 vpap
->update_pending
= 1;
413 spin_unlock(&tvcpu
->arch
.vpa_update_lock
);
418 static void kvmppc_update_vpa(struct kvm_vcpu
*vcpu
, struct kvmppc_vpa
*vpap
)
420 struct kvm
*kvm
= vcpu
->kvm
;
426 * We need to pin the page pointed to by vpap->next_gpa,
427 * but we can't call kvmppc_pin_guest_page under the lock
428 * as it does get_user_pages() and down_read(). So we
429 * have to drop the lock, pin the page, then get the lock
430 * again and check that a new area didn't get registered
434 gpa
= vpap
->next_gpa
;
435 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
439 va
= kvmppc_pin_guest_page(kvm
, gpa
, &nb
);
440 spin_lock(&vcpu
->arch
.vpa_update_lock
);
441 if (gpa
== vpap
->next_gpa
)
443 /* sigh... unpin that one and try again */
445 kvmppc_unpin_guest_page(kvm
, va
, gpa
, false);
448 vpap
->update_pending
= 0;
449 if (va
&& nb
< vpap
->len
) {
451 * If it's now too short, it must be that userspace
452 * has changed the mappings underlying guest memory,
453 * so unregister the region.
455 kvmppc_unpin_guest_page(kvm
, va
, gpa
, false);
458 if (vpap
->pinned_addr
)
459 kvmppc_unpin_guest_page(kvm
, vpap
->pinned_addr
, vpap
->gpa
,
462 vpap
->pinned_addr
= va
;
465 vpap
->pinned_end
= va
+ vpap
->len
;
468 static void kvmppc_update_vpas(struct kvm_vcpu
*vcpu
)
470 if (!(vcpu
->arch
.vpa
.update_pending
||
471 vcpu
->arch
.slb_shadow
.update_pending
||
472 vcpu
->arch
.dtl
.update_pending
))
475 spin_lock(&vcpu
->arch
.vpa_update_lock
);
476 if (vcpu
->arch
.vpa
.update_pending
) {
477 kvmppc_update_vpa(vcpu
, &vcpu
->arch
.vpa
);
478 if (vcpu
->arch
.vpa
.pinned_addr
)
479 init_vpa(vcpu
, vcpu
->arch
.vpa
.pinned_addr
);
481 if (vcpu
->arch
.dtl
.update_pending
) {
482 kvmppc_update_vpa(vcpu
, &vcpu
->arch
.dtl
);
483 vcpu
->arch
.dtl_ptr
= vcpu
->arch
.dtl
.pinned_addr
;
484 vcpu
->arch
.dtl_index
= 0;
486 if (vcpu
->arch
.slb_shadow
.update_pending
)
487 kvmppc_update_vpa(vcpu
, &vcpu
->arch
.slb_shadow
);
488 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
492 * Return the accumulated stolen time for the vcore up until `now'.
493 * The caller should hold the vcore lock.
495 static u64
vcore_stolen_time(struct kvmppc_vcore
*vc
, u64 now
)
500 * If we are the task running the vcore, then since we hold
501 * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb
502 * can't be updated, so we don't need the tbacct_lock.
503 * If the vcore is inactive, it can't become active (since we
504 * hold the vcore lock), so the vcpu load/put functions won't
505 * update stolen_tb/preempt_tb, and we don't need tbacct_lock.
507 if (vc
->vcore_state
!= VCORE_INACTIVE
&&
508 vc
->runner
->arch
.run_task
!= current
) {
509 spin_lock_irq(&vc
->runner
->arch
.tbacct_lock
);
511 if (vc
->preempt_tb
!= TB_NIL
)
512 p
+= now
- vc
->preempt_tb
;
513 spin_unlock_irq(&vc
->runner
->arch
.tbacct_lock
);
520 static void kvmppc_create_dtl_entry(struct kvm_vcpu
*vcpu
,
521 struct kvmppc_vcore
*vc
)
523 struct dtl_entry
*dt
;
525 unsigned long stolen
;
526 unsigned long core_stolen
;
529 dt
= vcpu
->arch
.dtl_ptr
;
530 vpa
= vcpu
->arch
.vpa
.pinned_addr
;
532 core_stolen
= vcore_stolen_time(vc
, now
);
533 stolen
= core_stolen
- vcpu
->arch
.stolen_logged
;
534 vcpu
->arch
.stolen_logged
= core_stolen
;
535 spin_lock_irq(&vcpu
->arch
.tbacct_lock
);
536 stolen
+= vcpu
->arch
.busy_stolen
;
537 vcpu
->arch
.busy_stolen
= 0;
538 spin_unlock_irq(&vcpu
->arch
.tbacct_lock
);
541 memset(dt
, 0, sizeof(struct dtl_entry
));
542 dt
->dispatch_reason
= 7;
543 dt
->processor_id
= vc
->pcpu
+ vcpu
->arch
.ptid
;
544 dt
->timebase
= now
+ vc
->tb_offset
;
545 dt
->enqueue_to_dispatch_time
= stolen
;
546 dt
->srr0
= kvmppc_get_pc(vcpu
);
547 dt
->srr1
= vcpu
->arch
.shregs
.msr
;
549 if (dt
== vcpu
->arch
.dtl
.pinned_end
)
550 dt
= vcpu
->arch
.dtl
.pinned_addr
;
551 vcpu
->arch
.dtl_ptr
= dt
;
552 /* order writing *dt vs. writing vpa->dtl_idx */
554 vpa
->dtl_idx
= ++vcpu
->arch
.dtl_index
;
555 vcpu
->arch
.dtl
.dirty
= true;
558 int kvmppc_pseries_do_hcall(struct kvm_vcpu
*vcpu
)
560 unsigned long req
= kvmppc_get_gpr(vcpu
, 3);
561 unsigned long target
, ret
= H_SUCCESS
;
562 struct kvm_vcpu
*tvcpu
;
567 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
568 ret
= kvmppc_virtmode_h_enter(vcpu
, kvmppc_get_gpr(vcpu
, 4),
569 kvmppc_get_gpr(vcpu
, 5),
570 kvmppc_get_gpr(vcpu
, 6),
571 kvmppc_get_gpr(vcpu
, 7));
572 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
577 target
= kvmppc_get_gpr(vcpu
, 4);
578 tvcpu
= kvmppc_find_vcpu(vcpu
->kvm
, target
);
583 tvcpu
->arch
.prodded
= 1;
585 if (vcpu
->arch
.ceded
) {
586 if (swaitqueue_active(&vcpu
->wq
)) {
587 swait_wake_interruptible(&vcpu
->wq
);
588 vcpu
->stat
.halt_wakeup
++;
593 target
= kvmppc_get_gpr(vcpu
, 4);
596 tvcpu
= kvmppc_find_vcpu(vcpu
->kvm
, target
);
601 kvm_vcpu_yield_to(tvcpu
);
604 ret
= do_h_register_vpa(vcpu
, kvmppc_get_gpr(vcpu
, 4),
605 kvmppc_get_gpr(vcpu
, 5),
606 kvmppc_get_gpr(vcpu
, 6));
609 if (list_empty(&vcpu
->kvm
->arch
.rtas_tokens
))
612 idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
613 rc
= kvmppc_rtas_hcall(vcpu
);
614 srcu_read_unlock(&vcpu
->kvm
->srcu
, idx
);
621 /* Send the error out to userspace via KVM_RUN */
630 if (kvmppc_xics_enabled(vcpu
)) {
631 ret
= kvmppc_xics_hcall(vcpu
, req
);
637 kvmppc_set_gpr(vcpu
, 3, ret
);
638 vcpu
->arch
.hcall_needed
= 0;
642 static int kvmppc_handle_exit_hv(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
643 struct task_struct
*tsk
)
647 vcpu
->stat
.sum_exits
++;
649 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
650 run
->ready_for_interrupt_injection
= 1;
651 switch (vcpu
->arch
.trap
) {
652 /* We're good on these - the host merely wanted to get our attention */
653 case BOOK3S_INTERRUPT_HV_DECREMENTER
:
654 vcpu
->stat
.dec_exits
++;
657 case BOOK3S_INTERRUPT_EXTERNAL
:
658 case BOOK3S_INTERRUPT_H_DOORBELL
:
659 vcpu
->stat
.ext_intr_exits
++;
662 case BOOK3S_INTERRUPT_PERFMON
:
665 case BOOK3S_INTERRUPT_MACHINE_CHECK
:
667 * Deliver a machine check interrupt to the guest.
668 * We have to do this, even if the host has handled the
669 * machine check, because machine checks use SRR0/1 and
670 * the interrupt might have trashed guest state in them.
672 kvmppc_book3s_queue_irqprio(vcpu
,
673 BOOK3S_INTERRUPT_MACHINE_CHECK
);
676 case BOOK3S_INTERRUPT_PROGRAM
:
680 * Normally program interrupts are delivered directly
681 * to the guest by the hardware, but we can get here
682 * as a result of a hypervisor emulation interrupt
683 * (e40) getting turned into a 700 by BML RTAS.
685 flags
= vcpu
->arch
.shregs
.msr
& 0x1f0000ull
;
686 kvmppc_core_queue_program(vcpu
, flags
);
690 case BOOK3S_INTERRUPT_SYSCALL
:
692 /* hcall - punt to userspace */
695 /* hypercall with MSR_PR has already been handled in rmode,
696 * and never reaches here.
699 run
->papr_hcall
.nr
= kvmppc_get_gpr(vcpu
, 3);
700 for (i
= 0; i
< 9; ++i
)
701 run
->papr_hcall
.args
[i
] = kvmppc_get_gpr(vcpu
, 4 + i
);
702 run
->exit_reason
= KVM_EXIT_PAPR_HCALL
;
703 vcpu
->arch
.hcall_needed
= 1;
708 * We get these next two if the guest accesses a page which it thinks
709 * it has mapped but which is not actually present, either because
710 * it is for an emulated I/O device or because the corresonding
711 * host page has been paged out. Any other HDSI/HISI interrupts
712 * have been handled already.
714 case BOOK3S_INTERRUPT_H_DATA_STORAGE
:
715 r
= RESUME_PAGE_FAULT
;
717 case BOOK3S_INTERRUPT_H_INST_STORAGE
:
718 vcpu
->arch
.fault_dar
= kvmppc_get_pc(vcpu
);
719 vcpu
->arch
.fault_dsisr
= 0;
720 r
= RESUME_PAGE_FAULT
;
723 * This occurs if the guest executes an illegal instruction.
724 * We just generate a program interrupt to the guest, since
725 * we don't emulate any guest instructions at this stage.
727 case BOOK3S_INTERRUPT_H_EMUL_ASSIST
:
728 kvmppc_core_queue_program(vcpu
, SRR1_PROGILL
);
732 * This occurs if the guest (kernel or userspace), does something that
733 * is prohibited by HFSCR. We just generate a program interrupt to
736 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL
:
737 kvmppc_core_queue_program(vcpu
, SRR1_PROGILL
);
741 kvmppc_dump_regs(vcpu
);
742 printk(KERN_EMERG
"trap=0x%x | pc=0x%lx | msr=0x%llx\n",
743 vcpu
->arch
.trap
, kvmppc_get_pc(vcpu
),
744 vcpu
->arch
.shregs
.msr
);
745 run
->hw
.hardware_exit_reason
= vcpu
->arch
.trap
;
753 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu
*vcpu
,
754 struct kvm_sregs
*sregs
)
758 memset(sregs
, 0, sizeof(struct kvm_sregs
));
759 sregs
->pvr
= vcpu
->arch
.pvr
;
760 for (i
= 0; i
< vcpu
->arch
.slb_max
; i
++) {
761 sregs
->u
.s
.ppc64
.slb
[i
].slbe
= vcpu
->arch
.slb
[i
].orige
;
762 sregs
->u
.s
.ppc64
.slb
[i
].slbv
= vcpu
->arch
.slb
[i
].origv
;
768 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu
*vcpu
,
769 struct kvm_sregs
*sregs
)
773 kvmppc_set_pvr_hv(vcpu
, sregs
->pvr
);
776 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
777 if (sregs
->u
.s
.ppc64
.slb
[i
].slbe
& SLB_ESID_V
) {
778 vcpu
->arch
.slb
[j
].orige
= sregs
->u
.s
.ppc64
.slb
[i
].slbe
;
779 vcpu
->arch
.slb
[j
].origv
= sregs
->u
.s
.ppc64
.slb
[i
].slbv
;
783 vcpu
->arch
.slb_max
= j
;
788 static void kvmppc_set_lpcr(struct kvm_vcpu
*vcpu
, u64 new_lpcr
)
790 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
793 spin_lock(&vc
->lock
);
795 * If ILE (interrupt little-endian) has changed, update the
796 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
798 if ((new_lpcr
& LPCR_ILE
) != (vc
->lpcr
& LPCR_ILE
)) {
799 struct kvm
*kvm
= vcpu
->kvm
;
800 struct kvm_vcpu
*vcpu
;
803 mutex_lock(&kvm
->lock
);
804 kvm_for_each_vcpu(i
, vcpu
, kvm
) {
805 if (vcpu
->arch
.vcore
!= vc
)
807 if (new_lpcr
& LPCR_ILE
)
808 vcpu
->arch
.intr_msr
|= MSR_LE
;
810 vcpu
->arch
.intr_msr
&= ~MSR_LE
;
812 mutex_unlock(&kvm
->lock
);
816 * Userspace can only modify DPFD (default prefetch depth),
817 * ILE (interrupt little-endian) and TC (translation control).
818 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
820 mask
= LPCR_DPFD
| LPCR_ILE
| LPCR_TC
;
821 if (cpu_has_feature(CPU_FTR_ARCH_207S
))
823 vc
->lpcr
= (vc
->lpcr
& ~mask
) | (new_lpcr
& mask
);
824 spin_unlock(&vc
->lock
);
827 static int kvmppc_get_one_reg_hv(struct kvm_vcpu
*vcpu
, u64 id
,
828 union kvmppc_one_reg
*val
)
834 case KVM_REG_PPC_HIOR
:
835 *val
= get_reg_val(id
, 0);
837 case KVM_REG_PPC_DABR
:
838 *val
= get_reg_val(id
, vcpu
->arch
.dabr
);
840 case KVM_REG_PPC_DABRX
:
841 *val
= get_reg_val(id
, vcpu
->arch
.dabrx
);
843 case KVM_REG_PPC_DSCR
:
844 *val
= get_reg_val(id
, vcpu
->arch
.dscr
);
846 case KVM_REG_PPC_PURR
:
847 *val
= get_reg_val(id
, vcpu
->arch
.purr
);
849 case KVM_REG_PPC_SPURR
:
850 *val
= get_reg_val(id
, vcpu
->arch
.spurr
);
852 case KVM_REG_PPC_AMR
:
853 *val
= get_reg_val(id
, vcpu
->arch
.amr
);
855 case KVM_REG_PPC_UAMOR
:
856 *val
= get_reg_val(id
, vcpu
->arch
.uamor
);
858 case KVM_REG_PPC_MMCR0
... KVM_REG_PPC_MMCRS
:
859 i
= id
- KVM_REG_PPC_MMCR0
;
860 *val
= get_reg_val(id
, vcpu
->arch
.mmcr
[i
]);
862 case KVM_REG_PPC_PMC1
... KVM_REG_PPC_PMC8
:
863 i
= id
- KVM_REG_PPC_PMC1
;
864 *val
= get_reg_val(id
, vcpu
->arch
.pmc
[i
]);
866 case KVM_REG_PPC_SPMC1
... KVM_REG_PPC_SPMC2
:
867 i
= id
- KVM_REG_PPC_SPMC1
;
868 *val
= get_reg_val(id
, vcpu
->arch
.spmc
[i
]);
870 case KVM_REG_PPC_SIAR
:
871 *val
= get_reg_val(id
, vcpu
->arch
.siar
);
873 case KVM_REG_PPC_SDAR
:
874 *val
= get_reg_val(id
, vcpu
->arch
.sdar
);
876 case KVM_REG_PPC_SIER
:
877 *val
= get_reg_val(id
, vcpu
->arch
.sier
);
879 case KVM_REG_PPC_IAMR
:
880 *val
= get_reg_val(id
, vcpu
->arch
.iamr
);
882 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
883 case KVM_REG_PPC_TFHAR
:
884 *val
= get_reg_val(id
, vcpu
->arch
.tfhar
);
886 case KVM_REG_PPC_TFIAR
:
887 *val
= get_reg_val(id
, vcpu
->arch
.tfiar
);
889 case KVM_REG_PPC_TEXASR
:
890 *val
= get_reg_val(id
, vcpu
->arch
.texasr
);
893 case KVM_REG_PPC_FSCR
:
894 *val
= get_reg_val(id
, vcpu
->arch
.fscr
);
896 case KVM_REG_PPC_PSPB
:
897 *val
= get_reg_val(id
, vcpu
->arch
.pspb
);
899 case KVM_REG_PPC_EBBHR
:
900 *val
= get_reg_val(id
, vcpu
->arch
.ebbhr
);
902 case KVM_REG_PPC_EBBRR
:
903 *val
= get_reg_val(id
, vcpu
->arch
.ebbrr
);
905 case KVM_REG_PPC_BESCR
:
906 *val
= get_reg_val(id
, vcpu
->arch
.bescr
);
908 case KVM_REG_PPC_TAR
:
909 *val
= get_reg_val(id
, vcpu
->arch
.tar
);
911 case KVM_REG_PPC_DPDES
:
912 *val
= get_reg_val(id
, vcpu
->arch
.vcore
->dpdes
);
914 case KVM_REG_PPC_DAWR
:
915 *val
= get_reg_val(id
, vcpu
->arch
.dawr
);
917 case KVM_REG_PPC_DAWRX
:
918 *val
= get_reg_val(id
, vcpu
->arch
.dawrx
);
920 case KVM_REG_PPC_CIABR
:
921 *val
= get_reg_val(id
, vcpu
->arch
.ciabr
);
924 *val
= get_reg_val(id
, vcpu
->arch
.ic
);
926 case KVM_REG_PPC_VTB
:
927 *val
= get_reg_val(id
, vcpu
->arch
.vtb
);
929 case KVM_REG_PPC_CSIGR
:
930 *val
= get_reg_val(id
, vcpu
->arch
.csigr
);
932 case KVM_REG_PPC_TACR
:
933 *val
= get_reg_val(id
, vcpu
->arch
.tacr
);
935 case KVM_REG_PPC_TCSCR
:
936 *val
= get_reg_val(id
, vcpu
->arch
.tcscr
);
938 case KVM_REG_PPC_PID
:
939 *val
= get_reg_val(id
, vcpu
->arch
.pid
);
941 case KVM_REG_PPC_ACOP
:
942 *val
= get_reg_val(id
, vcpu
->arch
.acop
);
944 case KVM_REG_PPC_WORT
:
945 *val
= get_reg_val(id
, vcpu
->arch
.wort
);
947 case KVM_REG_PPC_VPA_ADDR
:
948 spin_lock(&vcpu
->arch
.vpa_update_lock
);
949 *val
= get_reg_val(id
, vcpu
->arch
.vpa
.next_gpa
);
950 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
952 case KVM_REG_PPC_VPA_SLB
:
953 spin_lock(&vcpu
->arch
.vpa_update_lock
);
954 val
->vpaval
.addr
= vcpu
->arch
.slb_shadow
.next_gpa
;
955 val
->vpaval
.length
= vcpu
->arch
.slb_shadow
.len
;
956 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
958 case KVM_REG_PPC_VPA_DTL
:
959 spin_lock(&vcpu
->arch
.vpa_update_lock
);
960 val
->vpaval
.addr
= vcpu
->arch
.dtl
.next_gpa
;
961 val
->vpaval
.length
= vcpu
->arch
.dtl
.len
;
962 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
964 case KVM_REG_PPC_TB_OFFSET
:
965 *val
= get_reg_val(id
, vcpu
->arch
.vcore
->tb_offset
);
967 case KVM_REG_PPC_LPCR
:
968 *val
= get_reg_val(id
, vcpu
->arch
.vcore
->lpcr
);
970 case KVM_REG_PPC_PPR
:
971 *val
= get_reg_val(id
, vcpu
->arch
.ppr
);
973 case KVM_REG_PPC_ARCH_COMPAT
:
974 *val
= get_reg_val(id
, vcpu
->arch
.vcore
->arch_compat
);
984 static int kvmppc_set_one_reg_hv(struct kvm_vcpu
*vcpu
, u64 id
,
985 union kvmppc_one_reg
*val
)
989 unsigned long addr
, len
;
992 case KVM_REG_PPC_HIOR
:
993 /* Only allow this to be set to zero */
994 if (set_reg_val(id
, *val
))
997 case KVM_REG_PPC_DABR
:
998 vcpu
->arch
.dabr
= set_reg_val(id
, *val
);
1000 case KVM_REG_PPC_DABRX
:
1001 vcpu
->arch
.dabrx
= set_reg_val(id
, *val
) & ~DABRX_HYP
;
1003 case KVM_REG_PPC_DSCR
:
1004 vcpu
->arch
.dscr
= set_reg_val(id
, *val
);
1006 case KVM_REG_PPC_PURR
:
1007 vcpu
->arch
.purr
= set_reg_val(id
, *val
);
1009 case KVM_REG_PPC_SPURR
:
1010 vcpu
->arch
.spurr
= set_reg_val(id
, *val
);
1012 case KVM_REG_PPC_AMR
:
1013 vcpu
->arch
.amr
= set_reg_val(id
, *val
);
1015 case KVM_REG_PPC_UAMOR
:
1016 vcpu
->arch
.uamor
= set_reg_val(id
, *val
);
1018 case KVM_REG_PPC_MMCR0
... KVM_REG_PPC_MMCRS
:
1019 i
= id
- KVM_REG_PPC_MMCR0
;
1020 vcpu
->arch
.mmcr
[i
] = set_reg_val(id
, *val
);
1022 case KVM_REG_PPC_PMC1
... KVM_REG_PPC_PMC8
:
1023 i
= id
- KVM_REG_PPC_PMC1
;
1024 vcpu
->arch
.pmc
[i
] = set_reg_val(id
, *val
);
1026 case KVM_REG_PPC_SPMC1
... KVM_REG_PPC_SPMC2
:
1027 i
= id
- KVM_REG_PPC_SPMC1
;
1028 vcpu
->arch
.spmc
[i
] = set_reg_val(id
, *val
);
1030 case KVM_REG_PPC_SIAR
:
1031 vcpu
->arch
.siar
= set_reg_val(id
, *val
);
1033 case KVM_REG_PPC_SDAR
:
1034 vcpu
->arch
.sdar
= set_reg_val(id
, *val
);
1036 case KVM_REG_PPC_SIER
:
1037 vcpu
->arch
.sier
= set_reg_val(id
, *val
);
1039 case KVM_REG_PPC_IAMR
:
1040 vcpu
->arch
.iamr
= set_reg_val(id
, *val
);
1042 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1043 case KVM_REG_PPC_TFHAR
:
1044 vcpu
->arch
.tfhar
= set_reg_val(id
, *val
);
1046 case KVM_REG_PPC_TFIAR
:
1047 vcpu
->arch
.tfiar
= set_reg_val(id
, *val
);
1049 case KVM_REG_PPC_TEXASR
:
1050 vcpu
->arch
.texasr
= set_reg_val(id
, *val
);
1053 case KVM_REG_PPC_FSCR
:
1054 vcpu
->arch
.fscr
= set_reg_val(id
, *val
);
1056 case KVM_REG_PPC_PSPB
:
1057 vcpu
->arch
.pspb
= set_reg_val(id
, *val
);
1059 case KVM_REG_PPC_EBBHR
:
1060 vcpu
->arch
.ebbhr
= set_reg_val(id
, *val
);
1062 case KVM_REG_PPC_EBBRR
:
1063 vcpu
->arch
.ebbrr
= set_reg_val(id
, *val
);
1065 case KVM_REG_PPC_BESCR
:
1066 vcpu
->arch
.bescr
= set_reg_val(id
, *val
);
1068 case KVM_REG_PPC_TAR
:
1069 vcpu
->arch
.tar
= set_reg_val(id
, *val
);
1071 case KVM_REG_PPC_DPDES
:
1072 vcpu
->arch
.vcore
->dpdes
= set_reg_val(id
, *val
);
1074 case KVM_REG_PPC_DAWR
:
1075 vcpu
->arch
.dawr
= set_reg_val(id
, *val
);
1077 case KVM_REG_PPC_DAWRX
:
1078 vcpu
->arch
.dawrx
= set_reg_val(id
, *val
) & ~DAWRX_HYP
;
1080 case KVM_REG_PPC_CIABR
:
1081 vcpu
->arch
.ciabr
= set_reg_val(id
, *val
);
1082 /* Don't allow setting breakpoints in hypervisor code */
1083 if ((vcpu
->arch
.ciabr
& CIABR_PRIV
) == CIABR_PRIV_HYPER
)
1084 vcpu
->arch
.ciabr
&= ~CIABR_PRIV
; /* disable */
1086 case KVM_REG_PPC_IC
:
1087 vcpu
->arch
.ic
= set_reg_val(id
, *val
);
1089 case KVM_REG_PPC_VTB
:
1090 vcpu
->arch
.vtb
= set_reg_val(id
, *val
);
1092 case KVM_REG_PPC_CSIGR
:
1093 vcpu
->arch
.csigr
= set_reg_val(id
, *val
);
1095 case KVM_REG_PPC_TACR
:
1096 vcpu
->arch
.tacr
= set_reg_val(id
, *val
);
1098 case KVM_REG_PPC_TCSCR
:
1099 vcpu
->arch
.tcscr
= set_reg_val(id
, *val
);
1101 case KVM_REG_PPC_PID
:
1102 vcpu
->arch
.pid
= set_reg_val(id
, *val
);
1104 case KVM_REG_PPC_ACOP
:
1105 vcpu
->arch
.acop
= set_reg_val(id
, *val
);
1107 case KVM_REG_PPC_WORT
:
1108 vcpu
->arch
.wort
= set_reg_val(id
, *val
);
1110 case KVM_REG_PPC_VPA_ADDR
:
1111 addr
= set_reg_val(id
, *val
);
1113 if (!addr
&& (vcpu
->arch
.slb_shadow
.next_gpa
||
1114 vcpu
->arch
.dtl
.next_gpa
))
1116 r
= set_vpa(vcpu
, &vcpu
->arch
.vpa
, addr
, sizeof(struct lppaca
));
1118 case KVM_REG_PPC_VPA_SLB
:
1119 addr
= val
->vpaval
.addr
;
1120 len
= val
->vpaval
.length
;
1122 if (addr
&& !vcpu
->arch
.vpa
.next_gpa
)
1124 r
= set_vpa(vcpu
, &vcpu
->arch
.slb_shadow
, addr
, len
);
1126 case KVM_REG_PPC_VPA_DTL
:
1127 addr
= val
->vpaval
.addr
;
1128 len
= val
->vpaval
.length
;
1130 if (addr
&& (len
< sizeof(struct dtl_entry
) ||
1131 !vcpu
->arch
.vpa
.next_gpa
))
1133 len
-= len
% sizeof(struct dtl_entry
);
1134 r
= set_vpa(vcpu
, &vcpu
->arch
.dtl
, addr
, len
);
1136 case KVM_REG_PPC_TB_OFFSET
:
1137 /* round up to multiple of 2^24 */
1138 vcpu
->arch
.vcore
->tb_offset
=
1139 ALIGN(set_reg_val(id
, *val
), 1UL << 24);
1141 case KVM_REG_PPC_LPCR
:
1142 kvmppc_set_lpcr(vcpu
, set_reg_val(id
, *val
));
1144 case KVM_REG_PPC_PPR
:
1145 vcpu
->arch
.ppr
= set_reg_val(id
, *val
);
1147 case KVM_REG_PPC_ARCH_COMPAT
:
1148 r
= kvmppc_set_arch_compat(vcpu
, set_reg_val(id
, *val
));
1158 static struct kvm_vcpu
*kvmppc_core_vcpu_create_hv(struct kvm
*kvm
,
1161 struct kvm_vcpu
*vcpu
;
1164 struct kvmppc_vcore
*vcore
;
1166 core
= id
/ threads_per_core
;
1167 if (core
>= KVM_MAX_VCORES
)
1171 vcpu
= kmem_cache_zalloc(kvm_vcpu_cache
, GFP_KERNEL
);
1175 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
1179 vcpu
->arch
.shared
= &vcpu
->arch
.shregs
;
1180 vcpu
->arch
.mmcr
[0] = MMCR0_FC
;
1181 vcpu
->arch
.ctrl
= CTRL_RUNLATCH
;
1182 /* default to host PVR, since we can't spoof it */
1183 kvmppc_set_pvr_hv(vcpu
, mfspr(SPRN_PVR
));
1184 spin_lock_init(&vcpu
->arch
.vpa_update_lock
);
1185 spin_lock_init(&vcpu
->arch
.tbacct_lock
);
1186 vcpu
->arch
.busy_preempt
= TB_NIL
;
1187 vcpu
->arch
.intr_msr
= MSR_SF
| MSR_ME
;
1189 kvmppc_mmu_book3s_hv_init(vcpu
);
1191 vcpu
->arch
.state
= KVMPPC_VCPU_NOTREADY
;
1193 init_waitqueue_head(&vcpu
->arch
.cpu_run
);
1195 mutex_lock(&kvm
->lock
);
1196 vcore
= kvm
->arch
.vcores
[core
];
1198 vcore
= kzalloc(sizeof(struct kvmppc_vcore
), GFP_KERNEL
);
1200 INIT_LIST_HEAD(&vcore
->runnable_threads
);
1201 spin_lock_init(&vcore
->lock
);
1202 init_swait_head(&vcore
->wq
);
1203 vcore
->preempt_tb
= TB_NIL
;
1204 vcore
->lpcr
= kvm
->arch
.lpcr
;
1205 vcore
->first_vcpuid
= core
* threads_per_core
;
1208 kvm
->arch
.vcores
[core
] = vcore
;
1209 kvm
->arch
.online_vcores
++;
1211 mutex_unlock(&kvm
->lock
);
1216 spin_lock(&vcore
->lock
);
1217 ++vcore
->num_threads
;
1218 spin_unlock(&vcore
->lock
);
1219 vcpu
->arch
.vcore
= vcore
;
1220 vcpu
->arch
.ptid
= vcpu
->vcpu_id
- vcore
->first_vcpuid
;
1222 vcpu
->arch
.cpu_type
= KVM_CPU_3S_64
;
1223 kvmppc_sanity_check(vcpu
);
1228 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
1230 return ERR_PTR(err
);
1233 static void unpin_vpa(struct kvm
*kvm
, struct kvmppc_vpa
*vpa
)
1235 if (vpa
->pinned_addr
)
1236 kvmppc_unpin_guest_page(kvm
, vpa
->pinned_addr
, vpa
->gpa
,
1240 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu
*vcpu
)
1242 spin_lock(&vcpu
->arch
.vpa_update_lock
);
1243 unpin_vpa(vcpu
->kvm
, &vcpu
->arch
.dtl
);
1244 unpin_vpa(vcpu
->kvm
, &vcpu
->arch
.slb_shadow
);
1245 unpin_vpa(vcpu
->kvm
, &vcpu
->arch
.vpa
);
1246 spin_unlock(&vcpu
->arch
.vpa_update_lock
);
1247 kvm_vcpu_uninit(vcpu
);
1248 kmem_cache_free(kvm_vcpu_cache
, vcpu
);
1251 static int kvmppc_core_check_requests_hv(struct kvm_vcpu
*vcpu
)
1253 /* Indicate we want to get back into the guest */
1257 static void kvmppc_set_timer(struct kvm_vcpu
*vcpu
)
1259 unsigned long dec_nsec
, now
;
1262 if (now
> vcpu
->arch
.dec_expires
) {
1263 /* decrementer has already gone negative */
1264 kvmppc_core_queue_dec(vcpu
);
1265 kvmppc_core_prepare_to_enter(vcpu
);
1268 dec_nsec
= (vcpu
->arch
.dec_expires
- now
) * NSEC_PER_SEC
1270 hrtimer_start(&vcpu
->arch
.dec_timer
, ktime_set(0, dec_nsec
),
1272 vcpu
->arch
.timer_running
= 1;
1275 static void kvmppc_end_cede(struct kvm_vcpu
*vcpu
)
1277 vcpu
->arch
.ceded
= 0;
1278 if (vcpu
->arch
.timer_running
) {
1279 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
1280 vcpu
->arch
.timer_running
= 0;
1284 extern void __kvmppc_vcore_entry(void);
1286 static void kvmppc_remove_runnable(struct kvmppc_vcore
*vc
,
1287 struct kvm_vcpu
*vcpu
)
1291 if (vcpu
->arch
.state
!= KVMPPC_VCPU_RUNNABLE
)
1293 spin_lock_irq(&vcpu
->arch
.tbacct_lock
);
1295 vcpu
->arch
.busy_stolen
+= vcore_stolen_time(vc
, now
) -
1296 vcpu
->arch
.stolen_logged
;
1297 vcpu
->arch
.busy_preempt
= now
;
1298 vcpu
->arch
.state
= KVMPPC_VCPU_BUSY_IN_HOST
;
1299 spin_unlock_irq(&vcpu
->arch
.tbacct_lock
);
1301 list_del(&vcpu
->arch
.run_list
);
1304 static int kvmppc_grab_hwthread(int cpu
)
1306 struct paca_struct
*tpaca
;
1307 long timeout
= 1000;
1311 /* Ensure the thread won't go into the kernel if it wakes */
1312 tpaca
->kvm_hstate
.hwthread_req
= 1;
1313 tpaca
->kvm_hstate
.kvm_vcpu
= NULL
;
1316 * If the thread is already executing in the kernel (e.g. handling
1317 * a stray interrupt), wait for it to get back to nap mode.
1318 * The smp_mb() is to ensure that our setting of hwthread_req
1319 * is visible before we look at hwthread_state, so if this
1320 * races with the code at system_reset_pSeries and the thread
1321 * misses our setting of hwthread_req, we are sure to see its
1322 * setting of hwthread_state, and vice versa.
1325 while (tpaca
->kvm_hstate
.hwthread_state
== KVM_HWTHREAD_IN_KERNEL
) {
1326 if (--timeout
<= 0) {
1327 pr_err("KVM: couldn't grab cpu %d\n", cpu
);
1335 static void kvmppc_release_hwthread(int cpu
)
1337 struct paca_struct
*tpaca
;
1340 tpaca
->kvm_hstate
.hwthread_req
= 0;
1341 tpaca
->kvm_hstate
.kvm_vcpu
= NULL
;
1344 static void kvmppc_start_thread(struct kvm_vcpu
*vcpu
)
1347 struct paca_struct
*tpaca
;
1348 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
1350 if (vcpu
->arch
.timer_running
) {
1351 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
1352 vcpu
->arch
.timer_running
= 0;
1354 cpu
= vc
->pcpu
+ vcpu
->arch
.ptid
;
1356 tpaca
->kvm_hstate
.kvm_vcpu
= vcpu
;
1357 tpaca
->kvm_hstate
.kvm_vcore
= vc
;
1358 tpaca
->kvm_hstate
.ptid
= vcpu
->arch
.ptid
;
1359 vcpu
->cpu
= vc
->pcpu
;
1361 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
1362 if (cpu
!= smp_processor_id()) {
1364 if (vcpu
->arch
.ptid
)
1370 static void kvmppc_wait_for_nap(struct kvmppc_vcore
*vc
)
1376 while (vc
->nap_count
< vc
->n_woken
) {
1377 if (++i
>= 1000000) {
1378 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1379 vc
->nap_count
, vc
->n_woken
);
1388 * Check that we are on thread 0 and that any other threads in
1389 * this core are off-line. Then grab the threads so they can't
1392 static int on_primary_thread(void)
1394 int cpu
= smp_processor_id();
1395 int thr
= cpu_thread_in_core(cpu
);
1399 while (++thr
< threads_per_core
)
1400 if (cpu_online(cpu
+ thr
))
1403 /* Grab all hw threads so they can't go into the kernel */
1404 for (thr
= 1; thr
< threads_per_core
; ++thr
) {
1405 if (kvmppc_grab_hwthread(cpu
+ thr
)) {
1406 /* Couldn't grab one; let the others go */
1408 kvmppc_release_hwthread(cpu
+ thr
);
1409 } while (--thr
> 0);
1417 * Run a set of guest threads on a physical core.
1418 * Called with vc->lock held.
1420 static void kvmppc_run_core(struct kvmppc_vcore
*vc
)
1422 struct kvm_vcpu
*vcpu
, *vnext
;
1425 int i
, need_vpa_update
;
1427 struct kvm_vcpu
*vcpus_to_update
[threads_per_core
];
1429 /* don't start if any threads have a signal pending */
1430 need_vpa_update
= 0;
1431 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1432 if (signal_pending(vcpu
->arch
.run_task
))
1434 if (vcpu
->arch
.vpa
.update_pending
||
1435 vcpu
->arch
.slb_shadow
.update_pending
||
1436 vcpu
->arch
.dtl
.update_pending
)
1437 vcpus_to_update
[need_vpa_update
++] = vcpu
;
1441 * Initialize *vc, in particular vc->vcore_state, so we can
1442 * drop the vcore lock if necessary.
1446 vc
->entry_exit_count
= 0;
1447 vc
->vcore_state
= VCORE_STARTING
;
1449 vc
->napping_threads
= 0;
1452 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1453 * which can't be called with any spinlocks held.
1455 if (need_vpa_update
) {
1456 spin_unlock(&vc
->lock
);
1457 for (i
= 0; i
< need_vpa_update
; ++i
)
1458 kvmppc_update_vpas(vcpus_to_update
[i
]);
1459 spin_lock(&vc
->lock
);
1463 * Make sure we are running on thread 0, and that
1464 * secondary threads are offline.
1466 if (threads_per_core
> 1 && !on_primary_thread()) {
1467 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
)
1468 vcpu
->arch
.ret
= -EBUSY
;
1472 vc
->pcpu
= smp_processor_id();
1473 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1474 kvmppc_start_thread(vcpu
);
1475 kvmppc_create_dtl_entry(vcpu
, vc
);
1478 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1479 get_paca()->kvm_hstate
.kvm_vcore
= vc
;
1480 get_paca()->kvm_hstate
.ptid
= 0;
1482 vc
->vcore_state
= VCORE_RUNNING
;
1484 spin_unlock(&vc
->lock
);
1488 srcu_idx
= srcu_read_lock(&vc
->kvm
->srcu
);
1490 __kvmppc_vcore_entry();
1492 spin_lock(&vc
->lock
);
1493 /* disable sending of IPIs on virtual external irqs */
1494 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
)
1496 /* wait for secondary threads to finish writing their state to memory */
1497 if (vc
->nap_count
< vc
->n_woken
)
1498 kvmppc_wait_for_nap(vc
);
1499 for (i
= 0; i
< threads_per_core
; ++i
)
1500 kvmppc_release_hwthread(vc
->pcpu
+ i
);
1501 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1502 vc
->vcore_state
= VCORE_EXITING
;
1503 spin_unlock(&vc
->lock
);
1505 srcu_read_unlock(&vc
->kvm
->srcu
, srcu_idx
);
1507 /* make sure updates to secondary vcpu structs are visible now */
1514 spin_lock(&vc
->lock
);
1516 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1517 /* cancel pending dec exception if dec is positive */
1518 if (now
< vcpu
->arch
.dec_expires
&&
1519 kvmppc_core_pending_dec(vcpu
))
1520 kvmppc_core_dequeue_dec(vcpu
);
1523 if (vcpu
->arch
.trap
)
1524 ret
= kvmppc_handle_exit_hv(vcpu
->arch
.kvm_run
, vcpu
,
1525 vcpu
->arch
.run_task
);
1527 vcpu
->arch
.ret
= ret
;
1528 vcpu
->arch
.trap
= 0;
1530 if (vcpu
->arch
.ceded
) {
1531 if (ret
!= RESUME_GUEST
)
1532 kvmppc_end_cede(vcpu
);
1534 kvmppc_set_timer(vcpu
);
1539 vc
->vcore_state
= VCORE_INACTIVE
;
1540 list_for_each_entry_safe(vcpu
, vnext
, &vc
->runnable_threads
,
1542 if (vcpu
->arch
.ret
!= RESUME_GUEST
) {
1543 kvmppc_remove_runnable(vc
, vcpu
);
1544 wake_up(&vcpu
->arch
.cpu_run
);
1550 * Wait for some other vcpu thread to execute us, and
1551 * wake us up when we need to handle something in the host.
1553 static void kvmppc_wait_for_exec(struct kvm_vcpu
*vcpu
, int wait_state
)
1557 prepare_to_wait(&vcpu
->arch
.cpu_run
, &wait
, wait_state
);
1558 if (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
)
1560 finish_wait(&vcpu
->arch
.cpu_run
, &wait
);
1564 * All the vcpus in this vcore are idle, so wait for a decrementer
1565 * or external interrupt to one of the vcpus. vc->lock is held.
1567 static void kvmppc_vcore_blocked(struct kvmppc_vcore
*vc
)
1569 DEFINE_SWAITER(wait
);
1571 swait_prepare(&vc
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1572 vc
->vcore_state
= VCORE_SLEEPING
;
1573 spin_unlock(&vc
->lock
);
1575 swait_finish(&vc
->wq
, &wait
);
1576 spin_lock(&vc
->lock
);
1577 vc
->vcore_state
= VCORE_INACTIVE
;
1580 static int kvmppc_run_vcpu(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
1583 struct kvmppc_vcore
*vc
;
1584 struct kvm_vcpu
*v
, *vn
;
1586 kvm_run
->exit_reason
= 0;
1587 vcpu
->arch
.ret
= RESUME_GUEST
;
1588 vcpu
->arch
.trap
= 0;
1589 kvmppc_update_vpas(vcpu
);
1592 * Synchronize with other threads in this virtual core
1594 vc
= vcpu
->arch
.vcore
;
1595 spin_lock(&vc
->lock
);
1596 vcpu
->arch
.ceded
= 0;
1597 vcpu
->arch
.run_task
= current
;
1598 vcpu
->arch
.kvm_run
= kvm_run
;
1599 vcpu
->arch
.stolen_logged
= vcore_stolen_time(vc
, mftb());
1600 vcpu
->arch
.state
= KVMPPC_VCPU_RUNNABLE
;
1601 vcpu
->arch
.busy_preempt
= TB_NIL
;
1602 list_add_tail(&vcpu
->arch
.run_list
, &vc
->runnable_threads
);
1606 * This happens the first time this is called for a vcpu.
1607 * If the vcore is already running, we may be able to start
1608 * this thread straight away and have it join in.
1610 if (!signal_pending(current
)) {
1611 if (vc
->vcore_state
== VCORE_RUNNING
&&
1612 VCORE_EXIT_COUNT(vc
) == 0) {
1613 kvmppc_create_dtl_entry(vcpu
, vc
);
1614 kvmppc_start_thread(vcpu
);
1615 } else if (vc
->vcore_state
== VCORE_SLEEPING
) {
1616 swait_wake(&vc
->wq
);
1621 while (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
&&
1622 !signal_pending(current
)) {
1623 if (vc
->vcore_state
!= VCORE_INACTIVE
) {
1624 spin_unlock(&vc
->lock
);
1625 kvmppc_wait_for_exec(vcpu
, TASK_INTERRUPTIBLE
);
1626 spin_lock(&vc
->lock
);
1629 list_for_each_entry_safe(v
, vn
, &vc
->runnable_threads
,
1631 kvmppc_core_prepare_to_enter(v
);
1632 if (signal_pending(v
->arch
.run_task
)) {
1633 kvmppc_remove_runnable(vc
, v
);
1634 v
->stat
.signal_exits
++;
1635 v
->arch
.kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1636 v
->arch
.ret
= -EINTR
;
1637 wake_up(&v
->arch
.cpu_run
);
1640 if (!vc
->n_runnable
|| vcpu
->arch
.state
!= KVMPPC_VCPU_RUNNABLE
)
1644 list_for_each_entry(v
, &vc
->runnable_threads
, arch
.run_list
) {
1645 if (!v
->arch
.pending_exceptions
)
1646 n_ceded
+= v
->arch
.ceded
;
1650 if (n_ceded
== vc
->n_runnable
)
1651 kvmppc_vcore_blocked(vc
);
1653 kvmppc_run_core(vc
);
1657 while (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
&&
1658 (vc
->vcore_state
== VCORE_RUNNING
||
1659 vc
->vcore_state
== VCORE_EXITING
)) {
1660 spin_unlock(&vc
->lock
);
1661 kvmppc_wait_for_exec(vcpu
, TASK_UNINTERRUPTIBLE
);
1662 spin_lock(&vc
->lock
);
1665 if (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
) {
1666 kvmppc_remove_runnable(vc
, vcpu
);
1667 vcpu
->stat
.signal_exits
++;
1668 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1669 vcpu
->arch
.ret
= -EINTR
;
1672 if (vc
->n_runnable
&& vc
->vcore_state
== VCORE_INACTIVE
) {
1673 /* Wake up some vcpu to run the core */
1674 v
= list_first_entry(&vc
->runnable_threads
,
1675 struct kvm_vcpu
, arch
.run_list
);
1676 wake_up(&v
->arch
.cpu_run
);
1679 spin_unlock(&vc
->lock
);
1680 return vcpu
->arch
.ret
;
1683 static int kvmppc_vcpu_run_hv(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
1688 if (!vcpu
->arch
.sane
) {
1689 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1693 kvmppc_core_prepare_to_enter(vcpu
);
1695 /* No need to go into the guest when all we'll do is come back out */
1696 if (signal_pending(current
)) {
1697 run
->exit_reason
= KVM_EXIT_INTR
;
1701 atomic_inc(&vcpu
->kvm
->arch
.vcpus_running
);
1702 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1705 /* On the first time here, set up HTAB and VRMA or RMA */
1706 if (!vcpu
->kvm
->arch
.rma_setup_done
) {
1707 r
= kvmppc_hv_setup_htab_rma(vcpu
);
1712 flush_fp_to_thread(current
);
1713 flush_altivec_to_thread(current
);
1714 flush_vsx_to_thread(current
);
1715 vcpu
->arch
.wqp
= &vcpu
->arch
.vcore
->wq
;
1716 vcpu
->arch
.pgdir
= current
->mm
->pgd
;
1717 vcpu
->arch
.state
= KVMPPC_VCPU_BUSY_IN_HOST
;
1720 r
= kvmppc_run_vcpu(run
, vcpu
);
1722 if (run
->exit_reason
== KVM_EXIT_PAPR_HCALL
&&
1723 !(vcpu
->arch
.shregs
.msr
& MSR_PR
)) {
1724 r
= kvmppc_pseries_do_hcall(vcpu
);
1725 kvmppc_core_prepare_to_enter(vcpu
);
1726 } else if (r
== RESUME_PAGE_FAULT
) {
1727 srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1728 r
= kvmppc_book3s_hv_page_fault(run
, vcpu
,
1729 vcpu
->arch
.fault_dar
, vcpu
->arch
.fault_dsisr
);
1730 srcu_read_unlock(&vcpu
->kvm
->srcu
, srcu_idx
);
1732 } while (r
== RESUME_GUEST
);
1735 vcpu
->arch
.state
= KVMPPC_VCPU_NOTREADY
;
1736 atomic_dec(&vcpu
->kvm
->arch
.vcpus_running
);
1741 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1742 Assumes POWER7 or PPC970. */
1743 static inline int lpcr_rmls(unsigned long rma_size
)
1746 case 32ul << 20: /* 32 MB */
1747 if (cpu_has_feature(CPU_FTR_ARCH_206
))
1748 return 8; /* only supported on POWER7 */
1750 case 64ul << 20: /* 64 MB */
1752 case 128ul << 20: /* 128 MB */
1754 case 256ul << 20: /* 256 MB */
1756 case 1ul << 30: /* 1 GB */
1758 case 16ul << 30: /* 16 GB */
1760 case 256ul << 30: /* 256 GB */
1767 static int kvm_rma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1770 struct kvm_rma_info
*ri
= vma
->vm_file
->private_data
;
1772 if (vmf
->pgoff
>= kvm_rma_pages
)
1773 return VM_FAULT_SIGBUS
;
1775 page
= pfn_to_page(ri
->base_pfn
+ vmf
->pgoff
);
1781 static const struct vm_operations_struct kvm_rma_vm_ops
= {
1782 .fault
= kvm_rma_fault
,
1785 static int kvm_rma_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1787 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
1788 vma
->vm_ops
= &kvm_rma_vm_ops
;
1792 static int kvm_rma_release(struct inode
*inode
, struct file
*filp
)
1794 struct kvm_rma_info
*ri
= filp
->private_data
;
1796 kvm_release_rma(ri
);
1800 static const struct file_operations kvm_rma_fops
= {
1801 .mmap
= kvm_rma_mmap
,
1802 .release
= kvm_rma_release
,
1805 static long kvm_vm_ioctl_allocate_rma(struct kvm
*kvm
,
1806 struct kvm_allocate_rma
*ret
)
1809 struct kvm_rma_info
*ri
;
1811 * Only do this on PPC970 in HV mode
1813 if (!cpu_has_feature(CPU_FTR_HVMODE
) ||
1814 !cpu_has_feature(CPU_FTR_ARCH_201
))
1820 ri
= kvm_alloc_rma();
1824 fd
= anon_inode_getfd("kvm-rma", &kvm_rma_fops
, ri
, O_RDWR
| O_CLOEXEC
);
1826 kvm_release_rma(ri
);
1828 ret
->rma_size
= kvm_rma_pages
<< PAGE_SHIFT
;
1832 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size
**sps
,
1835 struct mmu_psize_def
*def
= &mmu_psize_defs
[linux_psize
];
1839 (*sps
)->page_shift
= def
->shift
;
1840 (*sps
)->slb_enc
= def
->sllp
;
1841 (*sps
)->enc
[0].page_shift
= def
->shift
;
1843 * Only return base page encoding. We don't want to return
1844 * all the supporting pte_enc, because our H_ENTER doesn't
1845 * support MPSS yet. Once they do, we can start passing all
1846 * support pte_enc here
1848 (*sps
)->enc
[0].pte_enc
= def
->penc
[linux_psize
];
1852 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm
*kvm
,
1853 struct kvm_ppc_smmu_info
*info
)
1855 struct kvm_ppc_one_seg_page_size
*sps
;
1857 info
->flags
= KVM_PPC_PAGE_SIZES_REAL
;
1858 if (mmu_has_feature(MMU_FTR_1T_SEGMENT
))
1859 info
->flags
|= KVM_PPC_1T_SEGMENTS
;
1860 info
->slb_size
= mmu_slb_size
;
1862 /* We only support these sizes for now, and no muti-size segments */
1863 sps
= &info
->sps
[0];
1864 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_4K
);
1865 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_64K
);
1866 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_16M
);
1872 * Get (and clear) the dirty memory log for a memory slot.
1874 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm
*kvm
,
1875 struct kvm_dirty_log
*log
)
1877 struct kvm_memory_slot
*memslot
;
1881 mutex_lock(&kvm
->slots_lock
);
1884 if (log
->slot
>= KVM_USER_MEM_SLOTS
)
1887 memslot
= id_to_memslot(kvm
->memslots
, log
->slot
);
1889 if (!memslot
->dirty_bitmap
)
1892 n
= kvm_dirty_bitmap_bytes(memslot
);
1893 memset(memslot
->dirty_bitmap
, 0, n
);
1895 r
= kvmppc_hv_get_dirty_log(kvm
, memslot
, memslot
->dirty_bitmap
);
1900 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1905 mutex_unlock(&kvm
->slots_lock
);
1909 static void unpin_slot(struct kvm_memory_slot
*memslot
)
1911 unsigned long *physp
;
1912 unsigned long j
, npages
, pfn
;
1915 physp
= memslot
->arch
.slot_phys
;
1916 npages
= memslot
->npages
;
1919 for (j
= 0; j
< npages
; j
++) {
1920 if (!(physp
[j
] & KVMPPC_GOT_PAGE
))
1922 pfn
= physp
[j
] >> PAGE_SHIFT
;
1923 page
= pfn_to_page(pfn
);
1929 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot
*free
,
1930 struct kvm_memory_slot
*dont
)
1932 if (!dont
|| free
->arch
.rmap
!= dont
->arch
.rmap
) {
1933 vfree(free
->arch
.rmap
);
1934 free
->arch
.rmap
= NULL
;
1936 if (!dont
|| free
->arch
.slot_phys
!= dont
->arch
.slot_phys
) {
1938 vfree(free
->arch
.slot_phys
);
1939 free
->arch
.slot_phys
= NULL
;
1943 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot
*slot
,
1944 unsigned long npages
)
1946 slot
->arch
.rmap
= vzalloc(npages
* sizeof(*slot
->arch
.rmap
));
1947 if (!slot
->arch
.rmap
)
1949 slot
->arch
.slot_phys
= NULL
;
1954 static int kvmppc_core_prepare_memory_region_hv(struct kvm
*kvm
,
1955 struct kvm_memory_slot
*memslot
,
1956 struct kvm_userspace_memory_region
*mem
)
1958 unsigned long *phys
;
1960 /* Allocate a slot_phys array if needed */
1961 phys
= memslot
->arch
.slot_phys
;
1962 if (!kvm
->arch
.using_mmu_notifiers
&& !phys
&& memslot
->npages
) {
1963 phys
= vzalloc(memslot
->npages
* sizeof(unsigned long));
1966 memslot
->arch
.slot_phys
= phys
;
1972 static void kvmppc_core_commit_memory_region_hv(struct kvm
*kvm
,
1973 struct kvm_userspace_memory_region
*mem
,
1974 const struct kvm_memory_slot
*old
)
1976 unsigned long npages
= mem
->memory_size
>> PAGE_SHIFT
;
1977 struct kvm_memory_slot
*memslot
;
1979 if (npages
&& old
->npages
) {
1981 * If modifying a memslot, reset all the rmap dirty bits.
1982 * If this is a new memslot, we don't need to do anything
1983 * since the rmap array starts out as all zeroes,
1984 * i.e. no pages are dirty.
1986 memslot
= id_to_memslot(kvm
->memslots
, mem
->slot
);
1987 kvmppc_hv_get_dirty_log(kvm
, memslot
, NULL
);
1992 * Update LPCR values in kvm->arch and in vcores.
1993 * Caller must hold kvm->lock.
1995 void kvmppc_update_lpcr(struct kvm
*kvm
, unsigned long lpcr
, unsigned long mask
)
2000 if ((kvm
->arch
.lpcr
& mask
) == lpcr
)
2003 kvm
->arch
.lpcr
= (kvm
->arch
.lpcr
& ~mask
) | lpcr
;
2005 for (i
= 0; i
< KVM_MAX_VCORES
; ++i
) {
2006 struct kvmppc_vcore
*vc
= kvm
->arch
.vcores
[i
];
2009 spin_lock(&vc
->lock
);
2010 vc
->lpcr
= (vc
->lpcr
& ~mask
) | lpcr
;
2011 spin_unlock(&vc
->lock
);
2012 if (++cores_done
>= kvm
->arch
.online_vcores
)
2017 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu
*vcpu
)
2022 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu
*vcpu
)
2025 struct kvm
*kvm
= vcpu
->kvm
;
2026 struct kvm_rma_info
*ri
= NULL
;
2028 struct kvm_memory_slot
*memslot
;
2029 struct vm_area_struct
*vma
;
2030 unsigned long lpcr
= 0, senc
;
2031 unsigned long lpcr_mask
= 0;
2032 unsigned long psize
, porder
;
2033 unsigned long rma_size
;
2035 unsigned long *physp
;
2036 unsigned long i
, npages
;
2039 mutex_lock(&kvm
->lock
);
2040 if (kvm
->arch
.rma_setup_done
)
2041 goto out
; /* another vcpu beat us to it */
2043 /* Allocate hashed page table (if not done already) and reset it */
2044 if (!kvm
->arch
.hpt_virt
) {
2045 err
= kvmppc_alloc_hpt(kvm
, NULL
);
2047 pr_err("KVM: Couldn't alloc HPT\n");
2052 /* Look up the memslot for guest physical address 0 */
2053 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
2054 memslot
= gfn_to_memslot(kvm
, 0);
2056 /* We must have some memory at 0 by now */
2058 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
2061 /* Look up the VMA for the start of this memory slot */
2062 hva
= memslot
->userspace_addr
;
2063 down_read(¤t
->mm
->mmap_sem
);
2064 vma
= find_vma(current
->mm
, hva
);
2065 if (!vma
|| vma
->vm_start
> hva
|| (vma
->vm_flags
& VM_IO
))
2068 psize
= vma_kernel_pagesize(vma
);
2069 porder
= __ilog2(psize
);
2071 /* Is this one of our preallocated RMAs? */
2072 if (vma
->vm_file
&& vma
->vm_file
->f_op
== &kvm_rma_fops
&&
2073 hva
== vma
->vm_start
)
2074 ri
= vma
->vm_file
->private_data
;
2076 up_read(¤t
->mm
->mmap_sem
);
2079 /* On POWER7, use VRMA; on PPC970, give up */
2081 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
2082 pr_err("KVM: CPU requires an RMO\n");
2086 /* We can handle 4k, 64k or 16M pages in the VRMA */
2088 if (!(psize
== 0x1000 || psize
== 0x10000 ||
2089 psize
== 0x1000000))
2092 /* Update VRMASD field in the LPCR */
2093 senc
= slb_pgsize_encoding(psize
);
2094 kvm
->arch
.vrma_slb_v
= senc
| SLB_VSID_B_1T
|
2095 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
2096 lpcr_mask
= LPCR_VRMASD
;
2097 /* the -4 is to account for senc values starting at 0x10 */
2098 lpcr
= senc
<< (LPCR_VRMASD_SH
- 4);
2100 /* Create HPTEs in the hash page table for the VRMA */
2101 kvmppc_map_vrma(vcpu
, memslot
, porder
);
2104 /* Set up to use an RMO region */
2105 rma_size
= kvm_rma_pages
;
2106 if (rma_size
> memslot
->npages
)
2107 rma_size
= memslot
->npages
;
2108 rma_size
<<= PAGE_SHIFT
;
2109 rmls
= lpcr_rmls(rma_size
);
2111 if ((long)rmls
< 0) {
2112 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size
);
2115 atomic_inc(&ri
->use_count
);
2118 /* Update LPCR and RMOR */
2119 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
2120 /* PPC970; insert RMLS value (split field) in HID4 */
2121 lpcr_mask
= (1ul << HID4_RMLS0_SH
) |
2122 (3ul << HID4_RMLS2_SH
) | HID4_RMOR
;
2123 lpcr
= ((rmls
>> 2) << HID4_RMLS0_SH
) |
2124 ((rmls
& 3) << HID4_RMLS2_SH
);
2125 /* RMOR is also in HID4 */
2126 lpcr
|= ((ri
->base_pfn
>> (26 - PAGE_SHIFT
)) & 0xffff)
2130 lpcr_mask
= LPCR_VPM0
| LPCR_VRMA_L
| LPCR_RMLS
;
2131 lpcr
= rmls
<< LPCR_RMLS_SH
;
2132 kvm
->arch
.rmor
= ri
->base_pfn
<< PAGE_SHIFT
;
2134 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2135 ri
->base_pfn
<< PAGE_SHIFT
, rma_size
, lpcr
);
2137 /* Initialize phys addrs of pages in RMO */
2138 npages
= kvm_rma_pages
;
2139 porder
= __ilog2(npages
);
2140 physp
= memslot
->arch
.slot_phys
;
2142 if (npages
> memslot
->npages
)
2143 npages
= memslot
->npages
;
2144 spin_lock(&kvm
->arch
.slot_phys_lock
);
2145 for (i
= 0; i
< npages
; ++i
)
2146 physp
[i
] = ((ri
->base_pfn
+ i
) << PAGE_SHIFT
) +
2148 spin_unlock(&kvm
->arch
.slot_phys_lock
);
2152 kvmppc_update_lpcr(kvm
, lpcr
, lpcr_mask
);
2154 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2156 kvm
->arch
.rma_setup_done
= 1;
2159 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
2161 mutex_unlock(&kvm
->lock
);
2165 up_read(¤t
->mm
->mmap_sem
);
2169 static int kvmppc_core_init_vm_hv(struct kvm
*kvm
)
2171 unsigned long lpcr
, lpid
;
2173 /* Allocate the guest's logical partition ID */
2175 lpid
= kvmppc_alloc_lpid();
2178 kvm
->arch
.lpid
= lpid
;
2181 * Since we don't flush the TLB when tearing down a VM,
2182 * and this lpid might have previously been used,
2183 * make sure we flush on each core before running the new VM.
2185 cpumask_setall(&kvm
->arch
.need_tlb_flush
);
2187 kvm
->arch
.rma
= NULL
;
2189 kvm
->arch
.host_sdr1
= mfspr(SPRN_SDR1
);
2191 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
2192 /* PPC970; HID4 is effectively the LPCR */
2193 kvm
->arch
.host_lpid
= 0;
2194 kvm
->arch
.host_lpcr
= lpcr
= mfspr(SPRN_HID4
);
2195 lpcr
&= ~((3 << HID4_LPID1_SH
) | (0xful
<< HID4_LPID5_SH
));
2196 lpcr
|= ((lpid
>> 4) << HID4_LPID1_SH
) |
2197 ((lpid
& 0xf) << HID4_LPID5_SH
);
2199 /* POWER7; init LPCR for virtual RMA mode */
2200 kvm
->arch
.host_lpid
= mfspr(SPRN_LPID
);
2201 kvm
->arch
.host_lpcr
= lpcr
= mfspr(SPRN_LPCR
);
2202 lpcr
&= LPCR_PECE
| LPCR_LPES
;
2203 lpcr
|= (4UL << LPCR_DPFD_SH
) | LPCR_HDICE
|
2204 LPCR_VPM0
| LPCR_VPM1
;
2205 kvm
->arch
.vrma_slb_v
= SLB_VSID_B_1T
|
2206 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
2207 /* On POWER8 turn on online bit to enable PURR/SPURR */
2208 if (cpu_has_feature(CPU_FTR_ARCH_207S
))
2211 kvm
->arch
.lpcr
= lpcr
;
2213 kvm
->arch
.using_mmu_notifiers
= !!cpu_has_feature(CPU_FTR_ARCH_206
);
2214 spin_lock_init(&kvm
->arch
.slot_phys_lock
);
2217 * Don't allow secondary CPU threads to come online
2218 * while any KVM VMs exist.
2220 inhibit_secondary_onlining();
2225 static void kvmppc_free_vcores(struct kvm
*kvm
)
2229 for (i
= 0; i
< KVM_MAX_VCORES
; ++i
)
2230 kfree(kvm
->arch
.vcores
[i
]);
2231 kvm
->arch
.online_vcores
= 0;
2234 static void kvmppc_core_destroy_vm_hv(struct kvm
*kvm
)
2236 uninhibit_secondary_onlining();
2238 kvmppc_free_vcores(kvm
);
2239 if (kvm
->arch
.rma
) {
2240 kvm_release_rma(kvm
->arch
.rma
);
2241 kvm
->arch
.rma
= NULL
;
2244 kvmppc_free_hpt(kvm
);
2247 /* We don't need to emulate any privileged instructions or dcbz */
2248 static int kvmppc_core_emulate_op_hv(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
2249 unsigned int inst
, int *advance
)
2251 return EMULATE_FAIL
;
2254 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu
*vcpu
, int sprn
,
2257 return EMULATE_FAIL
;
2260 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu
*vcpu
, int sprn
,
2263 return EMULATE_FAIL
;
2266 static int kvmppc_core_check_processor_compat_hv(void)
2268 if (!cpu_has_feature(CPU_FTR_HVMODE
))
2273 static long kvm_arch_vm_ioctl_hv(struct file
*filp
,
2274 unsigned int ioctl
, unsigned long arg
)
2276 struct kvm
*kvm __maybe_unused
= filp
->private_data
;
2277 void __user
*argp
= (void __user
*)arg
;
2282 case KVM_ALLOCATE_RMA
: {
2283 struct kvm_allocate_rma rma
;
2284 struct kvm
*kvm
= filp
->private_data
;
2286 r
= kvm_vm_ioctl_allocate_rma(kvm
, &rma
);
2287 if (r
>= 0 && copy_to_user(argp
, &rma
, sizeof(rma
)))
2292 case KVM_PPC_ALLOCATE_HTAB
: {
2296 if (get_user(htab_order
, (u32 __user
*)argp
))
2298 r
= kvmppc_alloc_reset_hpt(kvm
, &htab_order
);
2302 if (put_user(htab_order
, (u32 __user
*)argp
))
2308 case KVM_PPC_GET_HTAB_FD
: {
2309 struct kvm_get_htab_fd ghf
;
2312 if (copy_from_user(&ghf
, argp
, sizeof(ghf
)))
2314 r
= kvm_vm_ioctl_get_htab_fd(kvm
, &ghf
);
2325 static struct kvmppc_ops kvm_ops_hv
= {
2326 .get_sregs
= kvm_arch_vcpu_ioctl_get_sregs_hv
,
2327 .set_sregs
= kvm_arch_vcpu_ioctl_set_sregs_hv
,
2328 .get_one_reg
= kvmppc_get_one_reg_hv
,
2329 .set_one_reg
= kvmppc_set_one_reg_hv
,
2330 .vcpu_load
= kvmppc_core_vcpu_load_hv
,
2331 .vcpu_put
= kvmppc_core_vcpu_put_hv
,
2332 .set_msr
= kvmppc_set_msr_hv
,
2333 .vcpu_run
= kvmppc_vcpu_run_hv
,
2334 .vcpu_create
= kvmppc_core_vcpu_create_hv
,
2335 .vcpu_free
= kvmppc_core_vcpu_free_hv
,
2336 .check_requests
= kvmppc_core_check_requests_hv
,
2337 .get_dirty_log
= kvm_vm_ioctl_get_dirty_log_hv
,
2338 .flush_memslot
= kvmppc_core_flush_memslot_hv
,
2339 .prepare_memory_region
= kvmppc_core_prepare_memory_region_hv
,
2340 .commit_memory_region
= kvmppc_core_commit_memory_region_hv
,
2341 .unmap_hva
= kvm_unmap_hva_hv
,
2342 .unmap_hva_range
= kvm_unmap_hva_range_hv
,
2343 .age_hva
= kvm_age_hva_hv
,
2344 .test_age_hva
= kvm_test_age_hva_hv
,
2345 .set_spte_hva
= kvm_set_spte_hva_hv
,
2346 .mmu_destroy
= kvmppc_mmu_destroy_hv
,
2347 .free_memslot
= kvmppc_core_free_memslot_hv
,
2348 .create_memslot
= kvmppc_core_create_memslot_hv
,
2349 .init_vm
= kvmppc_core_init_vm_hv
,
2350 .destroy_vm
= kvmppc_core_destroy_vm_hv
,
2351 .get_smmu_info
= kvm_vm_ioctl_get_smmu_info_hv
,
2352 .emulate_op
= kvmppc_core_emulate_op_hv
,
2353 .emulate_mtspr
= kvmppc_core_emulate_mtspr_hv
,
2354 .emulate_mfspr
= kvmppc_core_emulate_mfspr_hv
,
2355 .fast_vcpu_kick
= kvmppc_fast_vcpu_kick_hv
,
2356 .arch_vm_ioctl
= kvm_arch_vm_ioctl_hv
,
2359 static int kvmppc_book3s_init_hv(void)
2363 * FIXME!! Do we need to check on all cpus ?
2365 r
= kvmppc_core_check_processor_compat_hv();
2369 kvm_ops_hv
.owner
= THIS_MODULE
;
2370 kvmppc_hv_ops
= &kvm_ops_hv
;
2372 r
= kvmppc_mmu_hv_init();
2376 static void kvmppc_book3s_exit_hv(void)
2378 kvmppc_hv_ops
= NULL
;
2381 module_init(kvmppc_book3s_init_hv
);
2382 module_exit(kvmppc_book3s_exit_hv
);
2383 MODULE_LICENSE("GPL");
2384 MODULE_ALIAS_MISCDEV(KVM_MINOR
);
2385 MODULE_ALIAS("devname:kvm");