]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - arch/powerpc/kvm/book3s_hv.c
KVM: PPC: Add support for Book3S processors in hypervisor mode
[mirror_ubuntu-jammy-kernel.git] / arch / powerpc / kvm / book3s_hv.c
1 /*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
9 *
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
12 *
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
15 *
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.
19 */
20
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/fs.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/cpumask.h>
30
31 #include <asm/reg.h>
32 #include <asm/cputable.h>
33 #include <asm/cacheflush.h>
34 #include <asm/tlbflush.h>
35 #include <asm/uaccess.h>
36 #include <asm/io.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>
46
47 /* #define EXIT_DEBUG */
48 /* #define EXIT_DEBUG_SIMPLE */
49 /* #define EXIT_DEBUG_INT */
50
51 void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
52 {
53 local_paca->kvm_hstate.kvm_vcpu = vcpu;
54 }
55
56 void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu)
57 {
58 }
59
60 void kvmppc_vcpu_block(struct kvm_vcpu *vcpu)
61 {
62 u64 now;
63 unsigned long dec_nsec;
64
65 now = get_tb();
66 if (now >= vcpu->arch.dec_expires && !kvmppc_core_pending_dec(vcpu))
67 kvmppc_core_queue_dec(vcpu);
68 if (vcpu->arch.pending_exceptions)
69 return;
70 if (vcpu->arch.dec_expires != ~(u64)0) {
71 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC /
72 tb_ticks_per_sec;
73 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
74 HRTIMER_MODE_REL);
75 }
76
77 kvm_vcpu_block(vcpu);
78 vcpu->stat.halt_wakeup++;
79
80 if (vcpu->arch.dec_expires != ~(u64)0)
81 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
82 }
83
84 void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr)
85 {
86 vcpu->arch.shregs.msr = msr;
87 }
88
89 void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr)
90 {
91 vcpu->arch.pvr = pvr;
92 }
93
94 void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
95 {
96 int r;
97
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);
125 }
126
127 static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu,
128 struct task_struct *tsk)
129 {
130 int r = RESUME_HOST;
131
132 vcpu->stat.sum_exits++;
133
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++;
140 r = RESUME_GUEST;
141 break;
142 case BOOK3S_INTERRUPT_EXTERNAL:
143 vcpu->stat.ext_intr_exits++;
144 r = RESUME_GUEST;
145 break;
146 case BOOK3S_INTERRUPT_PERFMON:
147 r = RESUME_GUEST;
148 break;
149 case BOOK3S_INTERRUPT_PROGRAM:
150 {
151 ulong flags;
152 /*
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.
157 */
158 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
159 kvmppc_core_queue_program(vcpu, flags);
160 r = RESUME_GUEST;
161 break;
162 }
163 case BOOK3S_INTERRUPT_SYSCALL:
164 {
165 /* hcall - punt to userspace */
166 int i;
167
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);
171 r = RESUME_GUEST;
172 break;
173 }
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;
179 r = RESUME_HOST;
180 break;
181 }
182 /*
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.
186 */
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);
191 r = RESUME_GUEST;
192 break;
193 case BOOK3S_INTERRUPT_H_INST_STORAGE:
194 kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE,
195 0x08000000);
196 r = RESUME_GUEST;
197 break;
198 /*
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.
202 */
203 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
204 kvmppc_core_queue_program(vcpu, 0x80000);
205 r = RESUME_GUEST;
206 break;
207 default:
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);
212 r = RESUME_HOST;
213 BUG();
214 break;
215 }
216
217
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
221 * reason. */
222 if (signal_pending(tsk)) {
223 vcpu->stat.signal_exits++;
224 run->exit_reason = KVM_EXIT_INTR;
225 r = -EINTR;
226 } else {
227 kvmppc_core_deliver_interrupts(vcpu);
228 }
229 }
230
231 return r;
232 }
233
234 int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu,
235 struct kvm_sregs *sregs)
236 {
237 int i;
238
239 sregs->pvr = vcpu->arch.pvr;
240
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;
245 }
246
247 return 0;
248 }
249
250 int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu,
251 struct kvm_sregs *sregs)
252 {
253 int i, j;
254
255 kvmppc_set_pvr(vcpu, sregs->pvr);
256
257 j = 0;
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;
262 ++j;
263 }
264 }
265 vcpu->arch.slb_max = j;
266
267 return 0;
268 }
269
270 int kvmppc_core_check_processor_compat(void)
271 {
272 if (cpu_has_feature(CPU_FTR_HVMODE_206))
273 return 0;
274 return -EIO;
275 }
276
277 struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id)
278 {
279 struct kvm_vcpu *vcpu;
280 int err = -ENOMEM;
281 unsigned long lpcr;
282
283 vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL);
284 if (!vcpu)
285 goto out;
286
287 err = kvm_vcpu_init(vcpu, kvm, id);
288 if (err)
289 goto free_vcpu;
290
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);
298
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;
302
303 kvmppc_mmu_book3s_hv_init(vcpu);
304
305 return vcpu;
306
307 free_vcpu:
308 kfree(vcpu);
309 out:
310 return ERR_PTR(err);
311 }
312
313 void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu)
314 {
315 kvm_vcpu_uninit(vcpu);
316 kfree(vcpu);
317 }
318
319 extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu);
320
321 int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu)
322 {
323 u64 now;
324
325 if (signal_pending(current)) {
326 run->exit_reason = KVM_EXIT_INTR;
327 return -EINTR;
328 }
329
330 flush_fp_to_thread(current);
331 flush_altivec_to_thread(current);
332 flush_vsx_to_thread(current);
333 preempt_disable();
334
335 /*
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.
340 */
341 if (threads_per_core > 1) {
342 int cpu = smp_processor_id();
343 int thr = cpu_thread_in_core(cpu);
344
345 if (thr)
346 goto out;
347 while (++thr < threads_per_core)
348 if (cpu_online(cpu + thr))
349 goto out;
350 }
351
352 kvm_guest_enter();
353
354 __kvmppc_vcore_entry(NULL, vcpu);
355
356 kvm_guest_exit();
357
358 preempt_enable();
359 kvm_resched(vcpu);
360
361 now = get_tb();
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);
365
366 return kvmppc_handle_exit(run, vcpu, current);
367
368 out:
369 preempt_enable();
370 return -EBUSY;
371 }
372
373 int kvmppc_core_prepare_memory_region(struct kvm *kvm,
374 struct kvm_userspace_memory_region *mem)
375 {
376 if (mem->guest_phys_addr == 0 && mem->memory_size != 0)
377 return kvmppc_prepare_vrma(kvm, mem);
378 return 0;
379 }
380
381 void kvmppc_core_commit_memory_region(struct kvm *kvm,
382 struct kvm_userspace_memory_region *mem)
383 {
384 if (mem->guest_phys_addr == 0 && mem->memory_size != 0)
385 kvmppc_map_vrma(kvm, mem);
386 }
387
388 int kvmppc_core_init_vm(struct kvm *kvm)
389 {
390 long r;
391
392 /* Allocate hashed page table */
393 r = kvmppc_alloc_hpt(kvm);
394
395 return r;
396 }
397
398 void kvmppc_core_destroy_vm(struct kvm *kvm)
399 {
400 kvmppc_free_hpt(kvm);
401 }
402
403 /* These are stubs for now */
404 void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end)
405 {
406 }
407
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)
411 {
412 return EMULATE_FAIL;
413 }
414
415 int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs)
416 {
417 return EMULATE_FAIL;
418 }
419
420 int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt)
421 {
422 return EMULATE_FAIL;
423 }
424
425 static int kvmppc_book3s_hv_init(void)
426 {
427 int r;
428
429 r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE);
430
431 if (r)
432 return r;
433
434 r = kvmppc_mmu_hv_init();
435
436 return r;
437 }
438
439 static void kvmppc_book3s_hv_exit(void)
440 {
441 kvm_exit();
442 }
443
444 module_init(kvmppc_book3s_hv_init);
445 module_exit(kvmppc_book3s_hv_exit);