Commit | Line | Data |
---|---|---|
de56a948 PM |
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> | |
66b15db6 | 27 | #include <linux/export.h> |
de56a948 PM |
28 | #include <linux/fs.h> |
29 | #include <linux/anon_inodes.h> | |
30 | #include <linux/cpumask.h> | |
aa04b4cc PM |
31 | #include <linux/spinlock.h> |
32 | #include <linux/page-flags.h> | |
de56a948 PM |
33 | |
34 | #include <asm/reg.h> | |
35 | #include <asm/cputable.h> | |
36 | #include <asm/cacheflush.h> | |
37 | #include <asm/tlbflush.h> | |
38 | #include <asm/uaccess.h> | |
39 | #include <asm/io.h> | |
40 | #include <asm/kvm_ppc.h> | |
41 | #include <asm/kvm_book3s.h> | |
42 | #include <asm/mmu_context.h> | |
43 | #include <asm/lppaca.h> | |
44 | #include <asm/processor.h> | |
371fefd6 | 45 | #include <asm/cputhreads.h> |
aa04b4cc | 46 | #include <asm/page.h> |
de1d9248 | 47 | #include <asm/hvcall.h> |
de56a948 PM |
48 | #include <linux/gfp.h> |
49 | #include <linux/sched.h> | |
50 | #include <linux/vmalloc.h> | |
51 | #include <linux/highmem.h> | |
c77162de | 52 | #include <linux/hugetlb.h> |
de56a948 | 53 | |
aa04b4cc PM |
54 | #define LARGE_PAGE_ORDER 24 /* 16MB pages */ |
55 | ||
de56a948 PM |
56 | /* #define EXIT_DEBUG */ |
57 | /* #define EXIT_DEBUG_SIMPLE */ | |
58 | /* #define EXIT_DEBUG_INT */ | |
59 | ||
19ccb76a | 60 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu); |
c77162de | 61 | static int kvmppc_hv_setup_rma(struct kvm_vcpu *vcpu); |
19ccb76a | 62 | |
de56a948 PM |
63 | void kvmppc_core_vcpu_load(struct kvm_vcpu *vcpu, int cpu) |
64 | { | |
65 | local_paca->kvm_hstate.kvm_vcpu = vcpu; | |
371fefd6 | 66 | local_paca->kvm_hstate.kvm_vcore = vcpu->arch.vcore; |
de56a948 PM |
67 | } |
68 | ||
69 | void kvmppc_core_vcpu_put(struct kvm_vcpu *vcpu) | |
70 | { | |
71 | } | |
72 | ||
de56a948 PM |
73 | void kvmppc_set_msr(struct kvm_vcpu *vcpu, u64 msr) |
74 | { | |
75 | vcpu->arch.shregs.msr = msr; | |
19ccb76a | 76 | kvmppc_end_cede(vcpu); |
de56a948 PM |
77 | } |
78 | ||
79 | void kvmppc_set_pvr(struct kvm_vcpu *vcpu, u32 pvr) | |
80 | { | |
81 | vcpu->arch.pvr = pvr; | |
82 | } | |
83 | ||
84 | void kvmppc_dump_regs(struct kvm_vcpu *vcpu) | |
85 | { | |
86 | int r; | |
87 | ||
88 | pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); | |
89 | pr_err("pc = %.16lx msr = %.16llx trap = %x\n", | |
90 | vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap); | |
91 | for (r = 0; r < 16; ++r) | |
92 | pr_err("r%2d = %.16lx r%d = %.16lx\n", | |
93 | r, kvmppc_get_gpr(vcpu, r), | |
94 | r+16, kvmppc_get_gpr(vcpu, r+16)); | |
95 | pr_err("ctr = %.16lx lr = %.16lx\n", | |
96 | vcpu->arch.ctr, vcpu->arch.lr); | |
97 | pr_err("srr0 = %.16llx srr1 = %.16llx\n", | |
98 | vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); | |
99 | pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", | |
100 | vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); | |
101 | pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", | |
102 | vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); | |
103 | pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n", | |
104 | vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr); | |
105 | pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); | |
106 | pr_err("fault dar = %.16lx dsisr = %.8x\n", | |
107 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); | |
108 | pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); | |
109 | for (r = 0; r < vcpu->arch.slb_max; ++r) | |
110 | pr_err(" ESID = %.16llx VSID = %.16llx\n", | |
111 | vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); | |
112 | pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n", | |
aa04b4cc | 113 | vcpu->kvm->arch.lpcr, vcpu->kvm->arch.sdr1, |
de56a948 PM |
114 | vcpu->arch.last_inst); |
115 | } | |
116 | ||
a8606e20 PM |
117 | struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) |
118 | { | |
119 | int r; | |
120 | struct kvm_vcpu *v, *ret = NULL; | |
121 | ||
122 | mutex_lock(&kvm->lock); | |
123 | kvm_for_each_vcpu(r, v, kvm) { | |
124 | if (v->vcpu_id == id) { | |
125 | ret = v; | |
126 | break; | |
127 | } | |
128 | } | |
129 | mutex_unlock(&kvm->lock); | |
130 | return ret; | |
131 | } | |
132 | ||
133 | static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) | |
134 | { | |
135 | vpa->shared_proc = 1; | |
136 | vpa->yield_count = 1; | |
137 | } | |
138 | ||
139 | static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, | |
140 | unsigned long flags, | |
141 | unsigned long vcpuid, unsigned long vpa) | |
142 | { | |
143 | struct kvm *kvm = vcpu->kvm; | |
93e60249 | 144 | unsigned long len, nb; |
a8606e20 PM |
145 | void *va; |
146 | struct kvm_vcpu *tvcpu; | |
93e60249 | 147 | int err = H_PARAMETER; |
a8606e20 PM |
148 | |
149 | tvcpu = kvmppc_find_vcpu(kvm, vcpuid); | |
150 | if (!tvcpu) | |
151 | return H_PARAMETER; | |
152 | ||
153 | flags >>= 63 - 18; | |
154 | flags &= 7; | |
155 | if (flags == 0 || flags == 4) | |
156 | return H_PARAMETER; | |
157 | if (flags < 4) { | |
158 | if (vpa & 0x7f) | |
159 | return H_PARAMETER; | |
93e60249 PM |
160 | if (flags >= 2 && !tvcpu->arch.vpa) |
161 | return H_RESOURCE; | |
a8606e20 | 162 | /* registering new area; convert logical addr to real */ |
93e60249 PM |
163 | va = kvmppc_pin_guest_page(kvm, vpa, &nb); |
164 | if (va == NULL) | |
b2b2f165 | 165 | return H_PARAMETER; |
a8606e20 PM |
166 | if (flags <= 1) |
167 | len = *(unsigned short *)(va + 4); | |
168 | else | |
169 | len = *(unsigned int *)(va + 4); | |
93e60249 PM |
170 | if (len > nb) |
171 | goto out_unpin; | |
a8606e20 PM |
172 | switch (flags) { |
173 | case 1: /* register VPA */ | |
174 | if (len < 640) | |
93e60249 PM |
175 | goto out_unpin; |
176 | if (tvcpu->arch.vpa) | |
177 | kvmppc_unpin_guest_page(kvm, vcpu->arch.vpa); | |
a8606e20 PM |
178 | tvcpu->arch.vpa = va; |
179 | init_vpa(vcpu, va); | |
180 | break; | |
181 | case 2: /* register DTL */ | |
182 | if (len < 48) | |
93e60249 | 183 | goto out_unpin; |
a8606e20 | 184 | len -= len % 48; |
93e60249 PM |
185 | if (tvcpu->arch.dtl) |
186 | kvmppc_unpin_guest_page(kvm, vcpu->arch.dtl); | |
a8606e20 PM |
187 | tvcpu->arch.dtl = va; |
188 | tvcpu->arch.dtl_end = va + len; | |
189 | break; | |
190 | case 3: /* register SLB shadow buffer */ | |
93e60249 PM |
191 | if (len < 16) |
192 | goto out_unpin; | |
193 | if (tvcpu->arch.slb_shadow) | |
194 | kvmppc_unpin_guest_page(kvm, vcpu->arch.slb_shadow); | |
a8606e20 PM |
195 | tvcpu->arch.slb_shadow = va; |
196 | break; | |
197 | } | |
198 | } else { | |
199 | switch (flags) { | |
200 | case 5: /* unregister VPA */ | |
201 | if (tvcpu->arch.slb_shadow || tvcpu->arch.dtl) | |
202 | return H_RESOURCE; | |
93e60249 PM |
203 | if (!tvcpu->arch.vpa) |
204 | break; | |
205 | kvmppc_unpin_guest_page(kvm, tvcpu->arch.vpa); | |
a8606e20 PM |
206 | tvcpu->arch.vpa = NULL; |
207 | break; | |
208 | case 6: /* unregister DTL */ | |
93e60249 PM |
209 | if (!tvcpu->arch.dtl) |
210 | break; | |
211 | kvmppc_unpin_guest_page(kvm, tvcpu->arch.dtl); | |
a8606e20 PM |
212 | tvcpu->arch.dtl = NULL; |
213 | break; | |
214 | case 7: /* unregister SLB shadow buffer */ | |
93e60249 PM |
215 | if (!tvcpu->arch.slb_shadow) |
216 | break; | |
217 | kvmppc_unpin_guest_page(kvm, tvcpu->arch.slb_shadow); | |
a8606e20 PM |
218 | tvcpu->arch.slb_shadow = NULL; |
219 | break; | |
220 | } | |
221 | } | |
222 | return H_SUCCESS; | |
93e60249 PM |
223 | |
224 | out_unpin: | |
225 | kvmppc_unpin_guest_page(kvm, va); | |
226 | return err; | |
a8606e20 PM |
227 | } |
228 | ||
229 | int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) | |
230 | { | |
231 | unsigned long req = kvmppc_get_gpr(vcpu, 3); | |
232 | unsigned long target, ret = H_SUCCESS; | |
233 | struct kvm_vcpu *tvcpu; | |
234 | ||
235 | switch (req) { | |
c77162de PM |
236 | case H_ENTER: |
237 | ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4), | |
238 | kvmppc_get_gpr(vcpu, 5), | |
239 | kvmppc_get_gpr(vcpu, 6), | |
240 | kvmppc_get_gpr(vcpu, 7)); | |
241 | break; | |
a8606e20 | 242 | case H_CEDE: |
a8606e20 PM |
243 | break; |
244 | case H_PROD: | |
245 | target = kvmppc_get_gpr(vcpu, 4); | |
246 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); | |
247 | if (!tvcpu) { | |
248 | ret = H_PARAMETER; | |
249 | break; | |
250 | } | |
251 | tvcpu->arch.prodded = 1; | |
252 | smp_mb(); | |
253 | if (vcpu->arch.ceded) { | |
254 | if (waitqueue_active(&vcpu->wq)) { | |
255 | wake_up_interruptible(&vcpu->wq); | |
256 | vcpu->stat.halt_wakeup++; | |
257 | } | |
258 | } | |
259 | break; | |
260 | case H_CONFER: | |
261 | break; | |
262 | case H_REGISTER_VPA: | |
263 | ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), | |
264 | kvmppc_get_gpr(vcpu, 5), | |
265 | kvmppc_get_gpr(vcpu, 6)); | |
266 | break; | |
267 | default: | |
268 | return RESUME_HOST; | |
269 | } | |
270 | kvmppc_set_gpr(vcpu, 3, ret); | |
271 | vcpu->arch.hcall_needed = 0; | |
272 | return RESUME_GUEST; | |
273 | } | |
274 | ||
de56a948 PM |
275 | static int kvmppc_handle_exit(struct kvm_run *run, struct kvm_vcpu *vcpu, |
276 | struct task_struct *tsk) | |
277 | { | |
278 | int r = RESUME_HOST; | |
279 | ||
280 | vcpu->stat.sum_exits++; | |
281 | ||
282 | run->exit_reason = KVM_EXIT_UNKNOWN; | |
283 | run->ready_for_interrupt_injection = 1; | |
284 | switch (vcpu->arch.trap) { | |
285 | /* We're good on these - the host merely wanted to get our attention */ | |
286 | case BOOK3S_INTERRUPT_HV_DECREMENTER: | |
287 | vcpu->stat.dec_exits++; | |
288 | r = RESUME_GUEST; | |
289 | break; | |
290 | case BOOK3S_INTERRUPT_EXTERNAL: | |
291 | vcpu->stat.ext_intr_exits++; | |
292 | r = RESUME_GUEST; | |
293 | break; | |
294 | case BOOK3S_INTERRUPT_PERFMON: | |
295 | r = RESUME_GUEST; | |
296 | break; | |
297 | case BOOK3S_INTERRUPT_PROGRAM: | |
298 | { | |
299 | ulong flags; | |
300 | /* | |
301 | * Normally program interrupts are delivered directly | |
302 | * to the guest by the hardware, but we can get here | |
303 | * as a result of a hypervisor emulation interrupt | |
304 | * (e40) getting turned into a 700 by BML RTAS. | |
305 | */ | |
306 | flags = vcpu->arch.shregs.msr & 0x1f0000ull; | |
307 | kvmppc_core_queue_program(vcpu, flags); | |
308 | r = RESUME_GUEST; | |
309 | break; | |
310 | } | |
311 | case BOOK3S_INTERRUPT_SYSCALL: | |
312 | { | |
313 | /* hcall - punt to userspace */ | |
314 | int i; | |
315 | ||
316 | if (vcpu->arch.shregs.msr & MSR_PR) { | |
317 | /* sc 1 from userspace - reflect to guest syscall */ | |
318 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL); | |
319 | r = RESUME_GUEST; | |
320 | break; | |
321 | } | |
322 | run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); | |
323 | for (i = 0; i < 9; ++i) | |
324 | run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); | |
325 | run->exit_reason = KVM_EXIT_PAPR_HCALL; | |
326 | vcpu->arch.hcall_needed = 1; | |
327 | r = RESUME_HOST; | |
328 | break; | |
329 | } | |
330 | /* | |
331 | * We get these next two if the guest does a bad real-mode access, | |
332 | * as we have enabled VRMA (virtualized real mode area) mode in the | |
333 | * LPCR. We just generate an appropriate DSI/ISI to the guest. | |
334 | */ | |
335 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: | |
336 | vcpu->arch.shregs.dsisr = vcpu->arch.fault_dsisr; | |
337 | vcpu->arch.shregs.dar = vcpu->arch.fault_dar; | |
338 | kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_DATA_STORAGE, 0); | |
339 | r = RESUME_GUEST; | |
340 | break; | |
341 | case BOOK3S_INTERRUPT_H_INST_STORAGE: | |
342 | kvmppc_inject_interrupt(vcpu, BOOK3S_INTERRUPT_INST_STORAGE, | |
343 | 0x08000000); | |
344 | r = RESUME_GUEST; | |
345 | break; | |
346 | /* | |
347 | * This occurs if the guest executes an illegal instruction. | |
348 | * We just generate a program interrupt to the guest, since | |
349 | * we don't emulate any guest instructions at this stage. | |
350 | */ | |
351 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: | |
352 | kvmppc_core_queue_program(vcpu, 0x80000); | |
353 | r = RESUME_GUEST; | |
354 | break; | |
355 | default: | |
356 | kvmppc_dump_regs(vcpu); | |
357 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", | |
358 | vcpu->arch.trap, kvmppc_get_pc(vcpu), | |
359 | vcpu->arch.shregs.msr); | |
360 | r = RESUME_HOST; | |
361 | BUG(); | |
362 | break; | |
363 | } | |
364 | ||
de56a948 PM |
365 | return r; |
366 | } | |
367 | ||
368 | int kvm_arch_vcpu_ioctl_get_sregs(struct kvm_vcpu *vcpu, | |
369 | struct kvm_sregs *sregs) | |
370 | { | |
371 | int i; | |
372 | ||
373 | sregs->pvr = vcpu->arch.pvr; | |
374 | ||
375 | memset(sregs, 0, sizeof(struct kvm_sregs)); | |
376 | for (i = 0; i < vcpu->arch.slb_max; i++) { | |
377 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; | |
378 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; | |
379 | } | |
380 | ||
381 | return 0; | |
382 | } | |
383 | ||
384 | int kvm_arch_vcpu_ioctl_set_sregs(struct kvm_vcpu *vcpu, | |
385 | struct kvm_sregs *sregs) | |
386 | { | |
387 | int i, j; | |
388 | ||
389 | kvmppc_set_pvr(vcpu, sregs->pvr); | |
390 | ||
391 | j = 0; | |
392 | for (i = 0; i < vcpu->arch.slb_nr; i++) { | |
393 | if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { | |
394 | vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; | |
395 | vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; | |
396 | ++j; | |
397 | } | |
398 | } | |
399 | vcpu->arch.slb_max = j; | |
400 | ||
401 | return 0; | |
402 | } | |
403 | ||
404 | int kvmppc_core_check_processor_compat(void) | |
405 | { | |
9e368f29 | 406 | if (cpu_has_feature(CPU_FTR_HVMODE)) |
de56a948 PM |
407 | return 0; |
408 | return -EIO; | |
409 | } | |
410 | ||
411 | struct kvm_vcpu *kvmppc_core_vcpu_create(struct kvm *kvm, unsigned int id) | |
412 | { | |
413 | struct kvm_vcpu *vcpu; | |
371fefd6 PM |
414 | int err = -EINVAL; |
415 | int core; | |
416 | struct kvmppc_vcore *vcore; | |
de56a948 | 417 | |
371fefd6 PM |
418 | core = id / threads_per_core; |
419 | if (core >= KVM_MAX_VCORES) | |
420 | goto out; | |
421 | ||
422 | err = -ENOMEM; | |
de56a948 PM |
423 | vcpu = kzalloc(sizeof(struct kvm_vcpu), GFP_KERNEL); |
424 | if (!vcpu) | |
425 | goto out; | |
426 | ||
427 | err = kvm_vcpu_init(vcpu, kvm, id); | |
428 | if (err) | |
429 | goto free_vcpu; | |
430 | ||
431 | vcpu->arch.shared = &vcpu->arch.shregs; | |
432 | vcpu->arch.last_cpu = -1; | |
433 | vcpu->arch.mmcr[0] = MMCR0_FC; | |
434 | vcpu->arch.ctrl = CTRL_RUNLATCH; | |
435 | /* default to host PVR, since we can't spoof it */ | |
436 | vcpu->arch.pvr = mfspr(SPRN_PVR); | |
437 | kvmppc_set_pvr(vcpu, vcpu->arch.pvr); | |
438 | ||
de56a948 PM |
439 | kvmppc_mmu_book3s_hv_init(vcpu); |
440 | ||
371fefd6 | 441 | /* |
19ccb76a | 442 | * We consider the vcpu stopped until we see the first run ioctl for it. |
371fefd6 | 443 | */ |
19ccb76a | 444 | vcpu->arch.state = KVMPPC_VCPU_STOPPED; |
371fefd6 PM |
445 | |
446 | init_waitqueue_head(&vcpu->arch.cpu_run); | |
447 | ||
448 | mutex_lock(&kvm->lock); | |
449 | vcore = kvm->arch.vcores[core]; | |
450 | if (!vcore) { | |
451 | vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); | |
452 | if (vcore) { | |
453 | INIT_LIST_HEAD(&vcore->runnable_threads); | |
454 | spin_lock_init(&vcore->lock); | |
19ccb76a | 455 | init_waitqueue_head(&vcore->wq); |
371fefd6 PM |
456 | } |
457 | kvm->arch.vcores[core] = vcore; | |
458 | } | |
459 | mutex_unlock(&kvm->lock); | |
460 | ||
461 | if (!vcore) | |
462 | goto free_vcpu; | |
463 | ||
464 | spin_lock(&vcore->lock); | |
465 | ++vcore->num_threads; | |
371fefd6 PM |
466 | spin_unlock(&vcore->lock); |
467 | vcpu->arch.vcore = vcore; | |
468 | ||
af8f38b3 AG |
469 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
470 | kvmppc_sanity_check(vcpu); | |
471 | ||
de56a948 PM |
472 | return vcpu; |
473 | ||
474 | free_vcpu: | |
475 | kfree(vcpu); | |
476 | out: | |
477 | return ERR_PTR(err); | |
478 | } | |
479 | ||
480 | void kvmppc_core_vcpu_free(struct kvm_vcpu *vcpu) | |
481 | { | |
93e60249 PM |
482 | if (vcpu->arch.dtl) |
483 | kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.dtl); | |
484 | if (vcpu->arch.slb_shadow) | |
485 | kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.slb_shadow); | |
486 | if (vcpu->arch.vpa) | |
487 | kvmppc_unpin_guest_page(vcpu->kvm, vcpu->arch.vpa); | |
de56a948 PM |
488 | kvm_vcpu_uninit(vcpu); |
489 | kfree(vcpu); | |
490 | } | |
491 | ||
19ccb76a | 492 | static void kvmppc_set_timer(struct kvm_vcpu *vcpu) |
371fefd6 | 493 | { |
19ccb76a | 494 | unsigned long dec_nsec, now; |
371fefd6 | 495 | |
19ccb76a PM |
496 | now = get_tb(); |
497 | if (now > vcpu->arch.dec_expires) { | |
498 | /* decrementer has already gone negative */ | |
499 | kvmppc_core_queue_dec(vcpu); | |
7e28e60e | 500 | kvmppc_core_prepare_to_enter(vcpu); |
19ccb76a | 501 | return; |
371fefd6 | 502 | } |
19ccb76a PM |
503 | dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC |
504 | / tb_ticks_per_sec; | |
505 | hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec), | |
506 | HRTIMER_MODE_REL); | |
507 | vcpu->arch.timer_running = 1; | |
371fefd6 PM |
508 | } |
509 | ||
19ccb76a | 510 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu) |
371fefd6 | 511 | { |
19ccb76a PM |
512 | vcpu->arch.ceded = 0; |
513 | if (vcpu->arch.timer_running) { | |
514 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
515 | vcpu->arch.timer_running = 0; | |
516 | } | |
371fefd6 PM |
517 | } |
518 | ||
de56a948 | 519 | extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu); |
371fefd6 | 520 | extern void xics_wake_cpu(int cpu); |
de56a948 | 521 | |
371fefd6 PM |
522 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
523 | struct kvm_vcpu *vcpu) | |
de56a948 | 524 | { |
371fefd6 | 525 | struct kvm_vcpu *v; |
de56a948 | 526 | |
371fefd6 PM |
527 | if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
528 | return; | |
529 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; | |
530 | --vc->n_runnable; | |
19ccb76a | 531 | ++vc->n_busy; |
371fefd6 PM |
532 | /* decrement the physical thread id of each following vcpu */ |
533 | v = vcpu; | |
534 | list_for_each_entry_continue(v, &vc->runnable_threads, arch.run_list) | |
535 | --v->arch.ptid; | |
536 | list_del(&vcpu->arch.run_list); | |
537 | } | |
538 | ||
539 | static void kvmppc_start_thread(struct kvm_vcpu *vcpu) | |
540 | { | |
541 | int cpu; | |
542 | struct paca_struct *tpaca; | |
543 | struct kvmppc_vcore *vc = vcpu->arch.vcore; | |
544 | ||
19ccb76a PM |
545 | if (vcpu->arch.timer_running) { |
546 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
547 | vcpu->arch.timer_running = 0; | |
548 | } | |
371fefd6 PM |
549 | cpu = vc->pcpu + vcpu->arch.ptid; |
550 | tpaca = &paca[cpu]; | |
551 | tpaca->kvm_hstate.kvm_vcpu = vcpu; | |
552 | tpaca->kvm_hstate.kvm_vcore = vc; | |
19ccb76a PM |
553 | tpaca->kvm_hstate.napping = 0; |
554 | vcpu->cpu = vc->pcpu; | |
371fefd6 | 555 | smp_wmb(); |
251da038 | 556 | #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) |
371fefd6 PM |
557 | if (vcpu->arch.ptid) { |
558 | tpaca->cpu_start = 0x80; | |
371fefd6 PM |
559 | wmb(); |
560 | xics_wake_cpu(cpu); | |
561 | ++vc->n_woken; | |
de56a948 | 562 | } |
371fefd6 PM |
563 | #endif |
564 | } | |
de56a948 | 565 | |
371fefd6 PM |
566 | static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc) |
567 | { | |
568 | int i; | |
569 | ||
570 | HMT_low(); | |
571 | i = 0; | |
572 | while (vc->nap_count < vc->n_woken) { | |
573 | if (++i >= 1000000) { | |
574 | pr_err("kvmppc_wait_for_nap timeout %d %d\n", | |
575 | vc->nap_count, vc->n_woken); | |
576 | break; | |
577 | } | |
578 | cpu_relax(); | |
579 | } | |
580 | HMT_medium(); | |
581 | } | |
582 | ||
583 | /* | |
584 | * Check that we are on thread 0 and that any other threads in | |
585 | * this core are off-line. | |
586 | */ | |
587 | static int on_primary_thread(void) | |
588 | { | |
589 | int cpu = smp_processor_id(); | |
590 | int thr = cpu_thread_in_core(cpu); | |
591 | ||
592 | if (thr) | |
593 | return 0; | |
594 | while (++thr < threads_per_core) | |
595 | if (cpu_online(cpu + thr)) | |
596 | return 0; | |
597 | return 1; | |
598 | } | |
599 | ||
600 | /* | |
601 | * Run a set of guest threads on a physical core. | |
602 | * Called with vc->lock held. | |
603 | */ | |
604 | static int kvmppc_run_core(struct kvmppc_vcore *vc) | |
605 | { | |
19ccb76a | 606 | struct kvm_vcpu *vcpu, *vcpu0, *vnext; |
371fefd6 PM |
607 | long ret; |
608 | u64 now; | |
19ccb76a | 609 | int ptid; |
371fefd6 PM |
610 | |
611 | /* don't start if any threads have a signal pending */ | |
612 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) | |
613 | if (signal_pending(vcpu->arch.run_task)) | |
614 | return 0; | |
de56a948 PM |
615 | |
616 | /* | |
617 | * Make sure we are running on thread 0, and that | |
618 | * secondary threads are offline. | |
619 | * XXX we should also block attempts to bring any | |
620 | * secondary threads online. | |
621 | */ | |
371fefd6 PM |
622 | if (threads_per_core > 1 && !on_primary_thread()) { |
623 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) | |
624 | vcpu->arch.ret = -EBUSY; | |
625 | goto out; | |
de56a948 PM |
626 | } |
627 | ||
19ccb76a PM |
628 | /* |
629 | * Assign physical thread IDs, first to non-ceded vcpus | |
630 | * and then to ceded ones. | |
631 | */ | |
632 | ptid = 0; | |
633 | vcpu0 = NULL; | |
634 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { | |
635 | if (!vcpu->arch.ceded) { | |
636 | if (!ptid) | |
637 | vcpu0 = vcpu; | |
638 | vcpu->arch.ptid = ptid++; | |
639 | } | |
640 | } | |
641 | if (!vcpu0) | |
642 | return 0; /* nothing to run */ | |
643 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) | |
644 | if (vcpu->arch.ceded) | |
645 | vcpu->arch.ptid = ptid++; | |
646 | ||
371fefd6 PM |
647 | vc->n_woken = 0; |
648 | vc->nap_count = 0; | |
649 | vc->entry_exit_count = 0; | |
19ccb76a | 650 | vc->vcore_state = VCORE_RUNNING; |
371fefd6 PM |
651 | vc->in_guest = 0; |
652 | vc->pcpu = smp_processor_id(); | |
19ccb76a | 653 | vc->napping_threads = 0; |
371fefd6 PM |
654 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) |
655 | kvmppc_start_thread(vcpu); | |
371fefd6 | 656 | |
19ccb76a | 657 | preempt_disable(); |
371fefd6 | 658 | spin_unlock(&vc->lock); |
de56a948 | 659 | |
371fefd6 | 660 | kvm_guest_enter(); |
19ccb76a | 661 | __kvmppc_vcore_entry(NULL, vcpu0); |
de56a948 | 662 | |
371fefd6 | 663 | spin_lock(&vc->lock); |
19ccb76a PM |
664 | /* disable sending of IPIs on virtual external irqs */ |
665 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) | |
666 | vcpu->cpu = -1; | |
667 | /* wait for secondary threads to finish writing their state to memory */ | |
371fefd6 PM |
668 | if (vc->nap_count < vc->n_woken) |
669 | kvmppc_wait_for_nap(vc); | |
670 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ | |
19ccb76a | 671 | vc->vcore_state = VCORE_EXITING; |
371fefd6 PM |
672 | spin_unlock(&vc->lock); |
673 | ||
674 | /* make sure updates to secondary vcpu structs are visible now */ | |
675 | smp_mb(); | |
de56a948 PM |
676 | kvm_guest_exit(); |
677 | ||
678 | preempt_enable(); | |
679 | kvm_resched(vcpu); | |
680 | ||
681 | now = get_tb(); | |
371fefd6 PM |
682 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
683 | /* cancel pending dec exception if dec is positive */ | |
684 | if (now < vcpu->arch.dec_expires && | |
685 | kvmppc_core_pending_dec(vcpu)) | |
686 | kvmppc_core_dequeue_dec(vcpu); | |
19ccb76a PM |
687 | |
688 | ret = RESUME_GUEST; | |
689 | if (vcpu->arch.trap) | |
690 | ret = kvmppc_handle_exit(vcpu->arch.kvm_run, vcpu, | |
691 | vcpu->arch.run_task); | |
692 | ||
371fefd6 PM |
693 | vcpu->arch.ret = ret; |
694 | vcpu->arch.trap = 0; | |
19ccb76a PM |
695 | |
696 | if (vcpu->arch.ceded) { | |
697 | if (ret != RESUME_GUEST) | |
698 | kvmppc_end_cede(vcpu); | |
699 | else | |
700 | kvmppc_set_timer(vcpu); | |
701 | } | |
371fefd6 | 702 | } |
de56a948 | 703 | |
371fefd6 | 704 | spin_lock(&vc->lock); |
de56a948 | 705 | out: |
19ccb76a | 706 | vc->vcore_state = VCORE_INACTIVE; |
371fefd6 PM |
707 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, |
708 | arch.run_list) { | |
709 | if (vcpu->arch.ret != RESUME_GUEST) { | |
710 | kvmppc_remove_runnable(vc, vcpu); | |
711 | wake_up(&vcpu->arch.cpu_run); | |
712 | } | |
713 | } | |
714 | ||
715 | return 1; | |
716 | } | |
717 | ||
19ccb76a PM |
718 | /* |
719 | * Wait for some other vcpu thread to execute us, and | |
720 | * wake us up when we need to handle something in the host. | |
721 | */ | |
722 | static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state) | |
371fefd6 | 723 | { |
371fefd6 PM |
724 | DEFINE_WAIT(wait); |
725 | ||
19ccb76a PM |
726 | prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); |
727 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) | |
728 | schedule(); | |
729 | finish_wait(&vcpu->arch.cpu_run, &wait); | |
730 | } | |
731 | ||
732 | /* | |
733 | * All the vcpus in this vcore are idle, so wait for a decrementer | |
734 | * or external interrupt to one of the vcpus. vc->lock is held. | |
735 | */ | |
736 | static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) | |
737 | { | |
738 | DEFINE_WAIT(wait); | |
739 | struct kvm_vcpu *v; | |
740 | int all_idle = 1; | |
741 | ||
742 | prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE); | |
743 | vc->vcore_state = VCORE_SLEEPING; | |
744 | spin_unlock(&vc->lock); | |
745 | list_for_each_entry(v, &vc->runnable_threads, arch.run_list) { | |
746 | if (!v->arch.ceded || v->arch.pending_exceptions) { | |
747 | all_idle = 0; | |
748 | break; | |
749 | } | |
371fefd6 | 750 | } |
19ccb76a PM |
751 | if (all_idle) |
752 | schedule(); | |
753 | finish_wait(&vc->wq, &wait); | |
754 | spin_lock(&vc->lock); | |
755 | vc->vcore_state = VCORE_INACTIVE; | |
756 | } | |
371fefd6 | 757 | |
19ccb76a PM |
758 | static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
759 | { | |
760 | int n_ceded; | |
761 | int prev_state; | |
762 | struct kvmppc_vcore *vc; | |
763 | struct kvm_vcpu *v, *vn; | |
9e368f29 | 764 | |
371fefd6 PM |
765 | kvm_run->exit_reason = 0; |
766 | vcpu->arch.ret = RESUME_GUEST; | |
767 | vcpu->arch.trap = 0; | |
768 | ||
371fefd6 PM |
769 | /* |
770 | * Synchronize with other threads in this virtual core | |
771 | */ | |
772 | vc = vcpu->arch.vcore; | |
773 | spin_lock(&vc->lock); | |
19ccb76a | 774 | vcpu->arch.ceded = 0; |
371fefd6 PM |
775 | vcpu->arch.run_task = current; |
776 | vcpu->arch.kvm_run = kvm_run; | |
19ccb76a PM |
777 | prev_state = vcpu->arch.state; |
778 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; | |
371fefd6 PM |
779 | list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads); |
780 | ++vc->n_runnable; | |
781 | ||
19ccb76a PM |
782 | /* |
783 | * This happens the first time this is called for a vcpu. | |
784 | * If the vcore is already running, we may be able to start | |
785 | * this thread straight away and have it join in. | |
786 | */ | |
787 | if (prev_state == KVMPPC_VCPU_STOPPED) { | |
788 | if (vc->vcore_state == VCORE_RUNNING && | |
789 | VCORE_EXIT_COUNT(vc) == 0) { | |
790 | vcpu->arch.ptid = vc->n_runnable - 1; | |
791 | kvmppc_start_thread(vcpu); | |
371fefd6 PM |
792 | } |
793 | ||
19ccb76a PM |
794 | } else if (prev_state == KVMPPC_VCPU_BUSY_IN_HOST) |
795 | --vc->n_busy; | |
371fefd6 | 796 | |
19ccb76a PM |
797 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
798 | !signal_pending(current)) { | |
799 | if (vc->n_busy || vc->vcore_state != VCORE_INACTIVE) { | |
800 | spin_unlock(&vc->lock); | |
801 | kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE); | |
802 | spin_lock(&vc->lock); | |
803 | continue; | |
804 | } | |
805 | n_ceded = 0; | |
806 | list_for_each_entry(v, &vc->runnable_threads, arch.run_list) | |
807 | n_ceded += v->arch.ceded; | |
808 | if (n_ceded == vc->n_runnable) | |
809 | kvmppc_vcore_blocked(vc); | |
810 | else | |
811 | kvmppc_run_core(vc); | |
812 | ||
813 | list_for_each_entry_safe(v, vn, &vc->runnable_threads, | |
814 | arch.run_list) { | |
7e28e60e | 815 | kvmppc_core_prepare_to_enter(v); |
19ccb76a PM |
816 | if (signal_pending(v->arch.run_task)) { |
817 | kvmppc_remove_runnable(vc, v); | |
818 | v->stat.signal_exits++; | |
819 | v->arch.kvm_run->exit_reason = KVM_EXIT_INTR; | |
820 | v->arch.ret = -EINTR; | |
821 | wake_up(&v->arch.cpu_run); | |
822 | } | |
823 | } | |
824 | } | |
371fefd6 | 825 | |
19ccb76a PM |
826 | if (signal_pending(current)) { |
827 | if (vc->vcore_state == VCORE_RUNNING || | |
828 | vc->vcore_state == VCORE_EXITING) { | |
829 | spin_unlock(&vc->lock); | |
830 | kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE); | |
831 | spin_lock(&vc->lock); | |
832 | } | |
833 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { | |
834 | kvmppc_remove_runnable(vc, vcpu); | |
835 | vcpu->stat.signal_exits++; | |
836 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
837 | vcpu->arch.ret = -EINTR; | |
838 | } | |
371fefd6 PM |
839 | } |
840 | ||
371fefd6 | 841 | spin_unlock(&vc->lock); |
371fefd6 | 842 | return vcpu->arch.ret; |
de56a948 PM |
843 | } |
844 | ||
a8606e20 PM |
845 | int kvmppc_vcpu_run(struct kvm_run *run, struct kvm_vcpu *vcpu) |
846 | { | |
847 | int r; | |
848 | ||
af8f38b3 AG |
849 | if (!vcpu->arch.sane) { |
850 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
851 | return -EINVAL; | |
852 | } | |
853 | ||
25051b5a SW |
854 | kvmppc_core_prepare_to_enter(vcpu); |
855 | ||
19ccb76a PM |
856 | /* No need to go into the guest when all we'll do is come back out */ |
857 | if (signal_pending(current)) { | |
858 | run->exit_reason = KVM_EXIT_INTR; | |
859 | return -EINTR; | |
860 | } | |
861 | ||
c77162de PM |
862 | /* On the first time here, set up VRMA or RMA */ |
863 | if (!vcpu->kvm->arch.rma_setup_done) { | |
864 | r = kvmppc_hv_setup_rma(vcpu); | |
865 | if (r) | |
866 | return r; | |
867 | } | |
19ccb76a PM |
868 | |
869 | flush_fp_to_thread(current); | |
870 | flush_altivec_to_thread(current); | |
871 | flush_vsx_to_thread(current); | |
872 | vcpu->arch.wqp = &vcpu->arch.vcore->wq; | |
873 | ||
a8606e20 PM |
874 | do { |
875 | r = kvmppc_run_vcpu(run, vcpu); | |
876 | ||
877 | if (run->exit_reason == KVM_EXIT_PAPR_HCALL && | |
878 | !(vcpu->arch.shregs.msr & MSR_PR)) { | |
879 | r = kvmppc_pseries_do_hcall(vcpu); | |
7e28e60e | 880 | kvmppc_core_prepare_to_enter(vcpu); |
a8606e20 PM |
881 | } |
882 | } while (r == RESUME_GUEST); | |
883 | return r; | |
884 | } | |
885 | ||
54738c09 DG |
886 | static long kvmppc_stt_npages(unsigned long window_size) |
887 | { | |
888 | return ALIGN((window_size >> SPAPR_TCE_SHIFT) | |
889 | * sizeof(u64), PAGE_SIZE) / PAGE_SIZE; | |
890 | } | |
891 | ||
892 | static void release_spapr_tce_table(struct kvmppc_spapr_tce_table *stt) | |
893 | { | |
894 | struct kvm *kvm = stt->kvm; | |
895 | int i; | |
896 | ||
897 | mutex_lock(&kvm->lock); | |
898 | list_del(&stt->list); | |
899 | for (i = 0; i < kvmppc_stt_npages(stt->window_size); i++) | |
900 | __free_page(stt->pages[i]); | |
901 | kfree(stt); | |
902 | mutex_unlock(&kvm->lock); | |
903 | ||
904 | kvm_put_kvm(kvm); | |
905 | } | |
906 | ||
907 | static int kvm_spapr_tce_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
908 | { | |
909 | struct kvmppc_spapr_tce_table *stt = vma->vm_file->private_data; | |
910 | struct page *page; | |
911 | ||
912 | if (vmf->pgoff >= kvmppc_stt_npages(stt->window_size)) | |
913 | return VM_FAULT_SIGBUS; | |
914 | ||
915 | page = stt->pages[vmf->pgoff]; | |
916 | get_page(page); | |
917 | vmf->page = page; | |
918 | return 0; | |
919 | } | |
920 | ||
921 | static const struct vm_operations_struct kvm_spapr_tce_vm_ops = { | |
922 | .fault = kvm_spapr_tce_fault, | |
923 | }; | |
924 | ||
925 | static int kvm_spapr_tce_mmap(struct file *file, struct vm_area_struct *vma) | |
926 | { | |
927 | vma->vm_ops = &kvm_spapr_tce_vm_ops; | |
928 | return 0; | |
929 | } | |
930 | ||
931 | static int kvm_spapr_tce_release(struct inode *inode, struct file *filp) | |
932 | { | |
933 | struct kvmppc_spapr_tce_table *stt = filp->private_data; | |
934 | ||
935 | release_spapr_tce_table(stt); | |
936 | return 0; | |
937 | } | |
938 | ||
939 | static struct file_operations kvm_spapr_tce_fops = { | |
940 | .mmap = kvm_spapr_tce_mmap, | |
941 | .release = kvm_spapr_tce_release, | |
942 | }; | |
943 | ||
944 | long kvm_vm_ioctl_create_spapr_tce(struct kvm *kvm, | |
945 | struct kvm_create_spapr_tce *args) | |
946 | { | |
947 | struct kvmppc_spapr_tce_table *stt = NULL; | |
948 | long npages; | |
949 | int ret = -ENOMEM; | |
950 | int i; | |
951 | ||
952 | /* Check this LIOBN hasn't been previously allocated */ | |
953 | list_for_each_entry(stt, &kvm->arch.spapr_tce_tables, list) { | |
954 | if (stt->liobn == args->liobn) | |
955 | return -EBUSY; | |
956 | } | |
957 | ||
958 | npages = kvmppc_stt_npages(args->window_size); | |
959 | ||
960 | stt = kzalloc(sizeof(*stt) + npages* sizeof(struct page *), | |
961 | GFP_KERNEL); | |
962 | if (!stt) | |
963 | goto fail; | |
964 | ||
965 | stt->liobn = args->liobn; | |
966 | stt->window_size = args->window_size; | |
967 | stt->kvm = kvm; | |
968 | ||
969 | for (i = 0; i < npages; i++) { | |
970 | stt->pages[i] = alloc_page(GFP_KERNEL | __GFP_ZERO); | |
971 | if (!stt->pages[i]) | |
972 | goto fail; | |
973 | } | |
974 | ||
975 | kvm_get_kvm(kvm); | |
976 | ||
977 | mutex_lock(&kvm->lock); | |
978 | list_add(&stt->list, &kvm->arch.spapr_tce_tables); | |
979 | ||
980 | mutex_unlock(&kvm->lock); | |
981 | ||
982 | return anon_inode_getfd("kvm-spapr-tce", &kvm_spapr_tce_fops, | |
983 | stt, O_RDWR); | |
984 | ||
985 | fail: | |
986 | if (stt) { | |
987 | for (i = 0; i < npages; i++) | |
988 | if (stt->pages[i]) | |
989 | __free_page(stt->pages[i]); | |
990 | ||
991 | kfree(stt); | |
992 | } | |
993 | return ret; | |
994 | } | |
995 | ||
aa04b4cc | 996 | /* Work out RMLS (real mode limit selector) field value for a given RMA size. |
9e368f29 | 997 | Assumes POWER7 or PPC970. */ |
aa04b4cc PM |
998 | static inline int lpcr_rmls(unsigned long rma_size) |
999 | { | |
1000 | switch (rma_size) { | |
1001 | case 32ul << 20: /* 32 MB */ | |
9e368f29 PM |
1002 | if (cpu_has_feature(CPU_FTR_ARCH_206)) |
1003 | return 8; /* only supported on POWER7 */ | |
1004 | return -1; | |
aa04b4cc PM |
1005 | case 64ul << 20: /* 64 MB */ |
1006 | return 3; | |
1007 | case 128ul << 20: /* 128 MB */ | |
1008 | return 7; | |
1009 | case 256ul << 20: /* 256 MB */ | |
1010 | return 4; | |
1011 | case 1ul << 30: /* 1 GB */ | |
1012 | return 2; | |
1013 | case 16ul << 30: /* 16 GB */ | |
1014 | return 1; | |
1015 | case 256ul << 30: /* 256 GB */ | |
1016 | return 0; | |
1017 | default: | |
1018 | return -1; | |
1019 | } | |
1020 | } | |
1021 | ||
1022 | static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1023 | { | |
1024 | struct kvmppc_rma_info *ri = vma->vm_file->private_data; | |
1025 | struct page *page; | |
1026 | ||
1027 | if (vmf->pgoff >= ri->npages) | |
1028 | return VM_FAULT_SIGBUS; | |
1029 | ||
1030 | page = pfn_to_page(ri->base_pfn + vmf->pgoff); | |
1031 | get_page(page); | |
1032 | vmf->page = page; | |
1033 | return 0; | |
1034 | } | |
1035 | ||
1036 | static const struct vm_operations_struct kvm_rma_vm_ops = { | |
1037 | .fault = kvm_rma_fault, | |
1038 | }; | |
1039 | ||
1040 | static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma) | |
1041 | { | |
1042 | vma->vm_flags |= VM_RESERVED; | |
1043 | vma->vm_ops = &kvm_rma_vm_ops; | |
1044 | return 0; | |
1045 | } | |
1046 | ||
1047 | static int kvm_rma_release(struct inode *inode, struct file *filp) | |
1048 | { | |
1049 | struct kvmppc_rma_info *ri = filp->private_data; | |
1050 | ||
1051 | kvm_release_rma(ri); | |
1052 | return 0; | |
1053 | } | |
1054 | ||
1055 | static struct file_operations kvm_rma_fops = { | |
1056 | .mmap = kvm_rma_mmap, | |
1057 | .release = kvm_rma_release, | |
1058 | }; | |
1059 | ||
1060 | long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, struct kvm_allocate_rma *ret) | |
1061 | { | |
1062 | struct kvmppc_rma_info *ri; | |
1063 | long fd; | |
1064 | ||
1065 | ri = kvm_alloc_rma(); | |
1066 | if (!ri) | |
1067 | return -ENOMEM; | |
1068 | ||
1069 | fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR); | |
1070 | if (fd < 0) | |
1071 | kvm_release_rma(ri); | |
1072 | ||
1073 | ret->rma_size = ri->npages << PAGE_SHIFT; | |
1074 | return fd; | |
1075 | } | |
1076 | ||
de56a948 PM |
1077 | int kvmppc_core_prepare_memory_region(struct kvm *kvm, |
1078 | struct kvm_userspace_memory_region *mem) | |
1079 | { | |
c77162de PM |
1080 | unsigned long psize; |
1081 | unsigned long npages; | |
b2b2f165 | 1082 | unsigned long *phys; |
aa04b4cc | 1083 | |
c77162de PM |
1084 | /* For now, only allow 16MB-aligned slots */ |
1085 | psize = kvm->arch.ram_psize; | |
aa04b4cc PM |
1086 | if ((mem->memory_size & (psize - 1)) || |
1087 | (mem->guest_phys_addr & (psize - 1))) { | |
1088 | pr_err("bad memory_size=%llx @ %llx\n", | |
1089 | mem->memory_size, mem->guest_phys_addr); | |
1090 | return -EINVAL; | |
1091 | } | |
1092 | ||
b2b2f165 | 1093 | /* Allocate a slot_phys array */ |
c77162de | 1094 | npages = mem->memory_size >> kvm->arch.ram_porder; |
b2b2f165 PM |
1095 | phys = kvm->arch.slot_phys[mem->slot]; |
1096 | if (!phys) { | |
1097 | phys = vzalloc(npages * sizeof(unsigned long)); | |
1098 | if (!phys) | |
1099 | return -ENOMEM; | |
1100 | kvm->arch.slot_phys[mem->slot] = phys; | |
1101 | kvm->arch.slot_npages[mem->slot] = npages; | |
1102 | } | |
aa04b4cc | 1103 | |
c77162de PM |
1104 | return 0; |
1105 | } | |
aa04b4cc | 1106 | |
c77162de PM |
1107 | static void unpin_slot(struct kvm *kvm, int slot_id) |
1108 | { | |
1109 | unsigned long *physp; | |
1110 | unsigned long j, npages, pfn; | |
1111 | struct page *page; | |
1112 | ||
1113 | physp = kvm->arch.slot_phys[slot_id]; | |
1114 | npages = kvm->arch.slot_npages[slot_id]; | |
1115 | if (physp) { | |
1116 | spin_lock(&kvm->arch.slot_phys_lock); | |
1117 | for (j = 0; j < npages; j++) { | |
1118 | if (!(physp[j] & KVMPPC_GOT_PAGE)) | |
1119 | continue; | |
1120 | pfn = physp[j] >> PAGE_SHIFT; | |
1121 | page = pfn_to_page(pfn); | |
1122 | SetPageDirty(page); | |
1123 | put_page(page); | |
9e368f29 | 1124 | } |
c77162de PM |
1125 | kvm->arch.slot_phys[slot_id] = NULL; |
1126 | spin_unlock(&kvm->arch.slot_phys_lock); | |
1127 | vfree(physp); | |
aa04b4cc | 1128 | } |
c77162de PM |
1129 | } |
1130 | ||
1131 | void kvmppc_core_commit_memory_region(struct kvm *kvm, | |
1132 | struct kvm_userspace_memory_region *mem) | |
1133 | { | |
1134 | } | |
1135 | ||
1136 | static int kvmppc_hv_setup_rma(struct kvm_vcpu *vcpu) | |
1137 | { | |
1138 | int err = 0; | |
1139 | struct kvm *kvm = vcpu->kvm; | |
1140 | struct kvmppc_rma_info *ri = NULL; | |
1141 | unsigned long hva; | |
1142 | struct kvm_memory_slot *memslot; | |
1143 | struct vm_area_struct *vma; | |
1144 | unsigned long lpcr; | |
1145 | unsigned long psize, porder; | |
1146 | unsigned long rma_size; | |
1147 | unsigned long rmls; | |
1148 | unsigned long *physp; | |
1149 | unsigned long i, npages, pa; | |
1150 | ||
1151 | mutex_lock(&kvm->lock); | |
1152 | if (kvm->arch.rma_setup_done) | |
1153 | goto out; /* another vcpu beat us to it */ | |
aa04b4cc | 1154 | |
c77162de PM |
1155 | /* Look up the memslot for guest physical address 0 */ |
1156 | memslot = gfn_to_memslot(kvm, 0); | |
aa04b4cc | 1157 | |
c77162de PM |
1158 | /* We must have some memory at 0 by now */ |
1159 | err = -EINVAL; | |
1160 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
1161 | goto out; | |
1162 | ||
1163 | /* Look up the VMA for the start of this memory slot */ | |
1164 | hva = memslot->userspace_addr; | |
1165 | down_read(¤t->mm->mmap_sem); | |
1166 | vma = find_vma(current->mm, hva); | |
1167 | if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO)) | |
1168 | goto up_out; | |
1169 | ||
1170 | psize = vma_kernel_pagesize(vma); | |
1171 | if (psize != kvm->arch.ram_psize) | |
1172 | goto up_out; | |
1173 | ||
1174 | /* Is this one of our preallocated RMAs? */ | |
1175 | if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops && | |
1176 | hva == vma->vm_start) | |
1177 | ri = vma->vm_file->private_data; | |
1178 | ||
1179 | up_read(¤t->mm->mmap_sem); | |
1180 | ||
1181 | if (!ri) { | |
1182 | /* On POWER7, use VRMA; on PPC970, give up */ | |
1183 | err = -EPERM; | |
1184 | if (cpu_has_feature(CPU_FTR_ARCH_201)) { | |
1185 | pr_err("KVM: CPU requires an RMO\n"); | |
1186 | goto out; | |
1187 | } | |
1188 | ||
1189 | /* Update VRMASD field in the LPCR */ | |
1190 | lpcr = kvm->arch.lpcr & ~(0x1fUL << LPCR_VRMASD_SH); | |
1191 | lpcr |= LPCR_VRMA_L; | |
1192 | kvm->arch.lpcr = lpcr; | |
1193 | ||
1194 | /* Create HPTEs in the hash page table for the VRMA */ | |
1195 | kvmppc_map_vrma(vcpu, memslot); | |
1196 | ||
1197 | } else { | |
1198 | /* Set up to use an RMO region */ | |
1199 | rma_size = ri->npages; | |
1200 | if (rma_size > memslot->npages) | |
1201 | rma_size = memslot->npages; | |
1202 | rma_size <<= PAGE_SHIFT; | |
aa04b4cc | 1203 | rmls = lpcr_rmls(rma_size); |
c77162de | 1204 | err = -EINVAL; |
aa04b4cc | 1205 | if (rmls < 0) { |
c77162de PM |
1206 | pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size); |
1207 | goto out; | |
aa04b4cc PM |
1208 | } |
1209 | atomic_inc(&ri->use_count); | |
1210 | kvm->arch.rma = ri; | |
9e368f29 PM |
1211 | |
1212 | /* Update LPCR and RMOR */ | |
1213 | lpcr = kvm->arch.lpcr; | |
1214 | if (cpu_has_feature(CPU_FTR_ARCH_201)) { | |
1215 | /* PPC970; insert RMLS value (split field) in HID4 */ | |
1216 | lpcr &= ~((1ul << HID4_RMLS0_SH) | | |
1217 | (3ul << HID4_RMLS2_SH)); | |
1218 | lpcr |= ((rmls >> 2) << HID4_RMLS0_SH) | | |
1219 | ((rmls & 3) << HID4_RMLS2_SH); | |
1220 | /* RMOR is also in HID4 */ | |
1221 | lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff) | |
1222 | << HID4_RMOR_SH; | |
1223 | } else { | |
1224 | /* POWER7 */ | |
1225 | lpcr &= ~(LPCR_VPM0 | LPCR_VRMA_L); | |
1226 | lpcr |= rmls << LPCR_RMLS_SH; | |
1227 | kvm->arch.rmor = kvm->arch.rma->base_pfn << PAGE_SHIFT; | |
1228 | } | |
aa04b4cc | 1229 | kvm->arch.lpcr = lpcr; |
c77162de | 1230 | pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n", |
aa04b4cc | 1231 | ri->base_pfn << PAGE_SHIFT, rma_size, lpcr); |
aa04b4cc | 1232 | |
c77162de PM |
1233 | /* Initialize phys addrs of pages in RMO */ |
1234 | porder = kvm->arch.ram_porder; | |
1235 | npages = rma_size >> porder; | |
1236 | pa = ri->base_pfn << PAGE_SHIFT; | |
1237 | physp = kvm->arch.slot_phys[memslot->id]; | |
1238 | spin_lock(&kvm->arch.slot_phys_lock); | |
1239 | for (i = 0; i < npages; ++i) | |
1240 | physp[i] = pa + (i << porder); | |
1241 | spin_unlock(&kvm->arch.slot_phys_lock); | |
aa04b4cc PM |
1242 | } |
1243 | ||
c77162de PM |
1244 | /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */ |
1245 | smp_wmb(); | |
1246 | kvm->arch.rma_setup_done = 1; | |
1247 | err = 0; | |
1248 | out: | |
1249 | mutex_unlock(&kvm->lock); | |
1250 | return err; | |
b2b2f165 | 1251 | |
c77162de PM |
1252 | up_out: |
1253 | up_read(¤t->mm->mmap_sem); | |
1254 | goto out; | |
de56a948 PM |
1255 | } |
1256 | ||
1257 | int kvmppc_core_init_vm(struct kvm *kvm) | |
1258 | { | |
1259 | long r; | |
aa04b4cc | 1260 | unsigned long lpcr; |
de56a948 PM |
1261 | |
1262 | /* Allocate hashed page table */ | |
1263 | r = kvmppc_alloc_hpt(kvm); | |
54738c09 DG |
1264 | if (r) |
1265 | return r; | |
de56a948 | 1266 | |
54738c09 | 1267 | INIT_LIST_HEAD(&kvm->arch.spapr_tce_tables); |
aa04b4cc | 1268 | |
aa04b4cc PM |
1269 | kvm->arch.ram_psize = 1ul << LARGE_PAGE_ORDER; |
1270 | kvm->arch.ram_porder = LARGE_PAGE_ORDER; | |
1271 | kvm->arch.rma = NULL; | |
aa04b4cc | 1272 | |
9e368f29 | 1273 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); |
aa04b4cc | 1274 | |
9e368f29 PM |
1275 | if (cpu_has_feature(CPU_FTR_ARCH_201)) { |
1276 | /* PPC970; HID4 is effectively the LPCR */ | |
1277 | unsigned long lpid = kvm->arch.lpid; | |
1278 | kvm->arch.host_lpid = 0; | |
1279 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4); | |
1280 | lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH)); | |
1281 | lpcr |= ((lpid >> 4) << HID4_LPID1_SH) | | |
1282 | ((lpid & 0xf) << HID4_LPID5_SH); | |
1283 | } else { | |
1284 | /* POWER7; init LPCR for virtual RMA mode */ | |
1285 | kvm->arch.host_lpid = mfspr(SPRN_LPID); | |
1286 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); | |
1287 | lpcr &= LPCR_PECE | LPCR_LPES; | |
1288 | lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | | |
1289 | LPCR_VPM0 | LPCR_VRMA_L; | |
1290 | } | |
1291 | kvm->arch.lpcr = lpcr; | |
aa04b4cc | 1292 | |
c77162de | 1293 | spin_lock_init(&kvm->arch.slot_phys_lock); |
54738c09 | 1294 | return 0; |
de56a948 PM |
1295 | } |
1296 | ||
1297 | void kvmppc_core_destroy_vm(struct kvm *kvm) | |
1298 | { | |
aa04b4cc PM |
1299 | unsigned long i; |
1300 | ||
b2b2f165 PM |
1301 | for (i = 0; i < KVM_MEM_SLOTS_NUM; i++) |
1302 | unpin_slot(kvm, i); | |
1303 | ||
aa04b4cc PM |
1304 | if (kvm->arch.rma) { |
1305 | kvm_release_rma(kvm->arch.rma); | |
1306 | kvm->arch.rma = NULL; | |
1307 | } | |
1308 | ||
de56a948 | 1309 | kvmppc_free_hpt(kvm); |
54738c09 | 1310 | WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables)); |
de56a948 PM |
1311 | } |
1312 | ||
1313 | /* These are stubs for now */ | |
1314 | void kvmppc_mmu_pte_pflush(struct kvm_vcpu *vcpu, ulong pa_start, ulong pa_end) | |
1315 | { | |
1316 | } | |
1317 | ||
1318 | /* We don't need to emulate any privileged instructions or dcbz */ | |
1319 | int kvmppc_core_emulate_op(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
1320 | unsigned int inst, int *advance) | |
1321 | { | |
1322 | return EMULATE_FAIL; | |
1323 | } | |
1324 | ||
1325 | int kvmppc_core_emulate_mtspr(struct kvm_vcpu *vcpu, int sprn, int rs) | |
1326 | { | |
1327 | return EMULATE_FAIL; | |
1328 | } | |
1329 | ||
1330 | int kvmppc_core_emulate_mfspr(struct kvm_vcpu *vcpu, int sprn, int rt) | |
1331 | { | |
1332 | return EMULATE_FAIL; | |
1333 | } | |
1334 | ||
1335 | static int kvmppc_book3s_hv_init(void) | |
1336 | { | |
1337 | int r; | |
1338 | ||
1339 | r = kvm_init(NULL, sizeof(struct kvm_vcpu), 0, THIS_MODULE); | |
1340 | ||
1341 | if (r) | |
1342 | return r; | |
1343 | ||
1344 | r = kvmppc_mmu_hv_init(); | |
1345 | ||
1346 | return r; | |
1347 | } | |
1348 | ||
1349 | static void kvmppc_book3s_hv_exit(void) | |
1350 | { | |
1351 | kvm_exit(); | |
1352 | } | |
1353 | ||
1354 | module_init(kvmppc_book3s_hv_init); | |
1355 | module_exit(kvmppc_book3s_hv_exit); |