]>
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> |
e23a808b | 35 | #include <linux/debugfs.h> |
de56a948 PM |
36 | |
37 | #include <asm/reg.h> | |
38 | #include <asm/cputable.h> | |
39 | #include <asm/cacheflush.h> | |
40 | #include <asm/tlbflush.h> | |
41 | #include <asm/uaccess.h> | |
42 | #include <asm/io.h> | |
43 | #include <asm/kvm_ppc.h> | |
44 | #include <asm/kvm_book3s.h> | |
45 | #include <asm/mmu_context.h> | |
46 | #include <asm/lppaca.h> | |
47 | #include <asm/processor.h> | |
371fefd6 | 48 | #include <asm/cputhreads.h> |
aa04b4cc | 49 | #include <asm/page.h> |
de1d9248 | 50 | #include <asm/hvcall.h> |
ae3a197e | 51 | #include <asm/switch_to.h> |
512691d4 | 52 | #include <asm/smp.h> |
66feed61 | 53 | #include <asm/dbell.h> |
de56a948 | 54 | #include <linux/gfp.h> |
de56a948 PM |
55 | #include <linux/vmalloc.h> |
56 | #include <linux/highmem.h> | |
c77162de | 57 | #include <linux/hugetlb.h> |
2ba9f0d8 | 58 | #include <linux/module.h> |
de56a948 | 59 | |
3a167bea AK |
60 | #include "book3s.h" |
61 | ||
3c78f78a SW |
62 | #define CREATE_TRACE_POINTS |
63 | #include "trace_hv.h" | |
64 | ||
de56a948 PM |
65 | /* #define EXIT_DEBUG */ |
66 | /* #define EXIT_DEBUG_SIMPLE */ | |
67 | /* #define EXIT_DEBUG_INT */ | |
68 | ||
913d3ff9 PM |
69 | /* Used to indicate that a guest page fault needs to be handled */ |
70 | #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1) | |
71 | ||
c7b67670 PM |
72 | /* Used as a "null" value for timebase values */ |
73 | #define TB_NIL (~(u64)0) | |
74 | ||
699a0ea0 PM |
75 | static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); |
76 | ||
b4deba5c PM |
77 | static int dynamic_mt_modes = 6; |
78 | module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR); | |
79 | MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)"); | |
ec257165 PM |
80 | static int target_smt_mode; |
81 | module_param(target_smt_mode, int, S_IRUGO | S_IWUSR); | |
82 | MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)"); | |
9678cdaa | 83 | |
19ccb76a | 84 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu); |
32fad281 | 85 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); |
19ccb76a | 86 | |
66feed61 PM |
87 | static bool kvmppc_ipi_thread(int cpu) |
88 | { | |
89 | /* On POWER8 for IPIs to threads in the same core, use msgsnd */ | |
90 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { | |
91 | preempt_disable(); | |
92 | if (cpu_first_thread_sibling(cpu) == | |
93 | cpu_first_thread_sibling(smp_processor_id())) { | |
94 | unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER); | |
95 | msg |= cpu_thread_in_core(cpu); | |
96 | smp_mb(); | |
97 | __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); | |
98 | preempt_enable(); | |
99 | return true; | |
100 | } | |
101 | preempt_enable(); | |
102 | } | |
103 | ||
104 | #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) | |
105 | if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) { | |
106 | xics_wake_cpu(cpu); | |
107 | return true; | |
108 | } | |
109 | #endif | |
110 | ||
111 | return false; | |
112 | } | |
113 | ||
3a167bea | 114 | static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) |
54695c30 | 115 | { |
ec257165 | 116 | int cpu; |
54695c30 BH |
117 | wait_queue_head_t *wqp; |
118 | ||
119 | wqp = kvm_arch_vcpu_wq(vcpu); | |
120 | if (waitqueue_active(wqp)) { | |
121 | wake_up_interruptible(wqp); | |
122 | ++vcpu->stat.halt_wakeup; | |
123 | } | |
124 | ||
ec257165 | 125 | if (kvmppc_ipi_thread(vcpu->arch.thread_cpu)) |
66feed61 | 126 | return; |
54695c30 BH |
127 | |
128 | /* CPU points to the first thread of the core */ | |
ec257165 | 129 | cpu = vcpu->cpu; |
66feed61 PM |
130 | if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu)) |
131 | smp_send_reschedule(cpu); | |
54695c30 BH |
132 | } |
133 | ||
c7b67670 PM |
134 | /* |
135 | * We use the vcpu_load/put functions to measure stolen time. | |
136 | * Stolen time is counted as time when either the vcpu is able to | |
137 | * run as part of a virtual core, but the task running the vcore | |
138 | * is preempted or sleeping, or when the vcpu needs something done | |
139 | * in the kernel by the task running the vcpu, but that task is | |
140 | * preempted or sleeping. Those two things have to be counted | |
141 | * separately, since one of the vcpu tasks will take on the job | |
142 | * of running the core, and the other vcpu tasks in the vcore will | |
143 | * sleep waiting for it to do that, but that sleep shouldn't count | |
144 | * as stolen time. | |
145 | * | |
146 | * Hence we accumulate stolen time when the vcpu can run as part of | |
147 | * a vcore using vc->stolen_tb, and the stolen time when the vcpu | |
148 | * needs its task to do other things in the kernel (for example, | |
149 | * service a page fault) in busy_stolen. We don't accumulate | |
150 | * stolen time for a vcore when it is inactive, or for a vcpu | |
151 | * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of | |
152 | * a misnomer; it means that the vcpu task is not executing in | |
153 | * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in | |
154 | * the kernel. We don't have any way of dividing up that time | |
155 | * between time that the vcpu is genuinely stopped, time that | |
156 | * the task is actively working on behalf of the vcpu, and time | |
157 | * that the task is preempted, so we don't count any of it as | |
158 | * stolen. | |
159 | * | |
160 | * Updates to busy_stolen are protected by arch.tbacct_lock; | |
2711e248 PM |
161 | * updates to vc->stolen_tb are protected by the vcore->stoltb_lock |
162 | * lock. The stolen times are measured in units of timebase ticks. | |
163 | * (Note that the != TB_NIL checks below are purely defensive; | |
164 | * they should never fail.) | |
c7b67670 PM |
165 | */ |
166 | ||
ec257165 PM |
167 | static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc) |
168 | { | |
169 | unsigned long flags; | |
170 | ||
171 | spin_lock_irqsave(&vc->stoltb_lock, flags); | |
172 | vc->preempt_tb = mftb(); | |
173 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); | |
174 | } | |
175 | ||
176 | static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc) | |
177 | { | |
178 | unsigned long flags; | |
179 | ||
180 | spin_lock_irqsave(&vc->stoltb_lock, flags); | |
181 | if (vc->preempt_tb != TB_NIL) { | |
182 | vc->stolen_tb += mftb() - vc->preempt_tb; | |
183 | vc->preempt_tb = TB_NIL; | |
184 | } | |
185 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); | |
186 | } | |
187 | ||
3a167bea | 188 | static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu) |
de56a948 | 189 | { |
0456ec4f | 190 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
bf3d32e1 | 191 | unsigned long flags; |
0456ec4f | 192 | |
2711e248 PM |
193 | /* |
194 | * We can test vc->runner without taking the vcore lock, | |
195 | * because only this task ever sets vc->runner to this | |
196 | * vcpu, and once it is set to this vcpu, only this task | |
197 | * ever sets it to NULL. | |
198 | */ | |
ec257165 PM |
199 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
200 | kvmppc_core_end_stolen(vc); | |
201 | ||
2711e248 | 202 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
c7b67670 PM |
203 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST && |
204 | vcpu->arch.busy_preempt != TB_NIL) { | |
205 | vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt; | |
206 | vcpu->arch.busy_preempt = TB_NIL; | |
207 | } | |
bf3d32e1 | 208 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
de56a948 PM |
209 | } |
210 | ||
3a167bea | 211 | static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu) |
de56a948 | 212 | { |
0456ec4f | 213 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
bf3d32e1 | 214 | unsigned long flags; |
0456ec4f | 215 | |
ec257165 PM |
216 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
217 | kvmppc_core_start_stolen(vc); | |
218 | ||
2711e248 | 219 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
c7b67670 PM |
220 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) |
221 | vcpu->arch.busy_preempt = mftb(); | |
bf3d32e1 | 222 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
de56a948 PM |
223 | } |
224 | ||
3a167bea | 225 | static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr) |
de56a948 | 226 | { |
c20875a3 PM |
227 | /* |
228 | * Check for illegal transactional state bit combination | |
229 | * and if we find it, force the TS field to a safe state. | |
230 | */ | |
231 | if ((msr & MSR_TS_MASK) == MSR_TS_MASK) | |
232 | msr &= ~MSR_TS_MASK; | |
de56a948 | 233 | vcpu->arch.shregs.msr = msr; |
19ccb76a | 234 | kvmppc_end_cede(vcpu); |
de56a948 PM |
235 | } |
236 | ||
5358a963 | 237 | static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr) |
de56a948 PM |
238 | { |
239 | vcpu->arch.pvr = pvr; | |
240 | } | |
241 | ||
5358a963 | 242 | static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) |
388cc6e1 PM |
243 | { |
244 | unsigned long pcr = 0; | |
245 | struct kvmppc_vcore *vc = vcpu->arch.vcore; | |
246 | ||
247 | if (arch_compat) { | |
388cc6e1 PM |
248 | switch (arch_compat) { |
249 | case PVR_ARCH_205: | |
5557ae0e PM |
250 | /* |
251 | * If an arch bit is set in PCR, all the defined | |
252 | * higher-order arch bits also have to be set. | |
253 | */ | |
254 | pcr = PCR_ARCH_206 | PCR_ARCH_205; | |
388cc6e1 PM |
255 | break; |
256 | case PVR_ARCH_206: | |
257 | case PVR_ARCH_206p: | |
5557ae0e PM |
258 | pcr = PCR_ARCH_206; |
259 | break; | |
260 | case PVR_ARCH_207: | |
388cc6e1 PM |
261 | break; |
262 | default: | |
263 | return -EINVAL; | |
264 | } | |
5557ae0e PM |
265 | |
266 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) { | |
267 | /* POWER7 can't emulate POWER8 */ | |
268 | if (!(pcr & PCR_ARCH_206)) | |
269 | return -EINVAL; | |
270 | pcr &= ~PCR_ARCH_206; | |
271 | } | |
388cc6e1 PM |
272 | } |
273 | ||
274 | spin_lock(&vc->lock); | |
275 | vc->arch_compat = arch_compat; | |
276 | vc->pcr = pcr; | |
277 | spin_unlock(&vc->lock); | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
5358a963 | 282 | static void kvmppc_dump_regs(struct kvm_vcpu *vcpu) |
de56a948 PM |
283 | { |
284 | int r; | |
285 | ||
286 | pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); | |
287 | pr_err("pc = %.16lx msr = %.16llx trap = %x\n", | |
288 | vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap); | |
289 | for (r = 0; r < 16; ++r) | |
290 | pr_err("r%2d = %.16lx r%d = %.16lx\n", | |
291 | r, kvmppc_get_gpr(vcpu, r), | |
292 | r+16, kvmppc_get_gpr(vcpu, r+16)); | |
293 | pr_err("ctr = %.16lx lr = %.16lx\n", | |
294 | vcpu->arch.ctr, vcpu->arch.lr); | |
295 | pr_err("srr0 = %.16llx srr1 = %.16llx\n", | |
296 | vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); | |
297 | pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", | |
298 | vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); | |
299 | pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", | |
300 | vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); | |
301 | pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n", | |
302 | vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr); | |
303 | pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); | |
304 | pr_err("fault dar = %.16lx dsisr = %.8x\n", | |
305 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); | |
306 | pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); | |
307 | for (r = 0; r < vcpu->arch.slb_max; ++r) | |
308 | pr_err(" ESID = %.16llx VSID = %.16llx\n", | |
309 | vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); | |
310 | pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n", | |
a0144e2a | 311 | vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1, |
de56a948 PM |
312 | vcpu->arch.last_inst); |
313 | } | |
314 | ||
5358a963 | 315 | static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) |
a8606e20 | 316 | { |
e09fefde | 317 | struct kvm_vcpu *ret; |
a8606e20 PM |
318 | |
319 | mutex_lock(&kvm->lock); | |
e09fefde | 320 | ret = kvm_get_vcpu_by_id(kvm, id); |
a8606e20 PM |
321 | mutex_unlock(&kvm->lock); |
322 | return ret; | |
323 | } | |
324 | ||
325 | static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) | |
326 | { | |
f13c13a0 | 327 | vpa->__old_status |= LPPACA_OLD_SHARED_PROC; |
02407552 | 328 | vpa->yield_count = cpu_to_be32(1); |
a8606e20 PM |
329 | } |
330 | ||
55b665b0 PM |
331 | static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v, |
332 | unsigned long addr, unsigned long len) | |
333 | { | |
334 | /* check address is cacheline aligned */ | |
335 | if (addr & (L1_CACHE_BYTES - 1)) | |
336 | return -EINVAL; | |
337 | spin_lock(&vcpu->arch.vpa_update_lock); | |
338 | if (v->next_gpa != addr || v->len != len) { | |
339 | v->next_gpa = addr; | |
340 | v->len = addr ? len : 0; | |
341 | v->update_pending = 1; | |
342 | } | |
343 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
344 | return 0; | |
345 | } | |
346 | ||
2e25aa5f PM |
347 | /* Length for a per-processor buffer is passed in at offset 4 in the buffer */ |
348 | struct reg_vpa { | |
349 | u32 dummy; | |
350 | union { | |
02407552 AG |
351 | __be16 hword; |
352 | __be32 word; | |
2e25aa5f PM |
353 | } length; |
354 | }; | |
355 | ||
356 | static int vpa_is_registered(struct kvmppc_vpa *vpap) | |
357 | { | |
358 | if (vpap->update_pending) | |
359 | return vpap->next_gpa != 0; | |
360 | return vpap->pinned_addr != NULL; | |
361 | } | |
362 | ||
a8606e20 PM |
363 | static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, |
364 | unsigned long flags, | |
365 | unsigned long vcpuid, unsigned long vpa) | |
366 | { | |
367 | struct kvm *kvm = vcpu->kvm; | |
93e60249 | 368 | unsigned long len, nb; |
a8606e20 PM |
369 | void *va; |
370 | struct kvm_vcpu *tvcpu; | |
2e25aa5f PM |
371 | int err; |
372 | int subfunc; | |
373 | struct kvmppc_vpa *vpap; | |
a8606e20 PM |
374 | |
375 | tvcpu = kvmppc_find_vcpu(kvm, vcpuid); | |
376 | if (!tvcpu) | |
377 | return H_PARAMETER; | |
378 | ||
2e25aa5f PM |
379 | subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK; |
380 | if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL || | |
381 | subfunc == H_VPA_REG_SLB) { | |
382 | /* Registering new area - address must be cache-line aligned */ | |
383 | if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa) | |
a8606e20 | 384 | return H_PARAMETER; |
2e25aa5f PM |
385 | |
386 | /* convert logical addr to kernel addr and read length */ | |
93e60249 PM |
387 | va = kvmppc_pin_guest_page(kvm, vpa, &nb); |
388 | if (va == NULL) | |
b2b2f165 | 389 | return H_PARAMETER; |
2e25aa5f | 390 | if (subfunc == H_VPA_REG_VPA) |
02407552 | 391 | len = be16_to_cpu(((struct reg_vpa *)va)->length.hword); |
a8606e20 | 392 | else |
02407552 | 393 | len = be32_to_cpu(((struct reg_vpa *)va)->length.word); |
c35635ef | 394 | kvmppc_unpin_guest_page(kvm, va, vpa, false); |
2e25aa5f PM |
395 | |
396 | /* Check length */ | |
397 | if (len > nb || len < sizeof(struct reg_vpa)) | |
398 | return H_PARAMETER; | |
399 | } else { | |
400 | vpa = 0; | |
401 | len = 0; | |
402 | } | |
403 | ||
404 | err = H_PARAMETER; | |
405 | vpap = NULL; | |
406 | spin_lock(&tvcpu->arch.vpa_update_lock); | |
407 | ||
408 | switch (subfunc) { | |
409 | case H_VPA_REG_VPA: /* register VPA */ | |
410 | if (len < sizeof(struct lppaca)) | |
a8606e20 | 411 | break; |
2e25aa5f PM |
412 | vpap = &tvcpu->arch.vpa; |
413 | err = 0; | |
414 | break; | |
415 | ||
416 | case H_VPA_REG_DTL: /* register DTL */ | |
417 | if (len < sizeof(struct dtl_entry)) | |
a8606e20 | 418 | break; |
2e25aa5f PM |
419 | len -= len % sizeof(struct dtl_entry); |
420 | ||
421 | /* Check that they have previously registered a VPA */ | |
422 | err = H_RESOURCE; | |
423 | if (!vpa_is_registered(&tvcpu->arch.vpa)) | |
a8606e20 | 424 | break; |
2e25aa5f PM |
425 | |
426 | vpap = &tvcpu->arch.dtl; | |
427 | err = 0; | |
428 | break; | |
429 | ||
430 | case H_VPA_REG_SLB: /* register SLB shadow buffer */ | |
431 | /* Check that they have previously registered a VPA */ | |
432 | err = H_RESOURCE; | |
433 | if (!vpa_is_registered(&tvcpu->arch.vpa)) | |
a8606e20 | 434 | break; |
2e25aa5f PM |
435 | |
436 | vpap = &tvcpu->arch.slb_shadow; | |
437 | err = 0; | |
438 | break; | |
439 | ||
440 | case H_VPA_DEREG_VPA: /* deregister VPA */ | |
441 | /* Check they don't still have a DTL or SLB buf registered */ | |
442 | err = H_RESOURCE; | |
443 | if (vpa_is_registered(&tvcpu->arch.dtl) || | |
444 | vpa_is_registered(&tvcpu->arch.slb_shadow)) | |
a8606e20 | 445 | break; |
2e25aa5f PM |
446 | |
447 | vpap = &tvcpu->arch.vpa; | |
448 | err = 0; | |
449 | break; | |
450 | ||
451 | case H_VPA_DEREG_DTL: /* deregister DTL */ | |
452 | vpap = &tvcpu->arch.dtl; | |
453 | err = 0; | |
454 | break; | |
455 | ||
456 | case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */ | |
457 | vpap = &tvcpu->arch.slb_shadow; | |
458 | err = 0; | |
459 | break; | |
460 | } | |
461 | ||
462 | if (vpap) { | |
463 | vpap->next_gpa = vpa; | |
464 | vpap->len = len; | |
465 | vpap->update_pending = 1; | |
a8606e20 | 466 | } |
93e60249 | 467 | |
2e25aa5f PM |
468 | spin_unlock(&tvcpu->arch.vpa_update_lock); |
469 | ||
93e60249 | 470 | return err; |
a8606e20 PM |
471 | } |
472 | ||
081f323b | 473 | static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap) |
2e25aa5f | 474 | { |
081f323b | 475 | struct kvm *kvm = vcpu->kvm; |
2e25aa5f PM |
476 | void *va; |
477 | unsigned long nb; | |
081f323b | 478 | unsigned long gpa; |
2e25aa5f | 479 | |
081f323b PM |
480 | /* |
481 | * We need to pin the page pointed to by vpap->next_gpa, | |
482 | * but we can't call kvmppc_pin_guest_page under the lock | |
483 | * as it does get_user_pages() and down_read(). So we | |
484 | * have to drop the lock, pin the page, then get the lock | |
485 | * again and check that a new area didn't get registered | |
486 | * in the meantime. | |
487 | */ | |
488 | for (;;) { | |
489 | gpa = vpap->next_gpa; | |
490 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
491 | va = NULL; | |
492 | nb = 0; | |
493 | if (gpa) | |
c35635ef | 494 | va = kvmppc_pin_guest_page(kvm, gpa, &nb); |
081f323b PM |
495 | spin_lock(&vcpu->arch.vpa_update_lock); |
496 | if (gpa == vpap->next_gpa) | |
497 | break; | |
498 | /* sigh... unpin that one and try again */ | |
499 | if (va) | |
c35635ef | 500 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
081f323b PM |
501 | } |
502 | ||
503 | vpap->update_pending = 0; | |
504 | if (va && nb < vpap->len) { | |
505 | /* | |
506 | * If it's now too short, it must be that userspace | |
507 | * has changed the mappings underlying guest memory, | |
508 | * so unregister the region. | |
509 | */ | |
c35635ef | 510 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
081f323b | 511 | va = NULL; |
2e25aa5f PM |
512 | } |
513 | if (vpap->pinned_addr) | |
c35635ef PM |
514 | kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa, |
515 | vpap->dirty); | |
516 | vpap->gpa = gpa; | |
2e25aa5f | 517 | vpap->pinned_addr = va; |
c35635ef | 518 | vpap->dirty = false; |
2e25aa5f PM |
519 | if (va) |
520 | vpap->pinned_end = va + vpap->len; | |
521 | } | |
522 | ||
523 | static void kvmppc_update_vpas(struct kvm_vcpu *vcpu) | |
524 | { | |
2f12f034 PM |
525 | if (!(vcpu->arch.vpa.update_pending || |
526 | vcpu->arch.slb_shadow.update_pending || | |
527 | vcpu->arch.dtl.update_pending)) | |
528 | return; | |
529 | ||
2e25aa5f PM |
530 | spin_lock(&vcpu->arch.vpa_update_lock); |
531 | if (vcpu->arch.vpa.update_pending) { | |
081f323b | 532 | kvmppc_update_vpa(vcpu, &vcpu->arch.vpa); |
55b665b0 PM |
533 | if (vcpu->arch.vpa.pinned_addr) |
534 | init_vpa(vcpu, vcpu->arch.vpa.pinned_addr); | |
2e25aa5f PM |
535 | } |
536 | if (vcpu->arch.dtl.update_pending) { | |
081f323b | 537 | kvmppc_update_vpa(vcpu, &vcpu->arch.dtl); |
2e25aa5f PM |
538 | vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr; |
539 | vcpu->arch.dtl_index = 0; | |
540 | } | |
541 | if (vcpu->arch.slb_shadow.update_pending) | |
081f323b | 542 | kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow); |
2e25aa5f PM |
543 | spin_unlock(&vcpu->arch.vpa_update_lock); |
544 | } | |
545 | ||
c7b67670 PM |
546 | /* |
547 | * Return the accumulated stolen time for the vcore up until `now'. | |
548 | * The caller should hold the vcore lock. | |
549 | */ | |
550 | static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now) | |
551 | { | |
552 | u64 p; | |
2711e248 | 553 | unsigned long flags; |
c7b67670 | 554 | |
2711e248 PM |
555 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
556 | p = vc->stolen_tb; | |
c7b67670 | 557 | if (vc->vcore_state != VCORE_INACTIVE && |
2711e248 PM |
558 | vc->preempt_tb != TB_NIL) |
559 | p += now - vc->preempt_tb; | |
560 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); | |
c7b67670 PM |
561 | return p; |
562 | } | |
563 | ||
0456ec4f PM |
564 | static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, |
565 | struct kvmppc_vcore *vc) | |
566 | { | |
567 | struct dtl_entry *dt; | |
568 | struct lppaca *vpa; | |
c7b67670 PM |
569 | unsigned long stolen; |
570 | unsigned long core_stolen; | |
571 | u64 now; | |
0456ec4f PM |
572 | |
573 | dt = vcpu->arch.dtl_ptr; | |
574 | vpa = vcpu->arch.vpa.pinned_addr; | |
c7b67670 PM |
575 | now = mftb(); |
576 | core_stolen = vcore_stolen_time(vc, now); | |
577 | stolen = core_stolen - vcpu->arch.stolen_logged; | |
578 | vcpu->arch.stolen_logged = core_stolen; | |
bf3d32e1 | 579 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
c7b67670 PM |
580 | stolen += vcpu->arch.busy_stolen; |
581 | vcpu->arch.busy_stolen = 0; | |
bf3d32e1 | 582 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
0456ec4f PM |
583 | if (!dt || !vpa) |
584 | return; | |
585 | memset(dt, 0, sizeof(struct dtl_entry)); | |
586 | dt->dispatch_reason = 7; | |
02407552 AG |
587 | dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid); |
588 | dt->timebase = cpu_to_be64(now + vc->tb_offset); | |
589 | dt->enqueue_to_dispatch_time = cpu_to_be32(stolen); | |
590 | dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu)); | |
591 | dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr); | |
0456ec4f PM |
592 | ++dt; |
593 | if (dt == vcpu->arch.dtl.pinned_end) | |
594 | dt = vcpu->arch.dtl.pinned_addr; | |
595 | vcpu->arch.dtl_ptr = dt; | |
596 | /* order writing *dt vs. writing vpa->dtl_idx */ | |
597 | smp_wmb(); | |
02407552 | 598 | vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index); |
c35635ef | 599 | vcpu->arch.dtl.dirty = true; |
0456ec4f PM |
600 | } |
601 | ||
9642382e MN |
602 | static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu) |
603 | { | |
604 | if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207) | |
605 | return true; | |
606 | if ((!vcpu->arch.vcore->arch_compat) && | |
607 | cpu_has_feature(CPU_FTR_ARCH_207S)) | |
608 | return true; | |
609 | return false; | |
610 | } | |
611 | ||
612 | static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags, | |
613 | unsigned long resource, unsigned long value1, | |
614 | unsigned long value2) | |
615 | { | |
616 | switch (resource) { | |
617 | case H_SET_MODE_RESOURCE_SET_CIABR: | |
618 | if (!kvmppc_power8_compatible(vcpu)) | |
619 | return H_P2; | |
620 | if (value2) | |
621 | return H_P4; | |
622 | if (mflags) | |
623 | return H_UNSUPPORTED_FLAG_START; | |
624 | /* Guests can't breakpoint the hypervisor */ | |
625 | if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER) | |
626 | return H_P3; | |
627 | vcpu->arch.ciabr = value1; | |
628 | return H_SUCCESS; | |
629 | case H_SET_MODE_RESOURCE_SET_DAWR: | |
630 | if (!kvmppc_power8_compatible(vcpu)) | |
631 | return H_P2; | |
632 | if (mflags) | |
633 | return H_UNSUPPORTED_FLAG_START; | |
634 | if (value2 & DABRX_HYP) | |
635 | return H_P4; | |
636 | vcpu->arch.dawr = value1; | |
637 | vcpu->arch.dawrx = value2; | |
638 | return H_SUCCESS; | |
639 | default: | |
640 | return H_TOO_HARD; | |
641 | } | |
642 | } | |
643 | ||
90fd09f8 SB |
644 | static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target) |
645 | { | |
646 | struct kvmppc_vcore *vcore = target->arch.vcore; | |
647 | ||
648 | /* | |
649 | * We expect to have been called by the real mode handler | |
650 | * (kvmppc_rm_h_confer()) which would have directly returned | |
651 | * H_SUCCESS if the source vcore wasn't idle (e.g. if it may | |
652 | * have useful work to do and should not confer) so we don't | |
653 | * recheck that here. | |
654 | */ | |
655 | ||
656 | spin_lock(&vcore->lock); | |
657 | if (target->arch.state == KVMPPC_VCPU_RUNNABLE && | |
ec257165 PM |
658 | vcore->vcore_state != VCORE_INACTIVE && |
659 | vcore->runner) | |
90fd09f8 SB |
660 | target = vcore->runner; |
661 | spin_unlock(&vcore->lock); | |
662 | ||
663 | return kvm_vcpu_yield_to(target); | |
664 | } | |
665 | ||
666 | static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu) | |
667 | { | |
668 | int yield_count = 0; | |
669 | struct lppaca *lppaca; | |
670 | ||
671 | spin_lock(&vcpu->arch.vpa_update_lock); | |
672 | lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr; | |
673 | if (lppaca) | |
ecb6d618 | 674 | yield_count = be32_to_cpu(lppaca->yield_count); |
90fd09f8 SB |
675 | spin_unlock(&vcpu->arch.vpa_update_lock); |
676 | return yield_count; | |
677 | } | |
678 | ||
a8606e20 PM |
679 | int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) |
680 | { | |
681 | unsigned long req = kvmppc_get_gpr(vcpu, 3); | |
682 | unsigned long target, ret = H_SUCCESS; | |
90fd09f8 | 683 | int yield_count; |
a8606e20 | 684 | struct kvm_vcpu *tvcpu; |
8e591cb7 | 685 | int idx, rc; |
a8606e20 | 686 | |
699a0ea0 PM |
687 | if (req <= MAX_HCALL_OPCODE && |
688 | !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls)) | |
689 | return RESUME_HOST; | |
690 | ||
a8606e20 PM |
691 | switch (req) { |
692 | case H_CEDE: | |
a8606e20 PM |
693 | break; |
694 | case H_PROD: | |
695 | target = kvmppc_get_gpr(vcpu, 4); | |
696 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); | |
697 | if (!tvcpu) { | |
698 | ret = H_PARAMETER; | |
699 | break; | |
700 | } | |
701 | tvcpu->arch.prodded = 1; | |
702 | smp_mb(); | |
703 | if (vcpu->arch.ceded) { | |
704 | if (waitqueue_active(&vcpu->wq)) { | |
705 | wake_up_interruptible(&vcpu->wq); | |
706 | vcpu->stat.halt_wakeup++; | |
707 | } | |
708 | } | |
709 | break; | |
710 | case H_CONFER: | |
42d7604d PM |
711 | target = kvmppc_get_gpr(vcpu, 4); |
712 | if (target == -1) | |
713 | break; | |
714 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); | |
715 | if (!tvcpu) { | |
716 | ret = H_PARAMETER; | |
717 | break; | |
718 | } | |
90fd09f8 SB |
719 | yield_count = kvmppc_get_gpr(vcpu, 5); |
720 | if (kvmppc_get_yield_count(tvcpu) != yield_count) | |
721 | break; | |
722 | kvm_arch_vcpu_yield_to(tvcpu); | |
a8606e20 PM |
723 | break; |
724 | case H_REGISTER_VPA: | |
725 | ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), | |
726 | kvmppc_get_gpr(vcpu, 5), | |
727 | kvmppc_get_gpr(vcpu, 6)); | |
728 | break; | |
8e591cb7 ME |
729 | case H_RTAS: |
730 | if (list_empty(&vcpu->kvm->arch.rtas_tokens)) | |
731 | return RESUME_HOST; | |
732 | ||
c9438092 | 733 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
8e591cb7 | 734 | rc = kvmppc_rtas_hcall(vcpu); |
c9438092 | 735 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8e591cb7 ME |
736 | |
737 | if (rc == -ENOENT) | |
738 | return RESUME_HOST; | |
739 | else if (rc == 0) | |
740 | break; | |
741 | ||
742 | /* Send the error out to userspace via KVM_RUN */ | |
743 | return rc; | |
99342cf8 DG |
744 | case H_LOGICAL_CI_LOAD: |
745 | ret = kvmppc_h_logical_ci_load(vcpu); | |
746 | if (ret == H_TOO_HARD) | |
747 | return RESUME_HOST; | |
748 | break; | |
749 | case H_LOGICAL_CI_STORE: | |
750 | ret = kvmppc_h_logical_ci_store(vcpu); | |
751 | if (ret == H_TOO_HARD) | |
752 | return RESUME_HOST; | |
753 | break; | |
9642382e MN |
754 | case H_SET_MODE: |
755 | ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4), | |
756 | kvmppc_get_gpr(vcpu, 5), | |
757 | kvmppc_get_gpr(vcpu, 6), | |
758 | kvmppc_get_gpr(vcpu, 7)); | |
759 | if (ret == H_TOO_HARD) | |
760 | return RESUME_HOST; | |
761 | break; | |
bc5ad3f3 BH |
762 | case H_XIRR: |
763 | case H_CPPR: | |
764 | case H_EOI: | |
765 | case H_IPI: | |
8e44ddc3 PM |
766 | case H_IPOLL: |
767 | case H_XIRR_X: | |
bc5ad3f3 BH |
768 | if (kvmppc_xics_enabled(vcpu)) { |
769 | ret = kvmppc_xics_hcall(vcpu, req); | |
770 | break; | |
771 | } /* fallthrough */ | |
a8606e20 PM |
772 | default: |
773 | return RESUME_HOST; | |
774 | } | |
775 | kvmppc_set_gpr(vcpu, 3, ret); | |
776 | vcpu->arch.hcall_needed = 0; | |
777 | return RESUME_GUEST; | |
778 | } | |
779 | ||
ae2113a4 PM |
780 | static int kvmppc_hcall_impl_hv(unsigned long cmd) |
781 | { | |
782 | switch (cmd) { | |
783 | case H_CEDE: | |
784 | case H_PROD: | |
785 | case H_CONFER: | |
786 | case H_REGISTER_VPA: | |
9642382e | 787 | case H_SET_MODE: |
99342cf8 DG |
788 | case H_LOGICAL_CI_LOAD: |
789 | case H_LOGICAL_CI_STORE: | |
ae2113a4 PM |
790 | #ifdef CONFIG_KVM_XICS |
791 | case H_XIRR: | |
792 | case H_CPPR: | |
793 | case H_EOI: | |
794 | case H_IPI: | |
795 | case H_IPOLL: | |
796 | case H_XIRR_X: | |
797 | #endif | |
798 | return 1; | |
799 | } | |
800 | ||
801 | /* See if it's in the real-mode table */ | |
802 | return kvmppc_hcall_impl_hv_realmode(cmd); | |
803 | } | |
804 | ||
a59c1d9e MS |
805 | static int kvmppc_emulate_debug_inst(struct kvm_run *run, |
806 | struct kvm_vcpu *vcpu) | |
807 | { | |
808 | u32 last_inst; | |
809 | ||
810 | if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) != | |
811 | EMULATE_DONE) { | |
812 | /* | |
813 | * Fetch failed, so return to guest and | |
814 | * try executing it again. | |
815 | */ | |
816 | return RESUME_GUEST; | |
817 | } | |
818 | ||
819 | if (last_inst == KVMPPC_INST_SW_BREAKPOINT) { | |
820 | run->exit_reason = KVM_EXIT_DEBUG; | |
821 | run->debug.arch.address = kvmppc_get_pc(vcpu); | |
822 | return RESUME_HOST; | |
823 | } else { | |
824 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); | |
825 | return RESUME_GUEST; | |
826 | } | |
827 | } | |
828 | ||
3a167bea AK |
829 | static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, |
830 | struct task_struct *tsk) | |
de56a948 PM |
831 | { |
832 | int r = RESUME_HOST; | |
833 | ||
834 | vcpu->stat.sum_exits++; | |
835 | ||
1c9e3d51 PM |
836 | /* |
837 | * This can happen if an interrupt occurs in the last stages | |
838 | * of guest entry or the first stages of guest exit (i.e. after | |
839 | * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV | |
840 | * and before setting it to KVM_GUEST_MODE_HOST_HV). | |
841 | * That can happen due to a bug, or due to a machine check | |
842 | * occurring at just the wrong time. | |
843 | */ | |
844 | if (vcpu->arch.shregs.msr & MSR_HV) { | |
845 | printk(KERN_EMERG "KVM trap in HV mode!\n"); | |
846 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", | |
847 | vcpu->arch.trap, kvmppc_get_pc(vcpu), | |
848 | vcpu->arch.shregs.msr); | |
849 | kvmppc_dump_regs(vcpu); | |
850 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
851 | run->hw.hardware_exit_reason = vcpu->arch.trap; | |
852 | return RESUME_HOST; | |
853 | } | |
de56a948 PM |
854 | run->exit_reason = KVM_EXIT_UNKNOWN; |
855 | run->ready_for_interrupt_injection = 1; | |
856 | switch (vcpu->arch.trap) { | |
857 | /* We're good on these - the host merely wanted to get our attention */ | |
858 | case BOOK3S_INTERRUPT_HV_DECREMENTER: | |
859 | vcpu->stat.dec_exits++; | |
860 | r = RESUME_GUEST; | |
861 | break; | |
862 | case BOOK3S_INTERRUPT_EXTERNAL: | |
5d00f66b | 863 | case BOOK3S_INTERRUPT_H_DOORBELL: |
de56a948 PM |
864 | vcpu->stat.ext_intr_exits++; |
865 | r = RESUME_GUEST; | |
866 | break; | |
dee6f24c MS |
867 | /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/ |
868 | case BOOK3S_INTERRUPT_HMI: | |
de56a948 PM |
869 | case BOOK3S_INTERRUPT_PERFMON: |
870 | r = RESUME_GUEST; | |
871 | break; | |
b4072df4 PM |
872 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
873 | /* | |
874 | * Deliver a machine check interrupt to the guest. | |
875 | * We have to do this, even if the host has handled the | |
876 | * machine check, because machine checks use SRR0/1 and | |
877 | * the interrupt might have trashed guest state in them. | |
878 | */ | |
879 | kvmppc_book3s_queue_irqprio(vcpu, | |
880 | BOOK3S_INTERRUPT_MACHINE_CHECK); | |
881 | r = RESUME_GUEST; | |
882 | break; | |
de56a948 PM |
883 | case BOOK3S_INTERRUPT_PROGRAM: |
884 | { | |
885 | ulong flags; | |
886 | /* | |
887 | * Normally program interrupts are delivered directly | |
888 | * to the guest by the hardware, but we can get here | |
889 | * as a result of a hypervisor emulation interrupt | |
890 | * (e40) getting turned into a 700 by BML RTAS. | |
891 | */ | |
892 | flags = vcpu->arch.shregs.msr & 0x1f0000ull; | |
893 | kvmppc_core_queue_program(vcpu, flags); | |
894 | r = RESUME_GUEST; | |
895 | break; | |
896 | } | |
897 | case BOOK3S_INTERRUPT_SYSCALL: | |
898 | { | |
899 | /* hcall - punt to userspace */ | |
900 | int i; | |
901 | ||
27025a60 LPF |
902 | /* hypercall with MSR_PR has already been handled in rmode, |
903 | * and never reaches here. | |
904 | */ | |
905 | ||
de56a948 PM |
906 | run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); |
907 | for (i = 0; i < 9; ++i) | |
908 | run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); | |
909 | run->exit_reason = KVM_EXIT_PAPR_HCALL; | |
910 | vcpu->arch.hcall_needed = 1; | |
911 | r = RESUME_HOST; | |
912 | break; | |
913 | } | |
914 | /* | |
342d3db7 PM |
915 | * We get these next two if the guest accesses a page which it thinks |
916 | * it has mapped but which is not actually present, either because | |
917 | * it is for an emulated I/O device or because the corresonding | |
918 | * host page has been paged out. Any other HDSI/HISI interrupts | |
919 | * have been handled already. | |
de56a948 PM |
920 | */ |
921 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: | |
913d3ff9 | 922 | r = RESUME_PAGE_FAULT; |
de56a948 PM |
923 | break; |
924 | case BOOK3S_INTERRUPT_H_INST_STORAGE: | |
913d3ff9 PM |
925 | vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); |
926 | vcpu->arch.fault_dsisr = 0; | |
927 | r = RESUME_PAGE_FAULT; | |
de56a948 PM |
928 | break; |
929 | /* | |
930 | * This occurs if the guest executes an illegal instruction. | |
a59c1d9e MS |
931 | * If the guest debug is disabled, generate a program interrupt |
932 | * to the guest. If guest debug is enabled, we need to check | |
933 | * whether the instruction is a software breakpoint instruction. | |
934 | * Accordingly return to Guest or Host. | |
de56a948 PM |
935 | */ |
936 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: | |
4a157d61 PM |
937 | if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED) |
938 | vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ? | |
939 | swab32(vcpu->arch.emul_inst) : | |
940 | vcpu->arch.emul_inst; | |
a59c1d9e MS |
941 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) { |
942 | r = kvmppc_emulate_debug_inst(run, vcpu); | |
943 | } else { | |
944 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); | |
945 | r = RESUME_GUEST; | |
946 | } | |
bd3048b8 ME |
947 | break; |
948 | /* | |
949 | * This occurs if the guest (kernel or userspace), does something that | |
950 | * is prohibited by HFSCR. We just generate a program interrupt to | |
951 | * the guest. | |
952 | */ | |
953 | case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: | |
954 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); | |
de56a948 PM |
955 | r = RESUME_GUEST; |
956 | break; | |
957 | default: | |
958 | kvmppc_dump_regs(vcpu); | |
959 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", | |
960 | vcpu->arch.trap, kvmppc_get_pc(vcpu), | |
961 | vcpu->arch.shregs.msr); | |
f3271d4c | 962 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
de56a948 | 963 | r = RESUME_HOST; |
de56a948 PM |
964 | break; |
965 | } | |
966 | ||
de56a948 PM |
967 | return r; |
968 | } | |
969 | ||
3a167bea AK |
970 | static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu, |
971 | struct kvm_sregs *sregs) | |
de56a948 PM |
972 | { |
973 | int i; | |
974 | ||
de56a948 | 975 | memset(sregs, 0, sizeof(struct kvm_sregs)); |
87916442 | 976 | sregs->pvr = vcpu->arch.pvr; |
de56a948 PM |
977 | for (i = 0; i < vcpu->arch.slb_max; i++) { |
978 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; | |
979 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; | |
980 | } | |
981 | ||
982 | return 0; | |
983 | } | |
984 | ||
3a167bea AK |
985 | static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu, |
986 | struct kvm_sregs *sregs) | |
de56a948 PM |
987 | { |
988 | int i, j; | |
989 | ||
9333e6c4 PM |
990 | /* Only accept the same PVR as the host's, since we can't spoof it */ |
991 | if (sregs->pvr != vcpu->arch.pvr) | |
992 | return -EINVAL; | |
de56a948 PM |
993 | |
994 | j = 0; | |
995 | for (i = 0; i < vcpu->arch.slb_nr; i++) { | |
996 | if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { | |
997 | vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; | |
998 | vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; | |
999 | ++j; | |
1000 | } | |
1001 | } | |
1002 | vcpu->arch.slb_max = j; | |
1003 | ||
1004 | return 0; | |
1005 | } | |
1006 | ||
a0840240 AK |
1007 | static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr, |
1008 | bool preserve_top32) | |
a0144e2a | 1009 | { |
8f902b00 | 1010 | struct kvm *kvm = vcpu->kvm; |
a0144e2a PM |
1011 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
1012 | u64 mask; | |
1013 | ||
8f902b00 | 1014 | mutex_lock(&kvm->lock); |
a0144e2a | 1015 | spin_lock(&vc->lock); |
d682916a AB |
1016 | /* |
1017 | * If ILE (interrupt little-endian) has changed, update the | |
1018 | * MSR_LE bit in the intr_msr for each vcpu in this vcore. | |
1019 | */ | |
1020 | if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) { | |
d682916a AB |
1021 | struct kvm_vcpu *vcpu; |
1022 | int i; | |
1023 | ||
d682916a AB |
1024 | kvm_for_each_vcpu(i, vcpu, kvm) { |
1025 | if (vcpu->arch.vcore != vc) | |
1026 | continue; | |
1027 | if (new_lpcr & LPCR_ILE) | |
1028 | vcpu->arch.intr_msr |= MSR_LE; | |
1029 | else | |
1030 | vcpu->arch.intr_msr &= ~MSR_LE; | |
1031 | } | |
d682916a AB |
1032 | } |
1033 | ||
a0144e2a PM |
1034 | /* |
1035 | * Userspace can only modify DPFD (default prefetch depth), | |
1036 | * ILE (interrupt little-endian) and TC (translation control). | |
e0622bd9 | 1037 | * On POWER8 userspace can also modify AIL (alt. interrupt loc.) |
a0144e2a PM |
1038 | */ |
1039 | mask = LPCR_DPFD | LPCR_ILE | LPCR_TC; | |
e0622bd9 PM |
1040 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
1041 | mask |= LPCR_AIL; | |
a0840240 AK |
1042 | |
1043 | /* Broken 32-bit version of LPCR must not clear top bits */ | |
1044 | if (preserve_top32) | |
1045 | mask &= 0xFFFFFFFF; | |
a0144e2a PM |
1046 | vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask); |
1047 | spin_unlock(&vc->lock); | |
8f902b00 | 1048 | mutex_unlock(&kvm->lock); |
a0144e2a PM |
1049 | } |
1050 | ||
3a167bea AK |
1051 | static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
1052 | union kvmppc_one_reg *val) | |
31f3438e | 1053 | { |
a136a8bd PM |
1054 | int r = 0; |
1055 | long int i; | |
31f3438e | 1056 | |
a136a8bd | 1057 | switch (id) { |
a59c1d9e MS |
1058 | case KVM_REG_PPC_DEBUG_INST: |
1059 | *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT); | |
1060 | break; | |
31f3438e | 1061 | case KVM_REG_PPC_HIOR: |
a136a8bd PM |
1062 | *val = get_reg_val(id, 0); |
1063 | break; | |
1064 | case KVM_REG_PPC_DABR: | |
1065 | *val = get_reg_val(id, vcpu->arch.dabr); | |
1066 | break; | |
8563bf52 PM |
1067 | case KVM_REG_PPC_DABRX: |
1068 | *val = get_reg_val(id, vcpu->arch.dabrx); | |
1069 | break; | |
a136a8bd PM |
1070 | case KVM_REG_PPC_DSCR: |
1071 | *val = get_reg_val(id, vcpu->arch.dscr); | |
1072 | break; | |
1073 | case KVM_REG_PPC_PURR: | |
1074 | *val = get_reg_val(id, vcpu->arch.purr); | |
1075 | break; | |
1076 | case KVM_REG_PPC_SPURR: | |
1077 | *val = get_reg_val(id, vcpu->arch.spurr); | |
1078 | break; | |
1079 | case KVM_REG_PPC_AMR: | |
1080 | *val = get_reg_val(id, vcpu->arch.amr); | |
1081 | break; | |
1082 | case KVM_REG_PPC_UAMOR: | |
1083 | *val = get_reg_val(id, vcpu->arch.uamor); | |
1084 | break; | |
b005255e | 1085 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
a136a8bd PM |
1086 | i = id - KVM_REG_PPC_MMCR0; |
1087 | *val = get_reg_val(id, vcpu->arch.mmcr[i]); | |
1088 | break; | |
1089 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: | |
1090 | i = id - KVM_REG_PPC_PMC1; | |
1091 | *val = get_reg_val(id, vcpu->arch.pmc[i]); | |
31f3438e | 1092 | break; |
b005255e MN |
1093 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
1094 | i = id - KVM_REG_PPC_SPMC1; | |
1095 | *val = get_reg_val(id, vcpu->arch.spmc[i]); | |
1096 | break; | |
14941789 PM |
1097 | case KVM_REG_PPC_SIAR: |
1098 | *val = get_reg_val(id, vcpu->arch.siar); | |
1099 | break; | |
1100 | case KVM_REG_PPC_SDAR: | |
1101 | *val = get_reg_val(id, vcpu->arch.sdar); | |
1102 | break; | |
b005255e MN |
1103 | case KVM_REG_PPC_SIER: |
1104 | *val = get_reg_val(id, vcpu->arch.sier); | |
a8bd19ef | 1105 | break; |
b005255e MN |
1106 | case KVM_REG_PPC_IAMR: |
1107 | *val = get_reg_val(id, vcpu->arch.iamr); | |
1108 | break; | |
b005255e MN |
1109 | case KVM_REG_PPC_PSPB: |
1110 | *val = get_reg_val(id, vcpu->arch.pspb); | |
1111 | break; | |
b005255e MN |
1112 | case KVM_REG_PPC_DPDES: |
1113 | *val = get_reg_val(id, vcpu->arch.vcore->dpdes); | |
1114 | break; | |
1115 | case KVM_REG_PPC_DAWR: | |
1116 | *val = get_reg_val(id, vcpu->arch.dawr); | |
1117 | break; | |
1118 | case KVM_REG_PPC_DAWRX: | |
1119 | *val = get_reg_val(id, vcpu->arch.dawrx); | |
1120 | break; | |
1121 | case KVM_REG_PPC_CIABR: | |
1122 | *val = get_reg_val(id, vcpu->arch.ciabr); | |
1123 | break; | |
b005255e MN |
1124 | case KVM_REG_PPC_CSIGR: |
1125 | *val = get_reg_val(id, vcpu->arch.csigr); | |
1126 | break; | |
1127 | case KVM_REG_PPC_TACR: | |
1128 | *val = get_reg_val(id, vcpu->arch.tacr); | |
1129 | break; | |
1130 | case KVM_REG_PPC_TCSCR: | |
1131 | *val = get_reg_val(id, vcpu->arch.tcscr); | |
1132 | break; | |
1133 | case KVM_REG_PPC_PID: | |
1134 | *val = get_reg_val(id, vcpu->arch.pid); | |
1135 | break; | |
1136 | case KVM_REG_PPC_ACOP: | |
1137 | *val = get_reg_val(id, vcpu->arch.acop); | |
1138 | break; | |
1139 | case KVM_REG_PPC_WORT: | |
1140 | *val = get_reg_val(id, vcpu->arch.wort); | |
a8bd19ef | 1141 | break; |
55b665b0 PM |
1142 | case KVM_REG_PPC_VPA_ADDR: |
1143 | spin_lock(&vcpu->arch.vpa_update_lock); | |
1144 | *val = get_reg_val(id, vcpu->arch.vpa.next_gpa); | |
1145 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
1146 | break; | |
1147 | case KVM_REG_PPC_VPA_SLB: | |
1148 | spin_lock(&vcpu->arch.vpa_update_lock); | |
1149 | val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa; | |
1150 | val->vpaval.length = vcpu->arch.slb_shadow.len; | |
1151 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
1152 | break; | |
1153 | case KVM_REG_PPC_VPA_DTL: | |
1154 | spin_lock(&vcpu->arch.vpa_update_lock); | |
1155 | val->vpaval.addr = vcpu->arch.dtl.next_gpa; | |
1156 | val->vpaval.length = vcpu->arch.dtl.len; | |
1157 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
1158 | break; | |
93b0f4dc PM |
1159 | case KVM_REG_PPC_TB_OFFSET: |
1160 | *val = get_reg_val(id, vcpu->arch.vcore->tb_offset); | |
1161 | break; | |
a0144e2a | 1162 | case KVM_REG_PPC_LPCR: |
a0840240 | 1163 | case KVM_REG_PPC_LPCR_64: |
a0144e2a PM |
1164 | *val = get_reg_val(id, vcpu->arch.vcore->lpcr); |
1165 | break; | |
4b8473c9 PM |
1166 | case KVM_REG_PPC_PPR: |
1167 | *val = get_reg_val(id, vcpu->arch.ppr); | |
1168 | break; | |
a7d80d01 MN |
1169 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
1170 | case KVM_REG_PPC_TFHAR: | |
1171 | *val = get_reg_val(id, vcpu->arch.tfhar); | |
1172 | break; | |
1173 | case KVM_REG_PPC_TFIAR: | |
1174 | *val = get_reg_val(id, vcpu->arch.tfiar); | |
1175 | break; | |
1176 | case KVM_REG_PPC_TEXASR: | |
1177 | *val = get_reg_val(id, vcpu->arch.texasr); | |
1178 | break; | |
1179 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: | |
1180 | i = id - KVM_REG_PPC_TM_GPR0; | |
1181 | *val = get_reg_val(id, vcpu->arch.gpr_tm[i]); | |
1182 | break; | |
1183 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: | |
1184 | { | |
1185 | int j; | |
1186 | i = id - KVM_REG_PPC_TM_VSR0; | |
1187 | if (i < 32) | |
1188 | for (j = 0; j < TS_FPRWIDTH; j++) | |
1189 | val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j]; | |
1190 | else { | |
1191 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1192 | val->vval = vcpu->arch.vr_tm.vr[i-32]; | |
1193 | else | |
1194 | r = -ENXIO; | |
1195 | } | |
1196 | break; | |
1197 | } | |
1198 | case KVM_REG_PPC_TM_CR: | |
1199 | *val = get_reg_val(id, vcpu->arch.cr_tm); | |
1200 | break; | |
1201 | case KVM_REG_PPC_TM_LR: | |
1202 | *val = get_reg_val(id, vcpu->arch.lr_tm); | |
1203 | break; | |
1204 | case KVM_REG_PPC_TM_CTR: | |
1205 | *val = get_reg_val(id, vcpu->arch.ctr_tm); | |
1206 | break; | |
1207 | case KVM_REG_PPC_TM_FPSCR: | |
1208 | *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr); | |
1209 | break; | |
1210 | case KVM_REG_PPC_TM_AMR: | |
1211 | *val = get_reg_val(id, vcpu->arch.amr_tm); | |
1212 | break; | |
1213 | case KVM_REG_PPC_TM_PPR: | |
1214 | *val = get_reg_val(id, vcpu->arch.ppr_tm); | |
1215 | break; | |
1216 | case KVM_REG_PPC_TM_VRSAVE: | |
1217 | *val = get_reg_val(id, vcpu->arch.vrsave_tm); | |
1218 | break; | |
1219 | case KVM_REG_PPC_TM_VSCR: | |
1220 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1221 | *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]); | |
1222 | else | |
1223 | r = -ENXIO; | |
1224 | break; | |
1225 | case KVM_REG_PPC_TM_DSCR: | |
1226 | *val = get_reg_val(id, vcpu->arch.dscr_tm); | |
1227 | break; | |
1228 | case KVM_REG_PPC_TM_TAR: | |
1229 | *val = get_reg_val(id, vcpu->arch.tar_tm); | |
1230 | break; | |
1231 | #endif | |
388cc6e1 PM |
1232 | case KVM_REG_PPC_ARCH_COMPAT: |
1233 | *val = get_reg_val(id, vcpu->arch.vcore->arch_compat); | |
1234 | break; | |
31f3438e | 1235 | default: |
a136a8bd | 1236 | r = -EINVAL; |
31f3438e PM |
1237 | break; |
1238 | } | |
1239 | ||
1240 | return r; | |
1241 | } | |
1242 | ||
3a167bea AK |
1243 | static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
1244 | union kvmppc_one_reg *val) | |
31f3438e | 1245 | { |
a136a8bd PM |
1246 | int r = 0; |
1247 | long int i; | |
55b665b0 | 1248 | unsigned long addr, len; |
31f3438e | 1249 | |
a136a8bd | 1250 | switch (id) { |
31f3438e | 1251 | case KVM_REG_PPC_HIOR: |
31f3438e | 1252 | /* Only allow this to be set to zero */ |
a136a8bd | 1253 | if (set_reg_val(id, *val)) |
31f3438e PM |
1254 | r = -EINVAL; |
1255 | break; | |
a136a8bd PM |
1256 | case KVM_REG_PPC_DABR: |
1257 | vcpu->arch.dabr = set_reg_val(id, *val); | |
1258 | break; | |
8563bf52 PM |
1259 | case KVM_REG_PPC_DABRX: |
1260 | vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP; | |
1261 | break; | |
a136a8bd PM |
1262 | case KVM_REG_PPC_DSCR: |
1263 | vcpu->arch.dscr = set_reg_val(id, *val); | |
1264 | break; | |
1265 | case KVM_REG_PPC_PURR: | |
1266 | vcpu->arch.purr = set_reg_val(id, *val); | |
1267 | break; | |
1268 | case KVM_REG_PPC_SPURR: | |
1269 | vcpu->arch.spurr = set_reg_val(id, *val); | |
1270 | break; | |
1271 | case KVM_REG_PPC_AMR: | |
1272 | vcpu->arch.amr = set_reg_val(id, *val); | |
1273 | break; | |
1274 | case KVM_REG_PPC_UAMOR: | |
1275 | vcpu->arch.uamor = set_reg_val(id, *val); | |
1276 | break; | |
b005255e | 1277 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
a136a8bd PM |
1278 | i = id - KVM_REG_PPC_MMCR0; |
1279 | vcpu->arch.mmcr[i] = set_reg_val(id, *val); | |
1280 | break; | |
1281 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: | |
1282 | i = id - KVM_REG_PPC_PMC1; | |
1283 | vcpu->arch.pmc[i] = set_reg_val(id, *val); | |
1284 | break; | |
b005255e MN |
1285 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
1286 | i = id - KVM_REG_PPC_SPMC1; | |
1287 | vcpu->arch.spmc[i] = set_reg_val(id, *val); | |
1288 | break; | |
14941789 PM |
1289 | case KVM_REG_PPC_SIAR: |
1290 | vcpu->arch.siar = set_reg_val(id, *val); | |
1291 | break; | |
1292 | case KVM_REG_PPC_SDAR: | |
1293 | vcpu->arch.sdar = set_reg_val(id, *val); | |
1294 | break; | |
b005255e MN |
1295 | case KVM_REG_PPC_SIER: |
1296 | vcpu->arch.sier = set_reg_val(id, *val); | |
a8bd19ef | 1297 | break; |
b005255e MN |
1298 | case KVM_REG_PPC_IAMR: |
1299 | vcpu->arch.iamr = set_reg_val(id, *val); | |
1300 | break; | |
b005255e MN |
1301 | case KVM_REG_PPC_PSPB: |
1302 | vcpu->arch.pspb = set_reg_val(id, *val); | |
1303 | break; | |
b005255e MN |
1304 | case KVM_REG_PPC_DPDES: |
1305 | vcpu->arch.vcore->dpdes = set_reg_val(id, *val); | |
1306 | break; | |
1307 | case KVM_REG_PPC_DAWR: | |
1308 | vcpu->arch.dawr = set_reg_val(id, *val); | |
1309 | break; | |
1310 | case KVM_REG_PPC_DAWRX: | |
1311 | vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP; | |
1312 | break; | |
1313 | case KVM_REG_PPC_CIABR: | |
1314 | vcpu->arch.ciabr = set_reg_val(id, *val); | |
1315 | /* Don't allow setting breakpoints in hypervisor code */ | |
1316 | if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER) | |
1317 | vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */ | |
1318 | break; | |
b005255e MN |
1319 | case KVM_REG_PPC_CSIGR: |
1320 | vcpu->arch.csigr = set_reg_val(id, *val); | |
1321 | break; | |
1322 | case KVM_REG_PPC_TACR: | |
1323 | vcpu->arch.tacr = set_reg_val(id, *val); | |
1324 | break; | |
1325 | case KVM_REG_PPC_TCSCR: | |
1326 | vcpu->arch.tcscr = set_reg_val(id, *val); | |
1327 | break; | |
1328 | case KVM_REG_PPC_PID: | |
1329 | vcpu->arch.pid = set_reg_val(id, *val); | |
1330 | break; | |
1331 | case KVM_REG_PPC_ACOP: | |
1332 | vcpu->arch.acop = set_reg_val(id, *val); | |
1333 | break; | |
1334 | case KVM_REG_PPC_WORT: | |
1335 | vcpu->arch.wort = set_reg_val(id, *val); | |
a8bd19ef | 1336 | break; |
55b665b0 PM |
1337 | case KVM_REG_PPC_VPA_ADDR: |
1338 | addr = set_reg_val(id, *val); | |
1339 | r = -EINVAL; | |
1340 | if (!addr && (vcpu->arch.slb_shadow.next_gpa || | |
1341 | vcpu->arch.dtl.next_gpa)) | |
1342 | break; | |
1343 | r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca)); | |
1344 | break; | |
1345 | case KVM_REG_PPC_VPA_SLB: | |
1346 | addr = val->vpaval.addr; | |
1347 | len = val->vpaval.length; | |
1348 | r = -EINVAL; | |
1349 | if (addr && !vcpu->arch.vpa.next_gpa) | |
1350 | break; | |
1351 | r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len); | |
1352 | break; | |
1353 | case KVM_REG_PPC_VPA_DTL: | |
1354 | addr = val->vpaval.addr; | |
1355 | len = val->vpaval.length; | |
1356 | r = -EINVAL; | |
9f8c8c78 PM |
1357 | if (addr && (len < sizeof(struct dtl_entry) || |
1358 | !vcpu->arch.vpa.next_gpa)) | |
55b665b0 PM |
1359 | break; |
1360 | len -= len % sizeof(struct dtl_entry); | |
1361 | r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len); | |
1362 | break; | |
93b0f4dc PM |
1363 | case KVM_REG_PPC_TB_OFFSET: |
1364 | /* round up to multiple of 2^24 */ | |
1365 | vcpu->arch.vcore->tb_offset = | |
1366 | ALIGN(set_reg_val(id, *val), 1UL << 24); | |
1367 | break; | |
a0144e2a | 1368 | case KVM_REG_PPC_LPCR: |
a0840240 AK |
1369 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true); |
1370 | break; | |
1371 | case KVM_REG_PPC_LPCR_64: | |
1372 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false); | |
a0144e2a | 1373 | break; |
4b8473c9 PM |
1374 | case KVM_REG_PPC_PPR: |
1375 | vcpu->arch.ppr = set_reg_val(id, *val); | |
1376 | break; | |
a7d80d01 MN |
1377 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
1378 | case KVM_REG_PPC_TFHAR: | |
1379 | vcpu->arch.tfhar = set_reg_val(id, *val); | |
1380 | break; | |
1381 | case KVM_REG_PPC_TFIAR: | |
1382 | vcpu->arch.tfiar = set_reg_val(id, *val); | |
1383 | break; | |
1384 | case KVM_REG_PPC_TEXASR: | |
1385 | vcpu->arch.texasr = set_reg_val(id, *val); | |
1386 | break; | |
1387 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: | |
1388 | i = id - KVM_REG_PPC_TM_GPR0; | |
1389 | vcpu->arch.gpr_tm[i] = set_reg_val(id, *val); | |
1390 | break; | |
1391 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: | |
1392 | { | |
1393 | int j; | |
1394 | i = id - KVM_REG_PPC_TM_VSR0; | |
1395 | if (i < 32) | |
1396 | for (j = 0; j < TS_FPRWIDTH; j++) | |
1397 | vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j]; | |
1398 | else | |
1399 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1400 | vcpu->arch.vr_tm.vr[i-32] = val->vval; | |
1401 | else | |
1402 | r = -ENXIO; | |
1403 | break; | |
1404 | } | |
1405 | case KVM_REG_PPC_TM_CR: | |
1406 | vcpu->arch.cr_tm = set_reg_val(id, *val); | |
1407 | break; | |
1408 | case KVM_REG_PPC_TM_LR: | |
1409 | vcpu->arch.lr_tm = set_reg_val(id, *val); | |
1410 | break; | |
1411 | case KVM_REG_PPC_TM_CTR: | |
1412 | vcpu->arch.ctr_tm = set_reg_val(id, *val); | |
1413 | break; | |
1414 | case KVM_REG_PPC_TM_FPSCR: | |
1415 | vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val); | |
1416 | break; | |
1417 | case KVM_REG_PPC_TM_AMR: | |
1418 | vcpu->arch.amr_tm = set_reg_val(id, *val); | |
1419 | break; | |
1420 | case KVM_REG_PPC_TM_PPR: | |
1421 | vcpu->arch.ppr_tm = set_reg_val(id, *val); | |
1422 | break; | |
1423 | case KVM_REG_PPC_TM_VRSAVE: | |
1424 | vcpu->arch.vrsave_tm = set_reg_val(id, *val); | |
1425 | break; | |
1426 | case KVM_REG_PPC_TM_VSCR: | |
1427 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1428 | vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val); | |
1429 | else | |
1430 | r = - ENXIO; | |
1431 | break; | |
1432 | case KVM_REG_PPC_TM_DSCR: | |
1433 | vcpu->arch.dscr_tm = set_reg_val(id, *val); | |
1434 | break; | |
1435 | case KVM_REG_PPC_TM_TAR: | |
1436 | vcpu->arch.tar_tm = set_reg_val(id, *val); | |
1437 | break; | |
1438 | #endif | |
388cc6e1 PM |
1439 | case KVM_REG_PPC_ARCH_COMPAT: |
1440 | r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val)); | |
1441 | break; | |
31f3438e | 1442 | default: |
a136a8bd | 1443 | r = -EINVAL; |
31f3438e PM |
1444 | break; |
1445 | } | |
1446 | ||
1447 | return r; | |
1448 | } | |
1449 | ||
de9bdd1a SS |
1450 | static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core) |
1451 | { | |
1452 | struct kvmppc_vcore *vcore; | |
1453 | ||
1454 | vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); | |
1455 | ||
1456 | if (vcore == NULL) | |
1457 | return NULL; | |
1458 | ||
1459 | INIT_LIST_HEAD(&vcore->runnable_threads); | |
1460 | spin_lock_init(&vcore->lock); | |
2711e248 | 1461 | spin_lock_init(&vcore->stoltb_lock); |
de9bdd1a SS |
1462 | init_waitqueue_head(&vcore->wq); |
1463 | vcore->preempt_tb = TB_NIL; | |
1464 | vcore->lpcr = kvm->arch.lpcr; | |
1465 | vcore->first_vcpuid = core * threads_per_subcore; | |
1466 | vcore->kvm = kvm; | |
ec257165 | 1467 | INIT_LIST_HEAD(&vcore->preempt_list); |
de9bdd1a SS |
1468 | |
1469 | return vcore; | |
1470 | } | |
1471 | ||
b6c295df PM |
1472 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
1473 | static struct debugfs_timings_element { | |
1474 | const char *name; | |
1475 | size_t offset; | |
1476 | } timings[] = { | |
1477 | {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)}, | |
1478 | {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)}, | |
1479 | {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)}, | |
1480 | {"guest", offsetof(struct kvm_vcpu, arch.guest_time)}, | |
1481 | {"cede", offsetof(struct kvm_vcpu, arch.cede_time)}, | |
1482 | }; | |
1483 | ||
1484 | #define N_TIMINGS (sizeof(timings) / sizeof(timings[0])) | |
1485 | ||
1486 | struct debugfs_timings_state { | |
1487 | struct kvm_vcpu *vcpu; | |
1488 | unsigned int buflen; | |
1489 | char buf[N_TIMINGS * 100]; | |
1490 | }; | |
1491 | ||
1492 | static int debugfs_timings_open(struct inode *inode, struct file *file) | |
1493 | { | |
1494 | struct kvm_vcpu *vcpu = inode->i_private; | |
1495 | struct debugfs_timings_state *p; | |
1496 | ||
1497 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
1498 | if (!p) | |
1499 | return -ENOMEM; | |
1500 | ||
1501 | kvm_get_kvm(vcpu->kvm); | |
1502 | p->vcpu = vcpu; | |
1503 | file->private_data = p; | |
1504 | ||
1505 | return nonseekable_open(inode, file); | |
1506 | } | |
1507 | ||
1508 | static int debugfs_timings_release(struct inode *inode, struct file *file) | |
1509 | { | |
1510 | struct debugfs_timings_state *p = file->private_data; | |
1511 | ||
1512 | kvm_put_kvm(p->vcpu->kvm); | |
1513 | kfree(p); | |
1514 | return 0; | |
1515 | } | |
1516 | ||
1517 | static ssize_t debugfs_timings_read(struct file *file, char __user *buf, | |
1518 | size_t len, loff_t *ppos) | |
1519 | { | |
1520 | struct debugfs_timings_state *p = file->private_data; | |
1521 | struct kvm_vcpu *vcpu = p->vcpu; | |
1522 | char *s, *buf_end; | |
1523 | struct kvmhv_tb_accumulator tb; | |
1524 | u64 count; | |
1525 | loff_t pos; | |
1526 | ssize_t n; | |
1527 | int i, loops; | |
1528 | bool ok; | |
1529 | ||
1530 | if (!p->buflen) { | |
1531 | s = p->buf; | |
1532 | buf_end = s + sizeof(p->buf); | |
1533 | for (i = 0; i < N_TIMINGS; ++i) { | |
1534 | struct kvmhv_tb_accumulator *acc; | |
1535 | ||
1536 | acc = (struct kvmhv_tb_accumulator *) | |
1537 | ((unsigned long)vcpu + timings[i].offset); | |
1538 | ok = false; | |
1539 | for (loops = 0; loops < 1000; ++loops) { | |
1540 | count = acc->seqcount; | |
1541 | if (!(count & 1)) { | |
1542 | smp_rmb(); | |
1543 | tb = *acc; | |
1544 | smp_rmb(); | |
1545 | if (count == acc->seqcount) { | |
1546 | ok = true; | |
1547 | break; | |
1548 | } | |
1549 | } | |
1550 | udelay(1); | |
1551 | } | |
1552 | if (!ok) | |
1553 | snprintf(s, buf_end - s, "%s: stuck\n", | |
1554 | timings[i].name); | |
1555 | else | |
1556 | snprintf(s, buf_end - s, | |
1557 | "%s: %llu %llu %llu %llu\n", | |
1558 | timings[i].name, count / 2, | |
1559 | tb_to_ns(tb.tb_total), | |
1560 | tb_to_ns(tb.tb_min), | |
1561 | tb_to_ns(tb.tb_max)); | |
1562 | s += strlen(s); | |
1563 | } | |
1564 | p->buflen = s - p->buf; | |
1565 | } | |
1566 | ||
1567 | pos = *ppos; | |
1568 | if (pos >= p->buflen) | |
1569 | return 0; | |
1570 | if (len > p->buflen - pos) | |
1571 | len = p->buflen - pos; | |
1572 | n = copy_to_user(buf, p->buf + pos, len); | |
1573 | if (n) { | |
1574 | if (n == len) | |
1575 | return -EFAULT; | |
1576 | len -= n; | |
1577 | } | |
1578 | *ppos = pos + len; | |
1579 | return len; | |
1580 | } | |
1581 | ||
1582 | static ssize_t debugfs_timings_write(struct file *file, const char __user *buf, | |
1583 | size_t len, loff_t *ppos) | |
1584 | { | |
1585 | return -EACCES; | |
1586 | } | |
1587 | ||
1588 | static const struct file_operations debugfs_timings_ops = { | |
1589 | .owner = THIS_MODULE, | |
1590 | .open = debugfs_timings_open, | |
1591 | .release = debugfs_timings_release, | |
1592 | .read = debugfs_timings_read, | |
1593 | .write = debugfs_timings_write, | |
1594 | .llseek = generic_file_llseek, | |
1595 | }; | |
1596 | ||
1597 | /* Create a debugfs directory for the vcpu */ | |
1598 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) | |
1599 | { | |
1600 | char buf[16]; | |
1601 | struct kvm *kvm = vcpu->kvm; | |
1602 | ||
1603 | snprintf(buf, sizeof(buf), "vcpu%u", id); | |
1604 | if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir)) | |
1605 | return; | |
1606 | vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir); | |
1607 | if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir)) | |
1608 | return; | |
1609 | vcpu->arch.debugfs_timings = | |
1610 | debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir, | |
1611 | vcpu, &debugfs_timings_ops); | |
1612 | } | |
1613 | ||
1614 | #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ | |
1615 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) | |
1616 | { | |
1617 | } | |
1618 | #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ | |
1619 | ||
3a167bea AK |
1620 | static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm, |
1621 | unsigned int id) | |
de56a948 PM |
1622 | { |
1623 | struct kvm_vcpu *vcpu; | |
371fefd6 PM |
1624 | int err = -EINVAL; |
1625 | int core; | |
1626 | struct kvmppc_vcore *vcore; | |
de56a948 | 1627 | |
3102f784 | 1628 | core = id / threads_per_subcore; |
371fefd6 PM |
1629 | if (core >= KVM_MAX_VCORES) |
1630 | goto out; | |
1631 | ||
1632 | err = -ENOMEM; | |
6b75e6bf | 1633 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
de56a948 PM |
1634 | if (!vcpu) |
1635 | goto out; | |
1636 | ||
1637 | err = kvm_vcpu_init(vcpu, kvm, id); | |
1638 | if (err) | |
1639 | goto free_vcpu; | |
1640 | ||
1641 | vcpu->arch.shared = &vcpu->arch.shregs; | |
5deb8e7a AG |
1642 | #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE |
1643 | /* | |
1644 | * The shared struct is never shared on HV, | |
1645 | * so we can always use host endianness | |
1646 | */ | |
1647 | #ifdef __BIG_ENDIAN__ | |
1648 | vcpu->arch.shared_big_endian = true; | |
1649 | #else | |
1650 | vcpu->arch.shared_big_endian = false; | |
1651 | #endif | |
1652 | #endif | |
de56a948 PM |
1653 | vcpu->arch.mmcr[0] = MMCR0_FC; |
1654 | vcpu->arch.ctrl = CTRL_RUNLATCH; | |
1655 | /* default to host PVR, since we can't spoof it */ | |
3a167bea | 1656 | kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR)); |
2e25aa5f | 1657 | spin_lock_init(&vcpu->arch.vpa_update_lock); |
c7b67670 PM |
1658 | spin_lock_init(&vcpu->arch.tbacct_lock); |
1659 | vcpu->arch.busy_preempt = TB_NIL; | |
d682916a | 1660 | vcpu->arch.intr_msr = MSR_SF | MSR_ME; |
de56a948 | 1661 | |
de56a948 PM |
1662 | kvmppc_mmu_book3s_hv_init(vcpu); |
1663 | ||
8455d79e | 1664 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
371fefd6 PM |
1665 | |
1666 | init_waitqueue_head(&vcpu->arch.cpu_run); | |
1667 | ||
1668 | mutex_lock(&kvm->lock); | |
1669 | vcore = kvm->arch.vcores[core]; | |
1670 | if (!vcore) { | |
de9bdd1a | 1671 | vcore = kvmppc_vcore_create(kvm, core); |
371fefd6 | 1672 | kvm->arch.vcores[core] = vcore; |
1b400ba0 | 1673 | kvm->arch.online_vcores++; |
371fefd6 PM |
1674 | } |
1675 | mutex_unlock(&kvm->lock); | |
1676 | ||
1677 | if (!vcore) | |
1678 | goto free_vcpu; | |
1679 | ||
1680 | spin_lock(&vcore->lock); | |
1681 | ++vcore->num_threads; | |
371fefd6 PM |
1682 | spin_unlock(&vcore->lock); |
1683 | vcpu->arch.vcore = vcore; | |
e0b7ec05 | 1684 | vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid; |
ec257165 | 1685 | vcpu->arch.thread_cpu = -1; |
371fefd6 | 1686 | |
af8f38b3 AG |
1687 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
1688 | kvmppc_sanity_check(vcpu); | |
1689 | ||
b6c295df PM |
1690 | debugfs_vcpu_init(vcpu, id); |
1691 | ||
de56a948 PM |
1692 | return vcpu; |
1693 | ||
1694 | free_vcpu: | |
6b75e6bf | 1695 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
de56a948 PM |
1696 | out: |
1697 | return ERR_PTR(err); | |
1698 | } | |
1699 | ||
c35635ef PM |
1700 | static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) |
1701 | { | |
1702 | if (vpa->pinned_addr) | |
1703 | kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa, | |
1704 | vpa->dirty); | |
1705 | } | |
1706 | ||
3a167bea | 1707 | static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu) |
de56a948 | 1708 | { |
2e25aa5f | 1709 | spin_lock(&vcpu->arch.vpa_update_lock); |
c35635ef PM |
1710 | unpin_vpa(vcpu->kvm, &vcpu->arch.dtl); |
1711 | unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow); | |
1712 | unpin_vpa(vcpu->kvm, &vcpu->arch.vpa); | |
2e25aa5f | 1713 | spin_unlock(&vcpu->arch.vpa_update_lock); |
de56a948 | 1714 | kvm_vcpu_uninit(vcpu); |
6b75e6bf | 1715 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
de56a948 PM |
1716 | } |
1717 | ||
3a167bea AK |
1718 | static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu) |
1719 | { | |
1720 | /* Indicate we want to get back into the guest */ | |
1721 | return 1; | |
1722 | } | |
1723 | ||
19ccb76a | 1724 | static void kvmppc_set_timer(struct kvm_vcpu *vcpu) |
371fefd6 | 1725 | { |
19ccb76a | 1726 | unsigned long dec_nsec, now; |
371fefd6 | 1727 | |
19ccb76a PM |
1728 | now = get_tb(); |
1729 | if (now > vcpu->arch.dec_expires) { | |
1730 | /* decrementer has already gone negative */ | |
1731 | kvmppc_core_queue_dec(vcpu); | |
7e28e60e | 1732 | kvmppc_core_prepare_to_enter(vcpu); |
19ccb76a | 1733 | return; |
371fefd6 | 1734 | } |
19ccb76a PM |
1735 | dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC |
1736 | / tb_ticks_per_sec; | |
1737 | hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec), | |
1738 | HRTIMER_MODE_REL); | |
1739 | vcpu->arch.timer_running = 1; | |
371fefd6 PM |
1740 | } |
1741 | ||
19ccb76a | 1742 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu) |
371fefd6 | 1743 | { |
19ccb76a PM |
1744 | vcpu->arch.ceded = 0; |
1745 | if (vcpu->arch.timer_running) { | |
1746 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
1747 | vcpu->arch.timer_running = 0; | |
1748 | } | |
371fefd6 PM |
1749 | } |
1750 | ||
e0b7ec05 | 1751 | extern void __kvmppc_vcore_entry(void); |
de56a948 | 1752 | |
371fefd6 PM |
1753 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
1754 | struct kvm_vcpu *vcpu) | |
de56a948 | 1755 | { |
c7b67670 PM |
1756 | u64 now; |
1757 | ||
371fefd6 PM |
1758 | if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
1759 | return; | |
bf3d32e1 | 1760 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
c7b67670 PM |
1761 | now = mftb(); |
1762 | vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) - | |
1763 | vcpu->arch.stolen_logged; | |
1764 | vcpu->arch.busy_preempt = now; | |
1765 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; | |
bf3d32e1 | 1766 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
371fefd6 | 1767 | --vc->n_runnable; |
371fefd6 PM |
1768 | list_del(&vcpu->arch.run_list); |
1769 | } | |
1770 | ||
f0888f70 PM |
1771 | static int kvmppc_grab_hwthread(int cpu) |
1772 | { | |
1773 | struct paca_struct *tpaca; | |
b754c739 | 1774 | long timeout = 10000; |
f0888f70 PM |
1775 | |
1776 | tpaca = &paca[cpu]; | |
1777 | ||
1778 | /* Ensure the thread won't go into the kernel if it wakes */ | |
7b444c67 | 1779 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
b4deba5c | 1780 | tpaca->kvm_hstate.kvm_vcore = NULL; |
5d5b99cd PM |
1781 | tpaca->kvm_hstate.napping = 0; |
1782 | smp_wmb(); | |
1783 | tpaca->kvm_hstate.hwthread_req = 1; | |
f0888f70 PM |
1784 | |
1785 | /* | |
1786 | * If the thread is already executing in the kernel (e.g. handling | |
1787 | * a stray interrupt), wait for it to get back to nap mode. | |
1788 | * The smp_mb() is to ensure that our setting of hwthread_req | |
1789 | * is visible before we look at hwthread_state, so if this | |
1790 | * races with the code at system_reset_pSeries and the thread | |
1791 | * misses our setting of hwthread_req, we are sure to see its | |
1792 | * setting of hwthread_state, and vice versa. | |
1793 | */ | |
1794 | smp_mb(); | |
1795 | while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) { | |
1796 | if (--timeout <= 0) { | |
1797 | pr_err("KVM: couldn't grab cpu %d\n", cpu); | |
1798 | return -EBUSY; | |
1799 | } | |
1800 | udelay(1); | |
1801 | } | |
1802 | return 0; | |
1803 | } | |
1804 | ||
1805 | static void kvmppc_release_hwthread(int cpu) | |
1806 | { | |
1807 | struct paca_struct *tpaca; | |
1808 | ||
1809 | tpaca = &paca[cpu]; | |
1810 | tpaca->kvm_hstate.hwthread_req = 0; | |
1811 | tpaca->kvm_hstate.kvm_vcpu = NULL; | |
b4deba5c PM |
1812 | tpaca->kvm_hstate.kvm_vcore = NULL; |
1813 | tpaca->kvm_hstate.kvm_split_mode = NULL; | |
f0888f70 PM |
1814 | } |
1815 | ||
b4deba5c | 1816 | static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc) |
371fefd6 PM |
1817 | { |
1818 | int cpu; | |
1819 | struct paca_struct *tpaca; | |
ec257165 | 1820 | struct kvmppc_vcore *mvc = vc->master_vcore; |
371fefd6 | 1821 | |
b4deba5c PM |
1822 | cpu = vc->pcpu; |
1823 | if (vcpu) { | |
1824 | if (vcpu->arch.timer_running) { | |
1825 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
1826 | vcpu->arch.timer_running = 0; | |
1827 | } | |
1828 | cpu += vcpu->arch.ptid; | |
1829 | vcpu->cpu = mvc->pcpu; | |
1830 | vcpu->arch.thread_cpu = cpu; | |
19ccb76a | 1831 | } |
371fefd6 | 1832 | tpaca = &paca[cpu]; |
5d5b99cd | 1833 | tpaca->kvm_hstate.kvm_vcpu = vcpu; |
ec257165 | 1834 | tpaca->kvm_hstate.ptid = cpu - mvc->pcpu; |
ec257165 | 1835 | /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */ |
371fefd6 | 1836 | smp_wmb(); |
b4deba5c | 1837 | tpaca->kvm_hstate.kvm_vcore = mvc; |
5d5b99cd | 1838 | if (cpu != smp_processor_id()) |
66feed61 | 1839 | kvmppc_ipi_thread(cpu); |
371fefd6 | 1840 | } |
de56a948 | 1841 | |
5d5b99cd | 1842 | static void kvmppc_wait_for_nap(void) |
371fefd6 | 1843 | { |
5d5b99cd PM |
1844 | int cpu = smp_processor_id(); |
1845 | int i, loops; | |
371fefd6 | 1846 | |
5d5b99cd PM |
1847 | for (loops = 0; loops < 1000000; ++loops) { |
1848 | /* | |
1849 | * Check if all threads are finished. | |
b4deba5c | 1850 | * We set the vcore pointer when starting a thread |
5d5b99cd | 1851 | * and the thread clears it when finished, so we look |
b4deba5c | 1852 | * for any threads that still have a non-NULL vcore ptr. |
5d5b99cd PM |
1853 | */ |
1854 | for (i = 1; i < threads_per_subcore; ++i) | |
b4deba5c | 1855 | if (paca[cpu + i].kvm_hstate.kvm_vcore) |
5d5b99cd PM |
1856 | break; |
1857 | if (i == threads_per_subcore) { | |
1858 | HMT_medium(); | |
1859 | return; | |
371fefd6 | 1860 | } |
5d5b99cd | 1861 | HMT_low(); |
371fefd6 PM |
1862 | } |
1863 | HMT_medium(); | |
5d5b99cd | 1864 | for (i = 1; i < threads_per_subcore; ++i) |
b4deba5c | 1865 | if (paca[cpu + i].kvm_hstate.kvm_vcore) |
5d5b99cd | 1866 | pr_err("KVM: CPU %d seems to be stuck\n", cpu + i); |
371fefd6 PM |
1867 | } |
1868 | ||
1869 | /* | |
1870 | * Check that we are on thread 0 and that any other threads in | |
7b444c67 PM |
1871 | * this core are off-line. Then grab the threads so they can't |
1872 | * enter the kernel. | |
371fefd6 PM |
1873 | */ |
1874 | static int on_primary_thread(void) | |
1875 | { | |
1876 | int cpu = smp_processor_id(); | |
3102f784 | 1877 | int thr; |
371fefd6 | 1878 | |
3102f784 ME |
1879 | /* Are we on a primary subcore? */ |
1880 | if (cpu_thread_in_subcore(cpu)) | |
371fefd6 | 1881 | return 0; |
3102f784 ME |
1882 | |
1883 | thr = 0; | |
1884 | while (++thr < threads_per_subcore) | |
371fefd6 PM |
1885 | if (cpu_online(cpu + thr)) |
1886 | return 0; | |
7b444c67 PM |
1887 | |
1888 | /* Grab all hw threads so they can't go into the kernel */ | |
3102f784 | 1889 | for (thr = 1; thr < threads_per_subcore; ++thr) { |
7b444c67 PM |
1890 | if (kvmppc_grab_hwthread(cpu + thr)) { |
1891 | /* Couldn't grab one; let the others go */ | |
1892 | do { | |
1893 | kvmppc_release_hwthread(cpu + thr); | |
1894 | } while (--thr > 0); | |
1895 | return 0; | |
1896 | } | |
1897 | } | |
371fefd6 PM |
1898 | return 1; |
1899 | } | |
1900 | ||
ec257165 PM |
1901 | /* |
1902 | * A list of virtual cores for each physical CPU. | |
1903 | * These are vcores that could run but their runner VCPU tasks are | |
1904 | * (or may be) preempted. | |
1905 | */ | |
1906 | struct preempted_vcore_list { | |
1907 | struct list_head list; | |
1908 | spinlock_t lock; | |
1909 | }; | |
1910 | ||
1911 | static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores); | |
1912 | ||
1913 | static void init_vcore_lists(void) | |
1914 | { | |
1915 | int cpu; | |
1916 | ||
1917 | for_each_possible_cpu(cpu) { | |
1918 | struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu); | |
1919 | spin_lock_init(&lp->lock); | |
1920 | INIT_LIST_HEAD(&lp->list); | |
1921 | } | |
1922 | } | |
1923 | ||
1924 | static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc) | |
1925 | { | |
1926 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); | |
1927 | ||
1928 | vc->vcore_state = VCORE_PREEMPT; | |
1929 | vc->pcpu = smp_processor_id(); | |
1930 | if (vc->num_threads < threads_per_subcore) { | |
1931 | spin_lock(&lp->lock); | |
1932 | list_add_tail(&vc->preempt_list, &lp->list); | |
1933 | spin_unlock(&lp->lock); | |
1934 | } | |
1935 | ||
1936 | /* Start accumulating stolen time */ | |
1937 | kvmppc_core_start_stolen(vc); | |
1938 | } | |
1939 | ||
1940 | static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc) | |
1941 | { | |
402813fe | 1942 | struct preempted_vcore_list *lp; |
ec257165 PM |
1943 | |
1944 | kvmppc_core_end_stolen(vc); | |
1945 | if (!list_empty(&vc->preempt_list)) { | |
402813fe | 1946 | lp = &per_cpu(preempted_vcores, vc->pcpu); |
ec257165 PM |
1947 | spin_lock(&lp->lock); |
1948 | list_del_init(&vc->preempt_list); | |
1949 | spin_unlock(&lp->lock); | |
1950 | } | |
1951 | vc->vcore_state = VCORE_INACTIVE; | |
1952 | } | |
1953 | ||
b4deba5c PM |
1954 | /* |
1955 | * This stores information about the virtual cores currently | |
1956 | * assigned to a physical core. | |
1957 | */ | |
ec257165 | 1958 | struct core_info { |
b4deba5c PM |
1959 | int n_subcores; |
1960 | int max_subcore_threads; | |
ec257165 | 1961 | int total_threads; |
b4deba5c PM |
1962 | int subcore_threads[MAX_SUBCORES]; |
1963 | struct kvm *subcore_vm[MAX_SUBCORES]; | |
1964 | struct list_head vcs[MAX_SUBCORES]; | |
ec257165 PM |
1965 | }; |
1966 | ||
b4deba5c PM |
1967 | /* |
1968 | * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7 | |
1969 | * respectively in 2-way micro-threading (split-core) mode. | |
1970 | */ | |
1971 | static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 }; | |
1972 | ||
ec257165 PM |
1973 | static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc) |
1974 | { | |
b4deba5c PM |
1975 | int sub; |
1976 | ||
ec257165 | 1977 | memset(cip, 0, sizeof(*cip)); |
b4deba5c PM |
1978 | cip->n_subcores = 1; |
1979 | cip->max_subcore_threads = vc->num_threads; | |
ec257165 | 1980 | cip->total_threads = vc->num_threads; |
b4deba5c PM |
1981 | cip->subcore_threads[0] = vc->num_threads; |
1982 | cip->subcore_vm[0] = vc->kvm; | |
1983 | for (sub = 0; sub < MAX_SUBCORES; ++sub) | |
1984 | INIT_LIST_HEAD(&cip->vcs[sub]); | |
1985 | list_add_tail(&vc->preempt_list, &cip->vcs[0]); | |
1986 | } | |
1987 | ||
1988 | static bool subcore_config_ok(int n_subcores, int n_threads) | |
1989 | { | |
1990 | /* Can only dynamically split if unsplit to begin with */ | |
1991 | if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS) | |
1992 | return false; | |
1993 | if (n_subcores > MAX_SUBCORES) | |
1994 | return false; | |
1995 | if (n_subcores > 1) { | |
1996 | if (!(dynamic_mt_modes & 2)) | |
1997 | n_subcores = 4; | |
1998 | if (n_subcores > 2 && !(dynamic_mt_modes & 4)) | |
1999 | return false; | |
2000 | } | |
2001 | ||
2002 | return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS; | |
ec257165 PM |
2003 | } |
2004 | ||
2005 | static void init_master_vcore(struct kvmppc_vcore *vc) | |
2006 | { | |
2007 | vc->master_vcore = vc; | |
2008 | vc->entry_exit_map = 0; | |
2009 | vc->in_guest = 0; | |
2010 | vc->napping_threads = 0; | |
2011 | vc->conferring_threads = 0; | |
2012 | } | |
2013 | ||
2014 | /* | |
b4deba5c PM |
2015 | * See if the existing subcores can be split into 3 (or fewer) subcores |
2016 | * of at most two threads each, so we can fit in another vcore. This | |
2017 | * assumes there are at most two subcores and at most 6 threads in total. | |
ec257165 | 2018 | */ |
b4deba5c PM |
2019 | static bool can_split_piggybacked_subcores(struct core_info *cip) |
2020 | { | |
2021 | int sub, new_sub; | |
2022 | int large_sub = -1; | |
2023 | int thr; | |
2024 | int n_subcores = cip->n_subcores; | |
2025 | struct kvmppc_vcore *vc, *vcnext; | |
2026 | struct kvmppc_vcore *master_vc = NULL; | |
2027 | ||
2028 | for (sub = 0; sub < cip->n_subcores; ++sub) { | |
2029 | if (cip->subcore_threads[sub] <= 2) | |
2030 | continue; | |
2031 | if (large_sub >= 0) | |
2032 | return false; | |
2033 | large_sub = sub; | |
2034 | vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore, | |
2035 | preempt_list); | |
2036 | if (vc->num_threads > 2) | |
2037 | return false; | |
2038 | n_subcores += (cip->subcore_threads[sub] - 1) >> 1; | |
2039 | } | |
f74f2e2e | 2040 | if (large_sub < 0 || !subcore_config_ok(n_subcores + 1, 2)) |
b4deba5c PM |
2041 | return false; |
2042 | ||
2043 | /* | |
2044 | * Seems feasible, so go through and move vcores to new subcores. | |
2045 | * Note that when we have two or more vcores in one subcore, | |
2046 | * all those vcores must have only one thread each. | |
2047 | */ | |
2048 | new_sub = cip->n_subcores; | |
2049 | thr = 0; | |
2050 | sub = large_sub; | |
2051 | list_for_each_entry_safe(vc, vcnext, &cip->vcs[sub], preempt_list) { | |
2052 | if (thr >= 2) { | |
2053 | list_del(&vc->preempt_list); | |
2054 | list_add_tail(&vc->preempt_list, &cip->vcs[new_sub]); | |
2055 | /* vc->num_threads must be 1 */ | |
2056 | if (++cip->subcore_threads[new_sub] == 1) { | |
2057 | cip->subcore_vm[new_sub] = vc->kvm; | |
2058 | init_master_vcore(vc); | |
2059 | master_vc = vc; | |
2060 | ++cip->n_subcores; | |
2061 | } else { | |
2062 | vc->master_vcore = master_vc; | |
2063 | ++new_sub; | |
2064 | } | |
2065 | } | |
2066 | thr += vc->num_threads; | |
2067 | } | |
2068 | cip->subcore_threads[large_sub] = 2; | |
2069 | cip->max_subcore_threads = 2; | |
2070 | ||
2071 | return true; | |
2072 | } | |
2073 | ||
2074 | static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip) | |
2075 | { | |
2076 | int n_threads = vc->num_threads; | |
2077 | int sub; | |
2078 | ||
2079 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) | |
2080 | return false; | |
2081 | ||
2082 | if (n_threads < cip->max_subcore_threads) | |
2083 | n_threads = cip->max_subcore_threads; | |
2084 | if (subcore_config_ok(cip->n_subcores + 1, n_threads)) { | |
2085 | cip->max_subcore_threads = n_threads; | |
2086 | } else if (cip->n_subcores <= 2 && cip->total_threads <= 6 && | |
2087 | vc->num_threads <= 2) { | |
2088 | /* | |
2089 | * We may be able to fit another subcore in by | |
2090 | * splitting an existing subcore with 3 or 4 | |
2091 | * threads into two 2-thread subcores, or one | |
2092 | * with 5 or 6 threads into three subcores. | |
2093 | * We can only do this if those subcores have | |
2094 | * piggybacked virtual cores. | |
2095 | */ | |
2096 | if (!can_split_piggybacked_subcores(cip)) | |
2097 | return false; | |
2098 | } else { | |
2099 | return false; | |
2100 | } | |
2101 | ||
2102 | sub = cip->n_subcores; | |
2103 | ++cip->n_subcores; | |
2104 | cip->total_threads += vc->num_threads; | |
2105 | cip->subcore_threads[sub] = vc->num_threads; | |
2106 | cip->subcore_vm[sub] = vc->kvm; | |
2107 | init_master_vcore(vc); | |
2108 | list_del(&vc->preempt_list); | |
2109 | list_add_tail(&vc->preempt_list, &cip->vcs[sub]); | |
2110 | ||
2111 | return true; | |
2112 | } | |
2113 | ||
2114 | static bool can_piggyback_subcore(struct kvmppc_vcore *pvc, | |
2115 | struct core_info *cip, int sub) | |
ec257165 PM |
2116 | { |
2117 | struct kvmppc_vcore *vc; | |
b4deba5c | 2118 | int n_thr; |
ec257165 | 2119 | |
b4deba5c PM |
2120 | vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore, |
2121 | preempt_list); | |
ec257165 PM |
2122 | |
2123 | /* require same VM and same per-core reg values */ | |
2124 | if (pvc->kvm != vc->kvm || | |
2125 | pvc->tb_offset != vc->tb_offset || | |
2126 | pvc->pcr != vc->pcr || | |
2127 | pvc->lpcr != vc->lpcr) | |
2128 | return false; | |
2129 | ||
2130 | /* P8 guest with > 1 thread per core would see wrong TIR value */ | |
2131 | if (cpu_has_feature(CPU_FTR_ARCH_207S) && | |
2132 | (vc->num_threads > 1 || pvc->num_threads > 1)) | |
2133 | return false; | |
2134 | ||
b4deba5c PM |
2135 | n_thr = cip->subcore_threads[sub] + pvc->num_threads; |
2136 | if (n_thr > cip->max_subcore_threads) { | |
2137 | if (!subcore_config_ok(cip->n_subcores, n_thr)) | |
2138 | return false; | |
2139 | cip->max_subcore_threads = n_thr; | |
2140 | } | |
ec257165 PM |
2141 | |
2142 | cip->total_threads += pvc->num_threads; | |
b4deba5c | 2143 | cip->subcore_threads[sub] = n_thr; |
ec257165 PM |
2144 | pvc->master_vcore = vc; |
2145 | list_del(&pvc->preempt_list); | |
b4deba5c | 2146 | list_add_tail(&pvc->preempt_list, &cip->vcs[sub]); |
ec257165 PM |
2147 | |
2148 | return true; | |
2149 | } | |
2150 | ||
b4deba5c PM |
2151 | /* |
2152 | * Work out whether it is possible to piggyback the execution of | |
2153 | * vcore *pvc onto the execution of the other vcores described in *cip. | |
2154 | */ | |
2155 | static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip, | |
2156 | int target_threads) | |
2157 | { | |
2158 | int sub; | |
2159 | ||
2160 | if (cip->total_threads + pvc->num_threads > target_threads) | |
2161 | return false; | |
2162 | for (sub = 0; sub < cip->n_subcores; ++sub) | |
2163 | if (cip->subcore_threads[sub] && | |
2164 | can_piggyback_subcore(pvc, cip, sub)) | |
2165 | return true; | |
2166 | ||
2167 | if (can_dynamic_split(pvc, cip)) | |
2168 | return true; | |
2169 | ||
2170 | return false; | |
2171 | } | |
2172 | ||
d911f0be PM |
2173 | static void prepare_threads(struct kvmppc_vcore *vc) |
2174 | { | |
2175 | struct kvm_vcpu *vcpu, *vnext; | |
2176 | ||
2177 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, | |
2178 | arch.run_list) { | |
2179 | if (signal_pending(vcpu->arch.run_task)) | |
2180 | vcpu->arch.ret = -EINTR; | |
2181 | else if (vcpu->arch.vpa.update_pending || | |
2182 | vcpu->arch.slb_shadow.update_pending || | |
2183 | vcpu->arch.dtl.update_pending) | |
2184 | vcpu->arch.ret = RESUME_GUEST; | |
2185 | else | |
2186 | continue; | |
2187 | kvmppc_remove_runnable(vc, vcpu); | |
2188 | wake_up(&vcpu->arch.cpu_run); | |
2189 | } | |
2190 | } | |
2191 | ||
ec257165 PM |
2192 | static void collect_piggybacks(struct core_info *cip, int target_threads) |
2193 | { | |
2194 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); | |
2195 | struct kvmppc_vcore *pvc, *vcnext; | |
2196 | ||
2197 | spin_lock(&lp->lock); | |
2198 | list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) { | |
2199 | if (!spin_trylock(&pvc->lock)) | |
2200 | continue; | |
2201 | prepare_threads(pvc); | |
2202 | if (!pvc->n_runnable) { | |
2203 | list_del_init(&pvc->preempt_list); | |
2204 | if (pvc->runner == NULL) { | |
2205 | pvc->vcore_state = VCORE_INACTIVE; | |
2206 | kvmppc_core_end_stolen(pvc); | |
2207 | } | |
2208 | spin_unlock(&pvc->lock); | |
2209 | continue; | |
2210 | } | |
2211 | if (!can_piggyback(pvc, cip, target_threads)) { | |
2212 | spin_unlock(&pvc->lock); | |
2213 | continue; | |
2214 | } | |
2215 | kvmppc_core_end_stolen(pvc); | |
2216 | pvc->vcore_state = VCORE_PIGGYBACK; | |
2217 | if (cip->total_threads >= target_threads) | |
2218 | break; | |
2219 | } | |
2220 | spin_unlock(&lp->lock); | |
2221 | } | |
2222 | ||
2223 | static void post_guest_process(struct kvmppc_vcore *vc, bool is_master) | |
25fedfca | 2224 | { |
ec257165 | 2225 | int still_running = 0; |
25fedfca PM |
2226 | u64 now; |
2227 | long ret; | |
2228 | struct kvm_vcpu *vcpu, *vnext; | |
2229 | ||
ec257165 | 2230 | spin_lock(&vc->lock); |
25fedfca PM |
2231 | now = get_tb(); |
2232 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, | |
2233 | arch.run_list) { | |
2234 | /* cancel pending dec exception if dec is positive */ | |
2235 | if (now < vcpu->arch.dec_expires && | |
2236 | kvmppc_core_pending_dec(vcpu)) | |
2237 | kvmppc_core_dequeue_dec(vcpu); | |
2238 | ||
2239 | trace_kvm_guest_exit(vcpu); | |
2240 | ||
2241 | ret = RESUME_GUEST; | |
2242 | if (vcpu->arch.trap) | |
2243 | ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu, | |
2244 | vcpu->arch.run_task); | |
2245 | ||
2246 | vcpu->arch.ret = ret; | |
2247 | vcpu->arch.trap = 0; | |
2248 | ||
ec257165 PM |
2249 | if (is_kvmppc_resume_guest(vcpu->arch.ret)) { |
2250 | if (vcpu->arch.pending_exceptions) | |
2251 | kvmppc_core_prepare_to_enter(vcpu); | |
2252 | if (vcpu->arch.ceded) | |
25fedfca | 2253 | kvmppc_set_timer(vcpu); |
ec257165 PM |
2254 | else |
2255 | ++still_running; | |
2256 | } else { | |
25fedfca PM |
2257 | kvmppc_remove_runnable(vc, vcpu); |
2258 | wake_up(&vcpu->arch.cpu_run); | |
2259 | } | |
2260 | } | |
ec257165 PM |
2261 | list_del_init(&vc->preempt_list); |
2262 | if (!is_master) { | |
563a1e93 | 2263 | if (still_running > 0) { |
ec257165 | 2264 | kvmppc_vcore_preempt(vc); |
563a1e93 PM |
2265 | } else if (vc->runner) { |
2266 | vc->vcore_state = VCORE_PREEMPT; | |
2267 | kvmppc_core_start_stolen(vc); | |
2268 | } else { | |
2269 | vc->vcore_state = VCORE_INACTIVE; | |
2270 | } | |
ec257165 PM |
2271 | if (vc->n_runnable > 0 && vc->runner == NULL) { |
2272 | /* make sure there's a candidate runner awake */ | |
2273 | vcpu = list_first_entry(&vc->runnable_threads, | |
2274 | struct kvm_vcpu, arch.run_list); | |
2275 | wake_up(&vcpu->arch.cpu_run); | |
2276 | } | |
2277 | } | |
2278 | spin_unlock(&vc->lock); | |
25fedfca PM |
2279 | } |
2280 | ||
371fefd6 PM |
2281 | /* |
2282 | * Run a set of guest threads on a physical core. | |
2283 | * Called with vc->lock held. | |
2284 | */ | |
66feed61 | 2285 | static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) |
371fefd6 | 2286 | { |
17d48901 | 2287 | struct kvm_vcpu *vcpu, *vnext; |
d911f0be | 2288 | int i; |
2c9097e4 | 2289 | int srcu_idx; |
ec257165 PM |
2290 | struct core_info core_info; |
2291 | struct kvmppc_vcore *pvc, *vcnext; | |
b4deba5c PM |
2292 | struct kvm_split_mode split_info, *sip; |
2293 | int split, subcore_size, active; | |
2294 | int sub; | |
2295 | bool thr0_done; | |
2296 | unsigned long cmd_bit, stat_bit; | |
ec257165 PM |
2297 | int pcpu, thr; |
2298 | int target_threads; | |
371fefd6 | 2299 | |
d911f0be PM |
2300 | /* |
2301 | * Remove from the list any threads that have a signal pending | |
2302 | * or need a VPA update done | |
2303 | */ | |
2304 | prepare_threads(vc); | |
2305 | ||
2306 | /* if the runner is no longer runnable, let the caller pick a new one */ | |
2307 | if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE) | |
2308 | return; | |
081f323b PM |
2309 | |
2310 | /* | |
d911f0be | 2311 | * Initialize *vc. |
081f323b | 2312 | */ |
ec257165 | 2313 | init_master_vcore(vc); |
2711e248 | 2314 | vc->preempt_tb = TB_NIL; |
081f323b | 2315 | |
7b444c67 | 2316 | /* |
3102f784 ME |
2317 | * Make sure we are running on primary threads, and that secondary |
2318 | * threads are offline. Also check if the number of threads in this | |
2319 | * guest are greater than the current system threads per guest. | |
7b444c67 | 2320 | */ |
3102f784 ME |
2321 | if ((threads_per_core > 1) && |
2322 | ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) { | |
17d48901 PM |
2323 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, |
2324 | arch.run_list) { | |
7b444c67 | 2325 | vcpu->arch.ret = -EBUSY; |
25fedfca PM |
2326 | kvmppc_remove_runnable(vc, vcpu); |
2327 | wake_up(&vcpu->arch.cpu_run); | |
2328 | } | |
7b444c67 PM |
2329 | goto out; |
2330 | } | |
2331 | ||
ec257165 PM |
2332 | /* |
2333 | * See if we could run any other vcores on the physical core | |
2334 | * along with this one. | |
2335 | */ | |
2336 | init_core_info(&core_info, vc); | |
2337 | pcpu = smp_processor_id(); | |
2338 | target_threads = threads_per_subcore; | |
2339 | if (target_smt_mode && target_smt_mode < target_threads) | |
2340 | target_threads = target_smt_mode; | |
2341 | if (vc->num_threads < target_threads) | |
2342 | collect_piggybacks(&core_info, target_threads); | |
3102f784 | 2343 | |
b4deba5c PM |
2344 | /* Decide on micro-threading (split-core) mode */ |
2345 | subcore_size = threads_per_subcore; | |
2346 | cmd_bit = stat_bit = 0; | |
2347 | split = core_info.n_subcores; | |
2348 | sip = NULL; | |
2349 | if (split > 1) { | |
2350 | /* threads_per_subcore must be MAX_SMT_THREADS (8) here */ | |
2351 | if (split == 2 && (dynamic_mt_modes & 2)) { | |
2352 | cmd_bit = HID0_POWER8_1TO2LPAR; | |
2353 | stat_bit = HID0_POWER8_2LPARMODE; | |
2354 | } else { | |
2355 | split = 4; | |
2356 | cmd_bit = HID0_POWER8_1TO4LPAR; | |
2357 | stat_bit = HID0_POWER8_4LPARMODE; | |
2358 | } | |
2359 | subcore_size = MAX_SMT_THREADS / split; | |
2360 | sip = &split_info; | |
2361 | memset(&split_info, 0, sizeof(split_info)); | |
2362 | split_info.rpr = mfspr(SPRN_RPR); | |
2363 | split_info.pmmar = mfspr(SPRN_PMMAR); | |
2364 | split_info.ldbar = mfspr(SPRN_LDBAR); | |
2365 | split_info.subcore_size = subcore_size; | |
2366 | for (sub = 0; sub < core_info.n_subcores; ++sub) | |
2367 | split_info.master_vcs[sub] = | |
2368 | list_first_entry(&core_info.vcs[sub], | |
2369 | struct kvmppc_vcore, preempt_list); | |
2370 | /* order writes to split_info before kvm_split_mode pointer */ | |
2371 | smp_wmb(); | |
2372 | } | |
2373 | pcpu = smp_processor_id(); | |
2374 | for (thr = 0; thr < threads_per_subcore; ++thr) | |
2375 | paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip; | |
2376 | ||
2377 | /* Initiate micro-threading (split-core) if required */ | |
2378 | if (cmd_bit) { | |
2379 | unsigned long hid0 = mfspr(SPRN_HID0); | |
2380 | ||
2381 | hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS; | |
2382 | mb(); | |
2383 | mtspr(SPRN_HID0, hid0); | |
2384 | isync(); | |
2385 | for (;;) { | |
2386 | hid0 = mfspr(SPRN_HID0); | |
2387 | if (hid0 & stat_bit) | |
2388 | break; | |
2389 | cpu_relax(); | |
ec257165 | 2390 | } |
2e25aa5f | 2391 | } |
3102f784 | 2392 | |
b4deba5c PM |
2393 | /* Start all the threads */ |
2394 | active = 0; | |
2395 | for (sub = 0; sub < core_info.n_subcores; ++sub) { | |
2396 | thr = subcore_thread_map[sub]; | |
2397 | thr0_done = false; | |
2398 | active |= 1 << thr; | |
2399 | list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) { | |
2400 | pvc->pcpu = pcpu + thr; | |
2401 | list_for_each_entry(vcpu, &pvc->runnable_threads, | |
2402 | arch.run_list) { | |
2403 | kvmppc_start_thread(vcpu, pvc); | |
2404 | kvmppc_create_dtl_entry(vcpu, pvc); | |
2405 | trace_kvm_guest_enter(vcpu); | |
2406 | if (!vcpu->arch.ptid) | |
2407 | thr0_done = true; | |
2408 | active |= 1 << (thr + vcpu->arch.ptid); | |
2409 | } | |
2410 | /* | |
2411 | * We need to start the first thread of each subcore | |
2412 | * even if it doesn't have a vcpu. | |
2413 | */ | |
2414 | if (pvc->master_vcore == pvc && !thr0_done) | |
2415 | kvmppc_start_thread(NULL, pvc); | |
2416 | thr += pvc->num_threads; | |
2417 | } | |
2e25aa5f | 2418 | } |
371fefd6 | 2419 | |
7f235328 GS |
2420 | /* |
2421 | * Ensure that split_info.do_nap is set after setting | |
2422 | * the vcore pointer in the PACA of the secondaries. | |
2423 | */ | |
2424 | smp_mb(); | |
2425 | if (cmd_bit) | |
2426 | split_info.do_nap = 1; /* ask secondaries to nap when done */ | |
2427 | ||
b4deba5c PM |
2428 | /* |
2429 | * When doing micro-threading, poke the inactive threads as well. | |
2430 | * This gets them to the nap instruction after kvm_do_nap, | |
2431 | * which reduces the time taken to unsplit later. | |
2432 | */ | |
2433 | if (split > 1) | |
2434 | for (thr = 1; thr < threads_per_subcore; ++thr) | |
2435 | if (!(active & (1 << thr))) | |
2436 | kvmppc_ipi_thread(pcpu + thr); | |
e0b7ec05 | 2437 | |
2f12f034 | 2438 | vc->vcore_state = VCORE_RUNNING; |
19ccb76a | 2439 | preempt_disable(); |
3c78f78a SW |
2440 | |
2441 | trace_kvmppc_run_core(vc, 0); | |
2442 | ||
b4deba5c PM |
2443 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
2444 | list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) | |
2445 | spin_unlock(&pvc->lock); | |
de56a948 | 2446 | |
371fefd6 | 2447 | kvm_guest_enter(); |
2c9097e4 | 2448 | |
e0b7ec05 | 2449 | srcu_idx = srcu_read_lock(&vc->kvm->srcu); |
2c9097e4 | 2450 | |
e0b7ec05 | 2451 | __kvmppc_vcore_entry(); |
de56a948 | 2452 | |
ec257165 PM |
2453 | srcu_read_unlock(&vc->kvm->srcu, srcu_idx); |
2454 | ||
2455 | spin_lock(&vc->lock); | |
371fefd6 | 2456 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ |
19ccb76a | 2457 | vc->vcore_state = VCORE_EXITING; |
371fefd6 | 2458 | |
19ccb76a | 2459 | /* wait for secondary threads to finish writing their state to memory */ |
5d5b99cd | 2460 | kvmppc_wait_for_nap(); |
b4deba5c PM |
2461 | |
2462 | /* Return to whole-core mode if we split the core earlier */ | |
2463 | if (split > 1) { | |
2464 | unsigned long hid0 = mfspr(SPRN_HID0); | |
2465 | unsigned long loops = 0; | |
2466 | ||
2467 | hid0 &= ~HID0_POWER8_DYNLPARDIS; | |
2468 | stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE; | |
2469 | mb(); | |
2470 | mtspr(SPRN_HID0, hid0); | |
2471 | isync(); | |
2472 | for (;;) { | |
2473 | hid0 = mfspr(SPRN_HID0); | |
2474 | if (!(hid0 & stat_bit)) | |
2475 | break; | |
2476 | cpu_relax(); | |
2477 | ++loops; | |
2478 | } | |
2479 | split_info.do_nap = 0; | |
2480 | } | |
2481 | ||
2482 | /* Let secondaries go back to the offline loop */ | |
2483 | for (i = 0; i < threads_per_subcore; ++i) { | |
2484 | kvmppc_release_hwthread(pcpu + i); | |
2485 | if (sip && sip->napped[i]) | |
2486 | kvmppc_ipi_thread(pcpu + i); | |
2487 | } | |
2488 | ||
371fefd6 | 2489 | spin_unlock(&vc->lock); |
2c9097e4 | 2490 | |
371fefd6 PM |
2491 | /* make sure updates to secondary vcpu structs are visible now */ |
2492 | smp_mb(); | |
de56a948 PM |
2493 | kvm_guest_exit(); |
2494 | ||
b4deba5c PM |
2495 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
2496 | list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub], | |
2497 | preempt_list) | |
2498 | post_guest_process(pvc, pvc == vc); | |
de56a948 | 2499 | |
913d3ff9 | 2500 | spin_lock(&vc->lock); |
ec257165 | 2501 | preempt_enable(); |
de56a948 PM |
2502 | |
2503 | out: | |
19ccb76a | 2504 | vc->vcore_state = VCORE_INACTIVE; |
3c78f78a | 2505 | trace_kvmppc_run_core(vc, 1); |
371fefd6 PM |
2506 | } |
2507 | ||
19ccb76a PM |
2508 | /* |
2509 | * Wait for some other vcpu thread to execute us, and | |
2510 | * wake us up when we need to handle something in the host. | |
2511 | */ | |
ec257165 PM |
2512 | static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc, |
2513 | struct kvm_vcpu *vcpu, int wait_state) | |
371fefd6 | 2514 | { |
371fefd6 PM |
2515 | DEFINE_WAIT(wait); |
2516 | ||
19ccb76a | 2517 | prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); |
ec257165 PM |
2518 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
2519 | spin_unlock(&vc->lock); | |
19ccb76a | 2520 | schedule(); |
ec257165 PM |
2521 | spin_lock(&vc->lock); |
2522 | } | |
19ccb76a PM |
2523 | finish_wait(&vcpu->arch.cpu_run, &wait); |
2524 | } | |
2525 | ||
2526 | /* | |
2527 | * All the vcpus in this vcore are idle, so wait for a decrementer | |
2528 | * or external interrupt to one of the vcpus. vc->lock is held. | |
2529 | */ | |
2530 | static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) | |
2531 | { | |
1bc5d59c SW |
2532 | struct kvm_vcpu *vcpu; |
2533 | int do_sleep = 1; | |
2534 | ||
19ccb76a | 2535 | DEFINE_WAIT(wait); |
19ccb76a PM |
2536 | |
2537 | prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE); | |
1bc5d59c SW |
2538 | |
2539 | /* | |
2540 | * Check one last time for pending exceptions and ceded state after | |
2541 | * we put ourselves on the wait queue | |
2542 | */ | |
2543 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { | |
2544 | if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) { | |
2545 | do_sleep = 0; | |
2546 | break; | |
2547 | } | |
2548 | } | |
2549 | ||
2550 | if (!do_sleep) { | |
2551 | finish_wait(&vc->wq, &wait); | |
2552 | return; | |
2553 | } | |
2554 | ||
19ccb76a | 2555 | vc->vcore_state = VCORE_SLEEPING; |
3c78f78a | 2556 | trace_kvmppc_vcore_blocked(vc, 0); |
19ccb76a | 2557 | spin_unlock(&vc->lock); |
913d3ff9 | 2558 | schedule(); |
19ccb76a PM |
2559 | finish_wait(&vc->wq, &wait); |
2560 | spin_lock(&vc->lock); | |
2561 | vc->vcore_state = VCORE_INACTIVE; | |
3c78f78a | 2562 | trace_kvmppc_vcore_blocked(vc, 1); |
19ccb76a | 2563 | } |
371fefd6 | 2564 | |
19ccb76a PM |
2565 | static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
2566 | { | |
2567 | int n_ceded; | |
19ccb76a PM |
2568 | struct kvmppc_vcore *vc; |
2569 | struct kvm_vcpu *v, *vn; | |
9e368f29 | 2570 | |
3c78f78a SW |
2571 | trace_kvmppc_run_vcpu_enter(vcpu); |
2572 | ||
371fefd6 PM |
2573 | kvm_run->exit_reason = 0; |
2574 | vcpu->arch.ret = RESUME_GUEST; | |
2575 | vcpu->arch.trap = 0; | |
2f12f034 | 2576 | kvmppc_update_vpas(vcpu); |
371fefd6 | 2577 | |
371fefd6 PM |
2578 | /* |
2579 | * Synchronize with other threads in this virtual core | |
2580 | */ | |
2581 | vc = vcpu->arch.vcore; | |
2582 | spin_lock(&vc->lock); | |
19ccb76a | 2583 | vcpu->arch.ceded = 0; |
371fefd6 PM |
2584 | vcpu->arch.run_task = current; |
2585 | vcpu->arch.kvm_run = kvm_run; | |
c7b67670 | 2586 | vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); |
19ccb76a | 2587 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; |
c7b67670 | 2588 | vcpu->arch.busy_preempt = TB_NIL; |
371fefd6 PM |
2589 | list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads); |
2590 | ++vc->n_runnable; | |
2591 | ||
19ccb76a PM |
2592 | /* |
2593 | * This happens the first time this is called for a vcpu. | |
2594 | * If the vcore is already running, we may be able to start | |
2595 | * this thread straight away and have it join in. | |
2596 | */ | |
8455d79e | 2597 | if (!signal_pending(current)) { |
ec257165 PM |
2598 | if (vc->vcore_state == VCORE_PIGGYBACK) { |
2599 | struct kvmppc_vcore *mvc = vc->master_vcore; | |
2600 | if (spin_trylock(&mvc->lock)) { | |
2601 | if (mvc->vcore_state == VCORE_RUNNING && | |
2602 | !VCORE_IS_EXITING(mvc)) { | |
2603 | kvmppc_create_dtl_entry(vcpu, vc); | |
b4deba5c | 2604 | kvmppc_start_thread(vcpu, vc); |
ec257165 PM |
2605 | trace_kvm_guest_enter(vcpu); |
2606 | } | |
2607 | spin_unlock(&mvc->lock); | |
2608 | } | |
2609 | } else if (vc->vcore_state == VCORE_RUNNING && | |
2610 | !VCORE_IS_EXITING(vc)) { | |
2f12f034 | 2611 | kvmppc_create_dtl_entry(vcpu, vc); |
b4deba5c | 2612 | kvmppc_start_thread(vcpu, vc); |
3c78f78a | 2613 | trace_kvm_guest_enter(vcpu); |
8455d79e PM |
2614 | } else if (vc->vcore_state == VCORE_SLEEPING) { |
2615 | wake_up(&vc->wq); | |
371fefd6 PM |
2616 | } |
2617 | ||
8455d79e | 2618 | } |
371fefd6 | 2619 | |
19ccb76a PM |
2620 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
2621 | !signal_pending(current)) { | |
ec257165 PM |
2622 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
2623 | kvmppc_vcore_end_preempt(vc); | |
2624 | ||
8455d79e | 2625 | if (vc->vcore_state != VCORE_INACTIVE) { |
ec257165 | 2626 | kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE); |
19ccb76a PM |
2627 | continue; |
2628 | } | |
19ccb76a PM |
2629 | list_for_each_entry_safe(v, vn, &vc->runnable_threads, |
2630 | arch.run_list) { | |
7e28e60e | 2631 | kvmppc_core_prepare_to_enter(v); |
19ccb76a PM |
2632 | if (signal_pending(v->arch.run_task)) { |
2633 | kvmppc_remove_runnable(vc, v); | |
2634 | v->stat.signal_exits++; | |
2635 | v->arch.kvm_run->exit_reason = KVM_EXIT_INTR; | |
2636 | v->arch.ret = -EINTR; | |
2637 | wake_up(&v->arch.cpu_run); | |
2638 | } | |
2639 | } | |
8455d79e PM |
2640 | if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
2641 | break; | |
8455d79e | 2642 | n_ceded = 0; |
4619ac88 | 2643 | list_for_each_entry(v, &vc->runnable_threads, arch.run_list) { |
8455d79e PM |
2644 | if (!v->arch.pending_exceptions) |
2645 | n_ceded += v->arch.ceded; | |
4619ac88 PM |
2646 | else |
2647 | v->arch.ceded = 0; | |
2648 | } | |
25fedfca PM |
2649 | vc->runner = vcpu; |
2650 | if (n_ceded == vc->n_runnable) { | |
8455d79e | 2651 | kvmppc_vcore_blocked(vc); |
c56dadf3 | 2652 | } else if (need_resched()) { |
ec257165 | 2653 | kvmppc_vcore_preempt(vc); |
25fedfca PM |
2654 | /* Let something else run */ |
2655 | cond_resched_lock(&vc->lock); | |
ec257165 PM |
2656 | if (vc->vcore_state == VCORE_PREEMPT) |
2657 | kvmppc_vcore_end_preempt(vc); | |
25fedfca | 2658 | } else { |
8455d79e | 2659 | kvmppc_run_core(vc); |
25fedfca | 2660 | } |
0456ec4f | 2661 | vc->runner = NULL; |
19ccb76a | 2662 | } |
371fefd6 | 2663 | |
8455d79e PM |
2664 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
2665 | (vc->vcore_state == VCORE_RUNNING || | |
5fc3e64f PM |
2666 | vc->vcore_state == VCORE_EXITING || |
2667 | vc->vcore_state == VCORE_PIGGYBACK)) | |
ec257165 | 2668 | kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE); |
8455d79e | 2669 | |
5fc3e64f PM |
2670 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
2671 | kvmppc_vcore_end_preempt(vc); | |
2672 | ||
8455d79e PM |
2673 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
2674 | kvmppc_remove_runnable(vc, vcpu); | |
2675 | vcpu->stat.signal_exits++; | |
2676 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2677 | vcpu->arch.ret = -EINTR; | |
2678 | } | |
2679 | ||
2680 | if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) { | |
2681 | /* Wake up some vcpu to run the core */ | |
2682 | v = list_first_entry(&vc->runnable_threads, | |
2683 | struct kvm_vcpu, arch.run_list); | |
2684 | wake_up(&v->arch.cpu_run); | |
371fefd6 PM |
2685 | } |
2686 | ||
3c78f78a | 2687 | trace_kvmppc_run_vcpu_exit(vcpu, kvm_run); |
371fefd6 | 2688 | spin_unlock(&vc->lock); |
371fefd6 | 2689 | return vcpu->arch.ret; |
de56a948 PM |
2690 | } |
2691 | ||
3a167bea | 2692 | static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu) |
a8606e20 PM |
2693 | { |
2694 | int r; | |
913d3ff9 | 2695 | int srcu_idx; |
a8606e20 | 2696 | |
af8f38b3 AG |
2697 | if (!vcpu->arch.sane) { |
2698 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
2699 | return -EINVAL; | |
2700 | } | |
2701 | ||
25051b5a SW |
2702 | kvmppc_core_prepare_to_enter(vcpu); |
2703 | ||
19ccb76a PM |
2704 | /* No need to go into the guest when all we'll do is come back out */ |
2705 | if (signal_pending(current)) { | |
2706 | run->exit_reason = KVM_EXIT_INTR; | |
2707 | return -EINTR; | |
2708 | } | |
2709 | ||
32fad281 | 2710 | atomic_inc(&vcpu->kvm->arch.vcpus_running); |
31037eca | 2711 | /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */ |
32fad281 PM |
2712 | smp_mb(); |
2713 | ||
c17b98cf | 2714 | /* On the first time here, set up HTAB and VRMA */ |
31037eca | 2715 | if (!vcpu->kvm->arch.hpte_setup_done) { |
32fad281 | 2716 | r = kvmppc_hv_setup_htab_rma(vcpu); |
c77162de | 2717 | if (r) |
32fad281 | 2718 | goto out; |
c77162de | 2719 | } |
19ccb76a | 2720 | |
579e633e AB |
2721 | flush_all_to_thread(current); |
2722 | ||
19ccb76a | 2723 | vcpu->arch.wqp = &vcpu->arch.vcore->wq; |
342d3db7 | 2724 | vcpu->arch.pgdir = current->mm->pgd; |
c7b67670 | 2725 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
19ccb76a | 2726 | |
a8606e20 PM |
2727 | do { |
2728 | r = kvmppc_run_vcpu(run, vcpu); | |
2729 | ||
2730 | if (run->exit_reason == KVM_EXIT_PAPR_HCALL && | |
2731 | !(vcpu->arch.shregs.msr & MSR_PR)) { | |
3c78f78a | 2732 | trace_kvm_hcall_enter(vcpu); |
a8606e20 | 2733 | r = kvmppc_pseries_do_hcall(vcpu); |
3c78f78a | 2734 | trace_kvm_hcall_exit(vcpu, r); |
7e28e60e | 2735 | kvmppc_core_prepare_to_enter(vcpu); |
913d3ff9 PM |
2736 | } else if (r == RESUME_PAGE_FAULT) { |
2737 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); | |
2738 | r = kvmppc_book3s_hv_page_fault(run, vcpu, | |
2739 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); | |
2740 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); | |
a8606e20 | 2741 | } |
e59d24e6 | 2742 | } while (is_kvmppc_resume_guest(r)); |
32fad281 PM |
2743 | |
2744 | out: | |
c7b67670 | 2745 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
32fad281 | 2746 | atomic_dec(&vcpu->kvm->arch.vcpus_running); |
a8606e20 PM |
2747 | return r; |
2748 | } | |
2749 | ||
5b74716e BH |
2750 | static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps, |
2751 | int linux_psize) | |
2752 | { | |
2753 | struct mmu_psize_def *def = &mmu_psize_defs[linux_psize]; | |
2754 | ||
2755 | if (!def->shift) | |
2756 | return; | |
2757 | (*sps)->page_shift = def->shift; | |
2758 | (*sps)->slb_enc = def->sllp; | |
2759 | (*sps)->enc[0].page_shift = def->shift; | |
b1022fbd | 2760 | (*sps)->enc[0].pte_enc = def->penc[linux_psize]; |
1f365bb0 AK |
2761 | /* |
2762 | * Add 16MB MPSS support if host supports it | |
2763 | */ | |
2764 | if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) { | |
2765 | (*sps)->enc[1].page_shift = 24; | |
2766 | (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M]; | |
2767 | } | |
5b74716e BH |
2768 | (*sps)++; |
2769 | } | |
2770 | ||
3a167bea AK |
2771 | static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, |
2772 | struct kvm_ppc_smmu_info *info) | |
5b74716e BH |
2773 | { |
2774 | struct kvm_ppc_one_seg_page_size *sps; | |
2775 | ||
2776 | info->flags = KVM_PPC_PAGE_SIZES_REAL; | |
2777 | if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) | |
2778 | info->flags |= KVM_PPC_1T_SEGMENTS; | |
2779 | info->slb_size = mmu_slb_size; | |
2780 | ||
2781 | /* We only support these sizes for now, and no muti-size segments */ | |
2782 | sps = &info->sps[0]; | |
2783 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K); | |
2784 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K); | |
2785 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M); | |
2786 | ||
2787 | return 0; | |
2788 | } | |
2789 | ||
82ed3616 PM |
2790 | /* |
2791 | * Get (and clear) the dirty memory log for a memory slot. | |
2792 | */ | |
3a167bea AK |
2793 | static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm, |
2794 | struct kvm_dirty_log *log) | |
82ed3616 | 2795 | { |
9f6b8029 | 2796 | struct kvm_memslots *slots; |
82ed3616 PM |
2797 | struct kvm_memory_slot *memslot; |
2798 | int r; | |
2799 | unsigned long n; | |
2800 | ||
2801 | mutex_lock(&kvm->slots_lock); | |
2802 | ||
2803 | r = -EINVAL; | |
bbacc0c1 | 2804 | if (log->slot >= KVM_USER_MEM_SLOTS) |
82ed3616 PM |
2805 | goto out; |
2806 | ||
9f6b8029 PB |
2807 | slots = kvm_memslots(kvm); |
2808 | memslot = id_to_memslot(slots, log->slot); | |
82ed3616 PM |
2809 | r = -ENOENT; |
2810 | if (!memslot->dirty_bitmap) | |
2811 | goto out; | |
2812 | ||
2813 | n = kvm_dirty_bitmap_bytes(memslot); | |
2814 | memset(memslot->dirty_bitmap, 0, n); | |
2815 | ||
dfe49dbd | 2816 | r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap); |
82ed3616 PM |
2817 | if (r) |
2818 | goto out; | |
2819 | ||
2820 | r = -EFAULT; | |
2821 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) | |
2822 | goto out; | |
2823 | ||
2824 | r = 0; | |
2825 | out: | |
2826 | mutex_unlock(&kvm->slots_lock); | |
2827 | return r; | |
2828 | } | |
2829 | ||
3a167bea AK |
2830 | static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, |
2831 | struct kvm_memory_slot *dont) | |
a66b48c3 PM |
2832 | { |
2833 | if (!dont || free->arch.rmap != dont->arch.rmap) { | |
2834 | vfree(free->arch.rmap); | |
2835 | free->arch.rmap = NULL; | |
b2b2f165 | 2836 | } |
a66b48c3 PM |
2837 | } |
2838 | ||
3a167bea AK |
2839 | static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot, |
2840 | unsigned long npages) | |
a66b48c3 PM |
2841 | { |
2842 | slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap)); | |
2843 | if (!slot->arch.rmap) | |
2844 | return -ENOMEM; | |
aa04b4cc | 2845 | |
c77162de PM |
2846 | return 0; |
2847 | } | |
aa04b4cc | 2848 | |
3a167bea AK |
2849 | static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, |
2850 | struct kvm_memory_slot *memslot, | |
09170a49 | 2851 | const struct kvm_userspace_memory_region *mem) |
c77162de | 2852 | { |
a66b48c3 | 2853 | return 0; |
c77162de PM |
2854 | } |
2855 | ||
3a167bea | 2856 | static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, |
09170a49 | 2857 | const struct kvm_userspace_memory_region *mem, |
f36f3f28 PB |
2858 | const struct kvm_memory_slot *old, |
2859 | const struct kvm_memory_slot *new) | |
c77162de | 2860 | { |
dfe49dbd | 2861 | unsigned long npages = mem->memory_size >> PAGE_SHIFT; |
9f6b8029 | 2862 | struct kvm_memslots *slots; |
dfe49dbd PM |
2863 | struct kvm_memory_slot *memslot; |
2864 | ||
8482644a | 2865 | if (npages && old->npages) { |
dfe49dbd PM |
2866 | /* |
2867 | * If modifying a memslot, reset all the rmap dirty bits. | |
2868 | * If this is a new memslot, we don't need to do anything | |
2869 | * since the rmap array starts out as all zeroes, | |
2870 | * i.e. no pages are dirty. | |
2871 | */ | |
9f6b8029 PB |
2872 | slots = kvm_memslots(kvm); |
2873 | memslot = id_to_memslot(slots, mem->slot); | |
dfe49dbd PM |
2874 | kvmppc_hv_get_dirty_log(kvm, memslot, NULL); |
2875 | } | |
c77162de PM |
2876 | } |
2877 | ||
a0144e2a PM |
2878 | /* |
2879 | * Update LPCR values in kvm->arch and in vcores. | |
2880 | * Caller must hold kvm->lock. | |
2881 | */ | |
2882 | void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask) | |
2883 | { | |
2884 | long int i; | |
2885 | u32 cores_done = 0; | |
2886 | ||
2887 | if ((kvm->arch.lpcr & mask) == lpcr) | |
2888 | return; | |
2889 | ||
2890 | kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr; | |
2891 | ||
2892 | for (i = 0; i < KVM_MAX_VCORES; ++i) { | |
2893 | struct kvmppc_vcore *vc = kvm->arch.vcores[i]; | |
2894 | if (!vc) | |
2895 | continue; | |
2896 | spin_lock(&vc->lock); | |
2897 | vc->lpcr = (vc->lpcr & ~mask) | lpcr; | |
2898 | spin_unlock(&vc->lock); | |
2899 | if (++cores_done >= kvm->arch.online_vcores) | |
2900 | break; | |
2901 | } | |
2902 | } | |
2903 | ||
3a167bea AK |
2904 | static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu) |
2905 | { | |
2906 | return; | |
2907 | } | |
2908 | ||
32fad281 | 2909 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) |
c77162de PM |
2910 | { |
2911 | int err = 0; | |
2912 | struct kvm *kvm = vcpu->kvm; | |
c77162de PM |
2913 | unsigned long hva; |
2914 | struct kvm_memory_slot *memslot; | |
2915 | struct vm_area_struct *vma; | |
a0144e2a | 2916 | unsigned long lpcr = 0, senc; |
c77162de | 2917 | unsigned long psize, porder; |
2c9097e4 | 2918 | int srcu_idx; |
c77162de PM |
2919 | |
2920 | mutex_lock(&kvm->lock); | |
31037eca | 2921 | if (kvm->arch.hpte_setup_done) |
c77162de | 2922 | goto out; /* another vcpu beat us to it */ |
aa04b4cc | 2923 | |
32fad281 PM |
2924 | /* Allocate hashed page table (if not done already) and reset it */ |
2925 | if (!kvm->arch.hpt_virt) { | |
2926 | err = kvmppc_alloc_hpt(kvm, NULL); | |
2927 | if (err) { | |
2928 | pr_err("KVM: Couldn't alloc HPT\n"); | |
2929 | goto out; | |
2930 | } | |
2931 | } | |
2932 | ||
c77162de | 2933 | /* Look up the memslot for guest physical address 0 */ |
2c9097e4 | 2934 | srcu_idx = srcu_read_lock(&kvm->srcu); |
c77162de | 2935 | memslot = gfn_to_memslot(kvm, 0); |
aa04b4cc | 2936 | |
c77162de PM |
2937 | /* We must have some memory at 0 by now */ |
2938 | err = -EINVAL; | |
2939 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
2c9097e4 | 2940 | goto out_srcu; |
c77162de PM |
2941 | |
2942 | /* Look up the VMA for the start of this memory slot */ | |
2943 | hva = memslot->userspace_addr; | |
2944 | down_read(¤t->mm->mmap_sem); | |
2945 | vma = find_vma(current->mm, hva); | |
2946 | if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO)) | |
2947 | goto up_out; | |
2948 | ||
2949 | psize = vma_kernel_pagesize(vma); | |
da9d1d7f | 2950 | porder = __ilog2(psize); |
c77162de | 2951 | |
c77162de PM |
2952 | up_read(¤t->mm->mmap_sem); |
2953 | ||
c17b98cf PM |
2954 | /* We can handle 4k, 64k or 16M pages in the VRMA */ |
2955 | err = -EINVAL; | |
2956 | if (!(psize == 0x1000 || psize == 0x10000 || | |
2957 | psize == 0x1000000)) | |
2958 | goto out_srcu; | |
c77162de | 2959 | |
c17b98cf PM |
2960 | /* Update VRMASD field in the LPCR */ |
2961 | senc = slb_pgsize_encoding(psize); | |
2962 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | | |
2963 | (VRMA_VSID << SLB_VSID_SHIFT_1T); | |
2964 | /* the -4 is to account for senc values starting at 0x10 */ | |
2965 | lpcr = senc << (LPCR_VRMASD_SH - 4); | |
c77162de | 2966 | |
c17b98cf PM |
2967 | /* Create HPTEs in the hash page table for the VRMA */ |
2968 | kvmppc_map_vrma(vcpu, memslot, porder); | |
aa04b4cc | 2969 | |
c17b98cf | 2970 | kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD); |
a0144e2a | 2971 | |
31037eca | 2972 | /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */ |
c77162de | 2973 | smp_wmb(); |
31037eca | 2974 | kvm->arch.hpte_setup_done = 1; |
c77162de | 2975 | err = 0; |
2c9097e4 PM |
2976 | out_srcu: |
2977 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
c77162de PM |
2978 | out: |
2979 | mutex_unlock(&kvm->lock); | |
2980 | return err; | |
b2b2f165 | 2981 | |
c77162de PM |
2982 | up_out: |
2983 | up_read(¤t->mm->mmap_sem); | |
505d6421 | 2984 | goto out_srcu; |
de56a948 PM |
2985 | } |
2986 | ||
3a167bea | 2987 | static int kvmppc_core_init_vm_hv(struct kvm *kvm) |
de56a948 | 2988 | { |
32fad281 | 2989 | unsigned long lpcr, lpid; |
e23a808b | 2990 | char buf[32]; |
de56a948 | 2991 | |
32fad281 PM |
2992 | /* Allocate the guest's logical partition ID */ |
2993 | ||
2994 | lpid = kvmppc_alloc_lpid(); | |
5d226ae5 | 2995 | if ((long)lpid < 0) |
32fad281 PM |
2996 | return -ENOMEM; |
2997 | kvm->arch.lpid = lpid; | |
de56a948 | 2998 | |
1b400ba0 PM |
2999 | /* |
3000 | * Since we don't flush the TLB when tearing down a VM, | |
3001 | * and this lpid might have previously been used, | |
3002 | * make sure we flush on each core before running the new VM. | |
3003 | */ | |
3004 | cpumask_setall(&kvm->arch.need_tlb_flush); | |
3005 | ||
699a0ea0 PM |
3006 | /* Start out with the default set of hcalls enabled */ |
3007 | memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls, | |
3008 | sizeof(kvm->arch.enabled_hcalls)); | |
3009 | ||
9e368f29 | 3010 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); |
aa04b4cc | 3011 | |
c17b98cf PM |
3012 | /* Init LPCR for virtual RMA mode */ |
3013 | kvm->arch.host_lpid = mfspr(SPRN_LPID); | |
3014 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); | |
3015 | lpcr &= LPCR_PECE | LPCR_LPES; | |
3016 | lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | | |
3017 | LPCR_VPM0 | LPCR_VPM1; | |
3018 | kvm->arch.vrma_slb_v = SLB_VSID_B_1T | | |
3019 | (VRMA_VSID << SLB_VSID_SHIFT_1T); | |
3020 | /* On POWER8 turn on online bit to enable PURR/SPURR */ | |
3021 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) | |
3022 | lpcr |= LPCR_ONL; | |
9e368f29 | 3023 | kvm->arch.lpcr = lpcr; |
aa04b4cc | 3024 | |
512691d4 | 3025 | /* |
441c19c8 ME |
3026 | * Track that we now have a HV mode VM active. This blocks secondary |
3027 | * CPU threads from coming online. | |
512691d4 | 3028 | */ |
441c19c8 | 3029 | kvm_hv_vm_activated(); |
512691d4 | 3030 | |
e23a808b PM |
3031 | /* |
3032 | * Create a debugfs directory for the VM | |
3033 | */ | |
3034 | snprintf(buf, sizeof(buf), "vm%d", current->pid); | |
3035 | kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir); | |
3036 | if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir)) | |
3037 | kvmppc_mmu_debugfs_init(kvm); | |
3038 | ||
54738c09 | 3039 | return 0; |
de56a948 PM |
3040 | } |
3041 | ||
f1378b1c PM |
3042 | static void kvmppc_free_vcores(struct kvm *kvm) |
3043 | { | |
3044 | long int i; | |
3045 | ||
23316316 | 3046 | for (i = 0; i < KVM_MAX_VCORES; ++i) |
f1378b1c PM |
3047 | kfree(kvm->arch.vcores[i]); |
3048 | kvm->arch.online_vcores = 0; | |
3049 | } | |
3050 | ||
3a167bea | 3051 | static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) |
de56a948 | 3052 | { |
e23a808b PM |
3053 | debugfs_remove_recursive(kvm->arch.debugfs_dir); |
3054 | ||
441c19c8 | 3055 | kvm_hv_vm_deactivated(); |
512691d4 | 3056 | |
f1378b1c | 3057 | kvmppc_free_vcores(kvm); |
aa04b4cc | 3058 | |
de56a948 PM |
3059 | kvmppc_free_hpt(kvm); |
3060 | } | |
3061 | ||
3a167bea AK |
3062 | /* We don't need to emulate any privileged instructions or dcbz */ |
3063 | static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
3064 | unsigned int inst, int *advance) | |
de56a948 | 3065 | { |
3a167bea | 3066 | return EMULATE_FAIL; |
de56a948 PM |
3067 | } |
3068 | ||
3a167bea AK |
3069 | static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn, |
3070 | ulong spr_val) | |
de56a948 PM |
3071 | { |
3072 | return EMULATE_FAIL; | |
3073 | } | |
3074 | ||
3a167bea AK |
3075 | static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, |
3076 | ulong *spr_val) | |
de56a948 PM |
3077 | { |
3078 | return EMULATE_FAIL; | |
3079 | } | |
3080 | ||
3a167bea | 3081 | static int kvmppc_core_check_processor_compat_hv(void) |
de56a948 | 3082 | { |
c17b98cf PM |
3083 | if (!cpu_has_feature(CPU_FTR_HVMODE) || |
3084 | !cpu_has_feature(CPU_FTR_ARCH_206)) | |
3a167bea AK |
3085 | return -EIO; |
3086 | return 0; | |
de56a948 PM |
3087 | } |
3088 | ||
3a167bea AK |
3089 | static long kvm_arch_vm_ioctl_hv(struct file *filp, |
3090 | unsigned int ioctl, unsigned long arg) | |
3091 | { | |
3092 | struct kvm *kvm __maybe_unused = filp->private_data; | |
3093 | void __user *argp = (void __user *)arg; | |
3094 | long r; | |
3095 | ||
3096 | switch (ioctl) { | |
3097 | ||
3a167bea AK |
3098 | case KVM_PPC_ALLOCATE_HTAB: { |
3099 | u32 htab_order; | |
3100 | ||
3101 | r = -EFAULT; | |
3102 | if (get_user(htab_order, (u32 __user *)argp)) | |
3103 | break; | |
3104 | r = kvmppc_alloc_reset_hpt(kvm, &htab_order); | |
3105 | if (r) | |
3106 | break; | |
3107 | r = -EFAULT; | |
3108 | if (put_user(htab_order, (u32 __user *)argp)) | |
3109 | break; | |
3110 | r = 0; | |
3111 | break; | |
3112 | } | |
3113 | ||
3114 | case KVM_PPC_GET_HTAB_FD: { | |
3115 | struct kvm_get_htab_fd ghf; | |
3116 | ||
3117 | r = -EFAULT; | |
3118 | if (copy_from_user(&ghf, argp, sizeof(ghf))) | |
3119 | break; | |
3120 | r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf); | |
3121 | break; | |
3122 | } | |
3123 | ||
3124 | default: | |
3125 | r = -ENOTTY; | |
3126 | } | |
3127 | ||
3128 | return r; | |
3129 | } | |
3130 | ||
699a0ea0 PM |
3131 | /* |
3132 | * List of hcall numbers to enable by default. | |
3133 | * For compatibility with old userspace, we enable by default | |
3134 | * all hcalls that were implemented before the hcall-enabling | |
3135 | * facility was added. Note this list should not include H_RTAS. | |
3136 | */ | |
3137 | static unsigned int default_hcall_list[] = { | |
3138 | H_REMOVE, | |
3139 | H_ENTER, | |
3140 | H_READ, | |
3141 | H_PROTECT, | |
3142 | H_BULK_REMOVE, | |
3143 | H_GET_TCE, | |
3144 | H_PUT_TCE, | |
3145 | H_SET_DABR, | |
3146 | H_SET_XDABR, | |
3147 | H_CEDE, | |
3148 | H_PROD, | |
3149 | H_CONFER, | |
3150 | H_REGISTER_VPA, | |
3151 | #ifdef CONFIG_KVM_XICS | |
3152 | H_EOI, | |
3153 | H_CPPR, | |
3154 | H_IPI, | |
3155 | H_IPOLL, | |
3156 | H_XIRR, | |
3157 | H_XIRR_X, | |
3158 | #endif | |
3159 | 0 | |
3160 | }; | |
3161 | ||
3162 | static void init_default_hcalls(void) | |
3163 | { | |
3164 | int i; | |
ae2113a4 | 3165 | unsigned int hcall; |
699a0ea0 | 3166 | |
ae2113a4 PM |
3167 | for (i = 0; default_hcall_list[i]; ++i) { |
3168 | hcall = default_hcall_list[i]; | |
3169 | WARN_ON(!kvmppc_hcall_impl_hv(hcall)); | |
3170 | __set_bit(hcall / 4, default_enabled_hcalls); | |
3171 | } | |
699a0ea0 PM |
3172 | } |
3173 | ||
cbbc58d4 | 3174 | static struct kvmppc_ops kvm_ops_hv = { |
3a167bea AK |
3175 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, |
3176 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, | |
3177 | .get_one_reg = kvmppc_get_one_reg_hv, | |
3178 | .set_one_reg = kvmppc_set_one_reg_hv, | |
3179 | .vcpu_load = kvmppc_core_vcpu_load_hv, | |
3180 | .vcpu_put = kvmppc_core_vcpu_put_hv, | |
3181 | .set_msr = kvmppc_set_msr_hv, | |
3182 | .vcpu_run = kvmppc_vcpu_run_hv, | |
3183 | .vcpu_create = kvmppc_core_vcpu_create_hv, | |
3184 | .vcpu_free = kvmppc_core_vcpu_free_hv, | |
3185 | .check_requests = kvmppc_core_check_requests_hv, | |
3186 | .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv, | |
3187 | .flush_memslot = kvmppc_core_flush_memslot_hv, | |
3188 | .prepare_memory_region = kvmppc_core_prepare_memory_region_hv, | |
3189 | .commit_memory_region = kvmppc_core_commit_memory_region_hv, | |
3190 | .unmap_hva = kvm_unmap_hva_hv, | |
3191 | .unmap_hva_range = kvm_unmap_hva_range_hv, | |
3192 | .age_hva = kvm_age_hva_hv, | |
3193 | .test_age_hva = kvm_test_age_hva_hv, | |
3194 | .set_spte_hva = kvm_set_spte_hva_hv, | |
3195 | .mmu_destroy = kvmppc_mmu_destroy_hv, | |
3196 | .free_memslot = kvmppc_core_free_memslot_hv, | |
3197 | .create_memslot = kvmppc_core_create_memslot_hv, | |
3198 | .init_vm = kvmppc_core_init_vm_hv, | |
3199 | .destroy_vm = kvmppc_core_destroy_vm_hv, | |
3a167bea AK |
3200 | .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv, |
3201 | .emulate_op = kvmppc_core_emulate_op_hv, | |
3202 | .emulate_mtspr = kvmppc_core_emulate_mtspr_hv, | |
3203 | .emulate_mfspr = kvmppc_core_emulate_mfspr_hv, | |
3204 | .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv, | |
3205 | .arch_vm_ioctl = kvm_arch_vm_ioctl_hv, | |
ae2113a4 | 3206 | .hcall_implemented = kvmppc_hcall_impl_hv, |
3a167bea AK |
3207 | }; |
3208 | ||
3209 | static int kvmppc_book3s_init_hv(void) | |
de56a948 PM |
3210 | { |
3211 | int r; | |
cbbc58d4 AK |
3212 | /* |
3213 | * FIXME!! Do we need to check on all cpus ? | |
3214 | */ | |
3215 | r = kvmppc_core_check_processor_compat_hv(); | |
3216 | if (r < 0) | |
739e2425 | 3217 | return -ENODEV; |
de56a948 | 3218 | |
cbbc58d4 AK |
3219 | kvm_ops_hv.owner = THIS_MODULE; |
3220 | kvmppc_hv_ops = &kvm_ops_hv; | |
de56a948 | 3221 | |
699a0ea0 PM |
3222 | init_default_hcalls(); |
3223 | ||
ec257165 PM |
3224 | init_vcore_lists(); |
3225 | ||
cbbc58d4 | 3226 | r = kvmppc_mmu_hv_init(); |
de56a948 PM |
3227 | return r; |
3228 | } | |
3229 | ||
3a167bea | 3230 | static void kvmppc_book3s_exit_hv(void) |
de56a948 | 3231 | { |
cbbc58d4 | 3232 | kvmppc_hv_ops = NULL; |
de56a948 PM |
3233 | } |
3234 | ||
3a167bea AK |
3235 | module_init(kvmppc_book3s_init_hv); |
3236 | module_exit(kvmppc_book3s_exit_hv); | |
2ba9f0d8 | 3237 | MODULE_LICENSE("GPL"); |
398a76c6 AG |
3238 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
3239 | MODULE_ALIAS("devname:kvm"); |