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