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>
28 #include <linux/anon_inodes.h>
29 #include <linux/cpumask.h>
32 #include <asm/cputable.h>
33 #include <asm/cacheflush.h>
34 #include <asm/tlbflush.h>
35 #include <asm/uaccess.h>
37 #include <asm/kvm_ppc.h>
38 #include <asm/kvm_book3s.h>
39 #include <asm/mmu_context.h>
40 #include <asm/lppaca.h>
41 #include <asm/processor.h>
42 #include <linux/gfp.h>
43 #include <linux/sched.h>
44 #include <linux/vmalloc.h>
45 #include <linux/highmem.h>
47 /* #define EXIT_DEBUG */
48 /* #define EXIT_DEBUG_SIMPLE */
49 /* #define EXIT_DEBUG_INT */
51 void kvmppc_core_vcpu_load(struct kvm_vcpu
*vcpu
, int cpu
)
53 local_paca
->kvm_hstate
.kvm_vcpu
= vcpu
;
56 void kvmppc_core_vcpu_put(struct kvm_vcpu
*vcpu
)
60 void kvmppc_vcpu_block(struct kvm_vcpu
*vcpu
)
63 unsigned long dec_nsec
;
66 if (now
>= vcpu
->arch
.dec_expires
&& !kvmppc_core_pending_dec(vcpu
))
67 kvmppc_core_queue_dec(vcpu
);
68 if (vcpu
->arch
.pending_exceptions
)
70 if (vcpu
->arch
.dec_expires
!= ~(u64
)0) {
71 dec_nsec
= (vcpu
->arch
.dec_expires
- now
) * NSEC_PER_SEC
/
73 hrtimer_start(&vcpu
->arch
.dec_timer
, ktime_set(0, dec_nsec
),
78 vcpu
->stat
.halt_wakeup
++;
80 if (vcpu
->arch
.dec_expires
!= ~(u64
)0)
81 hrtimer_try_to_cancel(&vcpu
->arch
.dec_timer
);
84 void kvmppc_set_msr(struct kvm_vcpu
*vcpu
, u64 msr
)
86 vcpu
->arch
.shregs
.msr
= msr
;
89 void kvmppc_set_pvr(struct kvm_vcpu
*vcpu
, u32 pvr
)
94 void kvmppc_dump_regs(struct kvm_vcpu
*vcpu
)
98 pr_err("vcpu %p (%d):\n", vcpu
, vcpu
->vcpu_id
);
99 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
100 vcpu
->arch
.pc
, vcpu
->arch
.shregs
.msr
, vcpu
->arch
.trap
);
101 for (r
= 0; r
< 16; ++r
)
102 pr_err("r%2d = %.16lx r%d = %.16lx\n",
103 r
, kvmppc_get_gpr(vcpu
, r
),
104 r
+16, kvmppc_get_gpr(vcpu
, r
+16));
105 pr_err("ctr = %.16lx lr = %.16lx\n",
106 vcpu
->arch
.ctr
, vcpu
->arch
.lr
);
107 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
108 vcpu
->arch
.shregs
.srr0
, vcpu
->arch
.shregs
.srr1
);
109 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
110 vcpu
->arch
.shregs
.sprg0
, vcpu
->arch
.shregs
.sprg1
);
111 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
112 vcpu
->arch
.shregs
.sprg2
, vcpu
->arch
.shregs
.sprg3
);
113 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
114 vcpu
->arch
.cr
, vcpu
->arch
.xer
, vcpu
->arch
.shregs
.dsisr
);
115 pr_err("dar = %.16llx\n", vcpu
->arch
.shregs
.dar
);
116 pr_err("fault dar = %.16lx dsisr = %.8x\n",
117 vcpu
->arch
.fault_dar
, vcpu
->arch
.fault_dsisr
);
118 pr_err("SLB (%d entries):\n", vcpu
->arch
.slb_max
);
119 for (r
= 0; r
< vcpu
->arch
.slb_max
; ++r
)
120 pr_err(" ESID = %.16llx VSID = %.16llx\n",
121 vcpu
->arch
.slb
[r
].orige
, vcpu
->arch
.slb
[r
].origv
);
122 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
123 vcpu
->arch
.lpcr
, vcpu
->kvm
->arch
.sdr1
,
124 vcpu
->arch
.last_inst
);
127 static int kvmppc_handle_exit(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
128 struct task_struct
*tsk
)
132 vcpu
->stat
.sum_exits
++;
134 run
->exit_reason
= KVM_EXIT_UNKNOWN
;
135 run
->ready_for_interrupt_injection
= 1;
136 switch (vcpu
->arch
.trap
) {
137 /* We're good on these - the host merely wanted to get our attention */
138 case BOOK3S_INTERRUPT_HV_DECREMENTER
:
139 vcpu
->stat
.dec_exits
++;
142 case BOOK3S_INTERRUPT_EXTERNAL
:
143 vcpu
->stat
.ext_intr_exits
++;
146 case BOOK3S_INTERRUPT_PERFMON
:
149 case BOOK3S_INTERRUPT_PROGRAM
:
153 * Normally program interrupts are delivered directly
154 * to the guest by the hardware, but we can get here
155 * as a result of a hypervisor emulation interrupt
156 * (e40) getting turned into a 700 by BML RTAS.
158 flags
= vcpu
->arch
.shregs
.msr
& 0x1f0000ull
;
159 kvmppc_core_queue_program(vcpu
, flags
);
163 case BOOK3S_INTERRUPT_SYSCALL
:
165 /* hcall - punt to userspace */
168 if (vcpu
->arch
.shregs
.msr
& MSR_PR
) {
169 /* sc 1 from userspace - reflect to guest syscall */
170 kvmppc_book3s_queue_irqprio(vcpu
, BOOK3S_INTERRUPT_SYSCALL
);
174 run
->papr_hcall
.nr
= kvmppc_get_gpr(vcpu
, 3);
175 for (i
= 0; i
< 9; ++i
)
176 run
->papr_hcall
.args
[i
] = kvmppc_get_gpr(vcpu
, 4 + i
);
177 run
->exit_reason
= KVM_EXIT_PAPR_HCALL
;
178 vcpu
->arch
.hcall_needed
= 1;
183 * We get these next two if the guest does a bad real-mode access,
184 * as we have enabled VRMA (virtualized real mode area) mode in the
185 * LPCR. We just generate an appropriate DSI/ISI to the guest.
187 case BOOK3S_INTERRUPT_H_DATA_STORAGE
:
188 vcpu
->arch
.shregs
.dsisr
= vcpu
->arch
.fault_dsisr
;
189 vcpu
->arch
.shregs
.dar
= vcpu
->arch
.fault_dar
;
190 kvmppc_inject_interrupt(vcpu
, BOOK3S_INTERRUPT_DATA_STORAGE
, 0);
193 case BOOK3S_INTERRUPT_H_INST_STORAGE
:
194 kvmppc_inject_interrupt(vcpu
, BOOK3S_INTERRUPT_INST_STORAGE
,
199 * This occurs if the guest executes an illegal instruction.
200 * We just generate a program interrupt to the guest, since
201 * we don't emulate any guest instructions at this stage.
203 case BOOK3S_INTERRUPT_H_EMUL_ASSIST
:
204 kvmppc_core_queue_program(vcpu
, 0x80000);
208 kvmppc_dump_regs(vcpu
);
209 printk(KERN_EMERG
"trap=0x%x | pc=0x%lx | msr=0x%llx\n",
210 vcpu
->arch
.trap
, kvmppc_get_pc(vcpu
),
211 vcpu
->arch
.shregs
.msr
);
218 if (!(r
& RESUME_HOST
)) {
219 /* To avoid clobbering exit_reason, only check for signals if
220 * we aren't already exiting to userspace for some other
222 if (signal_pending(tsk
)) {
223 vcpu
->stat
.signal_exits
++;
224 run
->exit_reason
= KVM_EXIT_INTR
;
227 kvmppc_core_deliver_interrupts(vcpu
);
234 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu
*vcpu
,
235 struct kvm_sregs
*sregs
)
239 sregs
->pvr
= vcpu
->arch
.pvr
;
241 memset(sregs
, 0, sizeof(struct kvm_sregs
));
242 for (i
= 0; i
< vcpu
->arch
.slb_max
; i
++) {
243 sregs
->u
.s
.ppc64
.slb
[i
].slbe
= vcpu
->arch
.slb
[i
].orige
;
244 sregs
->u
.s
.ppc64
.slb
[i
].slbv
= vcpu
->arch
.slb
[i
].origv
;
250 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu
*vcpu
,
251 struct kvm_sregs
*sregs
)
255 kvmppc_set_pvr(vcpu
, sregs
->pvr
);
258 for (i
= 0; i
< vcpu
->arch
.slb_nr
; i
++) {
259 if (sregs
->u
.s
.ppc64
.slb
[i
].slbe
& SLB_ESID_V
) {
260 vcpu
->arch
.slb
[j
].orige
= sregs
->u
.s
.ppc64
.slb
[i
].slbe
;
261 vcpu
->arch
.slb
[j
].origv
= sregs
->u
.s
.ppc64
.slb
[i
].slbv
;
265 vcpu
->arch
.slb_max
= j
;
270 int kvmppc_core_check_processor_compat(void)
272 if (cpu_has_feature(CPU_FTR_HVMODE_206
))
277 struct kvm_vcpu
*kvmppc_core_vcpu_create(struct kvm
*kvm
, unsigned int id
)
279 struct kvm_vcpu
*vcpu
;
283 vcpu
= kzalloc(sizeof(struct kvm_vcpu
), GFP_KERNEL
);
287 err
= kvm_vcpu_init(vcpu
, kvm
, id
);
291 vcpu
->arch
.shared
= &vcpu
->arch
.shregs
;
292 vcpu
->arch
.last_cpu
= -1;
293 vcpu
->arch
.mmcr
[0] = MMCR0_FC
;
294 vcpu
->arch
.ctrl
= CTRL_RUNLATCH
;
295 /* default to host PVR, since we can't spoof it */
296 vcpu
->arch
.pvr
= mfspr(SPRN_PVR
);
297 kvmppc_set_pvr(vcpu
, vcpu
->arch
.pvr
);
299 lpcr
= kvm
->arch
.host_lpcr
& (LPCR_PECE
| LPCR_LPES
);
300 lpcr
|= LPCR_VPM0
| LPCR_VRMA_L
| (4UL << LPCR_DPFD_SH
) | LPCR_HDICE
;
301 vcpu
->arch
.lpcr
= lpcr
;
303 kvmppc_mmu_book3s_hv_init(vcpu
);
313 void kvmppc_core_vcpu_free(struct kvm_vcpu
*vcpu
)
315 kvm_vcpu_uninit(vcpu
);
319 extern int __kvmppc_vcore_entry(struct kvm_run
*kvm_run
, struct kvm_vcpu
*vcpu
);
321 int kvmppc_vcpu_run(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
)
325 if (signal_pending(current
)) {
326 run
->exit_reason
= KVM_EXIT_INTR
;
330 flush_fp_to_thread(current
);
331 flush_altivec_to_thread(current
);
332 flush_vsx_to_thread(current
);
336 * Make sure we are running on thread 0, and that
337 * secondary threads are offline.
338 * XXX we should also block attempts to bring any
339 * secondary threads online.
341 if (threads_per_core
> 1) {
342 int cpu
= smp_processor_id();
343 int thr
= cpu_thread_in_core(cpu
);
347 while (++thr
< threads_per_core
)
348 if (cpu_online(cpu
+ thr
))
354 __kvmppc_vcore_entry(NULL
, vcpu
);
362 /* cancel pending dec exception if dec is positive */
363 if (now
< vcpu
->arch
.dec_expires
&& kvmppc_core_pending_dec(vcpu
))
364 kvmppc_core_dequeue_dec(vcpu
);
366 return kvmppc_handle_exit(run
, vcpu
, current
);
373 int kvmppc_core_prepare_memory_region(struct kvm
*kvm
,
374 struct kvm_userspace_memory_region
*mem
)
376 if (mem
->guest_phys_addr
== 0 && mem
->memory_size
!= 0)
377 return kvmppc_prepare_vrma(kvm
, mem
);
381 void kvmppc_core_commit_memory_region(struct kvm
*kvm
,
382 struct kvm_userspace_memory_region
*mem
)
384 if (mem
->guest_phys_addr
== 0 && mem
->memory_size
!= 0)
385 kvmppc_map_vrma(kvm
, mem
);
388 int kvmppc_core_init_vm(struct kvm
*kvm
)
392 /* Allocate hashed page table */
393 r
= kvmppc_alloc_hpt(kvm
);
398 void kvmppc_core_destroy_vm(struct kvm
*kvm
)
400 kvmppc_free_hpt(kvm
);
403 /* These are stubs for now */
404 void kvmppc_mmu_pte_pflush(struct kvm_vcpu
*vcpu
, ulong pa_start
, ulong pa_end
)
408 /* We don't need to emulate any privileged instructions or dcbz */
409 int kvmppc_core_emulate_op(struct kvm_run
*run
, struct kvm_vcpu
*vcpu
,
410 unsigned int inst
, int *advance
)
415 int kvmppc_core_emulate_mtspr(struct kvm_vcpu
*vcpu
, int sprn
, int rs
)
420 int kvmppc_core_emulate_mfspr(struct kvm_vcpu
*vcpu
, int sprn
, int rt
)
425 static int kvmppc_book3s_hv_init(void)
429 r
= kvm_init(NULL
, sizeof(struct kvm_vcpu
), 0, THIS_MODULE
);
434 r
= kvmppc_mmu_hv_init();
439 static void kvmppc_book3s_hv_exit(void)
444 module_init(kvmppc_book3s_hv_init
);
445 module_exit(kvmppc_book3s_hv_exit
);