]>
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> | |
2c9097e4 | 33 | #include <linux/srcu.h> |
de56a948 PM |
34 | |
35 | #include <asm/reg.h> | |
36 | #include <asm/cputable.h> | |
37 | #include <asm/cacheflush.h> | |
38 | #include <asm/tlbflush.h> | |
39 | #include <asm/uaccess.h> | |
40 | #include <asm/io.h> | |
41 | #include <asm/kvm_ppc.h> | |
42 | #include <asm/kvm_book3s.h> | |
43 | #include <asm/mmu_context.h> | |
44 | #include <asm/lppaca.h> | |
45 | #include <asm/processor.h> | |
371fefd6 | 46 | #include <asm/cputhreads.h> |
aa04b4cc | 47 | #include <asm/page.h> |
de1d9248 | 48 | #include <asm/hvcall.h> |
ae3a197e | 49 | #include <asm/switch_to.h> |
512691d4 | 50 | #include <asm/smp.h> |
de56a948 | 51 | #include <linux/gfp.h> |
de56a948 PM |
52 | #include <linux/vmalloc.h> |
53 | #include <linux/highmem.h> | |
c77162de | 54 | #include <linux/hugetlb.h> |
2ba9f0d8 | 55 | #include <linux/module.h> |
de56a948 | 56 | |
3a167bea AK |
57 | #include "book3s.h" |
58 | ||
de56a948 PM |
59 | /* #define EXIT_DEBUG */ |
60 | /* #define EXIT_DEBUG_SIMPLE */ | |
61 | /* #define EXIT_DEBUG_INT */ | |
62 | ||
913d3ff9 PM |
63 | /* Used to indicate that a guest page fault needs to be handled */ |
64 | #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1) | |
65 | ||
c7b67670 PM |
66 | /* Used as a "null" value for timebase values */ |
67 | #define TB_NIL (~(u64)0) | |
68 | ||
19ccb76a | 69 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu); |
32fad281 | 70 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); |
19ccb76a | 71 | |
3a167bea | 72 | static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) |
54695c30 BH |
73 | { |
74 | int me; | |
75 | int cpu = vcpu->cpu; | |
76 | wait_queue_head_t *wqp; | |
77 | ||
78 | wqp = kvm_arch_vcpu_wq(vcpu); | |
79 | if (waitqueue_active(wqp)) { | |
80 | wake_up_interruptible(wqp); | |
81 | ++vcpu->stat.halt_wakeup; | |
82 | } | |
83 | ||
84 | me = get_cpu(); | |
85 | ||
86 | /* CPU points to the first thread of the core */ | |
87 | if (cpu != me && cpu >= 0 && cpu < nr_cpu_ids) { | |
88 | int real_cpu = cpu + vcpu->arch.ptid; | |
89 | if (paca[real_cpu].kvm_hstate.xics_phys) | |
90 | xics_wake_cpu(real_cpu); | |
91 | else if (cpu_online(cpu)) | |
92 | smp_send_reschedule(cpu); | |
93 | } | |
94 | put_cpu(); | |
95 | } | |
96 | ||
c7b67670 PM |
97 | /* |
98 | * We use the vcpu_load/put functions to measure stolen time. | |
99 | * Stolen time is counted as time when either the vcpu is able to | |
100 | * run as part of a virtual core, but the task running the vcore | |
101 | * is preempted or sleeping, or when the vcpu needs something done | |
102 | * in the kernel by the task running the vcpu, but that task is | |
103 | * preempted or sleeping. Those two things have to be counted | |
104 | * separately, since one of the vcpu tasks will take on the job | |
105 | * of running the core, and the other vcpu tasks in the vcore will | |
106 | * sleep waiting for it to do that, but that sleep shouldn't count | |
107 | * as stolen time. | |
108 | * | |
109 | * Hence we accumulate stolen time when the vcpu can run as part of | |
110 | * a vcore using vc->stolen_tb, and the stolen time when the vcpu | |
111 | * needs its task to do other things in the kernel (for example, | |
112 | * service a page fault) in busy_stolen. We don't accumulate | |
113 | * stolen time for a vcore when it is inactive, or for a vcpu | |
114 | * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of | |
115 | * a misnomer; it means that the vcpu task is not executing in | |
116 | * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in | |
117 | * the kernel. We don't have any way of dividing up that time | |
118 | * between time that the vcpu is genuinely stopped, time that | |
119 | * the task is actively working on behalf of the vcpu, and time | |
120 | * that the task is preempted, so we don't count any of it as | |
121 | * stolen. | |
122 | * | |
123 | * Updates to busy_stolen are protected by arch.tbacct_lock; | |
124 | * updates to vc->stolen_tb are protected by the arch.tbacct_lock | |
125 | * of the vcpu that has taken responsibility for running the vcore | |
126 | * (i.e. vc->runner). The stolen times are measured in units of | |
127 | * timebase ticks. (Note that the != TB_NIL checks below are | |
128 | * purely defensive; they should never fail.) | |
129 | */ | |
130 | ||
3a167bea | 131 | static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu) |
de56a948 | 132 | { |
0456ec4f | 133 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
bf3d32e1 | 134 | unsigned long flags; |
0456ec4f | 135 | |
bf3d32e1 | 136 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
c7b67670 PM |
137 | if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE && |
138 | vc->preempt_tb != TB_NIL) { | |
0456ec4f | 139 | vc->stolen_tb += mftb() - vc->preempt_tb; |
c7b67670 PM |
140 | vc->preempt_tb = TB_NIL; |
141 | } | |
142 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST && | |
143 | vcpu->arch.busy_preempt != TB_NIL) { | |
144 | vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt; | |
145 | vcpu->arch.busy_preempt = TB_NIL; | |
146 | } | |
bf3d32e1 | 147 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
de56a948 PM |
148 | } |
149 | ||
3a167bea | 150 | static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu) |
de56a948 | 151 | { |
0456ec4f | 152 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
bf3d32e1 | 153 | unsigned long flags; |
0456ec4f | 154 | |
bf3d32e1 | 155 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
0456ec4f PM |
156 | if (vc->runner == vcpu && vc->vcore_state != VCORE_INACTIVE) |
157 | vc->preempt_tb = mftb(); | |
c7b67670 PM |
158 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) |
159 | vcpu->arch.busy_preempt = mftb(); | |
bf3d32e1 | 160 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
de56a948 PM |
161 | } |
162 | ||
3a167bea | 163 | static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr) |
de56a948 PM |
164 | { |
165 | vcpu->arch.shregs.msr = msr; | |
19ccb76a | 166 | kvmppc_end_cede(vcpu); |
de56a948 PM |
167 | } |
168 | ||
3a167bea | 169 | void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr) |
de56a948 PM |
170 | { |
171 | vcpu->arch.pvr = pvr; | |
172 | } | |
173 | ||
388cc6e1 PM |
174 | int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) |
175 | { | |
176 | unsigned long pcr = 0; | |
177 | struct kvmppc_vcore *vc = vcpu->arch.vcore; | |
178 | ||
179 | if (arch_compat) { | |
180 | if (!cpu_has_feature(CPU_FTR_ARCH_206)) | |
181 | return -EINVAL; /* 970 has no compat mode support */ | |
182 | ||
183 | switch (arch_compat) { | |
184 | case PVR_ARCH_205: | |
185 | pcr = PCR_ARCH_205; | |
186 | break; | |
187 | case PVR_ARCH_206: | |
188 | case PVR_ARCH_206p: | |
189 | break; | |
190 | default: | |
191 | return -EINVAL; | |
192 | } | |
193 | } | |
194 | ||
195 | spin_lock(&vc->lock); | |
196 | vc->arch_compat = arch_compat; | |
197 | vc->pcr = pcr; | |
198 | spin_unlock(&vc->lock); | |
199 | ||
200 | return 0; | |
201 | } | |
202 | ||
de56a948 PM |
203 | void kvmppc_dump_regs(struct kvm_vcpu *vcpu) |
204 | { | |
205 | int r; | |
206 | ||
207 | pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); | |
208 | pr_err("pc = %.16lx msr = %.16llx trap = %x\n", | |
209 | vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap); | |
210 | for (r = 0; r < 16; ++r) | |
211 | pr_err("r%2d = %.16lx r%d = %.16lx\n", | |
212 | r, kvmppc_get_gpr(vcpu, r), | |
213 | r+16, kvmppc_get_gpr(vcpu, r+16)); | |
214 | pr_err("ctr = %.16lx lr = %.16lx\n", | |
215 | vcpu->arch.ctr, vcpu->arch.lr); | |
216 | pr_err("srr0 = %.16llx srr1 = %.16llx\n", | |
217 | vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); | |
218 | pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", | |
219 | vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); | |
220 | pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", | |
221 | vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); | |
222 | pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n", | |
223 | vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr); | |
224 | pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); | |
225 | pr_err("fault dar = %.16lx dsisr = %.8x\n", | |
226 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); | |
227 | pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); | |
228 | for (r = 0; r < vcpu->arch.slb_max; ++r) | |
229 | pr_err(" ESID = %.16llx VSID = %.16llx\n", | |
230 | vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); | |
231 | pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n", | |
a0144e2a | 232 | vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1, |
de56a948 PM |
233 | vcpu->arch.last_inst); |
234 | } | |
235 | ||
a8606e20 PM |
236 | struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) |
237 | { | |
238 | int r; | |
239 | struct kvm_vcpu *v, *ret = NULL; | |
240 | ||
241 | mutex_lock(&kvm->lock); | |
242 | kvm_for_each_vcpu(r, v, kvm) { | |
243 | if (v->vcpu_id == id) { | |
244 | ret = v; | |
245 | break; | |
246 | } | |
247 | } | |
248 | mutex_unlock(&kvm->lock); | |
249 | return ret; | |
250 | } | |
251 | ||
252 | static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) | |
253 | { | |
f13c13a0 | 254 | vpa->__old_status |= LPPACA_OLD_SHARED_PROC; |
a8606e20 PM |
255 | vpa->yield_count = 1; |
256 | } | |
257 | ||
55b665b0 PM |
258 | static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v, |
259 | unsigned long addr, unsigned long len) | |
260 | { | |
261 | /* check address is cacheline aligned */ | |
262 | if (addr & (L1_CACHE_BYTES - 1)) | |
263 | return -EINVAL; | |
264 | spin_lock(&vcpu->arch.vpa_update_lock); | |
265 | if (v->next_gpa != addr || v->len != len) { | |
266 | v->next_gpa = addr; | |
267 | v->len = addr ? len : 0; | |
268 | v->update_pending = 1; | |
269 | } | |
270 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
271 | return 0; | |
272 | } | |
273 | ||
2e25aa5f PM |
274 | /* Length for a per-processor buffer is passed in at offset 4 in the buffer */ |
275 | struct reg_vpa { | |
276 | u32 dummy; | |
277 | union { | |
278 | u16 hword; | |
279 | u32 word; | |
280 | } length; | |
281 | }; | |
282 | ||
283 | static int vpa_is_registered(struct kvmppc_vpa *vpap) | |
284 | { | |
285 | if (vpap->update_pending) | |
286 | return vpap->next_gpa != 0; | |
287 | return vpap->pinned_addr != NULL; | |
288 | } | |
289 | ||
a8606e20 PM |
290 | static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, |
291 | unsigned long flags, | |
292 | unsigned long vcpuid, unsigned long vpa) | |
293 | { | |
294 | struct kvm *kvm = vcpu->kvm; | |
93e60249 | 295 | unsigned long len, nb; |
a8606e20 PM |
296 | void *va; |
297 | struct kvm_vcpu *tvcpu; | |
2e25aa5f PM |
298 | int err; |
299 | int subfunc; | |
300 | struct kvmppc_vpa *vpap; | |
a8606e20 PM |
301 | |
302 | tvcpu = kvmppc_find_vcpu(kvm, vcpuid); | |
303 | if (!tvcpu) | |
304 | return H_PARAMETER; | |
305 | ||
2e25aa5f PM |
306 | subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK; |
307 | if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL || | |
308 | subfunc == H_VPA_REG_SLB) { | |
309 | /* Registering new area - address must be cache-line aligned */ | |
310 | if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa) | |
a8606e20 | 311 | return H_PARAMETER; |
2e25aa5f PM |
312 | |
313 | /* convert logical addr to kernel addr and read length */ | |
93e60249 PM |
314 | va = kvmppc_pin_guest_page(kvm, vpa, &nb); |
315 | if (va == NULL) | |
b2b2f165 | 316 | return H_PARAMETER; |
2e25aa5f PM |
317 | if (subfunc == H_VPA_REG_VPA) |
318 | len = ((struct reg_vpa *)va)->length.hword; | |
a8606e20 | 319 | else |
2e25aa5f | 320 | len = ((struct reg_vpa *)va)->length.word; |
c35635ef | 321 | kvmppc_unpin_guest_page(kvm, va, vpa, false); |
2e25aa5f PM |
322 | |
323 | /* Check length */ | |
324 | if (len > nb || len < sizeof(struct reg_vpa)) | |
325 | return H_PARAMETER; | |
326 | } else { | |
327 | vpa = 0; | |
328 | len = 0; | |
329 | } | |
330 | ||
331 | err = H_PARAMETER; | |
332 | vpap = NULL; | |
333 | spin_lock(&tvcpu->arch.vpa_update_lock); | |
334 | ||
335 | switch (subfunc) { | |
336 | case H_VPA_REG_VPA: /* register VPA */ | |
337 | if (len < sizeof(struct lppaca)) | |
a8606e20 | 338 | break; |
2e25aa5f PM |
339 | vpap = &tvcpu->arch.vpa; |
340 | err = 0; | |
341 | break; | |
342 | ||
343 | case H_VPA_REG_DTL: /* register DTL */ | |
344 | if (len < sizeof(struct dtl_entry)) | |
a8606e20 | 345 | break; |
2e25aa5f PM |
346 | len -= len % sizeof(struct dtl_entry); |
347 | ||
348 | /* Check that they have previously registered a VPA */ | |
349 | err = H_RESOURCE; | |
350 | if (!vpa_is_registered(&tvcpu->arch.vpa)) | |
a8606e20 | 351 | break; |
2e25aa5f PM |
352 | |
353 | vpap = &tvcpu->arch.dtl; | |
354 | err = 0; | |
355 | break; | |
356 | ||
357 | case H_VPA_REG_SLB: /* register SLB shadow buffer */ | |
358 | /* Check that they have previously registered a VPA */ | |
359 | err = H_RESOURCE; | |
360 | if (!vpa_is_registered(&tvcpu->arch.vpa)) | |
a8606e20 | 361 | break; |
2e25aa5f PM |
362 | |
363 | vpap = &tvcpu->arch.slb_shadow; | |
364 | err = 0; | |
365 | break; | |
366 | ||
367 | case H_VPA_DEREG_VPA: /* deregister VPA */ | |
368 | /* Check they don't still have a DTL or SLB buf registered */ | |
369 | err = H_RESOURCE; | |
370 | if (vpa_is_registered(&tvcpu->arch.dtl) || | |
371 | vpa_is_registered(&tvcpu->arch.slb_shadow)) | |
a8606e20 | 372 | break; |
2e25aa5f PM |
373 | |
374 | vpap = &tvcpu->arch.vpa; | |
375 | err = 0; | |
376 | break; | |
377 | ||
378 | case H_VPA_DEREG_DTL: /* deregister DTL */ | |
379 | vpap = &tvcpu->arch.dtl; | |
380 | err = 0; | |
381 | break; | |
382 | ||
383 | case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */ | |
384 | vpap = &tvcpu->arch.slb_shadow; | |
385 | err = 0; | |
386 | break; | |
387 | } | |
388 | ||
389 | if (vpap) { | |
390 | vpap->next_gpa = vpa; | |
391 | vpap->len = len; | |
392 | vpap->update_pending = 1; | |
a8606e20 | 393 | } |
93e60249 | 394 | |
2e25aa5f PM |
395 | spin_unlock(&tvcpu->arch.vpa_update_lock); |
396 | ||
93e60249 | 397 | return err; |
a8606e20 PM |
398 | } |
399 | ||
081f323b | 400 | static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap) |
2e25aa5f | 401 | { |
081f323b | 402 | struct kvm *kvm = vcpu->kvm; |
2e25aa5f PM |
403 | void *va; |
404 | unsigned long nb; | |
081f323b | 405 | unsigned long gpa; |
2e25aa5f | 406 | |
081f323b PM |
407 | /* |
408 | * We need to pin the page pointed to by vpap->next_gpa, | |
409 | * but we can't call kvmppc_pin_guest_page under the lock | |
410 | * as it does get_user_pages() and down_read(). So we | |
411 | * have to drop the lock, pin the page, then get the lock | |
412 | * again and check that a new area didn't get registered | |
413 | * in the meantime. | |
414 | */ | |
415 | for (;;) { | |
416 | gpa = vpap->next_gpa; | |
417 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
418 | va = NULL; | |
419 | nb = 0; | |
420 | if (gpa) | |
c35635ef | 421 | va = kvmppc_pin_guest_page(kvm, gpa, &nb); |
081f323b PM |
422 | spin_lock(&vcpu->arch.vpa_update_lock); |
423 | if (gpa == vpap->next_gpa) | |
424 | break; | |
425 | /* sigh... unpin that one and try again */ | |
426 | if (va) | |
c35635ef | 427 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
081f323b PM |
428 | } |
429 | ||
430 | vpap->update_pending = 0; | |
431 | if (va && nb < vpap->len) { | |
432 | /* | |
433 | * If it's now too short, it must be that userspace | |
434 | * has changed the mappings underlying guest memory, | |
435 | * so unregister the region. | |
436 | */ | |
c35635ef | 437 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
081f323b | 438 | va = NULL; |
2e25aa5f PM |
439 | } |
440 | if (vpap->pinned_addr) | |
c35635ef PM |
441 | kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa, |
442 | vpap->dirty); | |
443 | vpap->gpa = gpa; | |
2e25aa5f | 444 | vpap->pinned_addr = va; |
c35635ef | 445 | vpap->dirty = false; |
2e25aa5f PM |
446 | if (va) |
447 | vpap->pinned_end = va + vpap->len; | |
448 | } | |
449 | ||
450 | static void kvmppc_update_vpas(struct kvm_vcpu *vcpu) | |
451 | { | |
2f12f034 PM |
452 | if (!(vcpu->arch.vpa.update_pending || |
453 | vcpu->arch.slb_shadow.update_pending || | |
454 | vcpu->arch.dtl.update_pending)) | |
455 | return; | |
456 | ||
2e25aa5f PM |
457 | spin_lock(&vcpu->arch.vpa_update_lock); |
458 | if (vcpu->arch.vpa.update_pending) { | |
081f323b | 459 | kvmppc_update_vpa(vcpu, &vcpu->arch.vpa); |
55b665b0 PM |
460 | if (vcpu->arch.vpa.pinned_addr) |
461 | init_vpa(vcpu, vcpu->arch.vpa.pinned_addr); | |
2e25aa5f PM |
462 | } |
463 | if (vcpu->arch.dtl.update_pending) { | |
081f323b | 464 | kvmppc_update_vpa(vcpu, &vcpu->arch.dtl); |
2e25aa5f PM |
465 | vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr; |
466 | vcpu->arch.dtl_index = 0; | |
467 | } | |
468 | if (vcpu->arch.slb_shadow.update_pending) | |
081f323b | 469 | kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow); |
2e25aa5f PM |
470 | spin_unlock(&vcpu->arch.vpa_update_lock); |
471 | } | |
472 | ||
c7b67670 PM |
473 | /* |
474 | * Return the accumulated stolen time for the vcore up until `now'. | |
475 | * The caller should hold the vcore lock. | |
476 | */ | |
477 | static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now) | |
478 | { | |
479 | u64 p; | |
480 | ||
481 | /* | |
482 | * If we are the task running the vcore, then since we hold | |
483 | * the vcore lock, we can't be preempted, so stolen_tb/preempt_tb | |
484 | * can't be updated, so we don't need the tbacct_lock. | |
485 | * If the vcore is inactive, it can't become active (since we | |
486 | * hold the vcore lock), so the vcpu load/put functions won't | |
487 | * update stolen_tb/preempt_tb, and we don't need tbacct_lock. | |
488 | */ | |
489 | if (vc->vcore_state != VCORE_INACTIVE && | |
490 | vc->runner->arch.run_task != current) { | |
bf3d32e1 | 491 | spin_lock_irq(&vc->runner->arch.tbacct_lock); |
c7b67670 PM |
492 | p = vc->stolen_tb; |
493 | if (vc->preempt_tb != TB_NIL) | |
494 | p += now - vc->preempt_tb; | |
bf3d32e1 | 495 | spin_unlock_irq(&vc->runner->arch.tbacct_lock); |
c7b67670 PM |
496 | } else { |
497 | p = vc->stolen_tb; | |
498 | } | |
499 | return p; | |
500 | } | |
501 | ||
0456ec4f PM |
502 | static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, |
503 | struct kvmppc_vcore *vc) | |
504 | { | |
505 | struct dtl_entry *dt; | |
506 | struct lppaca *vpa; | |
c7b67670 PM |
507 | unsigned long stolen; |
508 | unsigned long core_stolen; | |
509 | u64 now; | |
0456ec4f PM |
510 | |
511 | dt = vcpu->arch.dtl_ptr; | |
512 | vpa = vcpu->arch.vpa.pinned_addr; | |
c7b67670 PM |
513 | now = mftb(); |
514 | core_stolen = vcore_stolen_time(vc, now); | |
515 | stolen = core_stolen - vcpu->arch.stolen_logged; | |
516 | vcpu->arch.stolen_logged = core_stolen; | |
bf3d32e1 | 517 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
c7b67670 PM |
518 | stolen += vcpu->arch.busy_stolen; |
519 | vcpu->arch.busy_stolen = 0; | |
bf3d32e1 | 520 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
0456ec4f PM |
521 | if (!dt || !vpa) |
522 | return; | |
523 | memset(dt, 0, sizeof(struct dtl_entry)); | |
524 | dt->dispatch_reason = 7; | |
525 | dt->processor_id = vc->pcpu + vcpu->arch.ptid; | |
93b0f4dc | 526 | dt->timebase = now + vc->tb_offset; |
c7b67670 | 527 | dt->enqueue_to_dispatch_time = stolen; |
0456ec4f PM |
528 | dt->srr0 = kvmppc_get_pc(vcpu); |
529 | dt->srr1 = vcpu->arch.shregs.msr; | |
530 | ++dt; | |
531 | if (dt == vcpu->arch.dtl.pinned_end) | |
532 | dt = vcpu->arch.dtl.pinned_addr; | |
533 | vcpu->arch.dtl_ptr = dt; | |
534 | /* order writing *dt vs. writing vpa->dtl_idx */ | |
535 | smp_wmb(); | |
536 | vpa->dtl_idx = ++vcpu->arch.dtl_index; | |
c35635ef | 537 | vcpu->arch.dtl.dirty = true; |
0456ec4f PM |
538 | } |
539 | ||
a8606e20 PM |
540 | int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) |
541 | { | |
542 | unsigned long req = kvmppc_get_gpr(vcpu, 3); | |
543 | unsigned long target, ret = H_SUCCESS; | |
544 | struct kvm_vcpu *tvcpu; | |
8e591cb7 | 545 | int idx, rc; |
a8606e20 PM |
546 | |
547 | switch (req) { | |
c77162de | 548 | case H_ENTER: |
2c9097e4 | 549 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
c77162de PM |
550 | ret = kvmppc_virtmode_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4), |
551 | kvmppc_get_gpr(vcpu, 5), | |
552 | kvmppc_get_gpr(vcpu, 6), | |
553 | kvmppc_get_gpr(vcpu, 7)); | |
2c9097e4 | 554 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
c77162de | 555 | break; |
a8606e20 | 556 | case H_CEDE: |
a8606e20 PM |
557 | break; |
558 | case H_PROD: | |
559 | target = kvmppc_get_gpr(vcpu, 4); | |
560 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); | |
561 | if (!tvcpu) { | |
562 | ret = H_PARAMETER; | |
563 | break; | |
564 | } | |
565 | tvcpu->arch.prodded = 1; | |
566 | smp_mb(); | |
567 | if (vcpu->arch.ceded) { | |
568 | if (waitqueue_active(&vcpu->wq)) { | |
569 | wake_up_interruptible(&vcpu->wq); | |
570 | vcpu->stat.halt_wakeup++; | |
571 | } | |
572 | } | |
573 | break; | |
574 | case H_CONFER: | |
42d7604d PM |
575 | target = kvmppc_get_gpr(vcpu, 4); |
576 | if (target == -1) | |
577 | break; | |
578 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); | |
579 | if (!tvcpu) { | |
580 | ret = H_PARAMETER; | |
581 | break; | |
582 | } | |
583 | kvm_vcpu_yield_to(tvcpu); | |
a8606e20 PM |
584 | break; |
585 | case H_REGISTER_VPA: | |
586 | ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), | |
587 | kvmppc_get_gpr(vcpu, 5), | |
588 | kvmppc_get_gpr(vcpu, 6)); | |
589 | break; | |
8e591cb7 ME |
590 | case H_RTAS: |
591 | if (list_empty(&vcpu->kvm->arch.rtas_tokens)) | |
592 | return RESUME_HOST; | |
593 | ||
594 | rc = kvmppc_rtas_hcall(vcpu); | |
595 | ||
596 | if (rc == -ENOENT) | |
597 | return RESUME_HOST; | |
598 | else if (rc == 0) | |
599 | break; | |
600 | ||
601 | /* Send the error out to userspace via KVM_RUN */ | |
602 | return rc; | |
bc5ad3f3 BH |
603 | |
604 | case H_XIRR: | |
605 | case H_CPPR: | |
606 | case H_EOI: | |
607 | case H_IPI: | |
8e44ddc3 PM |
608 | case H_IPOLL: |
609 | case H_XIRR_X: | |
bc5ad3f3 BH |
610 | if (kvmppc_xics_enabled(vcpu)) { |
611 | ret = kvmppc_xics_hcall(vcpu, req); | |
612 | break; | |
613 | } /* fallthrough */ | |
a8606e20 PM |
614 | default: |
615 | return RESUME_HOST; | |
616 | } | |
617 | kvmppc_set_gpr(vcpu, 3, ret); | |
618 | vcpu->arch.hcall_needed = 0; | |
619 | return RESUME_GUEST; | |
620 | } | |
621 | ||
3a167bea AK |
622 | static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, |
623 | struct task_struct *tsk) | |
de56a948 PM |
624 | { |
625 | int r = RESUME_HOST; | |
626 | ||
627 | vcpu->stat.sum_exits++; | |
628 | ||
629 | run->exit_reason = KVM_EXIT_UNKNOWN; | |
630 | run->ready_for_interrupt_injection = 1; | |
631 | switch (vcpu->arch.trap) { | |
632 | /* We're good on these - the host merely wanted to get our attention */ | |
633 | case BOOK3S_INTERRUPT_HV_DECREMENTER: | |
634 | vcpu->stat.dec_exits++; | |
635 | r = RESUME_GUEST; | |
636 | break; | |
637 | case BOOK3S_INTERRUPT_EXTERNAL: | |
638 | vcpu->stat.ext_intr_exits++; | |
639 | r = RESUME_GUEST; | |
640 | break; | |
641 | case BOOK3S_INTERRUPT_PERFMON: | |
642 | r = RESUME_GUEST; | |
643 | break; | |
b4072df4 PM |
644 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
645 | /* | |
646 | * Deliver a machine check interrupt to the guest. | |
647 | * We have to do this, even if the host has handled the | |
648 | * machine check, because machine checks use SRR0/1 and | |
649 | * the interrupt might have trashed guest state in them. | |
650 | */ | |
651 | kvmppc_book3s_queue_irqprio(vcpu, | |
652 | BOOK3S_INTERRUPT_MACHINE_CHECK); | |
653 | r = RESUME_GUEST; | |
654 | break; | |
de56a948 PM |
655 | case BOOK3S_INTERRUPT_PROGRAM: |
656 | { | |
657 | ulong flags; | |
658 | /* | |
659 | * Normally program interrupts are delivered directly | |
660 | * to the guest by the hardware, but we can get here | |
661 | * as a result of a hypervisor emulation interrupt | |
662 | * (e40) getting turned into a 700 by BML RTAS. | |
663 | */ | |
664 | flags = vcpu->arch.shregs.msr & 0x1f0000ull; | |
665 | kvmppc_core_queue_program(vcpu, flags); | |
666 | r = RESUME_GUEST; | |
667 | break; | |
668 | } | |
669 | case BOOK3S_INTERRUPT_SYSCALL: | |
670 | { | |
671 | /* hcall - punt to userspace */ | |
672 | int i; | |
673 | ||
674 | if (vcpu->arch.shregs.msr & MSR_PR) { | |
675 | /* sc 1 from userspace - reflect to guest syscall */ | |
676 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_SYSCALL); | |
677 | r = RESUME_GUEST; | |
678 | break; | |
679 | } | |
680 | run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); | |
681 | for (i = 0; i < 9; ++i) | |
682 | run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); | |
683 | run->exit_reason = KVM_EXIT_PAPR_HCALL; | |
684 | vcpu->arch.hcall_needed = 1; | |
685 | r = RESUME_HOST; | |
686 | break; | |
687 | } | |
688 | /* | |
342d3db7 PM |
689 | * We get these next two if the guest accesses a page which it thinks |
690 | * it has mapped but which is not actually present, either because | |
691 | * it is for an emulated I/O device or because the corresonding | |
692 | * host page has been paged out. Any other HDSI/HISI interrupts | |
693 | * have been handled already. | |
de56a948 PM |
694 | */ |
695 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: | |
913d3ff9 | 696 | r = RESUME_PAGE_FAULT; |
de56a948 PM |
697 | break; |
698 | case BOOK3S_INTERRUPT_H_INST_STORAGE: | |
913d3ff9 PM |
699 | vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); |
700 | vcpu->arch.fault_dsisr = 0; | |
701 | r = RESUME_PAGE_FAULT; | |
de56a948 PM |
702 | break; |
703 | /* | |
704 | * This occurs if the guest executes an illegal instruction. | |
705 | * We just generate a program interrupt to the guest, since | |
706 | * we don't emulate any guest instructions at this stage. | |
707 | */ | |
708 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: | |
709 | kvmppc_core_queue_program(vcpu, 0x80000); | |
710 | r = RESUME_GUEST; | |
711 | break; | |
712 | default: | |
713 | kvmppc_dump_regs(vcpu); | |
714 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", | |
715 | vcpu->arch.trap, kvmppc_get_pc(vcpu), | |
716 | vcpu->arch.shregs.msr); | |
f3271d4c | 717 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
de56a948 | 718 | r = RESUME_HOST; |
de56a948 PM |
719 | break; |
720 | } | |
721 | ||
de56a948 PM |
722 | return r; |
723 | } | |
724 | ||
3a167bea AK |
725 | static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu, |
726 | struct kvm_sregs *sregs) | |
de56a948 PM |
727 | { |
728 | int i; | |
729 | ||
de56a948 | 730 | memset(sregs, 0, sizeof(struct kvm_sregs)); |
87916442 | 731 | sregs->pvr = vcpu->arch.pvr; |
de56a948 PM |
732 | for (i = 0; i < vcpu->arch.slb_max; i++) { |
733 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; | |
734 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; | |
735 | } | |
736 | ||
737 | return 0; | |
738 | } | |
739 | ||
3a167bea AK |
740 | static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu, |
741 | struct kvm_sregs *sregs) | |
de56a948 PM |
742 | { |
743 | int i, j; | |
744 | ||
3a167bea | 745 | kvmppc_set_pvr_hv(vcpu, sregs->pvr); |
de56a948 PM |
746 | |
747 | j = 0; | |
748 | for (i = 0; i < vcpu->arch.slb_nr; i++) { | |
749 | if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { | |
750 | vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; | |
751 | vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; | |
752 | ++j; | |
753 | } | |
754 | } | |
755 | vcpu->arch.slb_max = j; | |
756 | ||
757 | return 0; | |
758 | } | |
759 | ||
a0144e2a PM |
760 | static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr) |
761 | { | |
762 | struct kvmppc_vcore *vc = vcpu->arch.vcore; | |
763 | u64 mask; | |
764 | ||
765 | spin_lock(&vc->lock); | |
766 | /* | |
767 | * Userspace can only modify DPFD (default prefetch depth), | |
768 | * ILE (interrupt little-endian) and TC (translation control). | |
769 | */ | |
770 | mask = LPCR_DPFD | LPCR_ILE | LPCR_TC; | |
771 | vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask); | |
772 | spin_unlock(&vc->lock); | |
773 | } | |
774 | ||
3a167bea AK |
775 | static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
776 | union kvmppc_one_reg *val) | |
31f3438e | 777 | { |
a136a8bd PM |
778 | int r = 0; |
779 | long int i; | |
31f3438e | 780 | |
a136a8bd | 781 | switch (id) { |
31f3438e | 782 | case KVM_REG_PPC_HIOR: |
a136a8bd PM |
783 | *val = get_reg_val(id, 0); |
784 | break; | |
785 | case KVM_REG_PPC_DABR: | |
786 | *val = get_reg_val(id, vcpu->arch.dabr); | |
787 | break; | |
788 | case KVM_REG_PPC_DSCR: | |
789 | *val = get_reg_val(id, vcpu->arch.dscr); | |
790 | break; | |
791 | case KVM_REG_PPC_PURR: | |
792 | *val = get_reg_val(id, vcpu->arch.purr); | |
793 | break; | |
794 | case KVM_REG_PPC_SPURR: | |
795 | *val = get_reg_val(id, vcpu->arch.spurr); | |
796 | break; | |
797 | case KVM_REG_PPC_AMR: | |
798 | *val = get_reg_val(id, vcpu->arch.amr); | |
799 | break; | |
800 | case KVM_REG_PPC_UAMOR: | |
801 | *val = get_reg_val(id, vcpu->arch.uamor); | |
802 | break; | |
803 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA: | |
804 | i = id - KVM_REG_PPC_MMCR0; | |
805 | *val = get_reg_val(id, vcpu->arch.mmcr[i]); | |
806 | break; | |
807 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: | |
808 | i = id - KVM_REG_PPC_PMC1; | |
809 | *val = get_reg_val(id, vcpu->arch.pmc[i]); | |
31f3438e | 810 | break; |
14941789 PM |
811 | case KVM_REG_PPC_SIAR: |
812 | *val = get_reg_val(id, vcpu->arch.siar); | |
813 | break; | |
814 | case KVM_REG_PPC_SDAR: | |
815 | *val = get_reg_val(id, vcpu->arch.sdar); | |
816 | break; | |
a8bd19ef PM |
817 | #ifdef CONFIG_VSX |
818 | case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31: | |
819 | if (cpu_has_feature(CPU_FTR_VSX)) { | |
820 | /* VSX => FP reg i is stored in arch.vsr[2*i] */ | |
821 | long int i = id - KVM_REG_PPC_FPR0; | |
822 | *val = get_reg_val(id, vcpu->arch.vsr[2 * i]); | |
823 | } else { | |
824 | /* let generic code handle it */ | |
825 | r = -EINVAL; | |
826 | } | |
827 | break; | |
828 | case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: | |
829 | if (cpu_has_feature(CPU_FTR_VSX)) { | |
830 | long int i = id - KVM_REG_PPC_VSR0; | |
831 | val->vsxval[0] = vcpu->arch.vsr[2 * i]; | |
832 | val->vsxval[1] = vcpu->arch.vsr[2 * i + 1]; | |
833 | } else { | |
834 | r = -ENXIO; | |
835 | } | |
836 | break; | |
837 | #endif /* CONFIG_VSX */ | |
55b665b0 PM |
838 | case KVM_REG_PPC_VPA_ADDR: |
839 | spin_lock(&vcpu->arch.vpa_update_lock); | |
840 | *val = get_reg_val(id, vcpu->arch.vpa.next_gpa); | |
841 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
842 | break; | |
843 | case KVM_REG_PPC_VPA_SLB: | |
844 | spin_lock(&vcpu->arch.vpa_update_lock); | |
845 | val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa; | |
846 | val->vpaval.length = vcpu->arch.slb_shadow.len; | |
847 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
848 | break; | |
849 | case KVM_REG_PPC_VPA_DTL: | |
850 | spin_lock(&vcpu->arch.vpa_update_lock); | |
851 | val->vpaval.addr = vcpu->arch.dtl.next_gpa; | |
852 | val->vpaval.length = vcpu->arch.dtl.len; | |
853 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
854 | break; | |
93b0f4dc PM |
855 | case KVM_REG_PPC_TB_OFFSET: |
856 | *val = get_reg_val(id, vcpu->arch.vcore->tb_offset); | |
857 | break; | |
a0144e2a PM |
858 | case KVM_REG_PPC_LPCR: |
859 | *val = get_reg_val(id, vcpu->arch.vcore->lpcr); | |
860 | break; | |
4b8473c9 PM |
861 | case KVM_REG_PPC_PPR: |
862 | *val = get_reg_val(id, vcpu->arch.ppr); | |
863 | break; | |
388cc6e1 PM |
864 | case KVM_REG_PPC_ARCH_COMPAT: |
865 | *val = get_reg_val(id, vcpu->arch.vcore->arch_compat); | |
866 | break; | |
31f3438e | 867 | default: |
a136a8bd | 868 | r = -EINVAL; |
31f3438e PM |
869 | break; |
870 | } | |
871 | ||
872 | return r; | |
873 | } | |
874 | ||
3a167bea AK |
875 | static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
876 | union kvmppc_one_reg *val) | |
31f3438e | 877 | { |
a136a8bd PM |
878 | int r = 0; |
879 | long int i; | |
55b665b0 | 880 | unsigned long addr, len; |
31f3438e | 881 | |
a136a8bd | 882 | switch (id) { |
31f3438e | 883 | case KVM_REG_PPC_HIOR: |
31f3438e | 884 | /* Only allow this to be set to zero */ |
a136a8bd | 885 | if (set_reg_val(id, *val)) |
31f3438e PM |
886 | r = -EINVAL; |
887 | break; | |
a136a8bd PM |
888 | case KVM_REG_PPC_DABR: |
889 | vcpu->arch.dabr = set_reg_val(id, *val); | |
890 | break; | |
891 | case KVM_REG_PPC_DSCR: | |
892 | vcpu->arch.dscr = set_reg_val(id, *val); | |
893 | break; | |
894 | case KVM_REG_PPC_PURR: | |
895 | vcpu->arch.purr = set_reg_val(id, *val); | |
896 | break; | |
897 | case KVM_REG_PPC_SPURR: | |
898 | vcpu->arch.spurr = set_reg_val(id, *val); | |
899 | break; | |
900 | case KVM_REG_PPC_AMR: | |
901 | vcpu->arch.amr = set_reg_val(id, *val); | |
902 | break; | |
903 | case KVM_REG_PPC_UAMOR: | |
904 | vcpu->arch.uamor = set_reg_val(id, *val); | |
905 | break; | |
906 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRA: | |
907 | i = id - KVM_REG_PPC_MMCR0; | |
908 | vcpu->arch.mmcr[i] = set_reg_val(id, *val); | |
909 | break; | |
910 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: | |
911 | i = id - KVM_REG_PPC_PMC1; | |
912 | vcpu->arch.pmc[i] = set_reg_val(id, *val); | |
913 | break; | |
14941789 PM |
914 | case KVM_REG_PPC_SIAR: |
915 | vcpu->arch.siar = set_reg_val(id, *val); | |
916 | break; | |
917 | case KVM_REG_PPC_SDAR: | |
918 | vcpu->arch.sdar = set_reg_val(id, *val); | |
919 | break; | |
a8bd19ef PM |
920 | #ifdef CONFIG_VSX |
921 | case KVM_REG_PPC_FPR0 ... KVM_REG_PPC_FPR31: | |
922 | if (cpu_has_feature(CPU_FTR_VSX)) { | |
923 | /* VSX => FP reg i is stored in arch.vsr[2*i] */ | |
924 | long int i = id - KVM_REG_PPC_FPR0; | |
925 | vcpu->arch.vsr[2 * i] = set_reg_val(id, *val); | |
926 | } else { | |
927 | /* let generic code handle it */ | |
928 | r = -EINVAL; | |
929 | } | |
930 | break; | |
931 | case KVM_REG_PPC_VSR0 ... KVM_REG_PPC_VSR31: | |
932 | if (cpu_has_feature(CPU_FTR_VSX)) { | |
933 | long int i = id - KVM_REG_PPC_VSR0; | |
934 | vcpu->arch.vsr[2 * i] = val->vsxval[0]; | |
935 | vcpu->arch.vsr[2 * i + 1] = val->vsxval[1]; | |
936 | } else { | |
937 | r = -ENXIO; | |
938 | } | |
939 | break; | |
940 | #endif /* CONFIG_VSX */ | |
55b665b0 PM |
941 | case KVM_REG_PPC_VPA_ADDR: |
942 | addr = set_reg_val(id, *val); | |
943 | r = -EINVAL; | |
944 | if (!addr && (vcpu->arch.slb_shadow.next_gpa || | |
945 | vcpu->arch.dtl.next_gpa)) | |
946 | break; | |
947 | r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca)); | |
948 | break; | |
949 | case KVM_REG_PPC_VPA_SLB: | |
950 | addr = val->vpaval.addr; | |
951 | len = val->vpaval.length; | |
952 | r = -EINVAL; | |
953 | if (addr && !vcpu->arch.vpa.next_gpa) | |
954 | break; | |
955 | r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len); | |
956 | break; | |
957 | case KVM_REG_PPC_VPA_DTL: | |
958 | addr = val->vpaval.addr; | |
959 | len = val->vpaval.length; | |
960 | r = -EINVAL; | |
9f8c8c78 PM |
961 | if (addr && (len < sizeof(struct dtl_entry) || |
962 | !vcpu->arch.vpa.next_gpa)) | |
55b665b0 PM |
963 | break; |
964 | len -= len % sizeof(struct dtl_entry); | |
965 | r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len); | |
966 | break; | |
93b0f4dc PM |
967 | case KVM_REG_PPC_TB_OFFSET: |
968 | /* round up to multiple of 2^24 */ | |
969 | vcpu->arch.vcore->tb_offset = | |
970 | ALIGN(set_reg_val(id, *val), 1UL << 24); | |
971 | break; | |
a0144e2a PM |
972 | case KVM_REG_PPC_LPCR: |
973 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val)); | |
974 | break; | |
4b8473c9 PM |
975 | case KVM_REG_PPC_PPR: |
976 | vcpu->arch.ppr = set_reg_val(id, *val); | |
977 | break; | |
388cc6e1 PM |
978 | case KVM_REG_PPC_ARCH_COMPAT: |
979 | r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val)); | |
980 | break; | |
31f3438e | 981 | default: |
a136a8bd | 982 | r = -EINVAL; |
31f3438e PM |
983 | break; |
984 | } | |
985 | ||
986 | return r; | |
987 | } | |
988 | ||
3a167bea AK |
989 | static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm, |
990 | unsigned int id) | |
de56a948 PM |
991 | { |
992 | struct kvm_vcpu *vcpu; | |
371fefd6 PM |
993 | int err = -EINVAL; |
994 | int core; | |
995 | struct kvmppc_vcore *vcore; | |
de56a948 | 996 | |
371fefd6 PM |
997 | core = id / threads_per_core; |
998 | if (core >= KVM_MAX_VCORES) | |
999 | goto out; | |
1000 | ||
1001 | err = -ENOMEM; | |
6b75e6bf | 1002 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
de56a948 PM |
1003 | if (!vcpu) |
1004 | goto out; | |
1005 | ||
1006 | err = kvm_vcpu_init(vcpu, kvm, id); | |
1007 | if (err) | |
1008 | goto free_vcpu; | |
1009 | ||
1010 | vcpu->arch.shared = &vcpu->arch.shregs; | |
de56a948 PM |
1011 | vcpu->arch.mmcr[0] = MMCR0_FC; |
1012 | vcpu->arch.ctrl = CTRL_RUNLATCH; | |
1013 | /* default to host PVR, since we can't spoof it */ | |
3a167bea | 1014 | kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR)); |
2e25aa5f | 1015 | spin_lock_init(&vcpu->arch.vpa_update_lock); |
c7b67670 PM |
1016 | spin_lock_init(&vcpu->arch.tbacct_lock); |
1017 | vcpu->arch.busy_preempt = TB_NIL; | |
de56a948 | 1018 | |
de56a948 PM |
1019 | kvmppc_mmu_book3s_hv_init(vcpu); |
1020 | ||
8455d79e | 1021 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
371fefd6 PM |
1022 | |
1023 | init_waitqueue_head(&vcpu->arch.cpu_run); | |
1024 | ||
1025 | mutex_lock(&kvm->lock); | |
1026 | vcore = kvm->arch.vcores[core]; | |
1027 | if (!vcore) { | |
1028 | vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); | |
1029 | if (vcore) { | |
1030 | INIT_LIST_HEAD(&vcore->runnable_threads); | |
1031 | spin_lock_init(&vcore->lock); | |
19ccb76a | 1032 | init_waitqueue_head(&vcore->wq); |
c7b67670 | 1033 | vcore->preempt_tb = TB_NIL; |
a0144e2a | 1034 | vcore->lpcr = kvm->arch.lpcr; |
371fefd6 PM |
1035 | } |
1036 | kvm->arch.vcores[core] = vcore; | |
1b400ba0 | 1037 | kvm->arch.online_vcores++; |
371fefd6 PM |
1038 | } |
1039 | mutex_unlock(&kvm->lock); | |
1040 | ||
1041 | if (!vcore) | |
1042 | goto free_vcpu; | |
1043 | ||
1044 | spin_lock(&vcore->lock); | |
1045 | ++vcore->num_threads; | |
371fefd6 PM |
1046 | spin_unlock(&vcore->lock); |
1047 | vcpu->arch.vcore = vcore; | |
1048 | ||
af8f38b3 AG |
1049 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
1050 | kvmppc_sanity_check(vcpu); | |
1051 | ||
de56a948 PM |
1052 | return vcpu; |
1053 | ||
1054 | free_vcpu: | |
6b75e6bf | 1055 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
de56a948 PM |
1056 | out: |
1057 | return ERR_PTR(err); | |
1058 | } | |
1059 | ||
c35635ef PM |
1060 | static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) |
1061 | { | |
1062 | if (vpa->pinned_addr) | |
1063 | kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa, | |
1064 | vpa->dirty); | |
1065 | } | |
1066 | ||
3a167bea | 1067 | static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu) |
de56a948 | 1068 | { |
2e25aa5f | 1069 | spin_lock(&vcpu->arch.vpa_update_lock); |
c35635ef PM |
1070 | unpin_vpa(vcpu->kvm, &vcpu->arch.dtl); |
1071 | unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow); | |
1072 | unpin_vpa(vcpu->kvm, &vcpu->arch.vpa); | |
2e25aa5f | 1073 | spin_unlock(&vcpu->arch.vpa_update_lock); |
de56a948 | 1074 | kvm_vcpu_uninit(vcpu); |
6b75e6bf | 1075 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
de56a948 PM |
1076 | } |
1077 | ||
3a167bea AK |
1078 | static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu) |
1079 | { | |
1080 | /* Indicate we want to get back into the guest */ | |
1081 | return 1; | |
1082 | } | |
1083 | ||
19ccb76a | 1084 | static void kvmppc_set_timer(struct kvm_vcpu *vcpu) |
371fefd6 | 1085 | { |
19ccb76a | 1086 | unsigned long dec_nsec, now; |
371fefd6 | 1087 | |
19ccb76a PM |
1088 | now = get_tb(); |
1089 | if (now > vcpu->arch.dec_expires) { | |
1090 | /* decrementer has already gone negative */ | |
1091 | kvmppc_core_queue_dec(vcpu); | |
7e28e60e | 1092 | kvmppc_core_prepare_to_enter(vcpu); |
19ccb76a | 1093 | return; |
371fefd6 | 1094 | } |
19ccb76a PM |
1095 | dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC |
1096 | / tb_ticks_per_sec; | |
1097 | hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec), | |
1098 | HRTIMER_MODE_REL); | |
1099 | vcpu->arch.timer_running = 1; | |
371fefd6 PM |
1100 | } |
1101 | ||
19ccb76a | 1102 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu) |
371fefd6 | 1103 | { |
19ccb76a PM |
1104 | vcpu->arch.ceded = 0; |
1105 | if (vcpu->arch.timer_running) { | |
1106 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
1107 | vcpu->arch.timer_running = 0; | |
1108 | } | |
371fefd6 PM |
1109 | } |
1110 | ||
de56a948 PM |
1111 | extern int __kvmppc_vcore_entry(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu); |
1112 | ||
371fefd6 PM |
1113 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
1114 | struct kvm_vcpu *vcpu) | |
de56a948 | 1115 | { |
c7b67670 PM |
1116 | u64 now; |
1117 | ||
371fefd6 PM |
1118 | if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
1119 | return; | |
bf3d32e1 | 1120 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
c7b67670 PM |
1121 | now = mftb(); |
1122 | vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) - | |
1123 | vcpu->arch.stolen_logged; | |
1124 | vcpu->arch.busy_preempt = now; | |
1125 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; | |
bf3d32e1 | 1126 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
371fefd6 | 1127 | --vc->n_runnable; |
371fefd6 PM |
1128 | list_del(&vcpu->arch.run_list); |
1129 | } | |
1130 | ||
f0888f70 PM |
1131 | static int kvmppc_grab_hwthread(int cpu) |
1132 | { | |
1133 | struct paca_struct *tpaca; | |
1134 | long timeout = 1000; | |
1135 | ||
1136 | tpaca = &paca[cpu]; | |
1137 | ||
1138 | /* Ensure the thread won't go into the kernel if it wakes */ | |
1139 | tpaca->kvm_hstate.hwthread_req = 1; | |
7b444c67 | 1140 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
f0888f70 PM |
1141 | |
1142 | /* | |
1143 | * If the thread is already executing in the kernel (e.g. handling | |
1144 | * a stray interrupt), wait for it to get back to nap mode. | |
1145 | * The smp_mb() is to ensure that our setting of hwthread_req | |
1146 | * is visible before we look at hwthread_state, so if this | |
1147 | * races with the code at system_reset_pSeries and the thread | |
1148 | * misses our setting of hwthread_req, we are sure to see its | |
1149 | * setting of hwthread_state, and vice versa. | |
1150 | */ | |
1151 | smp_mb(); | |
1152 | while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) { | |
1153 | if (--timeout <= 0) { | |
1154 | pr_err("KVM: couldn't grab cpu %d\n", cpu); | |
1155 | return -EBUSY; | |
1156 | } | |
1157 | udelay(1); | |
1158 | } | |
1159 | return 0; | |
1160 | } | |
1161 | ||
1162 | static void kvmppc_release_hwthread(int cpu) | |
1163 | { | |
1164 | struct paca_struct *tpaca; | |
1165 | ||
1166 | tpaca = &paca[cpu]; | |
1167 | tpaca->kvm_hstate.hwthread_req = 0; | |
1168 | tpaca->kvm_hstate.kvm_vcpu = NULL; | |
1169 | } | |
1170 | ||
371fefd6 PM |
1171 | static void kvmppc_start_thread(struct kvm_vcpu *vcpu) |
1172 | { | |
1173 | int cpu; | |
1174 | struct paca_struct *tpaca; | |
1175 | struct kvmppc_vcore *vc = vcpu->arch.vcore; | |
1176 | ||
19ccb76a PM |
1177 | if (vcpu->arch.timer_running) { |
1178 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
1179 | vcpu->arch.timer_running = 0; | |
1180 | } | |
371fefd6 PM |
1181 | cpu = vc->pcpu + vcpu->arch.ptid; |
1182 | tpaca = &paca[cpu]; | |
1183 | tpaca->kvm_hstate.kvm_vcpu = vcpu; | |
1184 | tpaca->kvm_hstate.kvm_vcore = vc; | |
19ccb76a PM |
1185 | tpaca->kvm_hstate.napping = 0; |
1186 | vcpu->cpu = vc->pcpu; | |
371fefd6 | 1187 | smp_wmb(); |
251da038 | 1188 | #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) |
371fefd6 | 1189 | if (vcpu->arch.ptid) { |
371fefd6 PM |
1190 | xics_wake_cpu(cpu); |
1191 | ++vc->n_woken; | |
de56a948 | 1192 | } |
371fefd6 PM |
1193 | #endif |
1194 | } | |
de56a948 | 1195 | |
371fefd6 PM |
1196 | static void kvmppc_wait_for_nap(struct kvmppc_vcore *vc) |
1197 | { | |
1198 | int i; | |
1199 | ||
1200 | HMT_low(); | |
1201 | i = 0; | |
1202 | while (vc->nap_count < vc->n_woken) { | |
1203 | if (++i >= 1000000) { | |
1204 | pr_err("kvmppc_wait_for_nap timeout %d %d\n", | |
1205 | vc->nap_count, vc->n_woken); | |
1206 | break; | |
1207 | } | |
1208 | cpu_relax(); | |
1209 | } | |
1210 | HMT_medium(); | |
1211 | } | |
1212 | ||
1213 | /* | |
1214 | * Check that we are on thread 0 and that any other threads in | |
7b444c67 PM |
1215 | * this core are off-line. Then grab the threads so they can't |
1216 | * enter the kernel. | |
371fefd6 PM |
1217 | */ |
1218 | static int on_primary_thread(void) | |
1219 | { | |
1220 | int cpu = smp_processor_id(); | |
1221 | int thr = cpu_thread_in_core(cpu); | |
1222 | ||
1223 | if (thr) | |
1224 | return 0; | |
1225 | while (++thr < threads_per_core) | |
1226 | if (cpu_online(cpu + thr)) | |
1227 | return 0; | |
7b444c67 PM |
1228 | |
1229 | /* Grab all hw threads so they can't go into the kernel */ | |
1230 | for (thr = 1; thr < threads_per_core; ++thr) { | |
1231 | if (kvmppc_grab_hwthread(cpu + thr)) { | |
1232 | /* Couldn't grab one; let the others go */ | |
1233 | do { | |
1234 | kvmppc_release_hwthread(cpu + thr); | |
1235 | } while (--thr > 0); | |
1236 | return 0; | |
1237 | } | |
1238 | } | |
371fefd6 PM |
1239 | return 1; |
1240 | } | |
1241 | ||
1242 | /* | |
1243 | * Run a set of guest threads on a physical core. | |
1244 | * Called with vc->lock held. | |
1245 | */ | |
913d3ff9 | 1246 | static void kvmppc_run_core(struct kvmppc_vcore *vc) |
371fefd6 | 1247 | { |
19ccb76a | 1248 | struct kvm_vcpu *vcpu, *vcpu0, *vnext; |
371fefd6 PM |
1249 | long ret; |
1250 | u64 now; | |
081f323b | 1251 | int ptid, i, need_vpa_update; |
2c9097e4 | 1252 | int srcu_idx; |
913d3ff9 | 1253 | struct kvm_vcpu *vcpus_to_update[threads_per_core]; |
371fefd6 PM |
1254 | |
1255 | /* don't start if any threads have a signal pending */ | |
081f323b PM |
1256 | need_vpa_update = 0; |
1257 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { | |
371fefd6 | 1258 | if (signal_pending(vcpu->arch.run_task)) |
913d3ff9 PM |
1259 | return; |
1260 | if (vcpu->arch.vpa.update_pending || | |
1261 | vcpu->arch.slb_shadow.update_pending || | |
1262 | vcpu->arch.dtl.update_pending) | |
1263 | vcpus_to_update[need_vpa_update++] = vcpu; | |
081f323b PM |
1264 | } |
1265 | ||
1266 | /* | |
1267 | * Initialize *vc, in particular vc->vcore_state, so we can | |
1268 | * drop the vcore lock if necessary. | |
1269 | */ | |
1270 | vc->n_woken = 0; | |
1271 | vc->nap_count = 0; | |
1272 | vc->entry_exit_count = 0; | |
2f12f034 | 1273 | vc->vcore_state = VCORE_STARTING; |
081f323b PM |
1274 | vc->in_guest = 0; |
1275 | vc->napping_threads = 0; | |
1276 | ||
1277 | /* | |
1278 | * Updating any of the vpas requires calling kvmppc_pin_guest_page, | |
1279 | * which can't be called with any spinlocks held. | |
1280 | */ | |
1281 | if (need_vpa_update) { | |
1282 | spin_unlock(&vc->lock); | |
913d3ff9 PM |
1283 | for (i = 0; i < need_vpa_update; ++i) |
1284 | kvmppc_update_vpas(vcpus_to_update[i]); | |
081f323b PM |
1285 | spin_lock(&vc->lock); |
1286 | } | |
de56a948 | 1287 | |
19ccb76a PM |
1288 | /* |
1289 | * Assign physical thread IDs, first to non-ceded vcpus | |
1290 | * and then to ceded ones. | |
1291 | */ | |
1292 | ptid = 0; | |
1293 | vcpu0 = NULL; | |
1294 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { | |
1295 | if (!vcpu->arch.ceded) { | |
1296 | if (!ptid) | |
1297 | vcpu0 = vcpu; | |
1298 | vcpu->arch.ptid = ptid++; | |
1299 | } | |
1300 | } | |
c7b67670 PM |
1301 | if (!vcpu0) |
1302 | goto out; /* nothing to run; should never happen */ | |
19ccb76a PM |
1303 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) |
1304 | if (vcpu->arch.ceded) | |
1305 | vcpu->arch.ptid = ptid++; | |
1306 | ||
7b444c67 PM |
1307 | /* |
1308 | * Make sure we are running on thread 0, and that | |
1309 | * secondary threads are offline. | |
1310 | */ | |
1311 | if (threads_per_core > 1 && !on_primary_thread()) { | |
1312 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) | |
1313 | vcpu->arch.ret = -EBUSY; | |
1314 | goto out; | |
1315 | } | |
1316 | ||
371fefd6 | 1317 | vc->pcpu = smp_processor_id(); |
2e25aa5f | 1318 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
371fefd6 | 1319 | kvmppc_start_thread(vcpu); |
0456ec4f | 1320 | kvmppc_create_dtl_entry(vcpu, vc); |
2e25aa5f | 1321 | } |
371fefd6 | 1322 | |
2f12f034 | 1323 | vc->vcore_state = VCORE_RUNNING; |
19ccb76a | 1324 | preempt_disable(); |
371fefd6 | 1325 | spin_unlock(&vc->lock); |
de56a948 | 1326 | |
371fefd6 | 1327 | kvm_guest_enter(); |
2c9097e4 PM |
1328 | |
1329 | srcu_idx = srcu_read_lock(&vcpu0->kvm->srcu); | |
1330 | ||
19ccb76a | 1331 | __kvmppc_vcore_entry(NULL, vcpu0); |
de56a948 | 1332 | |
371fefd6 | 1333 | spin_lock(&vc->lock); |
19ccb76a PM |
1334 | /* disable sending of IPIs on virtual external irqs */ |
1335 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) | |
1336 | vcpu->cpu = -1; | |
1337 | /* wait for secondary threads to finish writing their state to memory */ | |
371fefd6 PM |
1338 | if (vc->nap_count < vc->n_woken) |
1339 | kvmppc_wait_for_nap(vc); | |
2f12f034 PM |
1340 | for (i = 0; i < threads_per_core; ++i) |
1341 | kvmppc_release_hwthread(vc->pcpu + i); | |
371fefd6 | 1342 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ |
19ccb76a | 1343 | vc->vcore_state = VCORE_EXITING; |
371fefd6 PM |
1344 | spin_unlock(&vc->lock); |
1345 | ||
2c9097e4 PM |
1346 | srcu_read_unlock(&vcpu0->kvm->srcu, srcu_idx); |
1347 | ||
371fefd6 PM |
1348 | /* make sure updates to secondary vcpu structs are visible now */ |
1349 | smp_mb(); | |
de56a948 PM |
1350 | kvm_guest_exit(); |
1351 | ||
1352 | preempt_enable(); | |
1353 | kvm_resched(vcpu); | |
1354 | ||
913d3ff9 | 1355 | spin_lock(&vc->lock); |
de56a948 | 1356 | now = get_tb(); |
371fefd6 PM |
1357 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
1358 | /* cancel pending dec exception if dec is positive */ | |
1359 | if (now < vcpu->arch.dec_expires && | |
1360 | kvmppc_core_pending_dec(vcpu)) | |
1361 | kvmppc_core_dequeue_dec(vcpu); | |
19ccb76a PM |
1362 | |
1363 | ret = RESUME_GUEST; | |
1364 | if (vcpu->arch.trap) | |
3a167bea AK |
1365 | ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu, |
1366 | vcpu->arch.run_task); | |
19ccb76a | 1367 | |
371fefd6 PM |
1368 | vcpu->arch.ret = ret; |
1369 | vcpu->arch.trap = 0; | |
19ccb76a PM |
1370 | |
1371 | if (vcpu->arch.ceded) { | |
1372 | if (ret != RESUME_GUEST) | |
1373 | kvmppc_end_cede(vcpu); | |
1374 | else | |
1375 | kvmppc_set_timer(vcpu); | |
1376 | } | |
371fefd6 | 1377 | } |
de56a948 PM |
1378 | |
1379 | out: | |
19ccb76a | 1380 | vc->vcore_state = VCORE_INACTIVE; |
371fefd6 PM |
1381 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, |
1382 | arch.run_list) { | |
1383 | if (vcpu->arch.ret != RESUME_GUEST) { | |
1384 | kvmppc_remove_runnable(vc, vcpu); | |
1385 | wake_up(&vcpu->arch.cpu_run); | |
1386 | } | |
1387 | } | |
371fefd6 PM |
1388 | } |
1389 | ||
19ccb76a PM |
1390 | /* |
1391 | * Wait for some other vcpu thread to execute us, and | |
1392 | * wake us up when we need to handle something in the host. | |
1393 | */ | |
1394 | static void kvmppc_wait_for_exec(struct kvm_vcpu *vcpu, int wait_state) | |
371fefd6 | 1395 | { |
371fefd6 PM |
1396 | DEFINE_WAIT(wait); |
1397 | ||
19ccb76a PM |
1398 | prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); |
1399 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) | |
1400 | schedule(); | |
1401 | finish_wait(&vcpu->arch.cpu_run, &wait); | |
1402 | } | |
1403 | ||
1404 | /* | |
1405 | * All the vcpus in this vcore are idle, so wait for a decrementer | |
1406 | * or external interrupt to one of the vcpus. vc->lock is held. | |
1407 | */ | |
1408 | static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) | |
1409 | { | |
1410 | DEFINE_WAIT(wait); | |
19ccb76a PM |
1411 | |
1412 | prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE); | |
1413 | vc->vcore_state = VCORE_SLEEPING; | |
1414 | spin_unlock(&vc->lock); | |
913d3ff9 | 1415 | schedule(); |
19ccb76a PM |
1416 | finish_wait(&vc->wq, &wait); |
1417 | spin_lock(&vc->lock); | |
1418 | vc->vcore_state = VCORE_INACTIVE; | |
1419 | } | |
371fefd6 | 1420 | |
19ccb76a PM |
1421 | static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
1422 | { | |
1423 | int n_ceded; | |
19ccb76a PM |
1424 | struct kvmppc_vcore *vc; |
1425 | struct kvm_vcpu *v, *vn; | |
9e368f29 | 1426 | |
371fefd6 PM |
1427 | kvm_run->exit_reason = 0; |
1428 | vcpu->arch.ret = RESUME_GUEST; | |
1429 | vcpu->arch.trap = 0; | |
2f12f034 | 1430 | kvmppc_update_vpas(vcpu); |
371fefd6 | 1431 | |
371fefd6 PM |
1432 | /* |
1433 | * Synchronize with other threads in this virtual core | |
1434 | */ | |
1435 | vc = vcpu->arch.vcore; | |
1436 | spin_lock(&vc->lock); | |
19ccb76a | 1437 | vcpu->arch.ceded = 0; |
371fefd6 PM |
1438 | vcpu->arch.run_task = current; |
1439 | vcpu->arch.kvm_run = kvm_run; | |
c7b67670 | 1440 | vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); |
19ccb76a | 1441 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; |
c7b67670 | 1442 | vcpu->arch.busy_preempt = TB_NIL; |
371fefd6 PM |
1443 | list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads); |
1444 | ++vc->n_runnable; | |
1445 | ||
19ccb76a PM |
1446 | /* |
1447 | * This happens the first time this is called for a vcpu. | |
1448 | * If the vcore is already running, we may be able to start | |
1449 | * this thread straight away and have it join in. | |
1450 | */ | |
8455d79e | 1451 | if (!signal_pending(current)) { |
19ccb76a PM |
1452 | if (vc->vcore_state == VCORE_RUNNING && |
1453 | VCORE_EXIT_COUNT(vc) == 0) { | |
1454 | vcpu->arch.ptid = vc->n_runnable - 1; | |
2f12f034 | 1455 | kvmppc_create_dtl_entry(vcpu, vc); |
19ccb76a | 1456 | kvmppc_start_thread(vcpu); |
8455d79e PM |
1457 | } else if (vc->vcore_state == VCORE_SLEEPING) { |
1458 | wake_up(&vc->wq); | |
371fefd6 PM |
1459 | } |
1460 | ||
8455d79e | 1461 | } |
371fefd6 | 1462 | |
19ccb76a PM |
1463 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
1464 | !signal_pending(current)) { | |
8455d79e | 1465 | if (vc->vcore_state != VCORE_INACTIVE) { |
19ccb76a PM |
1466 | spin_unlock(&vc->lock); |
1467 | kvmppc_wait_for_exec(vcpu, TASK_INTERRUPTIBLE); | |
1468 | spin_lock(&vc->lock); | |
1469 | continue; | |
1470 | } | |
19ccb76a PM |
1471 | list_for_each_entry_safe(v, vn, &vc->runnable_threads, |
1472 | arch.run_list) { | |
7e28e60e | 1473 | kvmppc_core_prepare_to_enter(v); |
19ccb76a PM |
1474 | if (signal_pending(v->arch.run_task)) { |
1475 | kvmppc_remove_runnable(vc, v); | |
1476 | v->stat.signal_exits++; | |
1477 | v->arch.kvm_run->exit_reason = KVM_EXIT_INTR; | |
1478 | v->arch.ret = -EINTR; | |
1479 | wake_up(&v->arch.cpu_run); | |
1480 | } | |
1481 | } | |
8455d79e PM |
1482 | if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
1483 | break; | |
1484 | vc->runner = vcpu; | |
1485 | n_ceded = 0; | |
4619ac88 | 1486 | list_for_each_entry(v, &vc->runnable_threads, arch.run_list) { |
8455d79e PM |
1487 | if (!v->arch.pending_exceptions) |
1488 | n_ceded += v->arch.ceded; | |
4619ac88 PM |
1489 | else |
1490 | v->arch.ceded = 0; | |
1491 | } | |
8455d79e PM |
1492 | if (n_ceded == vc->n_runnable) |
1493 | kvmppc_vcore_blocked(vc); | |
1494 | else | |
1495 | kvmppc_run_core(vc); | |
0456ec4f | 1496 | vc->runner = NULL; |
19ccb76a | 1497 | } |
371fefd6 | 1498 | |
8455d79e PM |
1499 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
1500 | (vc->vcore_state == VCORE_RUNNING || | |
1501 | vc->vcore_state == VCORE_EXITING)) { | |
1502 | spin_unlock(&vc->lock); | |
1503 | kvmppc_wait_for_exec(vcpu, TASK_UNINTERRUPTIBLE); | |
1504 | spin_lock(&vc->lock); | |
1505 | } | |
1506 | ||
1507 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { | |
1508 | kvmppc_remove_runnable(vc, vcpu); | |
1509 | vcpu->stat.signal_exits++; | |
1510 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
1511 | vcpu->arch.ret = -EINTR; | |
1512 | } | |
1513 | ||
1514 | if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) { | |
1515 | /* Wake up some vcpu to run the core */ | |
1516 | v = list_first_entry(&vc->runnable_threads, | |
1517 | struct kvm_vcpu, arch.run_list); | |
1518 | wake_up(&v->arch.cpu_run); | |
371fefd6 PM |
1519 | } |
1520 | ||
371fefd6 | 1521 | spin_unlock(&vc->lock); |
371fefd6 | 1522 | return vcpu->arch.ret; |
de56a948 PM |
1523 | } |
1524 | ||
3a167bea | 1525 | static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu) |
a8606e20 PM |
1526 | { |
1527 | int r; | |
913d3ff9 | 1528 | int srcu_idx; |
a8606e20 | 1529 | |
af8f38b3 AG |
1530 | if (!vcpu->arch.sane) { |
1531 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
1532 | return -EINVAL; | |
1533 | } | |
1534 | ||
25051b5a SW |
1535 | kvmppc_core_prepare_to_enter(vcpu); |
1536 | ||
19ccb76a PM |
1537 | /* No need to go into the guest when all we'll do is come back out */ |
1538 | if (signal_pending(current)) { | |
1539 | run->exit_reason = KVM_EXIT_INTR; | |
1540 | return -EINTR; | |
1541 | } | |
1542 | ||
32fad281 PM |
1543 | atomic_inc(&vcpu->kvm->arch.vcpus_running); |
1544 | /* Order vcpus_running vs. rma_setup_done, see kvmppc_alloc_reset_hpt */ | |
1545 | smp_mb(); | |
1546 | ||
1547 | /* On the first time here, set up HTAB and VRMA or RMA */ | |
c77162de | 1548 | if (!vcpu->kvm->arch.rma_setup_done) { |
32fad281 | 1549 | r = kvmppc_hv_setup_htab_rma(vcpu); |
c77162de | 1550 | if (r) |
32fad281 | 1551 | goto out; |
c77162de | 1552 | } |
19ccb76a PM |
1553 | |
1554 | flush_fp_to_thread(current); | |
1555 | flush_altivec_to_thread(current); | |
1556 | flush_vsx_to_thread(current); | |
1557 | vcpu->arch.wqp = &vcpu->arch.vcore->wq; | |
342d3db7 | 1558 | vcpu->arch.pgdir = current->mm->pgd; |
c7b67670 | 1559 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
19ccb76a | 1560 | |
a8606e20 PM |
1561 | do { |
1562 | r = kvmppc_run_vcpu(run, vcpu); | |
1563 | ||
1564 | if (run->exit_reason == KVM_EXIT_PAPR_HCALL && | |
1565 | !(vcpu->arch.shregs.msr & MSR_PR)) { | |
1566 | r = kvmppc_pseries_do_hcall(vcpu); | |
7e28e60e | 1567 | kvmppc_core_prepare_to_enter(vcpu); |
913d3ff9 PM |
1568 | } else if (r == RESUME_PAGE_FAULT) { |
1569 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); | |
1570 | r = kvmppc_book3s_hv_page_fault(run, vcpu, | |
1571 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); | |
1572 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); | |
a8606e20 PM |
1573 | } |
1574 | } while (r == RESUME_GUEST); | |
32fad281 PM |
1575 | |
1576 | out: | |
c7b67670 | 1577 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
32fad281 | 1578 | atomic_dec(&vcpu->kvm->arch.vcpus_running); |
a8606e20 PM |
1579 | return r; |
1580 | } | |
1581 | ||
54738c09 | 1582 | |
aa04b4cc | 1583 | /* Work out RMLS (real mode limit selector) field value for a given RMA size. |
9e368f29 | 1584 | Assumes POWER7 or PPC970. */ |
aa04b4cc PM |
1585 | static inline int lpcr_rmls(unsigned long rma_size) |
1586 | { | |
1587 | switch (rma_size) { | |
1588 | case 32ul << 20: /* 32 MB */ | |
9e368f29 PM |
1589 | if (cpu_has_feature(CPU_FTR_ARCH_206)) |
1590 | return 8; /* only supported on POWER7 */ | |
1591 | return -1; | |
aa04b4cc PM |
1592 | case 64ul << 20: /* 64 MB */ |
1593 | return 3; | |
1594 | case 128ul << 20: /* 128 MB */ | |
1595 | return 7; | |
1596 | case 256ul << 20: /* 256 MB */ | |
1597 | return 4; | |
1598 | case 1ul << 30: /* 1 GB */ | |
1599 | return 2; | |
1600 | case 16ul << 30: /* 16 GB */ | |
1601 | return 1; | |
1602 | case 256ul << 30: /* 256 GB */ | |
1603 | return 0; | |
1604 | default: | |
1605 | return -1; | |
1606 | } | |
1607 | } | |
1608 | ||
1609 | static int kvm_rma_fault(struct vm_area_struct *vma, struct vm_fault *vmf) | |
1610 | { | |
aa04b4cc | 1611 | struct page *page; |
6c45b810 | 1612 | struct kvm_rma_info *ri = vma->vm_file->private_data; |
aa04b4cc | 1613 | |
6c45b810 | 1614 | if (vmf->pgoff >= kvm_rma_pages) |
aa04b4cc PM |
1615 | return VM_FAULT_SIGBUS; |
1616 | ||
1617 | page = pfn_to_page(ri->base_pfn + vmf->pgoff); | |
1618 | get_page(page); | |
1619 | vmf->page = page; | |
1620 | return 0; | |
1621 | } | |
1622 | ||
1623 | static const struct vm_operations_struct kvm_rma_vm_ops = { | |
1624 | .fault = kvm_rma_fault, | |
1625 | }; | |
1626 | ||
1627 | static int kvm_rma_mmap(struct file *file, struct vm_area_struct *vma) | |
1628 | { | |
314e51b9 | 1629 | vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP; |
aa04b4cc PM |
1630 | vma->vm_ops = &kvm_rma_vm_ops; |
1631 | return 0; | |
1632 | } | |
1633 | ||
1634 | static int kvm_rma_release(struct inode *inode, struct file *filp) | |
1635 | { | |
6c45b810 | 1636 | struct kvm_rma_info *ri = filp->private_data; |
aa04b4cc PM |
1637 | |
1638 | kvm_release_rma(ri); | |
1639 | return 0; | |
1640 | } | |
1641 | ||
75ef9de1 | 1642 | static const struct file_operations kvm_rma_fops = { |
aa04b4cc PM |
1643 | .mmap = kvm_rma_mmap, |
1644 | .release = kvm_rma_release, | |
1645 | }; | |
1646 | ||
3a167bea AK |
1647 | static long kvm_vm_ioctl_allocate_rma(struct kvm *kvm, |
1648 | struct kvm_allocate_rma *ret) | |
aa04b4cc | 1649 | { |
aa04b4cc | 1650 | long fd; |
6c45b810 AK |
1651 | struct kvm_rma_info *ri; |
1652 | /* | |
1653 | * Only do this on PPC970 in HV mode | |
1654 | */ | |
1655 | if (!cpu_has_feature(CPU_FTR_HVMODE) || | |
1656 | !cpu_has_feature(CPU_FTR_ARCH_201)) | |
1657 | return -EINVAL; | |
1658 | ||
1659 | if (!kvm_rma_pages) | |
1660 | return -EINVAL; | |
aa04b4cc PM |
1661 | |
1662 | ri = kvm_alloc_rma(); | |
1663 | if (!ri) | |
1664 | return -ENOMEM; | |
1665 | ||
2f84d5ea | 1666 | fd = anon_inode_getfd("kvm-rma", &kvm_rma_fops, ri, O_RDWR | O_CLOEXEC); |
aa04b4cc PM |
1667 | if (fd < 0) |
1668 | kvm_release_rma(ri); | |
1669 | ||
6c45b810 | 1670 | ret->rma_size = kvm_rma_pages << PAGE_SHIFT; |
aa04b4cc PM |
1671 | return fd; |
1672 | } | |
1673 | ||
5b74716e BH |
1674 | static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps, |
1675 | int linux_psize) | |
1676 | { | |
1677 | struct mmu_psize_def *def = &mmu_psize_defs[linux_psize]; | |
1678 | ||
1679 | if (!def->shift) | |
1680 | return; | |
1681 | (*sps)->page_shift = def->shift; | |
1682 | (*sps)->slb_enc = def->sllp; | |
1683 | (*sps)->enc[0].page_shift = def->shift; | |
b1022fbd AK |
1684 | /* |
1685 | * Only return base page encoding. We don't want to return | |
1686 | * all the supporting pte_enc, because our H_ENTER doesn't | |
1687 | * support MPSS yet. Once they do, we can start passing all | |
1688 | * support pte_enc here | |
1689 | */ | |
1690 | (*sps)->enc[0].pte_enc = def->penc[linux_psize]; | |
5b74716e BH |
1691 | (*sps)++; |
1692 | } | |
1693 | ||
3a167bea AK |
1694 | static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, |
1695 | struct kvm_ppc_smmu_info *info) | |
5b74716e BH |
1696 | { |
1697 | struct kvm_ppc_one_seg_page_size *sps; | |
1698 | ||
1699 | info->flags = KVM_PPC_PAGE_SIZES_REAL; | |
1700 | if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) | |
1701 | info->flags |= KVM_PPC_1T_SEGMENTS; | |
1702 | info->slb_size = mmu_slb_size; | |
1703 | ||
1704 | /* We only support these sizes for now, and no muti-size segments */ | |
1705 | sps = &info->sps[0]; | |
1706 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K); | |
1707 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K); | |
1708 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M); | |
1709 | ||
1710 | return 0; | |
1711 | } | |
1712 | ||
82ed3616 PM |
1713 | /* |
1714 | * Get (and clear) the dirty memory log for a memory slot. | |
1715 | */ | |
3a167bea AK |
1716 | static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm, |
1717 | struct kvm_dirty_log *log) | |
82ed3616 PM |
1718 | { |
1719 | struct kvm_memory_slot *memslot; | |
1720 | int r; | |
1721 | unsigned long n; | |
1722 | ||
1723 | mutex_lock(&kvm->slots_lock); | |
1724 | ||
1725 | r = -EINVAL; | |
bbacc0c1 | 1726 | if (log->slot >= KVM_USER_MEM_SLOTS) |
82ed3616 PM |
1727 | goto out; |
1728 | ||
1729 | memslot = id_to_memslot(kvm->memslots, log->slot); | |
1730 | r = -ENOENT; | |
1731 | if (!memslot->dirty_bitmap) | |
1732 | goto out; | |
1733 | ||
1734 | n = kvm_dirty_bitmap_bytes(memslot); | |
1735 | memset(memslot->dirty_bitmap, 0, n); | |
1736 | ||
dfe49dbd | 1737 | r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap); |
82ed3616 PM |
1738 | if (r) |
1739 | goto out; | |
1740 | ||
1741 | r = -EFAULT; | |
1742 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
1743 | goto out; | |
1744 | ||
1745 | r = 0; | |
1746 | out: | |
1747 | mutex_unlock(&kvm->slots_lock); | |
1748 | return r; | |
1749 | } | |
1750 | ||
a66b48c3 | 1751 | static void unpin_slot(struct kvm_memory_slot *memslot) |
de56a948 | 1752 | { |
a66b48c3 PM |
1753 | unsigned long *physp; |
1754 | unsigned long j, npages, pfn; | |
1755 | struct page *page; | |
aa04b4cc | 1756 | |
a66b48c3 PM |
1757 | physp = memslot->arch.slot_phys; |
1758 | npages = memslot->npages; | |
1759 | if (!physp) | |
1760 | return; | |
1761 | for (j = 0; j < npages; j++) { | |
1762 | if (!(physp[j] & KVMPPC_GOT_PAGE)) | |
1763 | continue; | |
1764 | pfn = physp[j] >> PAGE_SHIFT; | |
1765 | page = pfn_to_page(pfn); | |
1766 | SetPageDirty(page); | |
1767 | put_page(page); | |
1768 | } | |
1769 | } | |
1770 | ||
3a167bea AK |
1771 | static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, |
1772 | struct kvm_memory_slot *dont) | |
a66b48c3 PM |
1773 | { |
1774 | if (!dont || free->arch.rmap != dont->arch.rmap) { | |
1775 | vfree(free->arch.rmap); | |
1776 | free->arch.rmap = NULL; | |
b2b2f165 | 1777 | } |
a66b48c3 PM |
1778 | if (!dont || free->arch.slot_phys != dont->arch.slot_phys) { |
1779 | unpin_slot(free); | |
1780 | vfree(free->arch.slot_phys); | |
1781 | free->arch.slot_phys = NULL; | |
1782 | } | |
1783 | } | |
1784 | ||
3a167bea AK |
1785 | static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot, |
1786 | unsigned long npages) | |
a66b48c3 PM |
1787 | { |
1788 | slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap)); | |
1789 | if (!slot->arch.rmap) | |
1790 | return -ENOMEM; | |
1791 | slot->arch.slot_phys = NULL; | |
aa04b4cc | 1792 | |
c77162de PM |
1793 | return 0; |
1794 | } | |
aa04b4cc | 1795 | |
3a167bea AK |
1796 | static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, |
1797 | struct kvm_memory_slot *memslot, | |
1798 | struct kvm_userspace_memory_region *mem) | |
c77162de | 1799 | { |
a66b48c3 | 1800 | unsigned long *phys; |
c77162de | 1801 | |
a66b48c3 PM |
1802 | /* Allocate a slot_phys array if needed */ |
1803 | phys = memslot->arch.slot_phys; | |
1804 | if (!kvm->arch.using_mmu_notifiers && !phys && memslot->npages) { | |
1805 | phys = vzalloc(memslot->npages * sizeof(unsigned long)); | |
1806 | if (!phys) | |
1807 | return -ENOMEM; | |
1808 | memslot->arch.slot_phys = phys; | |
aa04b4cc | 1809 | } |
a66b48c3 PM |
1810 | |
1811 | return 0; | |
c77162de PM |
1812 | } |
1813 | ||
3a167bea AK |
1814 | static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, |
1815 | struct kvm_userspace_memory_region *mem, | |
1816 | const struct kvm_memory_slot *old) | |
c77162de | 1817 | { |
dfe49dbd PM |
1818 | unsigned long npages = mem->memory_size >> PAGE_SHIFT; |
1819 | struct kvm_memory_slot *memslot; | |
1820 | ||
8482644a | 1821 | if (npages && old->npages) { |
dfe49dbd PM |
1822 | /* |
1823 | * If modifying a memslot, reset all the rmap dirty bits. | |
1824 | * If this is a new memslot, we don't need to do anything | |
1825 | * since the rmap array starts out as all zeroes, | |
1826 | * i.e. no pages are dirty. | |
1827 | */ | |
1828 | memslot = id_to_memslot(kvm->memslots, mem->slot); | |
1829 | kvmppc_hv_get_dirty_log(kvm, memslot, NULL); | |
1830 | } | |
c77162de PM |
1831 | } |
1832 | ||
a0144e2a PM |
1833 | /* |
1834 | * Update LPCR values in kvm->arch and in vcores. | |
1835 | * Caller must hold kvm->lock. | |
1836 | */ | |
1837 | void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask) | |
1838 | { | |
1839 | long int i; | |
1840 | u32 cores_done = 0; | |
1841 | ||
1842 | if ((kvm->arch.lpcr & mask) == lpcr) | |
1843 | return; | |
1844 | ||
1845 | kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr; | |
1846 | ||
1847 | for (i = 0; i < KVM_MAX_VCORES; ++i) { | |
1848 | struct kvmppc_vcore *vc = kvm->arch.vcores[i]; | |
1849 | if (!vc) | |
1850 | continue; | |
1851 | spin_lock(&vc->lock); | |
1852 | vc->lpcr = (vc->lpcr & ~mask) | lpcr; | |
1853 | spin_unlock(&vc->lock); | |
1854 | if (++cores_done >= kvm->arch.online_vcores) | |
1855 | break; | |
1856 | } | |
1857 | } | |
1858 | ||
3a167bea AK |
1859 | static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu) |
1860 | { | |
1861 | return; | |
1862 | } | |
1863 | ||
32fad281 | 1864 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) |
c77162de PM |
1865 | { |
1866 | int err = 0; | |
1867 | struct kvm *kvm = vcpu->kvm; | |
6c45b810 | 1868 | struct kvm_rma_info *ri = NULL; |
c77162de PM |
1869 | unsigned long hva; |
1870 | struct kvm_memory_slot *memslot; | |
1871 | struct vm_area_struct *vma; | |
a0144e2a PM |
1872 | unsigned long lpcr = 0, senc; |
1873 | unsigned long lpcr_mask = 0; | |
c77162de PM |
1874 | unsigned long psize, porder; |
1875 | unsigned long rma_size; | |
1876 | unsigned long rmls; | |
1877 | unsigned long *physp; | |
da9d1d7f | 1878 | unsigned long i, npages; |
2c9097e4 | 1879 | int srcu_idx; |
c77162de PM |
1880 | |
1881 | mutex_lock(&kvm->lock); | |
1882 | if (kvm->arch.rma_setup_done) | |
1883 | goto out; /* another vcpu beat us to it */ | |
aa04b4cc | 1884 | |
32fad281 PM |
1885 | /* Allocate hashed page table (if not done already) and reset it */ |
1886 | if (!kvm->arch.hpt_virt) { | |
1887 | err = kvmppc_alloc_hpt(kvm, NULL); | |
1888 | if (err) { | |
1889 | pr_err("KVM: Couldn't alloc HPT\n"); | |
1890 | goto out; | |
1891 | } | |
1892 | } | |
1893 | ||
c77162de | 1894 | /* Look up the memslot for guest physical address 0 */ |
2c9097e4 | 1895 | srcu_idx = srcu_read_lock(&kvm->srcu); |
c77162de | 1896 | memslot = gfn_to_memslot(kvm, 0); |
aa04b4cc | 1897 | |
c77162de PM |
1898 | /* We must have some memory at 0 by now */ |
1899 | err = -EINVAL; | |
1900 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
2c9097e4 | 1901 | goto out_srcu; |
c77162de PM |
1902 | |
1903 | /* Look up the VMA for the start of this memory slot */ | |
1904 | hva = memslot->userspace_addr; | |
1905 | down_read(¤t->mm->mmap_sem); | |
1906 | vma = find_vma(current->mm, hva); | |
1907 | if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO)) | |
1908 | goto up_out; | |
1909 | ||
1910 | psize = vma_kernel_pagesize(vma); | |
da9d1d7f | 1911 | porder = __ilog2(psize); |
c77162de PM |
1912 | |
1913 | /* Is this one of our preallocated RMAs? */ | |
1914 | if (vma->vm_file && vma->vm_file->f_op == &kvm_rma_fops && | |
1915 | hva == vma->vm_start) | |
1916 | ri = vma->vm_file->private_data; | |
1917 | ||
1918 | up_read(¤t->mm->mmap_sem); | |
1919 | ||
1920 | if (!ri) { | |
1921 | /* On POWER7, use VRMA; on PPC970, give up */ | |
1922 | err = -EPERM; | |
1923 | if (cpu_has_feature(CPU_FTR_ARCH_201)) { | |
1924 | pr_err("KVM: CPU requires an RMO\n"); | |
2c9097e4 | 1925 | goto out_srcu; |
c77162de PM |
1926 | } |
1927 | ||
da9d1d7f PM |
1928 | /* We can handle 4k, 64k or 16M pages in the VRMA */ |
1929 | err = -EINVAL; | |
1930 | if (!(psize == 0x1000 || psize == 0x10000 || | |
1931 | psize == 0x1000000)) | |
2c9097e4 | 1932 | goto out_srcu; |
da9d1d7f | 1933 | |
c77162de | 1934 | /* Update VRMASD field in the LPCR */ |
da9d1d7f | 1935 | senc = slb_pgsize_encoding(psize); |
697d3899 PM |
1936 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | |
1937 | (VRMA_VSID << SLB_VSID_SHIFT_1T); | |
a0144e2a PM |
1938 | lpcr_mask = LPCR_VRMASD; |
1939 | /* the -4 is to account for senc values starting at 0x10 */ | |
1940 | lpcr = senc << (LPCR_VRMASD_SH - 4); | |
c77162de PM |
1941 | |
1942 | /* Create HPTEs in the hash page table for the VRMA */ | |
da9d1d7f | 1943 | kvmppc_map_vrma(vcpu, memslot, porder); |
c77162de PM |
1944 | |
1945 | } else { | |
1946 | /* Set up to use an RMO region */ | |
6c45b810 | 1947 | rma_size = kvm_rma_pages; |
c77162de PM |
1948 | if (rma_size > memslot->npages) |
1949 | rma_size = memslot->npages; | |
1950 | rma_size <<= PAGE_SHIFT; | |
aa04b4cc | 1951 | rmls = lpcr_rmls(rma_size); |
c77162de | 1952 | err = -EINVAL; |
5d226ae5 | 1953 | if ((long)rmls < 0) { |
c77162de | 1954 | pr_err("KVM: Can't use RMA of 0x%lx bytes\n", rma_size); |
2c9097e4 | 1955 | goto out_srcu; |
aa04b4cc PM |
1956 | } |
1957 | atomic_inc(&ri->use_count); | |
1958 | kvm->arch.rma = ri; | |
9e368f29 PM |
1959 | |
1960 | /* Update LPCR and RMOR */ | |
9e368f29 PM |
1961 | if (cpu_has_feature(CPU_FTR_ARCH_201)) { |
1962 | /* PPC970; insert RMLS value (split field) in HID4 */ | |
a0144e2a PM |
1963 | lpcr_mask = (1ul << HID4_RMLS0_SH) | |
1964 | (3ul << HID4_RMLS2_SH) | HID4_RMOR; | |
1965 | lpcr = ((rmls >> 2) << HID4_RMLS0_SH) | | |
9e368f29 PM |
1966 | ((rmls & 3) << HID4_RMLS2_SH); |
1967 | /* RMOR is also in HID4 */ | |
1968 | lpcr |= ((ri->base_pfn >> (26 - PAGE_SHIFT)) & 0xffff) | |
1969 | << HID4_RMOR_SH; | |
1970 | } else { | |
1971 | /* POWER7 */ | |
a0144e2a PM |
1972 | lpcr_mask = LPCR_VPM0 | LPCR_VRMA_L | LPCR_RMLS; |
1973 | lpcr = rmls << LPCR_RMLS_SH; | |
6c45b810 | 1974 | kvm->arch.rmor = ri->base_pfn << PAGE_SHIFT; |
9e368f29 | 1975 | } |
c77162de | 1976 | pr_info("KVM: Using RMO at %lx size %lx (LPCR = %lx)\n", |
aa04b4cc | 1977 | ri->base_pfn << PAGE_SHIFT, rma_size, lpcr); |
aa04b4cc | 1978 | |
c77162de | 1979 | /* Initialize phys addrs of pages in RMO */ |
6c45b810 | 1980 | npages = kvm_rma_pages; |
da9d1d7f | 1981 | porder = __ilog2(npages); |
a66b48c3 PM |
1982 | physp = memslot->arch.slot_phys; |
1983 | if (physp) { | |
1984 | if (npages > memslot->npages) | |
1985 | npages = memslot->npages; | |
1986 | spin_lock(&kvm->arch.slot_phys_lock); | |
1987 | for (i = 0; i < npages; ++i) | |
1988 | physp[i] = ((ri->base_pfn + i) << PAGE_SHIFT) + | |
1989 | porder; | |
1990 | spin_unlock(&kvm->arch.slot_phys_lock); | |
1991 | } | |
aa04b4cc PM |
1992 | } |
1993 | ||
a0144e2a PM |
1994 | kvmppc_update_lpcr(kvm, lpcr, lpcr_mask); |
1995 | ||
c77162de PM |
1996 | /* Order updates to kvm->arch.lpcr etc. vs. rma_setup_done */ |
1997 | smp_wmb(); | |
1998 | kvm->arch.rma_setup_done = 1; | |
1999 | err = 0; | |
2c9097e4 PM |
2000 | out_srcu: |
2001 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
c77162de PM |
2002 | out: |
2003 | mutex_unlock(&kvm->lock); | |
2004 | return err; | |
b2b2f165 | 2005 | |
c77162de PM |
2006 | up_out: |
2007 | up_read(¤t->mm->mmap_sem); | |
505d6421 | 2008 | goto out_srcu; |
de56a948 PM |
2009 | } |
2010 | ||
3a167bea | 2011 | static int kvmppc_core_init_vm_hv(struct kvm *kvm) |
de56a948 | 2012 | { |
32fad281 | 2013 | unsigned long lpcr, lpid; |
de56a948 | 2014 | |
32fad281 PM |
2015 | /* Allocate the guest's logical partition ID */ |
2016 | ||
2017 | lpid = kvmppc_alloc_lpid(); | |
5d226ae5 | 2018 | if ((long)lpid < 0) |
32fad281 PM |
2019 | return -ENOMEM; |
2020 | kvm->arch.lpid = lpid; | |
de56a948 | 2021 | |
1b400ba0 PM |
2022 | /* |
2023 | * Since we don't flush the TLB when tearing down a VM, | |
2024 | * and this lpid might have previously been used, | |
2025 | * make sure we flush on each core before running the new VM. | |
2026 | */ | |
2027 | cpumask_setall(&kvm->arch.need_tlb_flush); | |
2028 | ||
aa04b4cc | 2029 | kvm->arch.rma = NULL; |
aa04b4cc | 2030 | |
9e368f29 | 2031 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); |
aa04b4cc | 2032 | |
9e368f29 PM |
2033 | if (cpu_has_feature(CPU_FTR_ARCH_201)) { |
2034 | /* PPC970; HID4 is effectively the LPCR */ | |
9e368f29 PM |
2035 | kvm->arch.host_lpid = 0; |
2036 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_HID4); | |
2037 | lpcr &= ~((3 << HID4_LPID1_SH) | (0xful << HID4_LPID5_SH)); | |
2038 | lpcr |= ((lpid >> 4) << HID4_LPID1_SH) | | |
2039 | ((lpid & 0xf) << HID4_LPID5_SH); | |
2040 | } else { | |
2041 | /* POWER7; init LPCR for virtual RMA mode */ | |
2042 | kvm->arch.host_lpid = mfspr(SPRN_LPID); | |
2043 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); | |
2044 | lpcr &= LPCR_PECE | LPCR_LPES; | |
2045 | lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | | |
697d3899 PM |
2046 | LPCR_VPM0 | LPCR_VPM1; |
2047 | kvm->arch.vrma_slb_v = SLB_VSID_B_1T | | |
2048 | (VRMA_VSID << SLB_VSID_SHIFT_1T); | |
9e368f29 PM |
2049 | } |
2050 | kvm->arch.lpcr = lpcr; | |
aa04b4cc | 2051 | |
342d3db7 | 2052 | kvm->arch.using_mmu_notifiers = !!cpu_has_feature(CPU_FTR_ARCH_206); |
c77162de | 2053 | spin_lock_init(&kvm->arch.slot_phys_lock); |
512691d4 PM |
2054 | |
2055 | /* | |
2056 | * Don't allow secondary CPU threads to come online | |
2057 | * while any KVM VMs exist. | |
2058 | */ | |
2059 | inhibit_secondary_onlining(); | |
2060 | ||
54738c09 | 2061 | return 0; |
de56a948 PM |
2062 | } |
2063 | ||
f1378b1c PM |
2064 | static void kvmppc_free_vcores(struct kvm *kvm) |
2065 | { | |
2066 | long int i; | |
2067 | ||
2068 | for (i = 0; i < KVM_MAX_VCORES; ++i) | |
2069 | kfree(kvm->arch.vcores[i]); | |
2070 | kvm->arch.online_vcores = 0; | |
2071 | } | |
2072 | ||
3a167bea | 2073 | static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) |
de56a948 | 2074 | { |
512691d4 PM |
2075 | uninhibit_secondary_onlining(); |
2076 | ||
f1378b1c | 2077 | kvmppc_free_vcores(kvm); |
aa04b4cc PM |
2078 | if (kvm->arch.rma) { |
2079 | kvm_release_rma(kvm->arch.rma); | |
2080 | kvm->arch.rma = NULL; | |
2081 | } | |
2082 | ||
de56a948 PM |
2083 | kvmppc_free_hpt(kvm); |
2084 | } | |
2085 | ||
3a167bea AK |
2086 | /* We don't need to emulate any privileged instructions or dcbz */ |
2087 | static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
2088 | unsigned int inst, int *advance) | |
de56a948 | 2089 | { |
3a167bea | 2090 | return EMULATE_FAIL; |
de56a948 PM |
2091 | } |
2092 | ||
3a167bea AK |
2093 | static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn, |
2094 | ulong spr_val) | |
de56a948 PM |
2095 | { |
2096 | return EMULATE_FAIL; | |
2097 | } | |
2098 | ||
3a167bea AK |
2099 | static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, |
2100 | ulong *spr_val) | |
de56a948 PM |
2101 | { |
2102 | return EMULATE_FAIL; | |
2103 | } | |
2104 | ||
3a167bea | 2105 | static int kvmppc_core_check_processor_compat_hv(void) |
de56a948 | 2106 | { |
3a167bea AK |
2107 | if (!cpu_has_feature(CPU_FTR_HVMODE)) |
2108 | return -EIO; | |
2109 | return 0; | |
de56a948 PM |
2110 | } |
2111 | ||
3a167bea AK |
2112 | static long kvm_arch_vm_ioctl_hv(struct file *filp, |
2113 | unsigned int ioctl, unsigned long arg) | |
2114 | { | |
2115 | struct kvm *kvm __maybe_unused = filp->private_data; | |
2116 | void __user *argp = (void __user *)arg; | |
2117 | long r; | |
2118 | ||
2119 | switch (ioctl) { | |
2120 | ||
2121 | case KVM_ALLOCATE_RMA: { | |
2122 | struct kvm_allocate_rma rma; | |
2123 | struct kvm *kvm = filp->private_data; | |
2124 | ||
2125 | r = kvm_vm_ioctl_allocate_rma(kvm, &rma); | |
2126 | if (r >= 0 && copy_to_user(argp, &rma, sizeof(rma))) | |
2127 | r = -EFAULT; | |
2128 | break; | |
2129 | } | |
2130 | ||
2131 | case KVM_PPC_ALLOCATE_HTAB: { | |
2132 | u32 htab_order; | |
2133 | ||
2134 | r = -EFAULT; | |
2135 | if (get_user(htab_order, (u32 __user *)argp)) | |
2136 | break; | |
2137 | r = kvmppc_alloc_reset_hpt(kvm, &htab_order); | |
2138 | if (r) | |
2139 | break; | |
2140 | r = -EFAULT; | |
2141 | if (put_user(htab_order, (u32 __user *)argp)) | |
2142 | break; | |
2143 | r = 0; | |
2144 | break; | |
2145 | } | |
2146 | ||
2147 | case KVM_PPC_GET_HTAB_FD: { | |
2148 | struct kvm_get_htab_fd ghf; | |
2149 | ||
2150 | r = -EFAULT; | |
2151 | if (copy_from_user(&ghf, argp, sizeof(ghf))) | |
2152 | break; | |
2153 | r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf); | |
2154 | break; | |
2155 | } | |
2156 | ||
2157 | default: | |
2158 | r = -ENOTTY; | |
2159 | } | |
2160 | ||
2161 | return r; | |
2162 | } | |
2163 | ||
cbbc58d4 | 2164 | static struct kvmppc_ops kvm_ops_hv = { |
3a167bea AK |
2165 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, |
2166 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, | |
2167 | .get_one_reg = kvmppc_get_one_reg_hv, | |
2168 | .set_one_reg = kvmppc_set_one_reg_hv, | |
2169 | .vcpu_load = kvmppc_core_vcpu_load_hv, | |
2170 | .vcpu_put = kvmppc_core_vcpu_put_hv, | |
2171 | .set_msr = kvmppc_set_msr_hv, | |
2172 | .vcpu_run = kvmppc_vcpu_run_hv, | |
2173 | .vcpu_create = kvmppc_core_vcpu_create_hv, | |
2174 | .vcpu_free = kvmppc_core_vcpu_free_hv, | |
2175 | .check_requests = kvmppc_core_check_requests_hv, | |
2176 | .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv, | |
2177 | .flush_memslot = kvmppc_core_flush_memslot_hv, | |
2178 | .prepare_memory_region = kvmppc_core_prepare_memory_region_hv, | |
2179 | .commit_memory_region = kvmppc_core_commit_memory_region_hv, | |
2180 | .unmap_hva = kvm_unmap_hva_hv, | |
2181 | .unmap_hva_range = kvm_unmap_hva_range_hv, | |
2182 | .age_hva = kvm_age_hva_hv, | |
2183 | .test_age_hva = kvm_test_age_hva_hv, | |
2184 | .set_spte_hva = kvm_set_spte_hva_hv, | |
2185 | .mmu_destroy = kvmppc_mmu_destroy_hv, | |
2186 | .free_memslot = kvmppc_core_free_memslot_hv, | |
2187 | .create_memslot = kvmppc_core_create_memslot_hv, | |
2188 | .init_vm = kvmppc_core_init_vm_hv, | |
2189 | .destroy_vm = kvmppc_core_destroy_vm_hv, | |
3a167bea AK |
2190 | .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv, |
2191 | .emulate_op = kvmppc_core_emulate_op_hv, | |
2192 | .emulate_mtspr = kvmppc_core_emulate_mtspr_hv, | |
2193 | .emulate_mfspr = kvmppc_core_emulate_mfspr_hv, | |
2194 | .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv, | |
2195 | .arch_vm_ioctl = kvm_arch_vm_ioctl_hv, | |
2196 | }; | |
2197 | ||
2198 | static int kvmppc_book3s_init_hv(void) | |
de56a948 PM |
2199 | { |
2200 | int r; | |
cbbc58d4 AK |
2201 | /* |
2202 | * FIXME!! Do we need to check on all cpus ? | |
2203 | */ | |
2204 | r = kvmppc_core_check_processor_compat_hv(); | |
2205 | if (r < 0) | |
de56a948 PM |
2206 | return r; |
2207 | ||
cbbc58d4 AK |
2208 | kvm_ops_hv.owner = THIS_MODULE; |
2209 | kvmppc_hv_ops = &kvm_ops_hv; | |
de56a948 | 2210 | |
cbbc58d4 | 2211 | r = kvmppc_mmu_hv_init(); |
de56a948 PM |
2212 | return r; |
2213 | } | |
2214 | ||
3a167bea | 2215 | static void kvmppc_book3s_exit_hv(void) |
de56a948 | 2216 | { |
cbbc58d4 | 2217 | kvmppc_hv_ops = NULL; |
de56a948 PM |
2218 | } |
2219 | ||
3a167bea AK |
2220 | module_init(kvmppc_book3s_init_hv); |
2221 | module_exit(kvmppc_book3s_exit_hv); | |
2ba9f0d8 | 2222 | MODULE_LICENSE("GPL"); |