2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
21 #include <linux/kvm_host.h>
22 #include <linux/err.h>
23 #include <linux/slab.h>
24 #include <linux/preempt.h>
25 #include <linux/sched.h>
26 #include <linux/delay.h>
27 #include <linux/export.h>
29 #include <linux/anon_inodes.h>
30 #include <linux/cpumask.h>
31 #include <linux/spinlock.h>
32 #include <linux/page-flags.h>
33 #include <linux/srcu.h>
34 #include <linux/miscdevice.h>
37 #include <asm/cputable.h>
38 #include <asm/cacheflush.h>
39 #include <asm/tlbflush.h>
40 #include <asm/uaccess.h>
42 #include <asm/kvm_ppc.h>
43 #include <asm/kvm_book3s.h>
44 #include <asm/mmu_context.h>
45 #include <asm/lppaca.h>
46 #include <asm/processor.h>
47 #include <asm/cputhreads.h>
49 #include <asm/hvcall.h>
50 #include <asm/switch_to.h>
52 #include <linux/gfp.h>
53 #include <linux/vmalloc.h>
54 #include <linux/highmem.h>
55 #include <linux/hugetlb.h>
56 #include <linux/module.h>
60 /* #define EXIT_DEBUG */
61 /* #define EXIT_DEBUG_SIMPLE */
62 /* #define EXIT_DEBUG_INT */
64 /* Used to indicate that a guest page fault needs to be handled */
65 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
67 /* Used as a "null" value for timebase values */
68 #define TB_NIL (~(u64)0)
70 static void kvmppc_end_cede(struct kvm_vcpu
*vcpu
);
71 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu
*vcpu
);
73 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu
*vcpu
)
77 wait_queue_head_t
*wqp
;
79 wqp
= kvm_arch_vcpu_wq(vcpu
);
80 if (waitqueue_active(wqp
)) {
81 wake_up_interruptible(wqp
);
82 ++vcpu
->stat
.halt_wakeup
;
87 /* CPU points to the first thread of the core */
88 if (cpu
!= me
&& cpu
>= 0 && cpu
< nr_cpu_ids
) {
89 #ifdef CONFIG_KVM_XICS
90 int real_cpu
= cpu
+ vcpu
->arch
.ptid
;
91 if (paca
[real_cpu
].kvm_hstate
.xics_phys
)
92 xics_wake_cpu(real_cpu
);
96 smp_send_reschedule(cpu
);
102 * We use the vcpu_load/put functions to measure stolen time.
103 * Stolen time is counted as time when either the vcpu is able to
104 * run as part of a virtual core, but the task running the vcore
105 * is preempted or sleeping, or when the vcpu needs something done
106 * in the kernel by the task running the vcpu, but that task is
107 * preempted or sleeping. Those two things have to be counted
108 * separately, since one of the vcpu tasks will take on the job
109 * of running the core, and the other vcpu tasks in the vcore will
110 * sleep waiting for it to do that, but that sleep shouldn't count
113 * Hence we accumulate stolen time when the vcpu can run as part of
114 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
115 * needs its task to do other things in the kernel (for example,
116 * service a page fault) in busy_stolen. We don't accumulate
117 * stolen time for a vcore when it is inactive, or for a vcpu
118 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
119 * a misnomer; it means that the vcpu task is not executing in
120 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
121 * the kernel. We don't have any way of dividing up that time
122 * between time that the vcpu is genuinely stopped, time that
123 * the task is actively working on behalf of the vcpu, and time
124 * that the task is preempted, so we don't count any of it as
127 * Updates to busy_stolen are protected by arch.tbacct_lock;
128 * updates to vc->stolen_tb are protected by the arch.tbacct_lock
129 * of the vcpu that has taken responsibility for running the vcore
130 * (i.e. vc->runner). The stolen times are measured in units of
131 * timebase ticks. (Note that the != TB_NIL checks below are
132 * purely defensive; they should never fail.)
135 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu
*vcpu
, int cpu
)
137 struct kvmppc_vcore
*vc
= vcpu
->arch
.vcore
;
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 (waitqueue_active(&vcpu
->wq
)) {
587 wake_up_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_waitqueue_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()) {
1363 #ifdef CONFIG_KVM_XICS
1366 if (vcpu
->arch
.ptid
)
1372 static void kvmppc_wait_for_nap(struct kvmppc_vcore
*vc
)
1378 while (vc
->nap_count
< vc
->n_woken
) {
1379 if (++i
>= 1000000) {
1380 pr_err("kvmppc_wait_for_nap timeout %d %d\n",
1381 vc
->nap_count
, vc
->n_woken
);
1390 * Check that we are on thread 0 and that any other threads in
1391 * this core are off-line. Then grab the threads so they can't
1394 static int on_primary_thread(void)
1396 int cpu
= smp_processor_id();
1397 int thr
= cpu_thread_in_core(cpu
);
1401 while (++thr
< threads_per_core
)
1402 if (cpu_online(cpu
+ thr
))
1405 /* Grab all hw threads so they can't go into the kernel */
1406 for (thr
= 1; thr
< threads_per_core
; ++thr
) {
1407 if (kvmppc_grab_hwthread(cpu
+ thr
)) {
1408 /* Couldn't grab one; let the others go */
1410 kvmppc_release_hwthread(cpu
+ thr
);
1411 } while (--thr
> 0);
1419 * Run a set of guest threads on a physical core.
1420 * Called with vc->lock held.
1422 static void kvmppc_run_core(struct kvmppc_vcore
*vc
)
1424 struct kvm_vcpu
*vcpu
, *vnext
;
1427 int i
, need_vpa_update
;
1429 struct kvm_vcpu
*vcpus_to_update
[threads_per_core
];
1431 /* don't start if any threads have a signal pending */
1432 need_vpa_update
= 0;
1433 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1434 if (signal_pending(vcpu
->arch
.run_task
))
1436 if (vcpu
->arch
.vpa
.update_pending
||
1437 vcpu
->arch
.slb_shadow
.update_pending
||
1438 vcpu
->arch
.dtl
.update_pending
)
1439 vcpus_to_update
[need_vpa_update
++] = vcpu
;
1443 * Initialize *vc, in particular vc->vcore_state, so we can
1444 * drop the vcore lock if necessary.
1448 vc
->entry_exit_count
= 0;
1449 vc
->vcore_state
= VCORE_STARTING
;
1451 vc
->napping_threads
= 0;
1454 * Updating any of the vpas requires calling kvmppc_pin_guest_page,
1455 * which can't be called with any spinlocks held.
1457 if (need_vpa_update
) {
1458 spin_unlock(&vc
->lock
);
1459 for (i
= 0; i
< need_vpa_update
; ++i
)
1460 kvmppc_update_vpas(vcpus_to_update
[i
]);
1461 spin_lock(&vc
->lock
);
1465 * Make sure we are running on thread 0, and that
1466 * secondary threads are offline.
1468 if (threads_per_core
> 1 && !on_primary_thread()) {
1469 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
)
1470 vcpu
->arch
.ret
= -EBUSY
;
1474 vc
->pcpu
= smp_processor_id();
1475 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1476 kvmppc_start_thread(vcpu
);
1477 kvmppc_create_dtl_entry(vcpu
, vc
);
1480 /* Set this explicitly in case thread 0 doesn't have a vcpu */
1481 get_paca()->kvm_hstate
.kvm_vcore
= vc
;
1482 get_paca()->kvm_hstate
.ptid
= 0;
1484 vc
->vcore_state
= VCORE_RUNNING
;
1486 spin_unlock(&vc
->lock
);
1490 srcu_idx
= srcu_read_lock(&vc
->kvm
->srcu
);
1492 __kvmppc_vcore_entry();
1494 spin_lock(&vc
->lock
);
1495 /* disable sending of IPIs on virtual external irqs */
1496 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
)
1498 /* wait for secondary threads to finish writing their state to memory */
1499 if (vc
->nap_count
< vc
->n_woken
)
1500 kvmppc_wait_for_nap(vc
);
1501 for (i
= 0; i
< threads_per_core
; ++i
)
1502 kvmppc_release_hwthread(vc
->pcpu
+ i
);
1503 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
1504 vc
->vcore_state
= VCORE_EXITING
;
1505 spin_unlock(&vc
->lock
);
1507 srcu_read_unlock(&vc
->kvm
->srcu
, srcu_idx
);
1509 /* make sure updates to secondary vcpu structs are visible now */
1516 spin_lock(&vc
->lock
);
1518 list_for_each_entry(vcpu
, &vc
->runnable_threads
, arch
.run_list
) {
1519 /* cancel pending dec exception if dec is positive */
1520 if (now
< vcpu
->arch
.dec_expires
&&
1521 kvmppc_core_pending_dec(vcpu
))
1522 kvmppc_core_dequeue_dec(vcpu
);
1525 if (vcpu
->arch
.trap
)
1526 ret
= kvmppc_handle_exit_hv(vcpu
->arch
.kvm_run
, vcpu
,
1527 vcpu
->arch
.run_task
);
1529 vcpu
->arch
.ret
= ret
;
1530 vcpu
->arch
.trap
= 0;
1532 if (vcpu
->arch
.ceded
) {
1533 if (ret
!= RESUME_GUEST
)
1534 kvmppc_end_cede(vcpu
);
1536 kvmppc_set_timer(vcpu
);
1541 vc
->vcore_state
= VCORE_INACTIVE
;
1542 list_for_each_entry_safe(vcpu
, vnext
, &vc
->runnable_threads
,
1544 if (vcpu
->arch
.ret
!= RESUME_GUEST
) {
1545 kvmppc_remove_runnable(vc
, vcpu
);
1546 wake_up(&vcpu
->arch
.cpu_run
);
1552 * Wait for some other vcpu thread to execute us, and
1553 * wake us up when we need to handle something in the host.
1555 static void kvmppc_wait_for_exec(struct kvm_vcpu
*vcpu
, int wait_state
)
1559 prepare_to_wait(&vcpu
->arch
.cpu_run
, &wait
, wait_state
);
1560 if (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
)
1562 finish_wait(&vcpu
->arch
.cpu_run
, &wait
);
1566 * All the vcpus in this vcore are idle, so wait for a decrementer
1567 * or external interrupt to one of the vcpus. vc->lock is held.
1569 static void kvmppc_vcore_blocked(struct kvmppc_vcore
*vc
)
1573 prepare_to_wait(&vc
->wq
, &wait
, TASK_INTERRUPTIBLE
);
1574 vc
->vcore_state
= VCORE_SLEEPING
;
1575 spin_unlock(&vc
->lock
);
1577 finish_wait(&vc
->wq
, &wait
);
1578 spin_lock(&vc
->lock
);
1579 vc
->vcore_state
= VCORE_INACTIVE
;
1582 static int kvmppc_run_vcpu(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
)
1585 struct kvmppc_vcore
*vc
;
1586 struct kvm_vcpu
*v
, *vn
;
1588 kvm_run
->exit_reason
= 0;
1589 vcpu
->arch
.ret
= RESUME_GUEST
;
1590 vcpu
->arch
.trap
= 0;
1591 kvmppc_update_vpas(vcpu
);
1594 * Synchronize with other threads in this virtual core
1596 vc
= vcpu
->arch
.vcore
;
1597 spin_lock(&vc
->lock
);
1598 vcpu
->arch
.ceded
= 0;
1599 vcpu
->arch
.run_task
= current
;
1600 vcpu
->arch
.kvm_run
= kvm_run
;
1601 vcpu
->arch
.stolen_logged
= vcore_stolen_time(vc
, mftb());
1602 vcpu
->arch
.state
= KVMPPC_VCPU_RUNNABLE
;
1603 vcpu
->arch
.busy_preempt
= TB_NIL
;
1604 list_add_tail(&vcpu
->arch
.run_list
, &vc
->runnable_threads
);
1608 * This happens the first time this is called for a vcpu.
1609 * If the vcore is already running, we may be able to start
1610 * this thread straight away and have it join in.
1612 if (!signal_pending(current
)) {
1613 if (vc
->vcore_state
== VCORE_RUNNING
&&
1614 VCORE_EXIT_COUNT(vc
) == 0) {
1615 kvmppc_create_dtl_entry(vcpu
, vc
);
1616 kvmppc_start_thread(vcpu
);
1617 } else if (vc
->vcore_state
== VCORE_SLEEPING
) {
1623 while (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
&&
1624 !signal_pending(current
)) {
1625 if (vc
->vcore_state
!= VCORE_INACTIVE
) {
1626 spin_unlock(&vc
->lock
);
1627 kvmppc_wait_for_exec(vcpu
, TASK_INTERRUPTIBLE
);
1628 spin_lock(&vc
->lock
);
1631 list_for_each_entry_safe(v
, vn
, &vc
->runnable_threads
,
1633 kvmppc_core_prepare_to_enter(v
);
1634 if (signal_pending(v
->arch
.run_task
)) {
1635 kvmppc_remove_runnable(vc
, v
);
1636 v
->stat
.signal_exits
++;
1637 v
->arch
.kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1638 v
->arch
.ret
= -EINTR
;
1639 wake_up(&v
->arch
.cpu_run
);
1642 if (!vc
->n_runnable
|| vcpu
->arch
.state
!= KVMPPC_VCPU_RUNNABLE
)
1646 list_for_each_entry(v
, &vc
->runnable_threads
, arch
.run_list
) {
1647 if (!v
->arch
.pending_exceptions
)
1648 n_ceded
+= v
->arch
.ceded
;
1652 if (n_ceded
== vc
->n_runnable
)
1653 kvmppc_vcore_blocked(vc
);
1655 kvmppc_run_core(vc
);
1659 while (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
&&
1660 (vc
->vcore_state
== VCORE_RUNNING
||
1661 vc
->vcore_state
== VCORE_EXITING
)) {
1662 spin_unlock(&vc
->lock
);
1663 kvmppc_wait_for_exec(vcpu
, TASK_UNINTERRUPTIBLE
);
1664 spin_lock(&vc
->lock
);
1667 if (vcpu
->arch
.state
== KVMPPC_VCPU_RUNNABLE
) {
1668 kvmppc_remove_runnable(vc
, vcpu
);
1669 vcpu
->stat
.signal_exits
++;
1670 kvm_run
->exit_reason
= KVM_EXIT_INTR
;
1671 vcpu
->arch
.ret
= -EINTR
;
1674 if (vc
->n_runnable
&& vc
->vcore_state
== VCORE_INACTIVE
) {
1675 /* Wake up some vcpu to run the core */
1676 v
= list_first_entry(&vc
->runnable_threads
,
1677 struct kvm_vcpu
, arch
.run_list
);
1678 wake_up(&v
->arch
.cpu_run
);
1681 spin_unlock(&vc
->lock
);
1682 return vcpu
->arch
.ret
;
1685 static int kvmppc_vcpu_run_hv(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
1690 if (!vcpu
->arch
.sane
) {
1691 run
->exit_reason
= KVM_EXIT_INTERNAL_ERROR
;
1695 kvmppc_core_prepare_to_enter(vcpu
);
1697 /* No need to go into the guest when all we'll do is come back out */
1698 if (signal_pending(current
)) {
1699 run
->exit_reason
= KVM_EXIT_INTR
;
1703 atomic_inc(&vcpu
->kvm
->arch
.vcpus_running
);
1704 /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */
1707 /* On the first time here, set up HTAB and VRMA or RMA */
1708 if (!vcpu
->kvm
->arch
.rma_setup_done
) {
1709 r
= kvmppc_hv_setup_htab_rma(vcpu
);
1714 flush_fp_to_thread(current
);
1715 flush_altivec_to_thread(current
);
1716 flush_vsx_to_thread(current
);
1717 vcpu
->arch
.wqp
= &vcpu
->arch
.vcore
->wq
;
1718 vcpu
->arch
.pgdir
= current
->mm
->pgd
;
1719 vcpu
->arch
.state
= KVMPPC_VCPU_BUSY_IN_HOST
;
1722 r
= kvmppc_run_vcpu(run
, vcpu
);
1724 if (run
->exit_reason
== KVM_EXIT_PAPR_HCALL
&&
1725 !(vcpu
->arch
.shregs
.msr
& MSR_PR
)) {
1726 r
= kvmppc_pseries_do_hcall(vcpu
);
1727 kvmppc_core_prepare_to_enter(vcpu
);
1728 } else if (r
== RESUME_PAGE_FAULT
) {
1729 srcu_idx
= srcu_read_lock(&vcpu
->kvm
->srcu
);
1730 r
= kvmppc_book3s_hv_page_fault(run
, vcpu
,
1731 vcpu
->arch
.fault_dar
, vcpu
->arch
.fault_dsisr
);
1732 srcu_read_unlock(&vcpu
->kvm
->srcu
, srcu_idx
);
1734 } while (r
== RESUME_GUEST
);
1737 vcpu
->arch
.state
= KVMPPC_VCPU_NOTREADY
;
1738 atomic_dec(&vcpu
->kvm
->arch
.vcpus_running
);
1743 /* Work out RMLS (real mode limit selector) field value for a given RMA size.
1744 Assumes POWER7 or PPC970. */
1745 static inline int lpcr_rmls(unsigned long rma_size
)
1748 case 32ul << 20: /* 32 MB */
1749 if (cpu_has_feature(CPU_FTR_ARCH_206
))
1750 return 8; /* only supported on POWER7 */
1752 case 64ul << 20: /* 64 MB */
1754 case 128ul << 20: /* 128 MB */
1756 case 256ul << 20: /* 256 MB */
1758 case 1ul << 30: /* 1 GB */
1760 case 16ul << 30: /* 16 GB */
1762 case 256ul << 30: /* 256 GB */
1769 static int kvm_rma_fault(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1772 struct kvm_rma_info
*ri
= vma
->vm_file
->private_data
;
1774 if (vmf
->pgoff
>= kvm_rma_pages
)
1775 return VM_FAULT_SIGBUS
;
1777 page
= pfn_to_page(ri
->base_pfn
+ vmf
->pgoff
);
1783 static const struct vm_operations_struct kvm_rma_vm_ops
= {
1784 .fault
= kvm_rma_fault
,
1787 static int kvm_rma_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1789 vma
->vm_flags
|= VM_DONTEXPAND
| VM_DONTDUMP
;
1790 vma
->vm_ops
= &kvm_rma_vm_ops
;
1794 static int kvm_rma_release(struct inode
*inode
, struct file
*filp
)
1796 struct kvm_rma_info
*ri
= filp
->private_data
;
1798 kvm_release_rma(ri
);
1802 static const struct file_operations kvm_rma_fops
= {
1803 .mmap
= kvm_rma_mmap
,
1804 .release
= kvm_rma_release
,
1807 static long kvm_vm_ioctl_allocate_rma(struct kvm
*kvm
,
1808 struct kvm_allocate_rma
*ret
)
1811 struct kvm_rma_info
*ri
;
1813 * Only do this on PPC970 in HV mode
1815 if (!cpu_has_feature(CPU_FTR_HVMODE
) ||
1816 !cpu_has_feature(CPU_FTR_ARCH_201
))
1822 ri
= kvm_alloc_rma();
1826 fd
= anon_inode_getfd("kvm-rma", &kvm_rma_fops
, ri
, O_RDWR
| O_CLOEXEC
);
1828 kvm_release_rma(ri
);
1830 ret
->rma_size
= kvm_rma_pages
<< PAGE_SHIFT
;
1834 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size
**sps
,
1837 struct mmu_psize_def
*def
= &mmu_psize_defs
[linux_psize
];
1841 (*sps
)->page_shift
= def
->shift
;
1842 (*sps
)->slb_enc
= def
->sllp
;
1843 (*sps
)->enc
[0].page_shift
= def
->shift
;
1845 * Only return base page encoding. We don't want to return
1846 * all the supporting pte_enc, because our H_ENTER doesn't
1847 * support MPSS yet. Once they do, we can start passing all
1848 * support pte_enc here
1850 (*sps
)->enc
[0].pte_enc
= def
->penc
[linux_psize
];
1854 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm
*kvm
,
1855 struct kvm_ppc_smmu_info
*info
)
1857 struct kvm_ppc_one_seg_page_size
*sps
;
1859 info
->flags
= KVM_PPC_PAGE_SIZES_REAL
;
1860 if (mmu_has_feature(MMU_FTR_1T_SEGMENT
))
1861 info
->flags
|= KVM_PPC_1T_SEGMENTS
;
1862 info
->slb_size
= mmu_slb_size
;
1864 /* We only support these sizes for now, and no muti-size segments */
1865 sps
= &info
->sps
[0];
1866 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_4K
);
1867 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_64K
);
1868 kvmppc_add_seg_page_size(&sps
, MMU_PAGE_16M
);
1874 * Get (and clear) the dirty memory log for a memory slot.
1876 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm
*kvm
,
1877 struct kvm_dirty_log
*log
)
1879 struct kvm_memory_slot
*memslot
;
1883 mutex_lock(&kvm
->slots_lock
);
1886 if (log
->slot
>= KVM_USER_MEM_SLOTS
)
1889 memslot
= id_to_memslot(kvm
->memslots
, log
->slot
);
1891 if (!memslot
->dirty_bitmap
)
1894 n
= kvm_dirty_bitmap_bytes(memslot
);
1895 memset(memslot
->dirty_bitmap
, 0, n
);
1897 r
= kvmppc_hv_get_dirty_log(kvm
, memslot
, memslot
->dirty_bitmap
);
1902 if (copy_to_user(log
->dirty_bitmap
, memslot
->dirty_bitmap
, n
))
1907 mutex_unlock(&kvm
->slots_lock
);
1911 static void unpin_slot(struct kvm_memory_slot
*memslot
)
1913 unsigned long *physp
;
1914 unsigned long j
, npages
, pfn
;
1917 physp
= memslot
->arch
.slot_phys
;
1918 npages
= memslot
->npages
;
1921 for (j
= 0; j
< npages
; j
++) {
1922 if (!(physp
[j
] & KVMPPC_GOT_PAGE
))
1924 pfn
= physp
[j
] >> PAGE_SHIFT
;
1925 page
= pfn_to_page(pfn
);
1931 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot
*free
,
1932 struct kvm_memory_slot
*dont
)
1934 if (!dont
|| free
->arch
.rmap
!= dont
->arch
.rmap
) {
1935 vfree(free
->arch
.rmap
);
1936 free
->arch
.rmap
= NULL
;
1938 if (!dont
|| free
->arch
.slot_phys
!= dont
->arch
.slot_phys
) {
1940 vfree(free
->arch
.slot_phys
);
1941 free
->arch
.slot_phys
= NULL
;
1945 static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot
*slot
,
1946 unsigned long npages
)
1948 slot
->arch
.rmap
= vzalloc(npages
* sizeof(*slot
->arch
.rmap
));
1949 if (!slot
->arch
.rmap
)
1951 slot
->arch
.slot_phys
= NULL
;
1956 static int kvmppc_core_prepare_memory_region_hv(struct kvm
*kvm
,
1957 struct kvm_memory_slot
*memslot
,
1958 struct kvm_userspace_memory_region
*mem
)
1960 unsigned long *phys
;
1962 /* Allocate a slot_phys array if needed */
1963 phys
= memslot
->arch
.slot_phys
;
1964 if (!kvm
->arch
.using_mmu_notifiers
&& !phys
&& memslot
->npages
) {
1965 phys
= vzalloc(memslot
->npages
* sizeof(unsigned long));
1968 memslot
->arch
.slot_phys
= phys
;
1974 static void kvmppc_core_commit_memory_region_hv(struct kvm
*kvm
,
1975 struct kvm_userspace_memory_region
*mem
,
1976 const struct kvm_memory_slot
*old
)
1978 unsigned long npages
= mem
->memory_size
>> PAGE_SHIFT
;
1979 struct kvm_memory_slot
*memslot
;
1981 if (npages
&& old
->npages
) {
1983 * If modifying a memslot, reset all the rmap dirty bits.
1984 * If this is a new memslot, we don't need to do anything
1985 * since the rmap array starts out as all zeroes,
1986 * i.e. no pages are dirty.
1988 memslot
= id_to_memslot(kvm
->memslots
, mem
->slot
);
1989 kvmppc_hv_get_dirty_log(kvm
, memslot
, NULL
);
1994 * Update LPCR values in kvm->arch and in vcores.
1995 * Caller must hold kvm->lock.
1997 void kvmppc_update_lpcr(struct kvm
*kvm
, unsigned long lpcr
, unsigned long mask
)
2002 if ((kvm
->arch
.lpcr
& mask
) == lpcr
)
2005 kvm
->arch
.lpcr
= (kvm
->arch
.lpcr
& ~mask
) | lpcr
;
2007 for (i
= 0; i
< KVM_MAX_VCORES
; ++i
) {
2008 struct kvmppc_vcore
*vc
= kvm
->arch
.vcores
[i
];
2011 spin_lock(&vc
->lock
);
2012 vc
->lpcr
= (vc
->lpcr
& ~mask
) | lpcr
;
2013 spin_unlock(&vc
->lock
);
2014 if (++cores_done
>= kvm
->arch
.online_vcores
)
2019 static void kvmppc_mmu_destroy_hv(struct kvm_vcpu
*vcpu
)
2024 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu
*vcpu
)
2027 struct kvm
*kvm
= vcpu
->kvm
;
2028 struct kvm_rma_info
*ri
= NULL
;
2030 struct kvm_memory_slot
*memslot
;
2031 struct vm_area_struct
*vma
;
2032 unsigned long lpcr
= 0, senc
;
2033 unsigned long lpcr_mask
= 0;
2034 unsigned long psize
, porder
;
2035 unsigned long rma_size
;
2037 unsigned long *physp
;
2038 unsigned long i
, npages
;
2041 mutex_lock(&kvm
->lock
);
2042 if (kvm
->arch
.rma_setup_done
)
2043 goto out
; /* another vcpu beat us to it */
2045 /* Allocate hashed page table (if not done already) and reset it */
2046 if (!kvm
->arch
.hpt_virt
) {
2047 err
= kvmppc_alloc_hpt(kvm
, NULL
);
2049 pr_err("KVM: Couldn't alloc HPT\n");
2054 /* Look up the memslot for guest physical address 0 */
2055 srcu_idx
= srcu_read_lock(&kvm
->srcu
);
2056 memslot
= gfn_to_memslot(kvm
, 0);
2058 /* We must have some memory at 0 by now */
2060 if (!memslot
|| (memslot
->flags
& KVM_MEMSLOT_INVALID
))
2063 /* Look up the VMA for the start of this memory slot */
2064 hva
= memslot
->userspace_addr
;
2065 down_read(¤t
->mm
->mmap_sem
);
2066 vma
= find_vma(current
->mm
, hva
);
2067 if (!vma
|| vma
->vm_start
> hva
|| (vma
->vm_flags
& VM_IO
))
2070 psize
= vma_kernel_pagesize(vma
);
2071 porder
= __ilog2(psize
);
2073 /* Is this one of our preallocated RMAs? */
2074 if (vma
->vm_file
&& vma
->vm_file
->f_op
== &kvm_rma_fops
&&
2075 hva
== vma
->vm_start
)
2076 ri
= vma
->vm_file
->private_data
;
2078 up_read(¤t
->mm
->mmap_sem
);
2081 /* On POWER7, use VRMA; on PPC970, give up */
2083 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
2084 pr_err("KVM: CPU requires an RMO\n");
2088 /* We can handle 4k, 64k or 16M pages in the VRMA */
2090 if (!(psize
== 0x1000 || psize
== 0x10000 ||
2091 psize
== 0x1000000))
2094 /* Update VRMASD field in the LPCR */
2095 senc
= slb_pgsize_encoding(psize
);
2096 kvm
->arch
.vrma_slb_v
= senc
| SLB_VSID_B_1T
|
2097 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
2098 lpcr_mask
= LPCR_VRMASD
;
2099 /* the -4 is to account for senc values starting at 0x10 */
2100 lpcr
= senc
<< (LPCR_VRMASD_SH
- 4);
2102 /* Create HPTEs in the hash page table for the VRMA */
2103 kvmppc_map_vrma(vcpu
, memslot
, porder
);
2106 /* Set up to use an RMO region */
2107 rma_size
= kvm_rma_pages
;
2108 if (rma_size
> memslot
->npages
)
2109 rma_size
= memslot
->npages
;
2110 rma_size
<<= PAGE_SHIFT
;
2111 rmls
= lpcr_rmls(rma_size
);
2113 if ((long)rmls
< 0) {
2114 pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size
);
2117 atomic_inc(&ri
->use_count
);
2120 /* Update LPCR and RMOR */
2121 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
2122 /* PPC970; insert RMLS value (split field) in HID4 */
2123 lpcr_mask
= (1ul << HID4_RMLS0_SH
) |
2124 (3ul << HID4_RMLS2_SH
) | HID4_RMOR
;
2125 lpcr
= ((rmls
>> 2) << HID4_RMLS0_SH
) |
2126 ((rmls
& 3) << HID4_RMLS2_SH
);
2127 /* RMOR is also in HID4 */
2128 lpcr
|= ((ri
->base_pfn
>> (26 - PAGE_SHIFT
)) & 0xffff)
2132 lpcr_mask
= LPCR_VPM0
| LPCR_VRMA_L
| LPCR_RMLS
;
2133 lpcr
= rmls
<< LPCR_RMLS_SH
;
2134 kvm
->arch
.rmor
= ri
->base_pfn
<< PAGE_SHIFT
;
2136 pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n",
2137 ri
->base_pfn
<< PAGE_SHIFT
, rma_size
, lpcr
);
2139 /* Initialize phys addrs of pages in RMO */
2140 npages
= kvm_rma_pages
;
2141 porder
= __ilog2(npages
);
2142 physp
= memslot
->arch
.slot_phys
;
2144 if (npages
> memslot
->npages
)
2145 npages
= memslot
->npages
;
2146 spin_lock(&kvm
->arch
.slot_phys_lock
);
2147 for (i
= 0; i
< npages
; ++i
)
2148 physp
[i
] = ((ri
->base_pfn
+ i
) << PAGE_SHIFT
) +
2150 spin_unlock(&kvm
->arch
.slot_phys_lock
);
2154 kvmppc_update_lpcr(kvm
, lpcr
, lpcr_mask
);
2156 /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */
2158 kvm
->arch
.rma_setup_done
= 1;
2161 srcu_read_unlock(&kvm
->srcu
, srcu_idx
);
2163 mutex_unlock(&kvm
->lock
);
2167 up_read(¤t
->mm
->mmap_sem
);
2171 static int kvmppc_core_init_vm_hv(struct kvm
*kvm
)
2173 unsigned long lpcr
, lpid
;
2175 /* Allocate the guest's logical partition ID */
2177 lpid
= kvmppc_alloc_lpid();
2180 kvm
->arch
.lpid
= lpid
;
2183 * Since we don't flush the TLB when tearing down a VM,
2184 * and this lpid might have previously been used,
2185 * make sure we flush on each core before running the new VM.
2187 cpumask_setall(&kvm
->arch
.need_tlb_flush
);
2189 kvm
->arch
.rma
= NULL
;
2191 kvm
->arch
.host_sdr1
= mfspr(SPRN_SDR1
);
2193 if (cpu_has_feature(CPU_FTR_ARCH_201
)) {
2194 /* PPC970; HID4 is effectively the LPCR */
2195 kvm
->arch
.host_lpid
= 0;
2196 kvm
->arch
.host_lpcr
= lpcr
= mfspr(SPRN_HID4
);
2197 lpcr
&= ~((3 << HID4_LPID1_SH
) | (0xful
<< HID4_LPID5_SH
));
2198 lpcr
|= ((lpid
>> 4) << HID4_LPID1_SH
) |
2199 ((lpid
& 0xf) << HID4_LPID5_SH
);
2201 /* POWER7; init LPCR for virtual RMA mode */
2202 kvm
->arch
.host_lpid
= mfspr(SPRN_LPID
);
2203 kvm
->arch
.host_lpcr
= lpcr
= mfspr(SPRN_LPCR
);
2204 lpcr
&= LPCR_PECE
| LPCR_LPES
;
2205 lpcr
|= (4UL << LPCR_DPFD_SH
) | LPCR_HDICE
|
2206 LPCR_VPM0
| LPCR_VPM1
;
2207 kvm
->arch
.vrma_slb_v
= SLB_VSID_B_1T
|
2208 (VRMA_VSID
<< SLB_VSID_SHIFT_1T
);
2209 /* On POWER8 turn on online bit to enable PURR/SPURR */
2210 if (cpu_has_feature(CPU_FTR_ARCH_207S
))
2213 kvm
->arch
.lpcr
= lpcr
;
2215 kvm
->arch
.using_mmu_notifiers
= !!cpu_has_feature(CPU_FTR_ARCH_206
);
2216 spin_lock_init(&kvm
->arch
.slot_phys_lock
);
2219 * Don't allow secondary CPU threads to come online
2220 * while any KVM VMs exist.
2222 inhibit_secondary_onlining();
2227 static void kvmppc_free_vcores(struct kvm
*kvm
)
2231 for (i
= 0; i
< KVM_MAX_VCORES
; ++i
)
2232 kfree(kvm
->arch
.vcores
[i
]);
2233 kvm
->arch
.online_vcores
= 0;
2236 static void kvmppc_core_destroy_vm_hv(struct kvm
*kvm
)
2238 uninhibit_secondary_onlining();
2240 kvmppc_free_vcores(kvm
);
2241 if (kvm
->arch
.rma
) {
2242 kvm_release_rma(kvm
->arch
.rma
);
2243 kvm
->arch
.rma
= NULL
;
2246 kvmppc_free_hpt(kvm
);
2249 /* We don't need to emulate any privileged instructions or dcbz */
2250 static int kvmppc_core_emulate_op_hv(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
2251 unsigned int inst
, int *advance
)
2253 return EMULATE_FAIL
;
2256 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu
*vcpu
, int sprn
,
2259 return EMULATE_FAIL
;
2262 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu
*vcpu
, int sprn
,
2265 return EMULATE_FAIL
;
2268 static int kvmppc_core_check_processor_compat_hv(void)
2270 if (!cpu_has_feature(CPU_FTR_HVMODE
))
2275 static long kvm_arch_vm_ioctl_hv(struct file
*filp
,
2276 unsigned int ioctl
, unsigned long arg
)
2278 struct kvm
*kvm __maybe_unused
= filp
->private_data
;
2279 void __user
*argp
= (void __user
*)arg
;
2284 case KVM_ALLOCATE_RMA
: {
2285 struct kvm_allocate_rma rma
;
2286 struct kvm
*kvm
= filp
->private_data
;
2288 r
= kvm_vm_ioctl_allocate_rma(kvm
, &rma
);
2289 if (r
>= 0 && copy_to_user(argp
, &rma
, sizeof(rma
)))
2294 case KVM_PPC_ALLOCATE_HTAB
: {
2298 if (get_user(htab_order
, (u32 __user
*)argp
))
2300 r
= kvmppc_alloc_reset_hpt(kvm
, &htab_order
);
2304 if (put_user(htab_order
, (u32 __user
*)argp
))
2310 case KVM_PPC_GET_HTAB_FD
: {
2311 struct kvm_get_htab_fd ghf
;
2314 if (copy_from_user(&ghf
, argp
, sizeof(ghf
)))
2316 r
= kvm_vm_ioctl_get_htab_fd(kvm
, &ghf
);
2327 static struct kvmppc_ops kvm_ops_hv
= {
2328 .get_sregs
= kvm_arch_vcpu_ioctl_get_sregs_hv
,
2329 .set_sregs
= kvm_arch_vcpu_ioctl_set_sregs_hv
,
2330 .get_one_reg
= kvmppc_get_one_reg_hv
,
2331 .set_one_reg
= kvmppc_set_one_reg_hv
,
2332 .vcpu_load
= kvmppc_core_vcpu_load_hv
,
2333 .vcpu_put
= kvmppc_core_vcpu_put_hv
,
2334 .set_msr
= kvmppc_set_msr_hv
,
2335 .vcpu_run
= kvmppc_vcpu_run_hv
,
2336 .vcpu_create
= kvmppc_core_vcpu_create_hv
,
2337 .vcpu_free
= kvmppc_core_vcpu_free_hv
,
2338 .check_requests
= kvmppc_core_check_requests_hv
,
2339 .get_dirty_log
= kvm_vm_ioctl_get_dirty_log_hv
,
2340 .flush_memslot
= kvmppc_core_flush_memslot_hv
,
2341 .prepare_memory_region
= kvmppc_core_prepare_memory_region_hv
,
2342 .commit_memory_region
= kvmppc_core_commit_memory_region_hv
,
2343 .unmap_hva
= kvm_unmap_hva_hv
,
2344 .unmap_hva_range
= kvm_unmap_hva_range_hv
,
2345 .age_hva
= kvm_age_hva_hv
,
2346 .test_age_hva
= kvm_test_age_hva_hv
,
2347 .set_spte_hva
= kvm_set_spte_hva_hv
,
2348 .mmu_destroy
= kvmppc_mmu_destroy_hv
,
2349 .free_memslot
= kvmppc_core_free_memslot_hv
,
2350 .create_memslot
= kvmppc_core_create_memslot_hv
,
2351 .init_vm
= kvmppc_core_init_vm_hv
,
2352 .destroy_vm
= kvmppc_core_destroy_vm_hv
,
2353 .get_smmu_info
= kvm_vm_ioctl_get_smmu_info_hv
,
2354 .emulate_op
= kvmppc_core_emulate_op_hv
,
2355 .emulate_mtspr
= kvmppc_core_emulate_mtspr_hv
,
2356 .emulate_mfspr
= kvmppc_core_emulate_mfspr_hv
,
2357 .fast_vcpu_kick
= kvmppc_fast_vcpu_kick_hv
,
2358 .arch_vm_ioctl
= kvm_arch_vm_ioctl_hv
,
2361 static int kvmppc_book3s_init_hv(void)
2365 * FIXME!! Do we need to check on all cpus ?
2367 r
= kvmppc_core_check_processor_compat_hv();
2371 kvm_ops_hv
.owner
= THIS_MODULE
;
2372 kvmppc_hv_ops
= &kvm_ops_hv
;
2374 r
= kvmppc_mmu_hv_init();
2378 static void kvmppc_book3s_exit_hv(void)
2380 kvmppc_hv_ops
= NULL
;
2383 module_init(kvmppc_book3s_init_hv
);
2384 module_exit(kvmppc_book3s_exit_hv
);
2385 MODULE_LICENSE("GPL");
2386 MODULE_ALIAS_MISCDEV(KVM_MINOR
);
2387 MODULE_ALIAS("devname:kvm");