]>
Commit | Line | Data |
---|---|---|
de56a948 PM |
1 | /* |
2 | * Copyright 2011 Paul Mackerras, IBM Corp. <[email protected]> | |
3 | * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved. | |
4 | * | |
5 | * Authors: | |
6 | * Paul Mackerras <[email protected]> | |
7 | * Alexander Graf <[email protected]> | |
8 | * Kevin Wolf <[email protected]> | |
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 <[email protected]>. | |
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> | |
174cd4b1 | 25 | #include <linux/sched/signal.h> |
03441a34 | 26 | #include <linux/sched/stat.h> |
de56a948 | 27 | #include <linux/delay.h> |
66b15db6 | 28 | #include <linux/export.h> |
de56a948 PM |
29 | #include <linux/fs.h> |
30 | #include <linux/anon_inodes.h> | |
07f8ab25 | 31 | #include <linux/cpu.h> |
de56a948 | 32 | #include <linux/cpumask.h> |
aa04b4cc PM |
33 | #include <linux/spinlock.h> |
34 | #include <linux/page-flags.h> | |
2c9097e4 | 35 | #include <linux/srcu.h> |
398a76c6 | 36 | #include <linux/miscdevice.h> |
e23a808b | 37 | #include <linux/debugfs.h> |
d3989143 BH |
38 | #include <linux/gfp.h> |
39 | #include <linux/vmalloc.h> | |
40 | #include <linux/highmem.h> | |
41 | #include <linux/hugetlb.h> | |
42 | #include <linux/kvm_irqfd.h> | |
43 | #include <linux/irqbypass.h> | |
44 | #include <linux/module.h> | |
45 | #include <linux/compiler.h> | |
46 | #include <linux/of.h> | |
de56a948 PM |
47 | |
48 | #include <asm/reg.h> | |
49 | #include <asm/cputable.h> | |
50 | #include <asm/cacheflush.h> | |
51 | #include <asm/tlbflush.h> | |
7c0f6ba6 | 52 | #include <linux/uaccess.h> |
de56a948 PM |
53 | #include <asm/io.h> |
54 | #include <asm/kvm_ppc.h> | |
55 | #include <asm/kvm_book3s.h> | |
56 | #include <asm/mmu_context.h> | |
57 | #include <asm/lppaca.h> | |
58 | #include <asm/processor.h> | |
371fefd6 | 59 | #include <asm/cputhreads.h> |
aa04b4cc | 60 | #include <asm/page.h> |
de1d9248 | 61 | #include <asm/hvcall.h> |
ae3a197e | 62 | #include <asm/switch_to.h> |
512691d4 | 63 | #include <asm/smp.h> |
66feed61 | 64 | #include <asm/dbell.h> |
fd7bacbc | 65 | #include <asm/hmi.h> |
c57875f5 | 66 | #include <asm/pnv-pci.h> |
7a84084c | 67 | #include <asm/mmu.h> |
f725758b PM |
68 | #include <asm/opal.h> |
69 | #include <asm/xics.h> | |
5af50993 | 70 | #include <asm/xive.h> |
de56a948 | 71 | |
3a167bea AK |
72 | #include "book3s.h" |
73 | ||
3c78f78a SW |
74 | #define CREATE_TRACE_POINTS |
75 | #include "trace_hv.h" | |
76 | ||
de56a948 PM |
77 | /* #define EXIT_DEBUG */ |
78 | /* #define EXIT_DEBUG_SIMPLE */ | |
79 | /* #define EXIT_DEBUG_INT */ | |
80 | ||
913d3ff9 PM |
81 | /* Used to indicate that a guest page fault needs to be handled */ |
82 | #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1) | |
f7af5209 SW |
83 | /* Used to indicate that a guest passthrough interrupt needs to be handled */ |
84 | #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2) | |
913d3ff9 | 85 | |
c7b67670 PM |
86 | /* Used as a "null" value for timebase values */ |
87 | #define TB_NIL (~(u64)0) | |
88 | ||
699a0ea0 PM |
89 | static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); |
90 | ||
b4deba5c PM |
91 | static int dynamic_mt_modes = 6; |
92 | module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR); | |
93 | MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)"); | |
ec257165 PM |
94 | static int target_smt_mode; |
95 | module_param(target_smt_mode, int, S_IRUGO | S_IWUSR); | |
96 | MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)"); | |
9678cdaa | 97 | |
520fe9c6 SW |
98 | #ifdef CONFIG_KVM_XICS |
99 | static struct kernel_param_ops module_param_ops = { | |
100 | .set = param_set_int, | |
101 | .get = param_get_int, | |
102 | }; | |
103 | ||
644abbb2 SW |
104 | module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, |
105 | S_IRUGO | S_IWUSR); | |
106 | MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization"); | |
107 | ||
520fe9c6 SW |
108 | module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, |
109 | S_IRUGO | S_IWUSR); | |
110 | MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core"); | |
111 | #endif | |
112 | ||
19ccb76a | 113 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu); |
32fad281 | 114 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); |
19ccb76a | 115 | |
7b5f8272 SJS |
116 | static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc, |
117 | int *ip) | |
118 | { | |
119 | int i = *ip; | |
120 | struct kvm_vcpu *vcpu; | |
121 | ||
122 | while (++i < MAX_SMT_THREADS) { | |
123 | vcpu = READ_ONCE(vc->runnable_threads[i]); | |
124 | if (vcpu) { | |
125 | *ip = i; | |
126 | return vcpu; | |
127 | } | |
128 | } | |
129 | return NULL; | |
130 | } | |
131 | ||
132 | /* Used to traverse the list of runnable threads for a given vcore */ | |
133 | #define for_each_runnable_thread(i, vcpu, vc) \ | |
134 | for (i = -1; (vcpu = next_runnable_thread(vc, &i)); ) | |
135 | ||
66feed61 PM |
136 | static bool kvmppc_ipi_thread(int cpu) |
137 | { | |
1704a81c PM |
138 | unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER); |
139 | ||
140 | /* On POWER9 we can use msgsnd to IPI any cpu */ | |
141 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { | |
142 | msg |= get_hard_smp_processor_id(cpu); | |
143 | smp_mb(); | |
144 | __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); | |
145 | return true; | |
146 | } | |
147 | ||
66feed61 PM |
148 | /* On POWER8 for IPIs to threads in the same core, use msgsnd */ |
149 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { | |
150 | preempt_disable(); | |
151 | if (cpu_first_thread_sibling(cpu) == | |
152 | cpu_first_thread_sibling(smp_processor_id())) { | |
66feed61 PM |
153 | msg |= cpu_thread_in_core(cpu); |
154 | smp_mb(); | |
155 | __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); | |
156 | preempt_enable(); | |
157 | return true; | |
158 | } | |
159 | preempt_enable(); | |
160 | } | |
161 | ||
162 | #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) | |
f725758b PM |
163 | if (cpu >= 0 && cpu < nr_cpu_ids) { |
164 | if (paca[cpu].kvm_hstate.xics_phys) { | |
165 | xics_wake_cpu(cpu); | |
166 | return true; | |
167 | } | |
168 | opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY); | |
66feed61 PM |
169 | return true; |
170 | } | |
171 | #endif | |
172 | ||
173 | return false; | |
174 | } | |
175 | ||
3a167bea | 176 | static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) |
54695c30 | 177 | { |
ec257165 | 178 | int cpu; |
8577370f | 179 | struct swait_queue_head *wqp; |
54695c30 BH |
180 | |
181 | wqp = kvm_arch_vcpu_wq(vcpu); | |
8577370f MT |
182 | if (swait_active(wqp)) { |
183 | swake_up(wqp); | |
54695c30 BH |
184 | ++vcpu->stat.halt_wakeup; |
185 | } | |
186 | ||
3deda5e5 PM |
187 | cpu = READ_ONCE(vcpu->arch.thread_cpu); |
188 | if (cpu >= 0 && kvmppc_ipi_thread(cpu)) | |
66feed61 | 189 | return; |
54695c30 BH |
190 | |
191 | /* CPU points to the first thread of the core */ | |
ec257165 | 192 | cpu = vcpu->cpu; |
66feed61 PM |
193 | if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu)) |
194 | smp_send_reschedule(cpu); | |
54695c30 BH |
195 | } |
196 | ||
c7b67670 PM |
197 | /* |
198 | * We use the vcpu_load/put functions to measure stolen time. | |
199 | * Stolen time is counted as time when either the vcpu is able to | |
200 | * run as part of a virtual core, but the task running the vcore | |
201 | * is preempted or sleeping, or when the vcpu needs something done | |
202 | * in the kernel by the task running the vcpu, but that task is | |
203 | * preempted or sleeping. Those two things have to be counted | |
204 | * separately, since one of the vcpu tasks will take on the job | |
205 | * of running the core, and the other vcpu tasks in the vcore will | |
206 | * sleep waiting for it to do that, but that sleep shouldn't count | |
207 | * as stolen time. | |
208 | * | |
209 | * Hence we accumulate stolen time when the vcpu can run as part of | |
210 | * a vcore using vc->stolen_tb, and the stolen time when the vcpu | |
211 | * needs its task to do other things in the kernel (for example, | |
212 | * service a page fault) in busy_stolen. We don't accumulate | |
213 | * stolen time for a vcore when it is inactive, or for a vcpu | |
214 | * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of | |
215 | * a misnomer; it means that the vcpu task is not executing in | |
216 | * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in | |
217 | * the kernel. We don't have any way of dividing up that time | |
218 | * between time that the vcpu is genuinely stopped, time that | |
219 | * the task is actively working on behalf of the vcpu, and time | |
220 | * that the task is preempted, so we don't count any of it as | |
221 | * stolen. | |
222 | * | |
223 | * Updates to busy_stolen are protected by arch.tbacct_lock; | |
2711e248 PM |
224 | * updates to vc->stolen_tb are protected by the vcore->stoltb_lock |
225 | * lock. The stolen times are measured in units of timebase ticks. | |
226 | * (Note that the != TB_NIL checks below are purely defensive; | |
227 | * they should never fail.) | |
c7b67670 PM |
228 | */ |
229 | ||
ec257165 PM |
230 | static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc) |
231 | { | |
232 | unsigned long flags; | |
233 | ||
234 | spin_lock_irqsave(&vc->stoltb_lock, flags); | |
235 | vc->preempt_tb = mftb(); | |
236 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); | |
237 | } | |
238 | ||
239 | static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc) | |
240 | { | |
241 | unsigned long flags; | |
242 | ||
243 | spin_lock_irqsave(&vc->stoltb_lock, flags); | |
244 | if (vc->preempt_tb != TB_NIL) { | |
245 | vc->stolen_tb += mftb() - vc->preempt_tb; | |
246 | vc->preempt_tb = TB_NIL; | |
247 | } | |
248 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); | |
249 | } | |
250 | ||
3a167bea | 251 | static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu) |
de56a948 | 252 | { |
0456ec4f | 253 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
bf3d32e1 | 254 | unsigned long flags; |
0456ec4f | 255 | |
2711e248 PM |
256 | /* |
257 | * We can test vc->runner without taking the vcore lock, | |
258 | * because only this task ever sets vc->runner to this | |
259 | * vcpu, and once it is set to this vcpu, only this task | |
260 | * ever sets it to NULL. | |
261 | */ | |
ec257165 PM |
262 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
263 | kvmppc_core_end_stolen(vc); | |
264 | ||
2711e248 | 265 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
c7b67670 PM |
266 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST && |
267 | vcpu->arch.busy_preempt != TB_NIL) { | |
268 | vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt; | |
269 | vcpu->arch.busy_preempt = TB_NIL; | |
270 | } | |
bf3d32e1 | 271 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
de56a948 PM |
272 | } |
273 | ||
3a167bea | 274 | static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu) |
de56a948 | 275 | { |
0456ec4f | 276 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
bf3d32e1 | 277 | unsigned long flags; |
0456ec4f | 278 | |
ec257165 PM |
279 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
280 | kvmppc_core_start_stolen(vc); | |
281 | ||
2711e248 | 282 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
c7b67670 PM |
283 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) |
284 | vcpu->arch.busy_preempt = mftb(); | |
bf3d32e1 | 285 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
de56a948 PM |
286 | } |
287 | ||
3a167bea | 288 | static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr) |
de56a948 | 289 | { |
c20875a3 PM |
290 | /* |
291 | * Check for illegal transactional state bit combination | |
292 | * and if we find it, force the TS field to a safe state. | |
293 | */ | |
294 | if ((msr & MSR_TS_MASK) == MSR_TS_MASK) | |
295 | msr &= ~MSR_TS_MASK; | |
de56a948 | 296 | vcpu->arch.shregs.msr = msr; |
19ccb76a | 297 | kvmppc_end_cede(vcpu); |
de56a948 PM |
298 | } |
299 | ||
5358a963 | 300 | static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr) |
de56a948 PM |
301 | { |
302 | vcpu->arch.pvr = pvr; | |
303 | } | |
304 | ||
2ee13be3 SJS |
305 | /* Dummy value used in computing PCR value below */ |
306 | #define PCR_ARCH_300 (PCR_ARCH_207 << 1) | |
307 | ||
5358a963 | 308 | static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) |
388cc6e1 | 309 | { |
2ee13be3 | 310 | unsigned long host_pcr_bit = 0, guest_pcr_bit = 0; |
388cc6e1 PM |
311 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
312 | ||
2ee13be3 SJS |
313 | /* We can (emulate) our own architecture version and anything older */ |
314 | if (cpu_has_feature(CPU_FTR_ARCH_300)) | |
315 | host_pcr_bit = PCR_ARCH_300; | |
316 | else if (cpu_has_feature(CPU_FTR_ARCH_207S)) | |
317 | host_pcr_bit = PCR_ARCH_207; | |
318 | else if (cpu_has_feature(CPU_FTR_ARCH_206)) | |
319 | host_pcr_bit = PCR_ARCH_206; | |
320 | else | |
321 | host_pcr_bit = PCR_ARCH_205; | |
322 | ||
323 | /* Determine lowest PCR bit needed to run guest in given PVR level */ | |
324 | guest_pcr_bit = host_pcr_bit; | |
388cc6e1 | 325 | if (arch_compat) { |
388cc6e1 PM |
326 | switch (arch_compat) { |
327 | case PVR_ARCH_205: | |
2ee13be3 | 328 | guest_pcr_bit = PCR_ARCH_205; |
388cc6e1 PM |
329 | break; |
330 | case PVR_ARCH_206: | |
331 | case PVR_ARCH_206p: | |
2ee13be3 | 332 | guest_pcr_bit = PCR_ARCH_206; |
5557ae0e PM |
333 | break; |
334 | case PVR_ARCH_207: | |
2ee13be3 SJS |
335 | guest_pcr_bit = PCR_ARCH_207; |
336 | break; | |
337 | case PVR_ARCH_300: | |
338 | guest_pcr_bit = PCR_ARCH_300; | |
388cc6e1 PM |
339 | break; |
340 | default: | |
341 | return -EINVAL; | |
342 | } | |
343 | } | |
344 | ||
2ee13be3 SJS |
345 | /* Check requested PCR bits don't exceed our capabilities */ |
346 | if (guest_pcr_bit > host_pcr_bit) | |
347 | return -EINVAL; | |
348 | ||
388cc6e1 PM |
349 | spin_lock(&vc->lock); |
350 | vc->arch_compat = arch_compat; | |
2ee13be3 SJS |
351 | /* Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit */ |
352 | vc->pcr = host_pcr_bit - guest_pcr_bit; | |
388cc6e1 PM |
353 | spin_unlock(&vc->lock); |
354 | ||
355 | return 0; | |
356 | } | |
357 | ||
5358a963 | 358 | static void kvmppc_dump_regs(struct kvm_vcpu *vcpu) |
de56a948 PM |
359 | { |
360 | int r; | |
361 | ||
362 | pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); | |
363 | pr_err("pc = %.16lx msr = %.16llx trap = %x\n", | |
364 | vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap); | |
365 | for (r = 0; r < 16; ++r) | |
366 | pr_err("r%2d = %.16lx r%d = %.16lx\n", | |
367 | r, kvmppc_get_gpr(vcpu, r), | |
368 | r+16, kvmppc_get_gpr(vcpu, r+16)); | |
369 | pr_err("ctr = %.16lx lr = %.16lx\n", | |
370 | vcpu->arch.ctr, vcpu->arch.lr); | |
371 | pr_err("srr0 = %.16llx srr1 = %.16llx\n", | |
372 | vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); | |
373 | pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", | |
374 | vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); | |
375 | pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", | |
376 | vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); | |
377 | pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n", | |
378 | vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr); | |
379 | pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); | |
380 | pr_err("fault dar = %.16lx dsisr = %.8x\n", | |
381 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); | |
382 | pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); | |
383 | for (r = 0; r < vcpu->arch.slb_max; ++r) | |
384 | pr_err(" ESID = %.16llx VSID = %.16llx\n", | |
385 | vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); | |
386 | pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n", | |
a0144e2a | 387 | vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1, |
de56a948 PM |
388 | vcpu->arch.last_inst); |
389 | } | |
390 | ||
5358a963 | 391 | static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) |
a8606e20 | 392 | { |
e09fefde | 393 | struct kvm_vcpu *ret; |
a8606e20 PM |
394 | |
395 | mutex_lock(&kvm->lock); | |
e09fefde | 396 | ret = kvm_get_vcpu_by_id(kvm, id); |
a8606e20 PM |
397 | mutex_unlock(&kvm->lock); |
398 | return ret; | |
399 | } | |
400 | ||
401 | static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) | |
402 | { | |
f13c13a0 | 403 | vpa->__old_status |= LPPACA_OLD_SHARED_PROC; |
02407552 | 404 | vpa->yield_count = cpu_to_be32(1); |
a8606e20 PM |
405 | } |
406 | ||
55b665b0 PM |
407 | static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v, |
408 | unsigned long addr, unsigned long len) | |
409 | { | |
410 | /* check address is cacheline aligned */ | |
411 | if (addr & (L1_CACHE_BYTES - 1)) | |
412 | return -EINVAL; | |
413 | spin_lock(&vcpu->arch.vpa_update_lock); | |
414 | if (v->next_gpa != addr || v->len != len) { | |
415 | v->next_gpa = addr; | |
416 | v->len = addr ? len : 0; | |
417 | v->update_pending = 1; | |
418 | } | |
419 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
420 | return 0; | |
421 | } | |
422 | ||
2e25aa5f PM |
423 | /* Length for a per-processor buffer is passed in at offset 4 in the buffer */ |
424 | struct reg_vpa { | |
425 | u32 dummy; | |
426 | union { | |
02407552 AG |
427 | __be16 hword; |
428 | __be32 word; | |
2e25aa5f PM |
429 | } length; |
430 | }; | |
431 | ||
432 | static int vpa_is_registered(struct kvmppc_vpa *vpap) | |
433 | { | |
434 | if (vpap->update_pending) | |
435 | return vpap->next_gpa != 0; | |
436 | return vpap->pinned_addr != NULL; | |
437 | } | |
438 | ||
a8606e20 PM |
439 | static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, |
440 | unsigned long flags, | |
441 | unsigned long vcpuid, unsigned long vpa) | |
442 | { | |
443 | struct kvm *kvm = vcpu->kvm; | |
93e60249 | 444 | unsigned long len, nb; |
a8606e20 PM |
445 | void *va; |
446 | struct kvm_vcpu *tvcpu; | |
2e25aa5f PM |
447 | int err; |
448 | int subfunc; | |
449 | struct kvmppc_vpa *vpap; | |
a8606e20 PM |
450 | |
451 | tvcpu = kvmppc_find_vcpu(kvm, vcpuid); | |
452 | if (!tvcpu) | |
453 | return H_PARAMETER; | |
454 | ||
2e25aa5f PM |
455 | subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK; |
456 | if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL || | |
457 | subfunc == H_VPA_REG_SLB) { | |
458 | /* Registering new area - address must be cache-line aligned */ | |
459 | if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa) | |
a8606e20 | 460 | return H_PARAMETER; |
2e25aa5f PM |
461 | |
462 | /* convert logical addr to kernel addr and read length */ | |
93e60249 PM |
463 | va = kvmppc_pin_guest_page(kvm, vpa, &nb); |
464 | if (va == NULL) | |
b2b2f165 | 465 | return H_PARAMETER; |
2e25aa5f | 466 | if (subfunc == H_VPA_REG_VPA) |
02407552 | 467 | len = be16_to_cpu(((struct reg_vpa *)va)->length.hword); |
a8606e20 | 468 | else |
02407552 | 469 | len = be32_to_cpu(((struct reg_vpa *)va)->length.word); |
c35635ef | 470 | kvmppc_unpin_guest_page(kvm, va, vpa, false); |
2e25aa5f PM |
471 | |
472 | /* Check length */ | |
473 | if (len > nb || len < sizeof(struct reg_vpa)) | |
474 | return H_PARAMETER; | |
475 | } else { | |
476 | vpa = 0; | |
477 | len = 0; | |
478 | } | |
479 | ||
480 | err = H_PARAMETER; | |
481 | vpap = NULL; | |
482 | spin_lock(&tvcpu->arch.vpa_update_lock); | |
483 | ||
484 | switch (subfunc) { | |
485 | case H_VPA_REG_VPA: /* register VPA */ | |
486 | if (len < sizeof(struct lppaca)) | |
a8606e20 | 487 | break; |
2e25aa5f PM |
488 | vpap = &tvcpu->arch.vpa; |
489 | err = 0; | |
490 | break; | |
491 | ||
492 | case H_VPA_REG_DTL: /* register DTL */ | |
493 | if (len < sizeof(struct dtl_entry)) | |
a8606e20 | 494 | break; |
2e25aa5f PM |
495 | len -= len % sizeof(struct dtl_entry); |
496 | ||
497 | /* Check that they have previously registered a VPA */ | |
498 | err = H_RESOURCE; | |
499 | if (!vpa_is_registered(&tvcpu->arch.vpa)) | |
a8606e20 | 500 | break; |
2e25aa5f PM |
501 | |
502 | vpap = &tvcpu->arch.dtl; | |
503 | err = 0; | |
504 | break; | |
505 | ||
506 | case H_VPA_REG_SLB: /* register SLB shadow buffer */ | |
507 | /* Check that they have previously registered a VPA */ | |
508 | err = H_RESOURCE; | |
509 | if (!vpa_is_registered(&tvcpu->arch.vpa)) | |
a8606e20 | 510 | break; |
2e25aa5f PM |
511 | |
512 | vpap = &tvcpu->arch.slb_shadow; | |
513 | err = 0; | |
514 | break; | |
515 | ||
516 | case H_VPA_DEREG_VPA: /* deregister VPA */ | |
517 | /* Check they don't still have a DTL or SLB buf registered */ | |
518 | err = H_RESOURCE; | |
519 | if (vpa_is_registered(&tvcpu->arch.dtl) || | |
520 | vpa_is_registered(&tvcpu->arch.slb_shadow)) | |
a8606e20 | 521 | break; |
2e25aa5f PM |
522 | |
523 | vpap = &tvcpu->arch.vpa; | |
524 | err = 0; | |
525 | break; | |
526 | ||
527 | case H_VPA_DEREG_DTL: /* deregister DTL */ | |
528 | vpap = &tvcpu->arch.dtl; | |
529 | err = 0; | |
530 | break; | |
531 | ||
532 | case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */ | |
533 | vpap = &tvcpu->arch.slb_shadow; | |
534 | err = 0; | |
535 | break; | |
536 | } | |
537 | ||
538 | if (vpap) { | |
539 | vpap->next_gpa = vpa; | |
540 | vpap->len = len; | |
541 | vpap->update_pending = 1; | |
a8606e20 | 542 | } |
93e60249 | 543 | |
2e25aa5f PM |
544 | spin_unlock(&tvcpu->arch.vpa_update_lock); |
545 | ||
93e60249 | 546 | return err; |
a8606e20 PM |
547 | } |
548 | ||
081f323b | 549 | static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap) |
2e25aa5f | 550 | { |
081f323b | 551 | struct kvm *kvm = vcpu->kvm; |
2e25aa5f PM |
552 | void *va; |
553 | unsigned long nb; | |
081f323b | 554 | unsigned long gpa; |
2e25aa5f | 555 | |
081f323b PM |
556 | /* |
557 | * We need to pin the page pointed to by vpap->next_gpa, | |
558 | * but we can't call kvmppc_pin_guest_page under the lock | |
559 | * as it does get_user_pages() and down_read(). So we | |
560 | * have to drop the lock, pin the page, then get the lock | |
561 | * again and check that a new area didn't get registered | |
562 | * in the meantime. | |
563 | */ | |
564 | for (;;) { | |
565 | gpa = vpap->next_gpa; | |
566 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
567 | va = NULL; | |
568 | nb = 0; | |
569 | if (gpa) | |
c35635ef | 570 | va = kvmppc_pin_guest_page(kvm, gpa, &nb); |
081f323b PM |
571 | spin_lock(&vcpu->arch.vpa_update_lock); |
572 | if (gpa == vpap->next_gpa) | |
573 | break; | |
574 | /* sigh... unpin that one and try again */ | |
575 | if (va) | |
c35635ef | 576 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
081f323b PM |
577 | } |
578 | ||
579 | vpap->update_pending = 0; | |
580 | if (va && nb < vpap->len) { | |
581 | /* | |
582 | * If it's now too short, it must be that userspace | |
583 | * has changed the mappings underlying guest memory, | |
584 | * so unregister the region. | |
585 | */ | |
c35635ef | 586 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
081f323b | 587 | va = NULL; |
2e25aa5f PM |
588 | } |
589 | if (vpap->pinned_addr) | |
c35635ef PM |
590 | kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa, |
591 | vpap->dirty); | |
592 | vpap->gpa = gpa; | |
2e25aa5f | 593 | vpap->pinned_addr = va; |
c35635ef | 594 | vpap->dirty = false; |
2e25aa5f PM |
595 | if (va) |
596 | vpap->pinned_end = va + vpap->len; | |
597 | } | |
598 | ||
599 | static void kvmppc_update_vpas(struct kvm_vcpu *vcpu) | |
600 | { | |
2f12f034 PM |
601 | if (!(vcpu->arch.vpa.update_pending || |
602 | vcpu->arch.slb_shadow.update_pending || | |
603 | vcpu->arch.dtl.update_pending)) | |
604 | return; | |
605 | ||
2e25aa5f PM |
606 | spin_lock(&vcpu->arch.vpa_update_lock); |
607 | if (vcpu->arch.vpa.update_pending) { | |
081f323b | 608 | kvmppc_update_vpa(vcpu, &vcpu->arch.vpa); |
55b665b0 PM |
609 | if (vcpu->arch.vpa.pinned_addr) |
610 | init_vpa(vcpu, vcpu->arch.vpa.pinned_addr); | |
2e25aa5f PM |
611 | } |
612 | if (vcpu->arch.dtl.update_pending) { | |
081f323b | 613 | kvmppc_update_vpa(vcpu, &vcpu->arch.dtl); |
2e25aa5f PM |
614 | vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr; |
615 | vcpu->arch.dtl_index = 0; | |
616 | } | |
617 | if (vcpu->arch.slb_shadow.update_pending) | |
081f323b | 618 | kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow); |
2e25aa5f PM |
619 | spin_unlock(&vcpu->arch.vpa_update_lock); |
620 | } | |
621 | ||
c7b67670 PM |
622 | /* |
623 | * Return the accumulated stolen time for the vcore up until `now'. | |
624 | * The caller should hold the vcore lock. | |
625 | */ | |
626 | static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now) | |
627 | { | |
628 | u64 p; | |
2711e248 | 629 | unsigned long flags; |
c7b67670 | 630 | |
2711e248 PM |
631 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
632 | p = vc->stolen_tb; | |
c7b67670 | 633 | if (vc->vcore_state != VCORE_INACTIVE && |
2711e248 PM |
634 | vc->preempt_tb != TB_NIL) |
635 | p += now - vc->preempt_tb; | |
636 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); | |
c7b67670 PM |
637 | return p; |
638 | } | |
639 | ||
0456ec4f PM |
640 | static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, |
641 | struct kvmppc_vcore *vc) | |
642 | { | |
643 | struct dtl_entry *dt; | |
644 | struct lppaca *vpa; | |
c7b67670 PM |
645 | unsigned long stolen; |
646 | unsigned long core_stolen; | |
647 | u64 now; | |
0456ec4f PM |
648 | |
649 | dt = vcpu->arch.dtl_ptr; | |
650 | vpa = vcpu->arch.vpa.pinned_addr; | |
c7b67670 PM |
651 | now = mftb(); |
652 | core_stolen = vcore_stolen_time(vc, now); | |
653 | stolen = core_stolen - vcpu->arch.stolen_logged; | |
654 | vcpu->arch.stolen_logged = core_stolen; | |
bf3d32e1 | 655 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
c7b67670 PM |
656 | stolen += vcpu->arch.busy_stolen; |
657 | vcpu->arch.busy_stolen = 0; | |
bf3d32e1 | 658 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
0456ec4f PM |
659 | if (!dt || !vpa) |
660 | return; | |
661 | memset(dt, 0, sizeof(struct dtl_entry)); | |
662 | dt->dispatch_reason = 7; | |
02407552 AG |
663 | dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid); |
664 | dt->timebase = cpu_to_be64(now + vc->tb_offset); | |
665 | dt->enqueue_to_dispatch_time = cpu_to_be32(stolen); | |
666 | dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu)); | |
667 | dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr); | |
0456ec4f PM |
668 | ++dt; |
669 | if (dt == vcpu->arch.dtl.pinned_end) | |
670 | dt = vcpu->arch.dtl.pinned_addr; | |
671 | vcpu->arch.dtl_ptr = dt; | |
672 | /* order writing *dt vs. writing vpa->dtl_idx */ | |
673 | smp_wmb(); | |
02407552 | 674 | vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index); |
c35635ef | 675 | vcpu->arch.dtl.dirty = true; |
0456ec4f PM |
676 | } |
677 | ||
9642382e MN |
678 | static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu) |
679 | { | |
680 | if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207) | |
681 | return true; | |
682 | if ((!vcpu->arch.vcore->arch_compat) && | |
683 | cpu_has_feature(CPU_FTR_ARCH_207S)) | |
684 | return true; | |
685 | return false; | |
686 | } | |
687 | ||
688 | static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags, | |
689 | unsigned long resource, unsigned long value1, | |
690 | unsigned long value2) | |
691 | { | |
692 | switch (resource) { | |
693 | case H_SET_MODE_RESOURCE_SET_CIABR: | |
694 | if (!kvmppc_power8_compatible(vcpu)) | |
695 | return H_P2; | |
696 | if (value2) | |
697 | return H_P4; | |
698 | if (mflags) | |
699 | return H_UNSUPPORTED_FLAG_START; | |
700 | /* Guests can't breakpoint the hypervisor */ | |
701 | if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER) | |
702 | return H_P3; | |
703 | vcpu->arch.ciabr = value1; | |
704 | return H_SUCCESS; | |
705 | case H_SET_MODE_RESOURCE_SET_DAWR: | |
706 | if (!kvmppc_power8_compatible(vcpu)) | |
707 | return H_P2; | |
708 | if (mflags) | |
709 | return H_UNSUPPORTED_FLAG_START; | |
710 | if (value2 & DABRX_HYP) | |
711 | return H_P4; | |
712 | vcpu->arch.dawr = value1; | |
713 | vcpu->arch.dawrx = value2; | |
714 | return H_SUCCESS; | |
715 | default: | |
716 | return H_TOO_HARD; | |
717 | } | |
718 | } | |
719 | ||
90fd09f8 SB |
720 | static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target) |
721 | { | |
722 | struct kvmppc_vcore *vcore = target->arch.vcore; | |
723 | ||
724 | /* | |
725 | * We expect to have been called by the real mode handler | |
726 | * (kvmppc_rm_h_confer()) which would have directly returned | |
727 | * H_SUCCESS if the source vcore wasn't idle (e.g. if it may | |
728 | * have useful work to do and should not confer) so we don't | |
729 | * recheck that here. | |
730 | */ | |
731 | ||
732 | spin_lock(&vcore->lock); | |
733 | if (target->arch.state == KVMPPC_VCPU_RUNNABLE && | |
ec257165 PM |
734 | vcore->vcore_state != VCORE_INACTIVE && |
735 | vcore->runner) | |
90fd09f8 SB |
736 | target = vcore->runner; |
737 | spin_unlock(&vcore->lock); | |
738 | ||
739 | return kvm_vcpu_yield_to(target); | |
740 | } | |
741 | ||
742 | static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu) | |
743 | { | |
744 | int yield_count = 0; | |
745 | struct lppaca *lppaca; | |
746 | ||
747 | spin_lock(&vcpu->arch.vpa_update_lock); | |
748 | lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr; | |
749 | if (lppaca) | |
ecb6d618 | 750 | yield_count = be32_to_cpu(lppaca->yield_count); |
90fd09f8 SB |
751 | spin_unlock(&vcpu->arch.vpa_update_lock); |
752 | return yield_count; | |
753 | } | |
754 | ||
a8606e20 PM |
755 | int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) |
756 | { | |
757 | unsigned long req = kvmppc_get_gpr(vcpu, 3); | |
758 | unsigned long target, ret = H_SUCCESS; | |
90fd09f8 | 759 | int yield_count; |
a8606e20 | 760 | struct kvm_vcpu *tvcpu; |
8e591cb7 | 761 | int idx, rc; |
a8606e20 | 762 | |
699a0ea0 PM |
763 | if (req <= MAX_HCALL_OPCODE && |
764 | !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls)) | |
765 | return RESUME_HOST; | |
766 | ||
a8606e20 PM |
767 | switch (req) { |
768 | case H_CEDE: | |
a8606e20 PM |
769 | break; |
770 | case H_PROD: | |
771 | target = kvmppc_get_gpr(vcpu, 4); | |
772 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); | |
773 | if (!tvcpu) { | |
774 | ret = H_PARAMETER; | |
775 | break; | |
776 | } | |
777 | tvcpu->arch.prodded = 1; | |
778 | smp_mb(); | |
8464c884 PM |
779 | if (tvcpu->arch.ceded) |
780 | kvmppc_fast_vcpu_kick_hv(tvcpu); | |
a8606e20 PM |
781 | break; |
782 | case H_CONFER: | |
42d7604d PM |
783 | target = kvmppc_get_gpr(vcpu, 4); |
784 | if (target == -1) | |
785 | break; | |
786 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); | |
787 | if (!tvcpu) { | |
788 | ret = H_PARAMETER; | |
789 | break; | |
790 | } | |
90fd09f8 SB |
791 | yield_count = kvmppc_get_gpr(vcpu, 5); |
792 | if (kvmppc_get_yield_count(tvcpu) != yield_count) | |
793 | break; | |
794 | kvm_arch_vcpu_yield_to(tvcpu); | |
a8606e20 PM |
795 | break; |
796 | case H_REGISTER_VPA: | |
797 | ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), | |
798 | kvmppc_get_gpr(vcpu, 5), | |
799 | kvmppc_get_gpr(vcpu, 6)); | |
800 | break; | |
8e591cb7 ME |
801 | case H_RTAS: |
802 | if (list_empty(&vcpu->kvm->arch.rtas_tokens)) | |
803 | return RESUME_HOST; | |
804 | ||
c9438092 | 805 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
8e591cb7 | 806 | rc = kvmppc_rtas_hcall(vcpu); |
c9438092 | 807 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
8e591cb7 ME |
808 | |
809 | if (rc == -ENOENT) | |
810 | return RESUME_HOST; | |
811 | else if (rc == 0) | |
812 | break; | |
813 | ||
814 | /* Send the error out to userspace via KVM_RUN */ | |
815 | return rc; | |
99342cf8 DG |
816 | case H_LOGICAL_CI_LOAD: |
817 | ret = kvmppc_h_logical_ci_load(vcpu); | |
818 | if (ret == H_TOO_HARD) | |
819 | return RESUME_HOST; | |
820 | break; | |
821 | case H_LOGICAL_CI_STORE: | |
822 | ret = kvmppc_h_logical_ci_store(vcpu); | |
823 | if (ret == H_TOO_HARD) | |
824 | return RESUME_HOST; | |
825 | break; | |
9642382e MN |
826 | case H_SET_MODE: |
827 | ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4), | |
828 | kvmppc_get_gpr(vcpu, 5), | |
829 | kvmppc_get_gpr(vcpu, 6), | |
830 | kvmppc_get_gpr(vcpu, 7)); | |
831 | if (ret == H_TOO_HARD) | |
832 | return RESUME_HOST; | |
833 | break; | |
bc5ad3f3 BH |
834 | case H_XIRR: |
835 | case H_CPPR: | |
836 | case H_EOI: | |
837 | case H_IPI: | |
8e44ddc3 PM |
838 | case H_IPOLL: |
839 | case H_XIRR_X: | |
bc5ad3f3 | 840 | if (kvmppc_xics_enabled(vcpu)) { |
5af50993 BH |
841 | if (xive_enabled()) { |
842 | ret = H_NOT_AVAILABLE; | |
843 | return RESUME_GUEST; | |
844 | } | |
bc5ad3f3 BH |
845 | ret = kvmppc_xics_hcall(vcpu, req); |
846 | break; | |
d3695aa4 AK |
847 | } |
848 | return RESUME_HOST; | |
849 | case H_PUT_TCE: | |
850 | ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4), | |
851 | kvmppc_get_gpr(vcpu, 5), | |
852 | kvmppc_get_gpr(vcpu, 6)); | |
853 | if (ret == H_TOO_HARD) | |
854 | return RESUME_HOST; | |
855 | break; | |
856 | case H_PUT_TCE_INDIRECT: | |
857 | ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4), | |
858 | kvmppc_get_gpr(vcpu, 5), | |
859 | kvmppc_get_gpr(vcpu, 6), | |
860 | kvmppc_get_gpr(vcpu, 7)); | |
861 | if (ret == H_TOO_HARD) | |
862 | return RESUME_HOST; | |
863 | break; | |
864 | case H_STUFF_TCE: | |
865 | ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4), | |
866 | kvmppc_get_gpr(vcpu, 5), | |
867 | kvmppc_get_gpr(vcpu, 6), | |
868 | kvmppc_get_gpr(vcpu, 7)); | |
869 | if (ret == H_TOO_HARD) | |
870 | return RESUME_HOST; | |
871 | break; | |
a8606e20 PM |
872 | default: |
873 | return RESUME_HOST; | |
874 | } | |
875 | kvmppc_set_gpr(vcpu, 3, ret); | |
876 | vcpu->arch.hcall_needed = 0; | |
877 | return RESUME_GUEST; | |
878 | } | |
879 | ||
ae2113a4 PM |
880 | static int kvmppc_hcall_impl_hv(unsigned long cmd) |
881 | { | |
882 | switch (cmd) { | |
883 | case H_CEDE: | |
884 | case H_PROD: | |
885 | case H_CONFER: | |
886 | case H_REGISTER_VPA: | |
9642382e | 887 | case H_SET_MODE: |
99342cf8 DG |
888 | case H_LOGICAL_CI_LOAD: |
889 | case H_LOGICAL_CI_STORE: | |
ae2113a4 PM |
890 | #ifdef CONFIG_KVM_XICS |
891 | case H_XIRR: | |
892 | case H_CPPR: | |
893 | case H_EOI: | |
894 | case H_IPI: | |
895 | case H_IPOLL: | |
896 | case H_XIRR_X: | |
897 | #endif | |
898 | return 1; | |
899 | } | |
900 | ||
901 | /* See if it's in the real-mode table */ | |
902 | return kvmppc_hcall_impl_hv_realmode(cmd); | |
903 | } | |
904 | ||
a59c1d9e MS |
905 | static int kvmppc_emulate_debug_inst(struct kvm_run *run, |
906 | struct kvm_vcpu *vcpu) | |
907 | { | |
908 | u32 last_inst; | |
909 | ||
910 | if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) != | |
911 | EMULATE_DONE) { | |
912 | /* | |
913 | * Fetch failed, so return to guest and | |
914 | * try executing it again. | |
915 | */ | |
916 | return RESUME_GUEST; | |
917 | } | |
918 | ||
919 | if (last_inst == KVMPPC_INST_SW_BREAKPOINT) { | |
920 | run->exit_reason = KVM_EXIT_DEBUG; | |
921 | run->debug.arch.address = kvmppc_get_pc(vcpu); | |
922 | return RESUME_HOST; | |
923 | } else { | |
924 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); | |
925 | return RESUME_GUEST; | |
926 | } | |
927 | } | |
928 | ||
3a167bea AK |
929 | static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, |
930 | struct task_struct *tsk) | |
de56a948 PM |
931 | { |
932 | int r = RESUME_HOST; | |
933 | ||
934 | vcpu->stat.sum_exits++; | |
935 | ||
1c9e3d51 PM |
936 | /* |
937 | * This can happen if an interrupt occurs in the last stages | |
938 | * of guest entry or the first stages of guest exit (i.e. after | |
939 | * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV | |
940 | * and before setting it to KVM_GUEST_MODE_HOST_HV). | |
941 | * That can happen due to a bug, or due to a machine check | |
942 | * occurring at just the wrong time. | |
943 | */ | |
944 | if (vcpu->arch.shregs.msr & MSR_HV) { | |
945 | printk(KERN_EMERG "KVM trap in HV mode!\n"); | |
946 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", | |
947 | vcpu->arch.trap, kvmppc_get_pc(vcpu), | |
948 | vcpu->arch.shregs.msr); | |
949 | kvmppc_dump_regs(vcpu); | |
950 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
951 | run->hw.hardware_exit_reason = vcpu->arch.trap; | |
952 | return RESUME_HOST; | |
953 | } | |
de56a948 PM |
954 | run->exit_reason = KVM_EXIT_UNKNOWN; |
955 | run->ready_for_interrupt_injection = 1; | |
956 | switch (vcpu->arch.trap) { | |
957 | /* We're good on these - the host merely wanted to get our attention */ | |
958 | case BOOK3S_INTERRUPT_HV_DECREMENTER: | |
959 | vcpu->stat.dec_exits++; | |
960 | r = RESUME_GUEST; | |
961 | break; | |
962 | case BOOK3S_INTERRUPT_EXTERNAL: | |
5d00f66b | 963 | case BOOK3S_INTERRUPT_H_DOORBELL: |
84f7139c | 964 | case BOOK3S_INTERRUPT_H_VIRT: |
de56a948 PM |
965 | vcpu->stat.ext_intr_exits++; |
966 | r = RESUME_GUEST; | |
967 | break; | |
dee6f24c MS |
968 | /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/ |
969 | case BOOK3S_INTERRUPT_HMI: | |
de56a948 PM |
970 | case BOOK3S_INTERRUPT_PERFMON: |
971 | r = RESUME_GUEST; | |
972 | break; | |
b4072df4 PM |
973 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
974 | /* | |
975 | * Deliver a machine check interrupt to the guest. | |
976 | * We have to do this, even if the host has handled the | |
977 | * machine check, because machine checks use SRR0/1 and | |
978 | * the interrupt might have trashed guest state in them. | |
979 | */ | |
980 | kvmppc_book3s_queue_irqprio(vcpu, | |
981 | BOOK3S_INTERRUPT_MACHINE_CHECK); | |
982 | r = RESUME_GUEST; | |
983 | break; | |
de56a948 PM |
984 | case BOOK3S_INTERRUPT_PROGRAM: |
985 | { | |
986 | ulong flags; | |
987 | /* | |
988 | * Normally program interrupts are delivered directly | |
989 | * to the guest by the hardware, but we can get here | |
990 | * as a result of a hypervisor emulation interrupt | |
991 | * (e40) getting turned into a 700 by BML RTAS. | |
992 | */ | |
993 | flags = vcpu->arch.shregs.msr & 0x1f0000ull; | |
994 | kvmppc_core_queue_program(vcpu, flags); | |
995 | r = RESUME_GUEST; | |
996 | break; | |
997 | } | |
998 | case BOOK3S_INTERRUPT_SYSCALL: | |
999 | { | |
1000 | /* hcall - punt to userspace */ | |
1001 | int i; | |
1002 | ||
27025a60 LPF |
1003 | /* hypercall with MSR_PR has already been handled in rmode, |
1004 | * and never reaches here. | |
1005 | */ | |
1006 | ||
de56a948 PM |
1007 | run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); |
1008 | for (i = 0; i < 9; ++i) | |
1009 | run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); | |
1010 | run->exit_reason = KVM_EXIT_PAPR_HCALL; | |
1011 | vcpu->arch.hcall_needed = 1; | |
1012 | r = RESUME_HOST; | |
1013 | break; | |
1014 | } | |
1015 | /* | |
342d3db7 PM |
1016 | * We get these next two if the guest accesses a page which it thinks |
1017 | * it has mapped but which is not actually present, either because | |
1018 | * it is for an emulated I/O device or because the corresonding | |
1019 | * host page has been paged out. Any other HDSI/HISI interrupts | |
1020 | * have been handled already. | |
de56a948 PM |
1021 | */ |
1022 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: | |
913d3ff9 | 1023 | r = RESUME_PAGE_FAULT; |
de56a948 PM |
1024 | break; |
1025 | case BOOK3S_INTERRUPT_H_INST_STORAGE: | |
913d3ff9 PM |
1026 | vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); |
1027 | vcpu->arch.fault_dsisr = 0; | |
1028 | r = RESUME_PAGE_FAULT; | |
de56a948 PM |
1029 | break; |
1030 | /* | |
1031 | * This occurs if the guest executes an illegal instruction. | |
a59c1d9e MS |
1032 | * If the guest debug is disabled, generate a program interrupt |
1033 | * to the guest. If guest debug is enabled, we need to check | |
1034 | * whether the instruction is a software breakpoint instruction. | |
1035 | * Accordingly return to Guest or Host. | |
de56a948 PM |
1036 | */ |
1037 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: | |
4a157d61 PM |
1038 | if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED) |
1039 | vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ? | |
1040 | swab32(vcpu->arch.emul_inst) : | |
1041 | vcpu->arch.emul_inst; | |
a59c1d9e MS |
1042 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) { |
1043 | r = kvmppc_emulate_debug_inst(run, vcpu); | |
1044 | } else { | |
1045 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); | |
1046 | r = RESUME_GUEST; | |
1047 | } | |
bd3048b8 ME |
1048 | break; |
1049 | /* | |
1050 | * This occurs if the guest (kernel or userspace), does something that | |
1051 | * is prohibited by HFSCR. We just generate a program interrupt to | |
1052 | * the guest. | |
1053 | */ | |
1054 | case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: | |
1055 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); | |
de56a948 PM |
1056 | r = RESUME_GUEST; |
1057 | break; | |
f7af5209 SW |
1058 | case BOOK3S_INTERRUPT_HV_RM_HARD: |
1059 | r = RESUME_PASSTHROUGH; | |
1060 | break; | |
de56a948 PM |
1061 | default: |
1062 | kvmppc_dump_regs(vcpu); | |
1063 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", | |
1064 | vcpu->arch.trap, kvmppc_get_pc(vcpu), | |
1065 | vcpu->arch.shregs.msr); | |
f3271d4c | 1066 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
de56a948 | 1067 | r = RESUME_HOST; |
de56a948 PM |
1068 | break; |
1069 | } | |
1070 | ||
de56a948 PM |
1071 | return r; |
1072 | } | |
1073 | ||
3a167bea AK |
1074 | static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu, |
1075 | struct kvm_sregs *sregs) | |
de56a948 PM |
1076 | { |
1077 | int i; | |
1078 | ||
de56a948 | 1079 | memset(sregs, 0, sizeof(struct kvm_sregs)); |
87916442 | 1080 | sregs->pvr = vcpu->arch.pvr; |
de56a948 PM |
1081 | for (i = 0; i < vcpu->arch.slb_max; i++) { |
1082 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; | |
1083 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; | |
1084 | } | |
1085 | ||
1086 | return 0; | |
1087 | } | |
1088 | ||
3a167bea AK |
1089 | static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu, |
1090 | struct kvm_sregs *sregs) | |
de56a948 PM |
1091 | { |
1092 | int i, j; | |
1093 | ||
9333e6c4 PM |
1094 | /* Only accept the same PVR as the host's, since we can't spoof it */ |
1095 | if (sregs->pvr != vcpu->arch.pvr) | |
1096 | return -EINVAL; | |
de56a948 PM |
1097 | |
1098 | j = 0; | |
1099 | for (i = 0; i < vcpu->arch.slb_nr; i++) { | |
1100 | if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { | |
1101 | vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; | |
1102 | vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; | |
1103 | ++j; | |
1104 | } | |
1105 | } | |
1106 | vcpu->arch.slb_max = j; | |
1107 | ||
1108 | return 0; | |
1109 | } | |
1110 | ||
a0840240 AK |
1111 | static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr, |
1112 | bool preserve_top32) | |
a0144e2a | 1113 | { |
8f902b00 | 1114 | struct kvm *kvm = vcpu->kvm; |
a0144e2a PM |
1115 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
1116 | u64 mask; | |
1117 | ||
8f902b00 | 1118 | mutex_lock(&kvm->lock); |
a0144e2a | 1119 | spin_lock(&vc->lock); |
d682916a AB |
1120 | /* |
1121 | * If ILE (interrupt little-endian) has changed, update the | |
1122 | * MSR_LE bit in the intr_msr for each vcpu in this vcore. | |
1123 | */ | |
1124 | if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) { | |
d682916a AB |
1125 | struct kvm_vcpu *vcpu; |
1126 | int i; | |
1127 | ||
d682916a AB |
1128 | kvm_for_each_vcpu(i, vcpu, kvm) { |
1129 | if (vcpu->arch.vcore != vc) | |
1130 | continue; | |
1131 | if (new_lpcr & LPCR_ILE) | |
1132 | vcpu->arch.intr_msr |= MSR_LE; | |
1133 | else | |
1134 | vcpu->arch.intr_msr &= ~MSR_LE; | |
1135 | } | |
d682916a AB |
1136 | } |
1137 | ||
a0144e2a PM |
1138 | /* |
1139 | * Userspace can only modify DPFD (default prefetch depth), | |
1140 | * ILE (interrupt little-endian) and TC (translation control). | |
8cf4ecc0 | 1141 | * On POWER8 and POWER9 userspace can also modify AIL (alt. interrupt loc.). |
a0144e2a PM |
1142 | */ |
1143 | mask = LPCR_DPFD | LPCR_ILE | LPCR_TC; | |
e0622bd9 PM |
1144 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
1145 | mask |= LPCR_AIL; | |
a0840240 AK |
1146 | |
1147 | /* Broken 32-bit version of LPCR must not clear top bits */ | |
1148 | if (preserve_top32) | |
1149 | mask &= 0xFFFFFFFF; | |
a0144e2a PM |
1150 | vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask); |
1151 | spin_unlock(&vc->lock); | |
8f902b00 | 1152 | mutex_unlock(&kvm->lock); |
a0144e2a PM |
1153 | } |
1154 | ||
3a167bea AK |
1155 | static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
1156 | union kvmppc_one_reg *val) | |
31f3438e | 1157 | { |
a136a8bd PM |
1158 | int r = 0; |
1159 | long int i; | |
31f3438e | 1160 | |
a136a8bd | 1161 | switch (id) { |
a59c1d9e MS |
1162 | case KVM_REG_PPC_DEBUG_INST: |
1163 | *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT); | |
1164 | break; | |
31f3438e | 1165 | case KVM_REG_PPC_HIOR: |
a136a8bd PM |
1166 | *val = get_reg_val(id, 0); |
1167 | break; | |
1168 | case KVM_REG_PPC_DABR: | |
1169 | *val = get_reg_val(id, vcpu->arch.dabr); | |
1170 | break; | |
8563bf52 PM |
1171 | case KVM_REG_PPC_DABRX: |
1172 | *val = get_reg_val(id, vcpu->arch.dabrx); | |
1173 | break; | |
a136a8bd PM |
1174 | case KVM_REG_PPC_DSCR: |
1175 | *val = get_reg_val(id, vcpu->arch.dscr); | |
1176 | break; | |
1177 | case KVM_REG_PPC_PURR: | |
1178 | *val = get_reg_val(id, vcpu->arch.purr); | |
1179 | break; | |
1180 | case KVM_REG_PPC_SPURR: | |
1181 | *val = get_reg_val(id, vcpu->arch.spurr); | |
1182 | break; | |
1183 | case KVM_REG_PPC_AMR: | |
1184 | *val = get_reg_val(id, vcpu->arch.amr); | |
1185 | break; | |
1186 | case KVM_REG_PPC_UAMOR: | |
1187 | *val = get_reg_val(id, vcpu->arch.uamor); | |
1188 | break; | |
b005255e | 1189 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
a136a8bd PM |
1190 | i = id - KVM_REG_PPC_MMCR0; |
1191 | *val = get_reg_val(id, vcpu->arch.mmcr[i]); | |
1192 | break; | |
1193 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: | |
1194 | i = id - KVM_REG_PPC_PMC1; | |
1195 | *val = get_reg_val(id, vcpu->arch.pmc[i]); | |
31f3438e | 1196 | break; |
b005255e MN |
1197 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
1198 | i = id - KVM_REG_PPC_SPMC1; | |
1199 | *val = get_reg_val(id, vcpu->arch.spmc[i]); | |
1200 | break; | |
14941789 PM |
1201 | case KVM_REG_PPC_SIAR: |
1202 | *val = get_reg_val(id, vcpu->arch.siar); | |
1203 | break; | |
1204 | case KVM_REG_PPC_SDAR: | |
1205 | *val = get_reg_val(id, vcpu->arch.sdar); | |
1206 | break; | |
b005255e MN |
1207 | case KVM_REG_PPC_SIER: |
1208 | *val = get_reg_val(id, vcpu->arch.sier); | |
a8bd19ef | 1209 | break; |
b005255e MN |
1210 | case KVM_REG_PPC_IAMR: |
1211 | *val = get_reg_val(id, vcpu->arch.iamr); | |
1212 | break; | |
b005255e MN |
1213 | case KVM_REG_PPC_PSPB: |
1214 | *val = get_reg_val(id, vcpu->arch.pspb); | |
1215 | break; | |
b005255e MN |
1216 | case KVM_REG_PPC_DPDES: |
1217 | *val = get_reg_val(id, vcpu->arch.vcore->dpdes); | |
1218 | break; | |
88b02cf9 PM |
1219 | case KVM_REG_PPC_VTB: |
1220 | *val = get_reg_val(id, vcpu->arch.vcore->vtb); | |
1221 | break; | |
b005255e MN |
1222 | case KVM_REG_PPC_DAWR: |
1223 | *val = get_reg_val(id, vcpu->arch.dawr); | |
1224 | break; | |
1225 | case KVM_REG_PPC_DAWRX: | |
1226 | *val = get_reg_val(id, vcpu->arch.dawrx); | |
1227 | break; | |
1228 | case KVM_REG_PPC_CIABR: | |
1229 | *val = get_reg_val(id, vcpu->arch.ciabr); | |
1230 | break; | |
b005255e MN |
1231 | case KVM_REG_PPC_CSIGR: |
1232 | *val = get_reg_val(id, vcpu->arch.csigr); | |
1233 | break; | |
1234 | case KVM_REG_PPC_TACR: | |
1235 | *val = get_reg_val(id, vcpu->arch.tacr); | |
1236 | break; | |
1237 | case KVM_REG_PPC_TCSCR: | |
1238 | *val = get_reg_val(id, vcpu->arch.tcscr); | |
1239 | break; | |
1240 | case KVM_REG_PPC_PID: | |
1241 | *val = get_reg_val(id, vcpu->arch.pid); | |
1242 | break; | |
1243 | case KVM_REG_PPC_ACOP: | |
1244 | *val = get_reg_val(id, vcpu->arch.acop); | |
1245 | break; | |
1246 | case KVM_REG_PPC_WORT: | |
1247 | *val = get_reg_val(id, vcpu->arch.wort); | |
a8bd19ef | 1248 | break; |
e9cf1e08 PM |
1249 | case KVM_REG_PPC_TIDR: |
1250 | *val = get_reg_val(id, vcpu->arch.tid); | |
1251 | break; | |
1252 | case KVM_REG_PPC_PSSCR: | |
1253 | *val = get_reg_val(id, vcpu->arch.psscr); | |
1254 | break; | |
55b665b0 PM |
1255 | case KVM_REG_PPC_VPA_ADDR: |
1256 | spin_lock(&vcpu->arch.vpa_update_lock); | |
1257 | *val = get_reg_val(id, vcpu->arch.vpa.next_gpa); | |
1258 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
1259 | break; | |
1260 | case KVM_REG_PPC_VPA_SLB: | |
1261 | spin_lock(&vcpu->arch.vpa_update_lock); | |
1262 | val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa; | |
1263 | val->vpaval.length = vcpu->arch.slb_shadow.len; | |
1264 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
1265 | break; | |
1266 | case KVM_REG_PPC_VPA_DTL: | |
1267 | spin_lock(&vcpu->arch.vpa_update_lock); | |
1268 | val->vpaval.addr = vcpu->arch.dtl.next_gpa; | |
1269 | val->vpaval.length = vcpu->arch.dtl.len; | |
1270 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
1271 | break; | |
93b0f4dc PM |
1272 | case KVM_REG_PPC_TB_OFFSET: |
1273 | *val = get_reg_val(id, vcpu->arch.vcore->tb_offset); | |
1274 | break; | |
a0144e2a | 1275 | case KVM_REG_PPC_LPCR: |
a0840240 | 1276 | case KVM_REG_PPC_LPCR_64: |
a0144e2a PM |
1277 | *val = get_reg_val(id, vcpu->arch.vcore->lpcr); |
1278 | break; | |
4b8473c9 PM |
1279 | case KVM_REG_PPC_PPR: |
1280 | *val = get_reg_val(id, vcpu->arch.ppr); | |
1281 | break; | |
a7d80d01 MN |
1282 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
1283 | case KVM_REG_PPC_TFHAR: | |
1284 | *val = get_reg_val(id, vcpu->arch.tfhar); | |
1285 | break; | |
1286 | case KVM_REG_PPC_TFIAR: | |
1287 | *val = get_reg_val(id, vcpu->arch.tfiar); | |
1288 | break; | |
1289 | case KVM_REG_PPC_TEXASR: | |
1290 | *val = get_reg_val(id, vcpu->arch.texasr); | |
1291 | break; | |
1292 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: | |
1293 | i = id - KVM_REG_PPC_TM_GPR0; | |
1294 | *val = get_reg_val(id, vcpu->arch.gpr_tm[i]); | |
1295 | break; | |
1296 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: | |
1297 | { | |
1298 | int j; | |
1299 | i = id - KVM_REG_PPC_TM_VSR0; | |
1300 | if (i < 32) | |
1301 | for (j = 0; j < TS_FPRWIDTH; j++) | |
1302 | val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j]; | |
1303 | else { | |
1304 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1305 | val->vval = vcpu->arch.vr_tm.vr[i-32]; | |
1306 | else | |
1307 | r = -ENXIO; | |
1308 | } | |
1309 | break; | |
1310 | } | |
1311 | case KVM_REG_PPC_TM_CR: | |
1312 | *val = get_reg_val(id, vcpu->arch.cr_tm); | |
1313 | break; | |
0d808df0 PM |
1314 | case KVM_REG_PPC_TM_XER: |
1315 | *val = get_reg_val(id, vcpu->arch.xer_tm); | |
1316 | break; | |
a7d80d01 MN |
1317 | case KVM_REG_PPC_TM_LR: |
1318 | *val = get_reg_val(id, vcpu->arch.lr_tm); | |
1319 | break; | |
1320 | case KVM_REG_PPC_TM_CTR: | |
1321 | *val = get_reg_val(id, vcpu->arch.ctr_tm); | |
1322 | break; | |
1323 | case KVM_REG_PPC_TM_FPSCR: | |
1324 | *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr); | |
1325 | break; | |
1326 | case KVM_REG_PPC_TM_AMR: | |
1327 | *val = get_reg_val(id, vcpu->arch.amr_tm); | |
1328 | break; | |
1329 | case KVM_REG_PPC_TM_PPR: | |
1330 | *val = get_reg_val(id, vcpu->arch.ppr_tm); | |
1331 | break; | |
1332 | case KVM_REG_PPC_TM_VRSAVE: | |
1333 | *val = get_reg_val(id, vcpu->arch.vrsave_tm); | |
1334 | break; | |
1335 | case KVM_REG_PPC_TM_VSCR: | |
1336 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1337 | *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]); | |
1338 | else | |
1339 | r = -ENXIO; | |
1340 | break; | |
1341 | case KVM_REG_PPC_TM_DSCR: | |
1342 | *val = get_reg_val(id, vcpu->arch.dscr_tm); | |
1343 | break; | |
1344 | case KVM_REG_PPC_TM_TAR: | |
1345 | *val = get_reg_val(id, vcpu->arch.tar_tm); | |
1346 | break; | |
1347 | #endif | |
388cc6e1 PM |
1348 | case KVM_REG_PPC_ARCH_COMPAT: |
1349 | *val = get_reg_val(id, vcpu->arch.vcore->arch_compat); | |
1350 | break; | |
31f3438e | 1351 | default: |
a136a8bd | 1352 | r = -EINVAL; |
31f3438e PM |
1353 | break; |
1354 | } | |
1355 | ||
1356 | return r; | |
1357 | } | |
1358 | ||
3a167bea AK |
1359 | static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
1360 | union kvmppc_one_reg *val) | |
31f3438e | 1361 | { |
a136a8bd PM |
1362 | int r = 0; |
1363 | long int i; | |
55b665b0 | 1364 | unsigned long addr, len; |
31f3438e | 1365 | |
a136a8bd | 1366 | switch (id) { |
31f3438e | 1367 | case KVM_REG_PPC_HIOR: |
31f3438e | 1368 | /* Only allow this to be set to zero */ |
a136a8bd | 1369 | if (set_reg_val(id, *val)) |
31f3438e PM |
1370 | r = -EINVAL; |
1371 | break; | |
a136a8bd PM |
1372 | case KVM_REG_PPC_DABR: |
1373 | vcpu->arch.dabr = set_reg_val(id, *val); | |
1374 | break; | |
8563bf52 PM |
1375 | case KVM_REG_PPC_DABRX: |
1376 | vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP; | |
1377 | break; | |
a136a8bd PM |
1378 | case KVM_REG_PPC_DSCR: |
1379 | vcpu->arch.dscr = set_reg_val(id, *val); | |
1380 | break; | |
1381 | case KVM_REG_PPC_PURR: | |
1382 | vcpu->arch.purr = set_reg_val(id, *val); | |
1383 | break; | |
1384 | case KVM_REG_PPC_SPURR: | |
1385 | vcpu->arch.spurr = set_reg_val(id, *val); | |
1386 | break; | |
1387 | case KVM_REG_PPC_AMR: | |
1388 | vcpu->arch.amr = set_reg_val(id, *val); | |
1389 | break; | |
1390 | case KVM_REG_PPC_UAMOR: | |
1391 | vcpu->arch.uamor = set_reg_val(id, *val); | |
1392 | break; | |
b005255e | 1393 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
a136a8bd PM |
1394 | i = id - KVM_REG_PPC_MMCR0; |
1395 | vcpu->arch.mmcr[i] = set_reg_val(id, *val); | |
1396 | break; | |
1397 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: | |
1398 | i = id - KVM_REG_PPC_PMC1; | |
1399 | vcpu->arch.pmc[i] = set_reg_val(id, *val); | |
1400 | break; | |
b005255e MN |
1401 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
1402 | i = id - KVM_REG_PPC_SPMC1; | |
1403 | vcpu->arch.spmc[i] = set_reg_val(id, *val); | |
1404 | break; | |
14941789 PM |
1405 | case KVM_REG_PPC_SIAR: |
1406 | vcpu->arch.siar = set_reg_val(id, *val); | |
1407 | break; | |
1408 | case KVM_REG_PPC_SDAR: | |
1409 | vcpu->arch.sdar = set_reg_val(id, *val); | |
1410 | break; | |
b005255e MN |
1411 | case KVM_REG_PPC_SIER: |
1412 | vcpu->arch.sier = set_reg_val(id, *val); | |
a8bd19ef | 1413 | break; |
b005255e MN |
1414 | case KVM_REG_PPC_IAMR: |
1415 | vcpu->arch.iamr = set_reg_val(id, *val); | |
1416 | break; | |
b005255e MN |
1417 | case KVM_REG_PPC_PSPB: |
1418 | vcpu->arch.pspb = set_reg_val(id, *val); | |
1419 | break; | |
b005255e MN |
1420 | case KVM_REG_PPC_DPDES: |
1421 | vcpu->arch.vcore->dpdes = set_reg_val(id, *val); | |
1422 | break; | |
88b02cf9 PM |
1423 | case KVM_REG_PPC_VTB: |
1424 | vcpu->arch.vcore->vtb = set_reg_val(id, *val); | |
1425 | break; | |
b005255e MN |
1426 | case KVM_REG_PPC_DAWR: |
1427 | vcpu->arch.dawr = set_reg_val(id, *val); | |
1428 | break; | |
1429 | case KVM_REG_PPC_DAWRX: | |
1430 | vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP; | |
1431 | break; | |
1432 | case KVM_REG_PPC_CIABR: | |
1433 | vcpu->arch.ciabr = set_reg_val(id, *val); | |
1434 | /* Don't allow setting breakpoints in hypervisor code */ | |
1435 | if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER) | |
1436 | vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */ | |
1437 | break; | |
b005255e MN |
1438 | case KVM_REG_PPC_CSIGR: |
1439 | vcpu->arch.csigr = set_reg_val(id, *val); | |
1440 | break; | |
1441 | case KVM_REG_PPC_TACR: | |
1442 | vcpu->arch.tacr = set_reg_val(id, *val); | |
1443 | break; | |
1444 | case KVM_REG_PPC_TCSCR: | |
1445 | vcpu->arch.tcscr = set_reg_val(id, *val); | |
1446 | break; | |
1447 | case KVM_REG_PPC_PID: | |
1448 | vcpu->arch.pid = set_reg_val(id, *val); | |
1449 | break; | |
1450 | case KVM_REG_PPC_ACOP: | |
1451 | vcpu->arch.acop = set_reg_val(id, *val); | |
1452 | break; | |
1453 | case KVM_REG_PPC_WORT: | |
1454 | vcpu->arch.wort = set_reg_val(id, *val); | |
a8bd19ef | 1455 | break; |
e9cf1e08 PM |
1456 | case KVM_REG_PPC_TIDR: |
1457 | vcpu->arch.tid = set_reg_val(id, *val); | |
1458 | break; | |
1459 | case KVM_REG_PPC_PSSCR: | |
1460 | vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS; | |
1461 | break; | |
55b665b0 PM |
1462 | case KVM_REG_PPC_VPA_ADDR: |
1463 | addr = set_reg_val(id, *val); | |
1464 | r = -EINVAL; | |
1465 | if (!addr && (vcpu->arch.slb_shadow.next_gpa || | |
1466 | vcpu->arch.dtl.next_gpa)) | |
1467 | break; | |
1468 | r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca)); | |
1469 | break; | |
1470 | case KVM_REG_PPC_VPA_SLB: | |
1471 | addr = val->vpaval.addr; | |
1472 | len = val->vpaval.length; | |
1473 | r = -EINVAL; | |
1474 | if (addr && !vcpu->arch.vpa.next_gpa) | |
1475 | break; | |
1476 | r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len); | |
1477 | break; | |
1478 | case KVM_REG_PPC_VPA_DTL: | |
1479 | addr = val->vpaval.addr; | |
1480 | len = val->vpaval.length; | |
1481 | r = -EINVAL; | |
9f8c8c78 PM |
1482 | if (addr && (len < sizeof(struct dtl_entry) || |
1483 | !vcpu->arch.vpa.next_gpa)) | |
55b665b0 PM |
1484 | break; |
1485 | len -= len % sizeof(struct dtl_entry); | |
1486 | r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len); | |
1487 | break; | |
93b0f4dc PM |
1488 | case KVM_REG_PPC_TB_OFFSET: |
1489 | /* round up to multiple of 2^24 */ | |
1490 | vcpu->arch.vcore->tb_offset = | |
1491 | ALIGN(set_reg_val(id, *val), 1UL << 24); | |
1492 | break; | |
a0144e2a | 1493 | case KVM_REG_PPC_LPCR: |
a0840240 AK |
1494 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true); |
1495 | break; | |
1496 | case KVM_REG_PPC_LPCR_64: | |
1497 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false); | |
a0144e2a | 1498 | break; |
4b8473c9 PM |
1499 | case KVM_REG_PPC_PPR: |
1500 | vcpu->arch.ppr = set_reg_val(id, *val); | |
1501 | break; | |
a7d80d01 MN |
1502 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
1503 | case KVM_REG_PPC_TFHAR: | |
1504 | vcpu->arch.tfhar = set_reg_val(id, *val); | |
1505 | break; | |
1506 | case KVM_REG_PPC_TFIAR: | |
1507 | vcpu->arch.tfiar = set_reg_val(id, *val); | |
1508 | break; | |
1509 | case KVM_REG_PPC_TEXASR: | |
1510 | vcpu->arch.texasr = set_reg_val(id, *val); | |
1511 | break; | |
1512 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: | |
1513 | i = id - KVM_REG_PPC_TM_GPR0; | |
1514 | vcpu->arch.gpr_tm[i] = set_reg_val(id, *val); | |
1515 | break; | |
1516 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: | |
1517 | { | |
1518 | int j; | |
1519 | i = id - KVM_REG_PPC_TM_VSR0; | |
1520 | if (i < 32) | |
1521 | for (j = 0; j < TS_FPRWIDTH; j++) | |
1522 | vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j]; | |
1523 | else | |
1524 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1525 | vcpu->arch.vr_tm.vr[i-32] = val->vval; | |
1526 | else | |
1527 | r = -ENXIO; | |
1528 | break; | |
1529 | } | |
1530 | case KVM_REG_PPC_TM_CR: | |
1531 | vcpu->arch.cr_tm = set_reg_val(id, *val); | |
1532 | break; | |
0d808df0 PM |
1533 | case KVM_REG_PPC_TM_XER: |
1534 | vcpu->arch.xer_tm = set_reg_val(id, *val); | |
1535 | break; | |
a7d80d01 MN |
1536 | case KVM_REG_PPC_TM_LR: |
1537 | vcpu->arch.lr_tm = set_reg_val(id, *val); | |
1538 | break; | |
1539 | case KVM_REG_PPC_TM_CTR: | |
1540 | vcpu->arch.ctr_tm = set_reg_val(id, *val); | |
1541 | break; | |
1542 | case KVM_REG_PPC_TM_FPSCR: | |
1543 | vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val); | |
1544 | break; | |
1545 | case KVM_REG_PPC_TM_AMR: | |
1546 | vcpu->arch.amr_tm = set_reg_val(id, *val); | |
1547 | break; | |
1548 | case KVM_REG_PPC_TM_PPR: | |
1549 | vcpu->arch.ppr_tm = set_reg_val(id, *val); | |
1550 | break; | |
1551 | case KVM_REG_PPC_TM_VRSAVE: | |
1552 | vcpu->arch.vrsave_tm = set_reg_val(id, *val); | |
1553 | break; | |
1554 | case KVM_REG_PPC_TM_VSCR: | |
1555 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) | |
1556 | vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val); | |
1557 | else | |
1558 | r = - ENXIO; | |
1559 | break; | |
1560 | case KVM_REG_PPC_TM_DSCR: | |
1561 | vcpu->arch.dscr_tm = set_reg_val(id, *val); | |
1562 | break; | |
1563 | case KVM_REG_PPC_TM_TAR: | |
1564 | vcpu->arch.tar_tm = set_reg_val(id, *val); | |
1565 | break; | |
1566 | #endif | |
388cc6e1 PM |
1567 | case KVM_REG_PPC_ARCH_COMPAT: |
1568 | r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val)); | |
1569 | break; | |
31f3438e | 1570 | default: |
a136a8bd | 1571 | r = -EINVAL; |
31f3438e PM |
1572 | break; |
1573 | } | |
1574 | ||
1575 | return r; | |
1576 | } | |
1577 | ||
45c940ba PM |
1578 | /* |
1579 | * On POWER9, threads are independent and can be in different partitions. | |
1580 | * Therefore we consider each thread to be a subcore. | |
1581 | * There is a restriction that all threads have to be in the same | |
1582 | * MMU mode (radix or HPT), unfortunately, but since we only support | |
1583 | * HPT guests on a HPT host so far, that isn't an impediment yet. | |
1584 | */ | |
1585 | static int threads_per_vcore(void) | |
1586 | { | |
1587 | if (cpu_has_feature(CPU_FTR_ARCH_300)) | |
1588 | return 1; | |
1589 | return threads_per_subcore; | |
1590 | } | |
1591 | ||
de9bdd1a SS |
1592 | static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core) |
1593 | { | |
1594 | struct kvmppc_vcore *vcore; | |
1595 | ||
1596 | vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); | |
1597 | ||
1598 | if (vcore == NULL) | |
1599 | return NULL; | |
1600 | ||
de9bdd1a | 1601 | spin_lock_init(&vcore->lock); |
2711e248 | 1602 | spin_lock_init(&vcore->stoltb_lock); |
8577370f | 1603 | init_swait_queue_head(&vcore->wq); |
de9bdd1a SS |
1604 | vcore->preempt_tb = TB_NIL; |
1605 | vcore->lpcr = kvm->arch.lpcr; | |
45c940ba | 1606 | vcore->first_vcpuid = core * threads_per_vcore(); |
de9bdd1a | 1607 | vcore->kvm = kvm; |
ec257165 | 1608 | INIT_LIST_HEAD(&vcore->preempt_list); |
de9bdd1a SS |
1609 | |
1610 | return vcore; | |
1611 | } | |
1612 | ||
b6c295df PM |
1613 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
1614 | static struct debugfs_timings_element { | |
1615 | const char *name; | |
1616 | size_t offset; | |
1617 | } timings[] = { | |
1618 | {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)}, | |
1619 | {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)}, | |
1620 | {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)}, | |
1621 | {"guest", offsetof(struct kvm_vcpu, arch.guest_time)}, | |
1622 | {"cede", offsetof(struct kvm_vcpu, arch.cede_time)}, | |
1623 | }; | |
1624 | ||
1625 | #define N_TIMINGS (sizeof(timings) / sizeof(timings[0])) | |
1626 | ||
1627 | struct debugfs_timings_state { | |
1628 | struct kvm_vcpu *vcpu; | |
1629 | unsigned int buflen; | |
1630 | char buf[N_TIMINGS * 100]; | |
1631 | }; | |
1632 | ||
1633 | static int debugfs_timings_open(struct inode *inode, struct file *file) | |
1634 | { | |
1635 | struct kvm_vcpu *vcpu = inode->i_private; | |
1636 | struct debugfs_timings_state *p; | |
1637 | ||
1638 | p = kzalloc(sizeof(*p), GFP_KERNEL); | |
1639 | if (!p) | |
1640 | return -ENOMEM; | |
1641 | ||
1642 | kvm_get_kvm(vcpu->kvm); | |
1643 | p->vcpu = vcpu; | |
1644 | file->private_data = p; | |
1645 | ||
1646 | return nonseekable_open(inode, file); | |
1647 | } | |
1648 | ||
1649 | static int debugfs_timings_release(struct inode *inode, struct file *file) | |
1650 | { | |
1651 | struct debugfs_timings_state *p = file->private_data; | |
1652 | ||
1653 | kvm_put_kvm(p->vcpu->kvm); | |
1654 | kfree(p); | |
1655 | return 0; | |
1656 | } | |
1657 | ||
1658 | static ssize_t debugfs_timings_read(struct file *file, char __user *buf, | |
1659 | size_t len, loff_t *ppos) | |
1660 | { | |
1661 | struct debugfs_timings_state *p = file->private_data; | |
1662 | struct kvm_vcpu *vcpu = p->vcpu; | |
1663 | char *s, *buf_end; | |
1664 | struct kvmhv_tb_accumulator tb; | |
1665 | u64 count; | |
1666 | loff_t pos; | |
1667 | ssize_t n; | |
1668 | int i, loops; | |
1669 | bool ok; | |
1670 | ||
1671 | if (!p->buflen) { | |
1672 | s = p->buf; | |
1673 | buf_end = s + sizeof(p->buf); | |
1674 | for (i = 0; i < N_TIMINGS; ++i) { | |
1675 | struct kvmhv_tb_accumulator *acc; | |
1676 | ||
1677 | acc = (struct kvmhv_tb_accumulator *) | |
1678 | ((unsigned long)vcpu + timings[i].offset); | |
1679 | ok = false; | |
1680 | for (loops = 0; loops < 1000; ++loops) { | |
1681 | count = acc->seqcount; | |
1682 | if (!(count & 1)) { | |
1683 | smp_rmb(); | |
1684 | tb = *acc; | |
1685 | smp_rmb(); | |
1686 | if (count == acc->seqcount) { | |
1687 | ok = true; | |
1688 | break; | |
1689 | } | |
1690 | } | |
1691 | udelay(1); | |
1692 | } | |
1693 | if (!ok) | |
1694 | snprintf(s, buf_end - s, "%s: stuck\n", | |
1695 | timings[i].name); | |
1696 | else | |
1697 | snprintf(s, buf_end - s, | |
1698 | "%s: %llu %llu %llu %llu\n", | |
1699 | timings[i].name, count / 2, | |
1700 | tb_to_ns(tb.tb_total), | |
1701 | tb_to_ns(tb.tb_min), | |
1702 | tb_to_ns(tb.tb_max)); | |
1703 | s += strlen(s); | |
1704 | } | |
1705 | p->buflen = s - p->buf; | |
1706 | } | |
1707 | ||
1708 | pos = *ppos; | |
1709 | if (pos >= p->buflen) | |
1710 | return 0; | |
1711 | if (len > p->buflen - pos) | |
1712 | len = p->buflen - pos; | |
1713 | n = copy_to_user(buf, p->buf + pos, len); | |
1714 | if (n) { | |
1715 | if (n == len) | |
1716 | return -EFAULT; | |
1717 | len -= n; | |
1718 | } | |
1719 | *ppos = pos + len; | |
1720 | return len; | |
1721 | } | |
1722 | ||
1723 | static ssize_t debugfs_timings_write(struct file *file, const char __user *buf, | |
1724 | size_t len, loff_t *ppos) | |
1725 | { | |
1726 | return -EACCES; | |
1727 | } | |
1728 | ||
1729 | static const struct file_operations debugfs_timings_ops = { | |
1730 | .owner = THIS_MODULE, | |
1731 | .open = debugfs_timings_open, | |
1732 | .release = debugfs_timings_release, | |
1733 | .read = debugfs_timings_read, | |
1734 | .write = debugfs_timings_write, | |
1735 | .llseek = generic_file_llseek, | |
1736 | }; | |
1737 | ||
1738 | /* Create a debugfs directory for the vcpu */ | |
1739 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) | |
1740 | { | |
1741 | char buf[16]; | |
1742 | struct kvm *kvm = vcpu->kvm; | |
1743 | ||
1744 | snprintf(buf, sizeof(buf), "vcpu%u", id); | |
1745 | if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir)) | |
1746 | return; | |
1747 | vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir); | |
1748 | if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir)) | |
1749 | return; | |
1750 | vcpu->arch.debugfs_timings = | |
1751 | debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir, | |
1752 | vcpu, &debugfs_timings_ops); | |
1753 | } | |
1754 | ||
1755 | #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ | |
1756 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) | |
1757 | { | |
1758 | } | |
1759 | #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ | |
1760 | ||
3a167bea AK |
1761 | static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm, |
1762 | unsigned int id) | |
de56a948 PM |
1763 | { |
1764 | struct kvm_vcpu *vcpu; | |
371fefd6 PM |
1765 | int err = -EINVAL; |
1766 | int core; | |
1767 | struct kvmppc_vcore *vcore; | |
de56a948 | 1768 | |
45c940ba | 1769 | core = id / threads_per_vcore(); |
371fefd6 PM |
1770 | if (core >= KVM_MAX_VCORES) |
1771 | goto out; | |
1772 | ||
1773 | err = -ENOMEM; | |
6b75e6bf | 1774 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
de56a948 PM |
1775 | if (!vcpu) |
1776 | goto out; | |
1777 | ||
1778 | err = kvm_vcpu_init(vcpu, kvm, id); | |
1779 | if (err) | |
1780 | goto free_vcpu; | |
1781 | ||
1782 | vcpu->arch.shared = &vcpu->arch.shregs; | |
5deb8e7a AG |
1783 | #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE |
1784 | /* | |
1785 | * The shared struct is never shared on HV, | |
1786 | * so we can always use host endianness | |
1787 | */ | |
1788 | #ifdef __BIG_ENDIAN__ | |
1789 | vcpu->arch.shared_big_endian = true; | |
1790 | #else | |
1791 | vcpu->arch.shared_big_endian = false; | |
1792 | #endif | |
1793 | #endif | |
de56a948 PM |
1794 | vcpu->arch.mmcr[0] = MMCR0_FC; |
1795 | vcpu->arch.ctrl = CTRL_RUNLATCH; | |
1796 | /* default to host PVR, since we can't spoof it */ | |
3a167bea | 1797 | kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR)); |
2e25aa5f | 1798 | spin_lock_init(&vcpu->arch.vpa_update_lock); |
c7b67670 PM |
1799 | spin_lock_init(&vcpu->arch.tbacct_lock); |
1800 | vcpu->arch.busy_preempt = TB_NIL; | |
d682916a | 1801 | vcpu->arch.intr_msr = MSR_SF | MSR_ME; |
de56a948 | 1802 | |
de56a948 PM |
1803 | kvmppc_mmu_book3s_hv_init(vcpu); |
1804 | ||
8455d79e | 1805 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
371fefd6 PM |
1806 | |
1807 | init_waitqueue_head(&vcpu->arch.cpu_run); | |
1808 | ||
1809 | mutex_lock(&kvm->lock); | |
1810 | vcore = kvm->arch.vcores[core]; | |
1811 | if (!vcore) { | |
de9bdd1a | 1812 | vcore = kvmppc_vcore_create(kvm, core); |
371fefd6 | 1813 | kvm->arch.vcores[core] = vcore; |
1b400ba0 | 1814 | kvm->arch.online_vcores++; |
371fefd6 PM |
1815 | } |
1816 | mutex_unlock(&kvm->lock); | |
1817 | ||
1818 | if (!vcore) | |
1819 | goto free_vcpu; | |
1820 | ||
1821 | spin_lock(&vcore->lock); | |
1822 | ++vcore->num_threads; | |
371fefd6 PM |
1823 | spin_unlock(&vcore->lock); |
1824 | vcpu->arch.vcore = vcore; | |
e0b7ec05 | 1825 | vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid; |
ec257165 | 1826 | vcpu->arch.thread_cpu = -1; |
a29ebeaf | 1827 | vcpu->arch.prev_cpu = -1; |
371fefd6 | 1828 | |
af8f38b3 AG |
1829 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
1830 | kvmppc_sanity_check(vcpu); | |
1831 | ||
b6c295df PM |
1832 | debugfs_vcpu_init(vcpu, id); |
1833 | ||
de56a948 PM |
1834 | return vcpu; |
1835 | ||
1836 | free_vcpu: | |
6b75e6bf | 1837 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
de56a948 PM |
1838 | out: |
1839 | return ERR_PTR(err); | |
1840 | } | |
1841 | ||
c35635ef PM |
1842 | static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) |
1843 | { | |
1844 | if (vpa->pinned_addr) | |
1845 | kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa, | |
1846 | vpa->dirty); | |
1847 | } | |
1848 | ||
3a167bea | 1849 | static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu) |
de56a948 | 1850 | { |
2e25aa5f | 1851 | spin_lock(&vcpu->arch.vpa_update_lock); |
c35635ef PM |
1852 | unpin_vpa(vcpu->kvm, &vcpu->arch.dtl); |
1853 | unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow); | |
1854 | unpin_vpa(vcpu->kvm, &vcpu->arch.vpa); | |
2e25aa5f | 1855 | spin_unlock(&vcpu->arch.vpa_update_lock); |
de56a948 | 1856 | kvm_vcpu_uninit(vcpu); |
6b75e6bf | 1857 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
de56a948 PM |
1858 | } |
1859 | ||
3a167bea AK |
1860 | static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu) |
1861 | { | |
1862 | /* Indicate we want to get back into the guest */ | |
1863 | return 1; | |
1864 | } | |
1865 | ||
19ccb76a | 1866 | static void kvmppc_set_timer(struct kvm_vcpu *vcpu) |
371fefd6 | 1867 | { |
19ccb76a | 1868 | unsigned long dec_nsec, now; |
371fefd6 | 1869 | |
19ccb76a PM |
1870 | now = get_tb(); |
1871 | if (now > vcpu->arch.dec_expires) { | |
1872 | /* decrementer has already gone negative */ | |
1873 | kvmppc_core_queue_dec(vcpu); | |
7e28e60e | 1874 | kvmppc_core_prepare_to_enter(vcpu); |
19ccb76a | 1875 | return; |
371fefd6 | 1876 | } |
19ccb76a PM |
1877 | dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC |
1878 | / tb_ticks_per_sec; | |
8b0e1953 | 1879 | hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL); |
19ccb76a | 1880 | vcpu->arch.timer_running = 1; |
371fefd6 PM |
1881 | } |
1882 | ||
19ccb76a | 1883 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu) |
371fefd6 | 1884 | { |
19ccb76a PM |
1885 | vcpu->arch.ceded = 0; |
1886 | if (vcpu->arch.timer_running) { | |
1887 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
1888 | vcpu->arch.timer_running = 0; | |
1889 | } | |
371fefd6 PM |
1890 | } |
1891 | ||
e0b7ec05 | 1892 | extern void __kvmppc_vcore_entry(void); |
de56a948 | 1893 | |
371fefd6 PM |
1894 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
1895 | struct kvm_vcpu *vcpu) | |
de56a948 | 1896 | { |
c7b67670 PM |
1897 | u64 now; |
1898 | ||
371fefd6 PM |
1899 | if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
1900 | return; | |
bf3d32e1 | 1901 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
c7b67670 PM |
1902 | now = mftb(); |
1903 | vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) - | |
1904 | vcpu->arch.stolen_logged; | |
1905 | vcpu->arch.busy_preempt = now; | |
1906 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; | |
bf3d32e1 | 1907 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
371fefd6 | 1908 | --vc->n_runnable; |
7b5f8272 | 1909 | WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL); |
371fefd6 PM |
1910 | } |
1911 | ||
f0888f70 PM |
1912 | static int kvmppc_grab_hwthread(int cpu) |
1913 | { | |
1914 | struct paca_struct *tpaca; | |
b754c739 | 1915 | long timeout = 10000; |
f0888f70 PM |
1916 | |
1917 | tpaca = &paca[cpu]; | |
1918 | ||
1919 | /* Ensure the thread won't go into the kernel if it wakes */ | |
7b444c67 | 1920 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
b4deba5c | 1921 | tpaca->kvm_hstate.kvm_vcore = NULL; |
5d5b99cd PM |
1922 | tpaca->kvm_hstate.napping = 0; |
1923 | smp_wmb(); | |
1924 | tpaca->kvm_hstate.hwthread_req = 1; | |
f0888f70 PM |
1925 | |
1926 | /* | |
1927 | * If the thread is already executing in the kernel (e.g. handling | |
1928 | * a stray interrupt), wait for it to get back to nap mode. | |
1929 | * The smp_mb() is to ensure that our setting of hwthread_req | |
1930 | * is visible before we look at hwthread_state, so if this | |
1931 | * races with the code at system_reset_pSeries and the thread | |
1932 | * misses our setting of hwthread_req, we are sure to see its | |
1933 | * setting of hwthread_state, and vice versa. | |
1934 | */ | |
1935 | smp_mb(); | |
1936 | while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) { | |
1937 | if (--timeout <= 0) { | |
1938 | pr_err("KVM: couldn't grab cpu %d\n", cpu); | |
1939 | return -EBUSY; | |
1940 | } | |
1941 | udelay(1); | |
1942 | } | |
1943 | return 0; | |
1944 | } | |
1945 | ||
1946 | static void kvmppc_release_hwthread(int cpu) | |
1947 | { | |
1948 | struct paca_struct *tpaca; | |
1949 | ||
1950 | tpaca = &paca[cpu]; | |
1951 | tpaca->kvm_hstate.hwthread_req = 0; | |
1952 | tpaca->kvm_hstate.kvm_vcpu = NULL; | |
b4deba5c PM |
1953 | tpaca->kvm_hstate.kvm_vcore = NULL; |
1954 | tpaca->kvm_hstate.kvm_split_mode = NULL; | |
f0888f70 PM |
1955 | } |
1956 | ||
a29ebeaf PM |
1957 | static void do_nothing(void *x) |
1958 | { | |
1959 | } | |
1960 | ||
1961 | static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu) | |
1962 | { | |
1963 | int i; | |
1964 | ||
1965 | cpu = cpu_first_thread_sibling(cpu); | |
1966 | cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush); | |
1967 | /* | |
1968 | * Make sure setting of bit in need_tlb_flush precedes | |
1969 | * testing of cpu_in_guest bits. The matching barrier on | |
1970 | * the other side is the first smp_mb() in kvmppc_run_core(). | |
1971 | */ | |
1972 | smp_mb(); | |
1973 | for (i = 0; i < threads_per_core; ++i) | |
1974 | if (cpumask_test_cpu(cpu + i, &kvm->arch.cpu_in_guest)) | |
1975 | smp_call_function_single(cpu + i, do_nothing, NULL, 1); | |
1976 | } | |
1977 | ||
b4deba5c | 1978 | static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc) |
371fefd6 PM |
1979 | { |
1980 | int cpu; | |
1981 | struct paca_struct *tpaca; | |
ec257165 | 1982 | struct kvmppc_vcore *mvc = vc->master_vcore; |
a29ebeaf | 1983 | struct kvm *kvm = vc->kvm; |
371fefd6 | 1984 | |
b4deba5c PM |
1985 | cpu = vc->pcpu; |
1986 | if (vcpu) { | |
1987 | if (vcpu->arch.timer_running) { | |
1988 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); | |
1989 | vcpu->arch.timer_running = 0; | |
1990 | } | |
1991 | cpu += vcpu->arch.ptid; | |
1992 | vcpu->cpu = mvc->pcpu; | |
1993 | vcpu->arch.thread_cpu = cpu; | |
a29ebeaf PM |
1994 | |
1995 | /* | |
1996 | * With radix, the guest can do TLB invalidations itself, | |
1997 | * and it could choose to use the local form (tlbiel) if | |
1998 | * it is invalidating a translation that has only ever been | |
1999 | * used on one vcpu. However, that doesn't mean it has | |
2000 | * only ever been used on one physical cpu, since vcpus | |
2001 | * can move around between pcpus. To cope with this, when | |
2002 | * a vcpu moves from one pcpu to another, we need to tell | |
2003 | * any vcpus running on the same core as this vcpu previously | |
2004 | * ran to flush the TLB. The TLB is shared between threads, | |
2005 | * so we use a single bit in .need_tlb_flush for all 4 threads. | |
2006 | */ | |
2007 | if (kvm_is_radix(kvm) && vcpu->arch.prev_cpu != cpu) { | |
2008 | if (vcpu->arch.prev_cpu >= 0 && | |
2009 | cpu_first_thread_sibling(vcpu->arch.prev_cpu) != | |
2010 | cpu_first_thread_sibling(cpu)) | |
2011 | radix_flush_cpu(kvm, vcpu->arch.prev_cpu, vcpu); | |
2012 | vcpu->arch.prev_cpu = cpu; | |
2013 | } | |
2014 | cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest); | |
19ccb76a | 2015 | } |
371fefd6 | 2016 | tpaca = &paca[cpu]; |
5d5b99cd | 2017 | tpaca->kvm_hstate.kvm_vcpu = vcpu; |
ec257165 | 2018 | tpaca->kvm_hstate.ptid = cpu - mvc->pcpu; |
ec257165 | 2019 | /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */ |
371fefd6 | 2020 | smp_wmb(); |
b4deba5c | 2021 | tpaca->kvm_hstate.kvm_vcore = mvc; |
5d5b99cd | 2022 | if (cpu != smp_processor_id()) |
66feed61 | 2023 | kvmppc_ipi_thread(cpu); |
371fefd6 | 2024 | } |
de56a948 | 2025 | |
5d5b99cd | 2026 | static void kvmppc_wait_for_nap(void) |
371fefd6 | 2027 | { |
5d5b99cd PM |
2028 | int cpu = smp_processor_id(); |
2029 | int i, loops; | |
45c940ba | 2030 | int n_threads = threads_per_vcore(); |
371fefd6 | 2031 | |
45c940ba PM |
2032 | if (n_threads <= 1) |
2033 | return; | |
5d5b99cd PM |
2034 | for (loops = 0; loops < 1000000; ++loops) { |
2035 | /* | |
2036 | * Check if all threads are finished. | |
b4deba5c | 2037 | * We set the vcore pointer when starting a thread |
5d5b99cd | 2038 | * and the thread clears it when finished, so we look |
b4deba5c | 2039 | * for any threads that still have a non-NULL vcore ptr. |
5d5b99cd | 2040 | */ |
45c940ba | 2041 | for (i = 1; i < n_threads; ++i) |
b4deba5c | 2042 | if (paca[cpu + i].kvm_hstate.kvm_vcore) |
5d5b99cd | 2043 | break; |
45c940ba | 2044 | if (i == n_threads) { |
5d5b99cd PM |
2045 | HMT_medium(); |
2046 | return; | |
371fefd6 | 2047 | } |
5d5b99cd | 2048 | HMT_low(); |
371fefd6 PM |
2049 | } |
2050 | HMT_medium(); | |
45c940ba | 2051 | for (i = 1; i < n_threads; ++i) |
b4deba5c | 2052 | if (paca[cpu + i].kvm_hstate.kvm_vcore) |
5d5b99cd | 2053 | pr_err("KVM: CPU %d seems to be stuck\n", cpu + i); |
371fefd6 PM |
2054 | } |
2055 | ||
2056 | /* | |
2057 | * Check that we are on thread 0 and that any other threads in | |
7b444c67 PM |
2058 | * this core are off-line. Then grab the threads so they can't |
2059 | * enter the kernel. | |
371fefd6 PM |
2060 | */ |
2061 | static int on_primary_thread(void) | |
2062 | { | |
2063 | int cpu = smp_processor_id(); | |
3102f784 | 2064 | int thr; |
371fefd6 | 2065 | |
3102f784 ME |
2066 | /* Are we on a primary subcore? */ |
2067 | if (cpu_thread_in_subcore(cpu)) | |
371fefd6 | 2068 | return 0; |
3102f784 ME |
2069 | |
2070 | thr = 0; | |
2071 | while (++thr < threads_per_subcore) | |
371fefd6 PM |
2072 | if (cpu_online(cpu + thr)) |
2073 | return 0; | |
7b444c67 PM |
2074 | |
2075 | /* Grab all hw threads so they can't go into the kernel */ | |
3102f784 | 2076 | for (thr = 1; thr < threads_per_subcore; ++thr) { |
7b444c67 PM |
2077 | if (kvmppc_grab_hwthread(cpu + thr)) { |
2078 | /* Couldn't grab one; let the others go */ | |
2079 | do { | |
2080 | kvmppc_release_hwthread(cpu + thr); | |
2081 | } while (--thr > 0); | |
2082 | return 0; | |
2083 | } | |
2084 | } | |
371fefd6 PM |
2085 | return 1; |
2086 | } | |
2087 | ||
ec257165 PM |
2088 | /* |
2089 | * A list of virtual cores for each physical CPU. | |
2090 | * These are vcores that could run but their runner VCPU tasks are | |
2091 | * (or may be) preempted. | |
2092 | */ | |
2093 | struct preempted_vcore_list { | |
2094 | struct list_head list; | |
2095 | spinlock_t lock; | |
2096 | }; | |
2097 | ||
2098 | static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores); | |
2099 | ||
2100 | static void init_vcore_lists(void) | |
2101 | { | |
2102 | int cpu; | |
2103 | ||
2104 | for_each_possible_cpu(cpu) { | |
2105 | struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu); | |
2106 | spin_lock_init(&lp->lock); | |
2107 | INIT_LIST_HEAD(&lp->list); | |
2108 | } | |
2109 | } | |
2110 | ||
2111 | static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc) | |
2112 | { | |
2113 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); | |
2114 | ||
2115 | vc->vcore_state = VCORE_PREEMPT; | |
2116 | vc->pcpu = smp_processor_id(); | |
45c940ba | 2117 | if (vc->num_threads < threads_per_vcore()) { |
ec257165 PM |
2118 | spin_lock(&lp->lock); |
2119 | list_add_tail(&vc->preempt_list, &lp->list); | |
2120 | spin_unlock(&lp->lock); | |
2121 | } | |
2122 | ||
2123 | /* Start accumulating stolen time */ | |
2124 | kvmppc_core_start_stolen(vc); | |
2125 | } | |
2126 | ||
2127 | static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc) | |
2128 | { | |
402813fe | 2129 | struct preempted_vcore_list *lp; |
ec257165 PM |
2130 | |
2131 | kvmppc_core_end_stolen(vc); | |
2132 | if (!list_empty(&vc->preempt_list)) { | |
402813fe | 2133 | lp = &per_cpu(preempted_vcores, vc->pcpu); |
ec257165 PM |
2134 | spin_lock(&lp->lock); |
2135 | list_del_init(&vc->preempt_list); | |
2136 | spin_unlock(&lp->lock); | |
2137 | } | |
2138 | vc->vcore_state = VCORE_INACTIVE; | |
2139 | } | |
2140 | ||
b4deba5c PM |
2141 | /* |
2142 | * This stores information about the virtual cores currently | |
2143 | * assigned to a physical core. | |
2144 | */ | |
ec257165 | 2145 | struct core_info { |
b4deba5c PM |
2146 | int n_subcores; |
2147 | int max_subcore_threads; | |
ec257165 | 2148 | int total_threads; |
b4deba5c PM |
2149 | int subcore_threads[MAX_SUBCORES]; |
2150 | struct kvm *subcore_vm[MAX_SUBCORES]; | |
2151 | struct list_head vcs[MAX_SUBCORES]; | |
ec257165 PM |
2152 | }; |
2153 | ||
b4deba5c PM |
2154 | /* |
2155 | * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7 | |
2156 | * respectively in 2-way micro-threading (split-core) mode. | |
2157 | */ | |
2158 | static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 }; | |
2159 | ||
ec257165 PM |
2160 | static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc) |
2161 | { | |
b4deba5c PM |
2162 | int sub; |
2163 | ||
ec257165 | 2164 | memset(cip, 0, sizeof(*cip)); |
b4deba5c PM |
2165 | cip->n_subcores = 1; |
2166 | cip->max_subcore_threads = vc->num_threads; | |
ec257165 | 2167 | cip->total_threads = vc->num_threads; |
b4deba5c PM |
2168 | cip->subcore_threads[0] = vc->num_threads; |
2169 | cip->subcore_vm[0] = vc->kvm; | |
2170 | for (sub = 0; sub < MAX_SUBCORES; ++sub) | |
2171 | INIT_LIST_HEAD(&cip->vcs[sub]); | |
2172 | list_add_tail(&vc->preempt_list, &cip->vcs[0]); | |
2173 | } | |
2174 | ||
2175 | static bool subcore_config_ok(int n_subcores, int n_threads) | |
2176 | { | |
2177 | /* Can only dynamically split if unsplit to begin with */ | |
2178 | if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS) | |
2179 | return false; | |
2180 | if (n_subcores > MAX_SUBCORES) | |
2181 | return false; | |
2182 | if (n_subcores > 1) { | |
2183 | if (!(dynamic_mt_modes & 2)) | |
2184 | n_subcores = 4; | |
2185 | if (n_subcores > 2 && !(dynamic_mt_modes & 4)) | |
2186 | return false; | |
2187 | } | |
2188 | ||
2189 | return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS; | |
ec257165 PM |
2190 | } |
2191 | ||
2192 | static void init_master_vcore(struct kvmppc_vcore *vc) | |
2193 | { | |
2194 | vc->master_vcore = vc; | |
2195 | vc->entry_exit_map = 0; | |
2196 | vc->in_guest = 0; | |
2197 | vc->napping_threads = 0; | |
2198 | vc->conferring_threads = 0; | |
2199 | } | |
2200 | ||
b4deba5c PM |
2201 | static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip) |
2202 | { | |
2203 | int n_threads = vc->num_threads; | |
2204 | int sub; | |
2205 | ||
2206 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) | |
2207 | return false; | |
2208 | ||
2209 | if (n_threads < cip->max_subcore_threads) | |
2210 | n_threads = cip->max_subcore_threads; | |
b009031f | 2211 | if (!subcore_config_ok(cip->n_subcores + 1, n_threads)) |
b4deba5c | 2212 | return false; |
b009031f | 2213 | cip->max_subcore_threads = n_threads; |
b4deba5c PM |
2214 | |
2215 | sub = cip->n_subcores; | |
2216 | ++cip->n_subcores; | |
2217 | cip->total_threads += vc->num_threads; | |
2218 | cip->subcore_threads[sub] = vc->num_threads; | |
2219 | cip->subcore_vm[sub] = vc->kvm; | |
2220 | init_master_vcore(vc); | |
28d057c8 | 2221 | list_move_tail(&vc->preempt_list, &cip->vcs[sub]); |
b4deba5c PM |
2222 | |
2223 | return true; | |
2224 | } | |
2225 | ||
b4deba5c PM |
2226 | /* |
2227 | * Work out whether it is possible to piggyback the execution of | |
2228 | * vcore *pvc onto the execution of the other vcores described in *cip. | |
2229 | */ | |
2230 | static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip, | |
2231 | int target_threads) | |
2232 | { | |
b4deba5c PM |
2233 | if (cip->total_threads + pvc->num_threads > target_threads) |
2234 | return false; | |
b4deba5c | 2235 | |
b009031f | 2236 | return can_dynamic_split(pvc, cip); |
b4deba5c PM |
2237 | } |
2238 | ||
d911f0be PM |
2239 | static void prepare_threads(struct kvmppc_vcore *vc) |
2240 | { | |
7b5f8272 SJS |
2241 | int i; |
2242 | struct kvm_vcpu *vcpu; | |
d911f0be | 2243 | |
7b5f8272 | 2244 | for_each_runnable_thread(i, vcpu, vc) { |
d911f0be PM |
2245 | if (signal_pending(vcpu->arch.run_task)) |
2246 | vcpu->arch.ret = -EINTR; | |
2247 | else if (vcpu->arch.vpa.update_pending || | |
2248 | vcpu->arch.slb_shadow.update_pending || | |
2249 | vcpu->arch.dtl.update_pending) | |
2250 | vcpu->arch.ret = RESUME_GUEST; | |
2251 | else | |
2252 | continue; | |
2253 | kvmppc_remove_runnable(vc, vcpu); | |
2254 | wake_up(&vcpu->arch.cpu_run); | |
2255 | } | |
2256 | } | |
2257 | ||
ec257165 PM |
2258 | static void collect_piggybacks(struct core_info *cip, int target_threads) |
2259 | { | |
2260 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); | |
2261 | struct kvmppc_vcore *pvc, *vcnext; | |
2262 | ||
2263 | spin_lock(&lp->lock); | |
2264 | list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) { | |
2265 | if (!spin_trylock(&pvc->lock)) | |
2266 | continue; | |
2267 | prepare_threads(pvc); | |
2268 | if (!pvc->n_runnable) { | |
2269 | list_del_init(&pvc->preempt_list); | |
2270 | if (pvc->runner == NULL) { | |
2271 | pvc->vcore_state = VCORE_INACTIVE; | |
2272 | kvmppc_core_end_stolen(pvc); | |
2273 | } | |
2274 | spin_unlock(&pvc->lock); | |
2275 | continue; | |
2276 | } | |
2277 | if (!can_piggyback(pvc, cip, target_threads)) { | |
2278 | spin_unlock(&pvc->lock); | |
2279 | continue; | |
2280 | } | |
2281 | kvmppc_core_end_stolen(pvc); | |
2282 | pvc->vcore_state = VCORE_PIGGYBACK; | |
2283 | if (cip->total_threads >= target_threads) | |
2284 | break; | |
2285 | } | |
2286 | spin_unlock(&lp->lock); | |
2287 | } | |
2288 | ||
2289 | static void post_guest_process(struct kvmppc_vcore *vc, bool is_master) | |
25fedfca | 2290 | { |
7b5f8272 | 2291 | int still_running = 0, i; |
25fedfca PM |
2292 | u64 now; |
2293 | long ret; | |
7b5f8272 | 2294 | struct kvm_vcpu *vcpu; |
25fedfca | 2295 | |
ec257165 | 2296 | spin_lock(&vc->lock); |
25fedfca | 2297 | now = get_tb(); |
7b5f8272 | 2298 | for_each_runnable_thread(i, vcpu, vc) { |
25fedfca PM |
2299 | /* cancel pending dec exception if dec is positive */ |
2300 | if (now < vcpu->arch.dec_expires && | |
2301 | kvmppc_core_pending_dec(vcpu)) | |
2302 | kvmppc_core_dequeue_dec(vcpu); | |
2303 | ||
2304 | trace_kvm_guest_exit(vcpu); | |
2305 | ||
2306 | ret = RESUME_GUEST; | |
2307 | if (vcpu->arch.trap) | |
2308 | ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu, | |
2309 | vcpu->arch.run_task); | |
2310 | ||
2311 | vcpu->arch.ret = ret; | |
2312 | vcpu->arch.trap = 0; | |
2313 | ||
ec257165 PM |
2314 | if (is_kvmppc_resume_guest(vcpu->arch.ret)) { |
2315 | if (vcpu->arch.pending_exceptions) | |
2316 | kvmppc_core_prepare_to_enter(vcpu); | |
2317 | if (vcpu->arch.ceded) | |
25fedfca | 2318 | kvmppc_set_timer(vcpu); |
ec257165 PM |
2319 | else |
2320 | ++still_running; | |
2321 | } else { | |
25fedfca PM |
2322 | kvmppc_remove_runnable(vc, vcpu); |
2323 | wake_up(&vcpu->arch.cpu_run); | |
2324 | } | |
2325 | } | |
ec257165 PM |
2326 | list_del_init(&vc->preempt_list); |
2327 | if (!is_master) { | |
563a1e93 | 2328 | if (still_running > 0) { |
ec257165 | 2329 | kvmppc_vcore_preempt(vc); |
563a1e93 PM |
2330 | } else if (vc->runner) { |
2331 | vc->vcore_state = VCORE_PREEMPT; | |
2332 | kvmppc_core_start_stolen(vc); | |
2333 | } else { | |
2334 | vc->vcore_state = VCORE_INACTIVE; | |
2335 | } | |
ec257165 PM |
2336 | if (vc->n_runnable > 0 && vc->runner == NULL) { |
2337 | /* make sure there's a candidate runner awake */ | |
7b5f8272 SJS |
2338 | i = -1; |
2339 | vcpu = next_runnable_thread(vc, &i); | |
ec257165 PM |
2340 | wake_up(&vcpu->arch.cpu_run); |
2341 | } | |
2342 | } | |
2343 | spin_unlock(&vc->lock); | |
25fedfca PM |
2344 | } |
2345 | ||
b8e6a87c SW |
2346 | /* |
2347 | * Clear core from the list of active host cores as we are about to | |
2348 | * enter the guest. Only do this if it is the primary thread of the | |
2349 | * core (not if a subcore) that is entering the guest. | |
2350 | */ | |
3f7cd919 | 2351 | static inline int kvmppc_clear_host_core(unsigned int cpu) |
b8e6a87c SW |
2352 | { |
2353 | int core; | |
2354 | ||
2355 | if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu)) | |
3f7cd919 | 2356 | return 0; |
b8e6a87c SW |
2357 | /* |
2358 | * Memory barrier can be omitted here as we will do a smp_wmb() | |
2359 | * later in kvmppc_start_thread and we need ensure that state is | |
2360 | * visible to other CPUs only after we enter guest. | |
2361 | */ | |
2362 | core = cpu >> threads_shift; | |
2363 | kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0; | |
3f7cd919 | 2364 | return 0; |
b8e6a87c SW |
2365 | } |
2366 | ||
2367 | /* | |
2368 | * Advertise this core as an active host core since we exited the guest | |
2369 | * Only need to do this if it is the primary thread of the core that is | |
2370 | * exiting. | |
2371 | */ | |
3f7cd919 | 2372 | static inline int kvmppc_set_host_core(unsigned int cpu) |
b8e6a87c SW |
2373 | { |
2374 | int core; | |
2375 | ||
2376 | if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu)) | |
3f7cd919 | 2377 | return 0; |
b8e6a87c SW |
2378 | |
2379 | /* | |
2380 | * Memory barrier can be omitted here because we do a spin_unlock | |
2381 | * immediately after this which provides the memory barrier. | |
2382 | */ | |
2383 | core = cpu >> threads_shift; | |
2384 | kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1; | |
3f7cd919 | 2385 | return 0; |
b8e6a87c SW |
2386 | } |
2387 | ||
371fefd6 PM |
2388 | /* |
2389 | * Run a set of guest threads on a physical core. | |
2390 | * Called with vc->lock held. | |
2391 | */ | |
66feed61 | 2392 | static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) |
371fefd6 | 2393 | { |
7b5f8272 | 2394 | struct kvm_vcpu *vcpu; |
d911f0be | 2395 | int i; |
2c9097e4 | 2396 | int srcu_idx; |
ec257165 PM |
2397 | struct core_info core_info; |
2398 | struct kvmppc_vcore *pvc, *vcnext; | |
b4deba5c PM |
2399 | struct kvm_split_mode split_info, *sip; |
2400 | int split, subcore_size, active; | |
2401 | int sub; | |
2402 | bool thr0_done; | |
2403 | unsigned long cmd_bit, stat_bit; | |
ec257165 PM |
2404 | int pcpu, thr; |
2405 | int target_threads; | |
45c940ba | 2406 | int controlled_threads; |
371fefd6 | 2407 | |
d911f0be PM |
2408 | /* |
2409 | * Remove from the list any threads that have a signal pending | |
2410 | * or need a VPA update done | |
2411 | */ | |
2412 | prepare_threads(vc); | |
2413 | ||
2414 | /* if the runner is no longer runnable, let the caller pick a new one */ | |
2415 | if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE) | |
2416 | return; | |
081f323b PM |
2417 | |
2418 | /* | |
d911f0be | 2419 | * Initialize *vc. |
081f323b | 2420 | */ |
ec257165 | 2421 | init_master_vcore(vc); |
2711e248 | 2422 | vc->preempt_tb = TB_NIL; |
081f323b | 2423 | |
45c940ba PM |
2424 | /* |
2425 | * Number of threads that we will be controlling: the same as | |
2426 | * the number of threads per subcore, except on POWER9, | |
2427 | * where it's 1 because the threads are (mostly) independent. | |
2428 | */ | |
2429 | controlled_threads = threads_per_vcore(); | |
2430 | ||
7b444c67 | 2431 | /* |
3102f784 ME |
2432 | * Make sure we are running on primary threads, and that secondary |
2433 | * threads are offline. Also check if the number of threads in this | |
2434 | * guest are greater than the current system threads per guest. | |
7b444c67 | 2435 | */ |
45c940ba | 2436 | if ((controlled_threads > 1) && |
3102f784 | 2437 | ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) { |
7b5f8272 | 2438 | for_each_runnable_thread(i, vcpu, vc) { |
7b444c67 | 2439 | vcpu->arch.ret = -EBUSY; |
25fedfca PM |
2440 | kvmppc_remove_runnable(vc, vcpu); |
2441 | wake_up(&vcpu->arch.cpu_run); | |
2442 | } | |
7b444c67 PM |
2443 | goto out; |
2444 | } | |
2445 | ||
ec257165 PM |
2446 | /* |
2447 | * See if we could run any other vcores on the physical core | |
2448 | * along with this one. | |
2449 | */ | |
2450 | init_core_info(&core_info, vc); | |
2451 | pcpu = smp_processor_id(); | |
45c940ba | 2452 | target_threads = controlled_threads; |
ec257165 PM |
2453 | if (target_smt_mode && target_smt_mode < target_threads) |
2454 | target_threads = target_smt_mode; | |
2455 | if (vc->num_threads < target_threads) | |
2456 | collect_piggybacks(&core_info, target_threads); | |
3102f784 | 2457 | |
b4deba5c PM |
2458 | /* Decide on micro-threading (split-core) mode */ |
2459 | subcore_size = threads_per_subcore; | |
2460 | cmd_bit = stat_bit = 0; | |
2461 | split = core_info.n_subcores; | |
2462 | sip = NULL; | |
2463 | if (split > 1) { | |
2464 | /* threads_per_subcore must be MAX_SMT_THREADS (8) here */ | |
2465 | if (split == 2 && (dynamic_mt_modes & 2)) { | |
2466 | cmd_bit = HID0_POWER8_1TO2LPAR; | |
2467 | stat_bit = HID0_POWER8_2LPARMODE; | |
2468 | } else { | |
2469 | split = 4; | |
2470 | cmd_bit = HID0_POWER8_1TO4LPAR; | |
2471 | stat_bit = HID0_POWER8_4LPARMODE; | |
2472 | } | |
2473 | subcore_size = MAX_SMT_THREADS / split; | |
2474 | sip = &split_info; | |
2475 | memset(&split_info, 0, sizeof(split_info)); | |
2476 | split_info.rpr = mfspr(SPRN_RPR); | |
2477 | split_info.pmmar = mfspr(SPRN_PMMAR); | |
2478 | split_info.ldbar = mfspr(SPRN_LDBAR); | |
2479 | split_info.subcore_size = subcore_size; | |
2480 | for (sub = 0; sub < core_info.n_subcores; ++sub) | |
2481 | split_info.master_vcs[sub] = | |
2482 | list_first_entry(&core_info.vcs[sub], | |
2483 | struct kvmppc_vcore, preempt_list); | |
2484 | /* order writes to split_info before kvm_split_mode pointer */ | |
2485 | smp_wmb(); | |
2486 | } | |
2487 | pcpu = smp_processor_id(); | |
45c940ba | 2488 | for (thr = 0; thr < controlled_threads; ++thr) |
b4deba5c PM |
2489 | paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip; |
2490 | ||
2491 | /* Initiate micro-threading (split-core) if required */ | |
2492 | if (cmd_bit) { | |
2493 | unsigned long hid0 = mfspr(SPRN_HID0); | |
2494 | ||
2495 | hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS; | |
2496 | mb(); | |
2497 | mtspr(SPRN_HID0, hid0); | |
2498 | isync(); | |
2499 | for (;;) { | |
2500 | hid0 = mfspr(SPRN_HID0); | |
2501 | if (hid0 & stat_bit) | |
2502 | break; | |
2503 | cpu_relax(); | |
ec257165 | 2504 | } |
2e25aa5f | 2505 | } |
3102f784 | 2506 | |
b8e6a87c SW |
2507 | kvmppc_clear_host_core(pcpu); |
2508 | ||
b4deba5c PM |
2509 | /* Start all the threads */ |
2510 | active = 0; | |
2511 | for (sub = 0; sub < core_info.n_subcores; ++sub) { | |
2512 | thr = subcore_thread_map[sub]; | |
2513 | thr0_done = false; | |
2514 | active |= 1 << thr; | |
2515 | list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) { | |
2516 | pvc->pcpu = pcpu + thr; | |
7b5f8272 | 2517 | for_each_runnable_thread(i, vcpu, pvc) { |
b4deba5c PM |
2518 | kvmppc_start_thread(vcpu, pvc); |
2519 | kvmppc_create_dtl_entry(vcpu, pvc); | |
2520 | trace_kvm_guest_enter(vcpu); | |
2521 | if (!vcpu->arch.ptid) | |
2522 | thr0_done = true; | |
2523 | active |= 1 << (thr + vcpu->arch.ptid); | |
2524 | } | |
2525 | /* | |
2526 | * We need to start the first thread of each subcore | |
2527 | * even if it doesn't have a vcpu. | |
2528 | */ | |
2529 | if (pvc->master_vcore == pvc && !thr0_done) | |
2530 | kvmppc_start_thread(NULL, pvc); | |
2531 | thr += pvc->num_threads; | |
2532 | } | |
2e25aa5f | 2533 | } |
371fefd6 | 2534 | |
7f235328 GS |
2535 | /* |
2536 | * Ensure that split_info.do_nap is set after setting | |
2537 | * the vcore pointer in the PACA of the secondaries. | |
2538 | */ | |
2539 | smp_mb(); | |
2540 | if (cmd_bit) | |
2541 | split_info.do_nap = 1; /* ask secondaries to nap when done */ | |
2542 | ||
b4deba5c PM |
2543 | /* |
2544 | * When doing micro-threading, poke the inactive threads as well. | |
2545 | * This gets them to the nap instruction after kvm_do_nap, | |
2546 | * which reduces the time taken to unsplit later. | |
2547 | */ | |
2548 | if (split > 1) | |
2549 | for (thr = 1; thr < threads_per_subcore; ++thr) | |
2550 | if (!(active & (1 << thr))) | |
2551 | kvmppc_ipi_thread(pcpu + thr); | |
e0b7ec05 | 2552 | |
2f12f034 | 2553 | vc->vcore_state = VCORE_RUNNING; |
19ccb76a | 2554 | preempt_disable(); |
3c78f78a SW |
2555 | |
2556 | trace_kvmppc_run_core(vc, 0); | |
2557 | ||
b4deba5c PM |
2558 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
2559 | list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) | |
2560 | spin_unlock(&pvc->lock); | |
de56a948 | 2561 | |
6edaa530 | 2562 | guest_enter(); |
2c9097e4 | 2563 | |
e0b7ec05 | 2564 | srcu_idx = srcu_read_lock(&vc->kvm->srcu); |
2c9097e4 | 2565 | |
e0b7ec05 | 2566 | __kvmppc_vcore_entry(); |
de56a948 | 2567 | |
ec257165 PM |
2568 | srcu_read_unlock(&vc->kvm->srcu, srcu_idx); |
2569 | ||
2570 | spin_lock(&vc->lock); | |
371fefd6 | 2571 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ |
19ccb76a | 2572 | vc->vcore_state = VCORE_EXITING; |
371fefd6 | 2573 | |
19ccb76a | 2574 | /* wait for secondary threads to finish writing their state to memory */ |
5d5b99cd | 2575 | kvmppc_wait_for_nap(); |
b4deba5c PM |
2576 | |
2577 | /* Return to whole-core mode if we split the core earlier */ | |
2578 | if (split > 1) { | |
2579 | unsigned long hid0 = mfspr(SPRN_HID0); | |
2580 | unsigned long loops = 0; | |
2581 | ||
2582 | hid0 &= ~HID0_POWER8_DYNLPARDIS; | |
2583 | stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE; | |
2584 | mb(); | |
2585 | mtspr(SPRN_HID0, hid0); | |
2586 | isync(); | |
2587 | for (;;) { | |
2588 | hid0 = mfspr(SPRN_HID0); | |
2589 | if (!(hid0 & stat_bit)) | |
2590 | break; | |
2591 | cpu_relax(); | |
2592 | ++loops; | |
2593 | } | |
2594 | split_info.do_nap = 0; | |
2595 | } | |
2596 | ||
2597 | /* Let secondaries go back to the offline loop */ | |
45c940ba | 2598 | for (i = 0; i < controlled_threads; ++i) { |
b4deba5c PM |
2599 | kvmppc_release_hwthread(pcpu + i); |
2600 | if (sip && sip->napped[i]) | |
2601 | kvmppc_ipi_thread(pcpu + i); | |
a29ebeaf | 2602 | cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest); |
b4deba5c PM |
2603 | } |
2604 | ||
b8e6a87c SW |
2605 | kvmppc_set_host_core(pcpu); |
2606 | ||
371fefd6 | 2607 | spin_unlock(&vc->lock); |
2c9097e4 | 2608 | |
371fefd6 PM |
2609 | /* make sure updates to secondary vcpu structs are visible now */ |
2610 | smp_mb(); | |
6edaa530 | 2611 | guest_exit(); |
de56a948 | 2612 | |
b4deba5c PM |
2613 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
2614 | list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub], | |
2615 | preempt_list) | |
2616 | post_guest_process(pvc, pvc == vc); | |
de56a948 | 2617 | |
913d3ff9 | 2618 | spin_lock(&vc->lock); |
ec257165 | 2619 | preempt_enable(); |
de56a948 PM |
2620 | |
2621 | out: | |
19ccb76a | 2622 | vc->vcore_state = VCORE_INACTIVE; |
3c78f78a | 2623 | trace_kvmppc_run_core(vc, 1); |
371fefd6 PM |
2624 | } |
2625 | ||
19ccb76a PM |
2626 | /* |
2627 | * Wait for some other vcpu thread to execute us, and | |
2628 | * wake us up when we need to handle something in the host. | |
2629 | */ | |
ec257165 PM |
2630 | static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc, |
2631 | struct kvm_vcpu *vcpu, int wait_state) | |
371fefd6 | 2632 | { |
371fefd6 PM |
2633 | DEFINE_WAIT(wait); |
2634 | ||
19ccb76a | 2635 | prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); |
ec257165 PM |
2636 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
2637 | spin_unlock(&vc->lock); | |
19ccb76a | 2638 | schedule(); |
ec257165 PM |
2639 | spin_lock(&vc->lock); |
2640 | } | |
19ccb76a PM |
2641 | finish_wait(&vcpu->arch.cpu_run, &wait); |
2642 | } | |
2643 | ||
0cda69dd SJS |
2644 | static void grow_halt_poll_ns(struct kvmppc_vcore *vc) |
2645 | { | |
2646 | /* 10us base */ | |
2647 | if (vc->halt_poll_ns == 0 && halt_poll_ns_grow) | |
2648 | vc->halt_poll_ns = 10000; | |
2649 | else | |
2650 | vc->halt_poll_ns *= halt_poll_ns_grow; | |
0cda69dd SJS |
2651 | } |
2652 | ||
2653 | static void shrink_halt_poll_ns(struct kvmppc_vcore *vc) | |
2654 | { | |
2655 | if (halt_poll_ns_shrink == 0) | |
2656 | vc->halt_poll_ns = 0; | |
2657 | else | |
2658 | vc->halt_poll_ns /= halt_poll_ns_shrink; | |
2659 | } | |
2660 | ||
908a0935 SJS |
2661 | /* |
2662 | * Check to see if any of the runnable vcpus on the vcore have pending | |
0cda69dd SJS |
2663 | * exceptions or are no longer ceded |
2664 | */ | |
2665 | static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc) | |
2666 | { | |
2667 | struct kvm_vcpu *vcpu; | |
2668 | int i; | |
2669 | ||
2670 | for_each_runnable_thread(i, vcpu, vc) { | |
8464c884 PM |
2671 | if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded || |
2672 | vcpu->arch.prodded) | |
0cda69dd SJS |
2673 | return 1; |
2674 | } | |
2675 | ||
2676 | return 0; | |
2677 | } | |
2678 | ||
19ccb76a PM |
2679 | /* |
2680 | * All the vcpus in this vcore are idle, so wait for a decrementer | |
2681 | * or external interrupt to one of the vcpus. vc->lock is held. | |
2682 | */ | |
2683 | static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) | |
2684 | { | |
2a27f514 | 2685 | ktime_t cur, start_poll, start_wait; |
0cda69dd | 2686 | int do_sleep = 1; |
0cda69dd | 2687 | u64 block_ns; |
8577370f | 2688 | DECLARE_SWAITQUEUE(wait); |
1bc5d59c | 2689 | |
0cda69dd | 2690 | /* Poll for pending exceptions and ceded state */ |
2a27f514 | 2691 | cur = start_poll = ktime_get(); |
0cda69dd | 2692 | if (vc->halt_poll_ns) { |
2a27f514 SJS |
2693 | ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns); |
2694 | ++vc->runner->stat.halt_attempted_poll; | |
1bc5d59c | 2695 | |
0cda69dd SJS |
2696 | vc->vcore_state = VCORE_POLLING; |
2697 | spin_unlock(&vc->lock); | |
2698 | ||
2699 | do { | |
2700 | if (kvmppc_vcore_check_block(vc)) { | |
2701 | do_sleep = 0; | |
2702 | break; | |
2703 | } | |
2704 | cur = ktime_get(); | |
2705 | } while (single_task_running() && ktime_before(cur, stop)); | |
2706 | ||
2707 | spin_lock(&vc->lock); | |
2708 | vc->vcore_state = VCORE_INACTIVE; | |
2709 | ||
2a27f514 SJS |
2710 | if (!do_sleep) { |
2711 | ++vc->runner->stat.halt_successful_poll; | |
0cda69dd | 2712 | goto out; |
2a27f514 | 2713 | } |
1bc5d59c SW |
2714 | } |
2715 | ||
0cda69dd SJS |
2716 | prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE); |
2717 | ||
2718 | if (kvmppc_vcore_check_block(vc)) { | |
8577370f | 2719 | finish_swait(&vc->wq, &wait); |
0cda69dd | 2720 | do_sleep = 0; |
2a27f514 SJS |
2721 | /* If we polled, count this as a successful poll */ |
2722 | if (vc->halt_poll_ns) | |
2723 | ++vc->runner->stat.halt_successful_poll; | |
0cda69dd | 2724 | goto out; |
1bc5d59c SW |
2725 | } |
2726 | ||
2a27f514 SJS |
2727 | start_wait = ktime_get(); |
2728 | ||
19ccb76a | 2729 | vc->vcore_state = VCORE_SLEEPING; |
3c78f78a | 2730 | trace_kvmppc_vcore_blocked(vc, 0); |
19ccb76a | 2731 | spin_unlock(&vc->lock); |
913d3ff9 | 2732 | schedule(); |
8577370f | 2733 | finish_swait(&vc->wq, &wait); |
19ccb76a PM |
2734 | spin_lock(&vc->lock); |
2735 | vc->vcore_state = VCORE_INACTIVE; | |
3c78f78a | 2736 | trace_kvmppc_vcore_blocked(vc, 1); |
2a27f514 | 2737 | ++vc->runner->stat.halt_successful_wait; |
0cda69dd SJS |
2738 | |
2739 | cur = ktime_get(); | |
2740 | ||
2741 | out: | |
2a27f514 SJS |
2742 | block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll); |
2743 | ||
2744 | /* Attribute wait time */ | |
2745 | if (do_sleep) { | |
2746 | vc->runner->stat.halt_wait_ns += | |
2747 | ktime_to_ns(cur) - ktime_to_ns(start_wait); | |
2748 | /* Attribute failed poll time */ | |
2749 | if (vc->halt_poll_ns) | |
2750 | vc->runner->stat.halt_poll_fail_ns += | |
2751 | ktime_to_ns(start_wait) - | |
2752 | ktime_to_ns(start_poll); | |
2753 | } else { | |
2754 | /* Attribute successful poll time */ | |
2755 | if (vc->halt_poll_ns) | |
2756 | vc->runner->stat.halt_poll_success_ns += | |
2757 | ktime_to_ns(cur) - | |
2758 | ktime_to_ns(start_poll); | |
2759 | } | |
0cda69dd SJS |
2760 | |
2761 | /* Adjust poll time */ | |
307d93e4 | 2762 | if (halt_poll_ns) { |
0cda69dd SJS |
2763 | if (block_ns <= vc->halt_poll_ns) |
2764 | ; | |
2765 | /* We slept and blocked for longer than the max halt time */ | |
307d93e4 | 2766 | else if (vc->halt_poll_ns && block_ns > halt_poll_ns) |
0cda69dd SJS |
2767 | shrink_halt_poll_ns(vc); |
2768 | /* We slept and our poll time is too small */ | |
307d93e4 SJS |
2769 | else if (vc->halt_poll_ns < halt_poll_ns && |
2770 | block_ns < halt_poll_ns) | |
0cda69dd | 2771 | grow_halt_poll_ns(vc); |
e03f3921 SJS |
2772 | if (vc->halt_poll_ns > halt_poll_ns) |
2773 | vc->halt_poll_ns = halt_poll_ns; | |
0cda69dd SJS |
2774 | } else |
2775 | vc->halt_poll_ns = 0; | |
2776 | ||
2777 | trace_kvmppc_vcore_wakeup(do_sleep, block_ns); | |
19ccb76a | 2778 | } |
371fefd6 | 2779 | |
19ccb76a PM |
2780 | static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
2781 | { | |
7b5f8272 | 2782 | int n_ceded, i; |
19ccb76a | 2783 | struct kvmppc_vcore *vc; |
7b5f8272 | 2784 | struct kvm_vcpu *v; |
9e368f29 | 2785 | |
3c78f78a SW |
2786 | trace_kvmppc_run_vcpu_enter(vcpu); |
2787 | ||
371fefd6 PM |
2788 | kvm_run->exit_reason = 0; |
2789 | vcpu->arch.ret = RESUME_GUEST; | |
2790 | vcpu->arch.trap = 0; | |
2f12f034 | 2791 | kvmppc_update_vpas(vcpu); |
371fefd6 | 2792 | |
371fefd6 PM |
2793 | /* |
2794 | * Synchronize with other threads in this virtual core | |
2795 | */ | |
2796 | vc = vcpu->arch.vcore; | |
2797 | spin_lock(&vc->lock); | |
19ccb76a | 2798 | vcpu->arch.ceded = 0; |
371fefd6 PM |
2799 | vcpu->arch.run_task = current; |
2800 | vcpu->arch.kvm_run = kvm_run; | |
c7b67670 | 2801 | vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); |
19ccb76a | 2802 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; |
c7b67670 | 2803 | vcpu->arch.busy_preempt = TB_NIL; |
7b5f8272 | 2804 | WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu); |
371fefd6 PM |
2805 | ++vc->n_runnable; |
2806 | ||
19ccb76a PM |
2807 | /* |
2808 | * This happens the first time this is called for a vcpu. | |
2809 | * If the vcore is already running, we may be able to start | |
2810 | * this thread straight away and have it join in. | |
2811 | */ | |
8455d79e | 2812 | if (!signal_pending(current)) { |
ec257165 PM |
2813 | if (vc->vcore_state == VCORE_PIGGYBACK) { |
2814 | struct kvmppc_vcore *mvc = vc->master_vcore; | |
2815 | if (spin_trylock(&mvc->lock)) { | |
2816 | if (mvc->vcore_state == VCORE_RUNNING && | |
2817 | !VCORE_IS_EXITING(mvc)) { | |
2818 | kvmppc_create_dtl_entry(vcpu, vc); | |
b4deba5c | 2819 | kvmppc_start_thread(vcpu, vc); |
ec257165 PM |
2820 | trace_kvm_guest_enter(vcpu); |
2821 | } | |
2822 | spin_unlock(&mvc->lock); | |
2823 | } | |
2824 | } else if (vc->vcore_state == VCORE_RUNNING && | |
2825 | !VCORE_IS_EXITING(vc)) { | |
2f12f034 | 2826 | kvmppc_create_dtl_entry(vcpu, vc); |
b4deba5c | 2827 | kvmppc_start_thread(vcpu, vc); |
3c78f78a | 2828 | trace_kvm_guest_enter(vcpu); |
8455d79e | 2829 | } else if (vc->vcore_state == VCORE_SLEEPING) { |
8577370f | 2830 | swake_up(&vc->wq); |
371fefd6 PM |
2831 | } |
2832 | ||
8455d79e | 2833 | } |
371fefd6 | 2834 | |
19ccb76a PM |
2835 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
2836 | !signal_pending(current)) { | |
ec257165 PM |
2837 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
2838 | kvmppc_vcore_end_preempt(vc); | |
2839 | ||
8455d79e | 2840 | if (vc->vcore_state != VCORE_INACTIVE) { |
ec257165 | 2841 | kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE); |
19ccb76a PM |
2842 | continue; |
2843 | } | |
7b5f8272 | 2844 | for_each_runnable_thread(i, v, vc) { |
7e28e60e | 2845 | kvmppc_core_prepare_to_enter(v); |
19ccb76a PM |
2846 | if (signal_pending(v->arch.run_task)) { |
2847 | kvmppc_remove_runnable(vc, v); | |
2848 | v->stat.signal_exits++; | |
2849 | v->arch.kvm_run->exit_reason = KVM_EXIT_INTR; | |
2850 | v->arch.ret = -EINTR; | |
2851 | wake_up(&v->arch.cpu_run); | |
2852 | } | |
2853 | } | |
8455d79e PM |
2854 | if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
2855 | break; | |
8455d79e | 2856 | n_ceded = 0; |
7b5f8272 | 2857 | for_each_runnable_thread(i, v, vc) { |
8464c884 | 2858 | if (!v->arch.pending_exceptions && !v->arch.prodded) |
8455d79e | 2859 | n_ceded += v->arch.ceded; |
4619ac88 PM |
2860 | else |
2861 | v->arch.ceded = 0; | |
2862 | } | |
25fedfca PM |
2863 | vc->runner = vcpu; |
2864 | if (n_ceded == vc->n_runnable) { | |
8455d79e | 2865 | kvmppc_vcore_blocked(vc); |
c56dadf3 | 2866 | } else if (need_resched()) { |
ec257165 | 2867 | kvmppc_vcore_preempt(vc); |
25fedfca PM |
2868 | /* Let something else run */ |
2869 | cond_resched_lock(&vc->lock); | |
ec257165 PM |
2870 | if (vc->vcore_state == VCORE_PREEMPT) |
2871 | kvmppc_vcore_end_preempt(vc); | |
25fedfca | 2872 | } else { |
8455d79e | 2873 | kvmppc_run_core(vc); |
25fedfca | 2874 | } |
0456ec4f | 2875 | vc->runner = NULL; |
19ccb76a | 2876 | } |
371fefd6 | 2877 | |
8455d79e PM |
2878 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
2879 | (vc->vcore_state == VCORE_RUNNING || | |
5fc3e64f PM |
2880 | vc->vcore_state == VCORE_EXITING || |
2881 | vc->vcore_state == VCORE_PIGGYBACK)) | |
ec257165 | 2882 | kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE); |
8455d79e | 2883 | |
5fc3e64f PM |
2884 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
2885 | kvmppc_vcore_end_preempt(vc); | |
2886 | ||
8455d79e PM |
2887 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
2888 | kvmppc_remove_runnable(vc, vcpu); | |
2889 | vcpu->stat.signal_exits++; | |
2890 | kvm_run->exit_reason = KVM_EXIT_INTR; | |
2891 | vcpu->arch.ret = -EINTR; | |
2892 | } | |
2893 | ||
2894 | if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) { | |
2895 | /* Wake up some vcpu to run the core */ | |
7b5f8272 SJS |
2896 | i = -1; |
2897 | v = next_runnable_thread(vc, &i); | |
8455d79e | 2898 | wake_up(&v->arch.cpu_run); |
371fefd6 PM |
2899 | } |
2900 | ||
3c78f78a | 2901 | trace_kvmppc_run_vcpu_exit(vcpu, kvm_run); |
371fefd6 | 2902 | spin_unlock(&vc->lock); |
371fefd6 | 2903 | return vcpu->arch.ret; |
de56a948 PM |
2904 | } |
2905 | ||
3a167bea | 2906 | static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu) |
a8606e20 PM |
2907 | { |
2908 | int r; | |
913d3ff9 | 2909 | int srcu_idx; |
ca8efa1d | 2910 | unsigned long ebb_regs[3] = {}; /* shut up GCC */ |
4c3bb4cc PM |
2911 | unsigned long user_tar = 0; |
2912 | unsigned int user_vrsave; | |
a8606e20 | 2913 | |
af8f38b3 AG |
2914 | if (!vcpu->arch.sane) { |
2915 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; | |
2916 | return -EINVAL; | |
2917 | } | |
2918 | ||
46a704f8 PM |
2919 | /* |
2920 | * Don't allow entry with a suspended transaction, because | |
2921 | * the guest entry/exit code will lose it. | |
2922 | * If the guest has TM enabled, save away their TM-related SPRs | |
2923 | * (they will get restored by the TM unavailable interrupt). | |
2924 | */ | |
2925 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM | |
2926 | if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs && | |
2927 | (current->thread.regs->msr & MSR_TM)) { | |
2928 | if (MSR_TM_ACTIVE(current->thread.regs->msr)) { | |
2929 | run->exit_reason = KVM_EXIT_FAIL_ENTRY; | |
2930 | run->fail_entry.hardware_entry_failure_reason = 0; | |
2931 | return -EINVAL; | |
2932 | } | |
2933 | current->thread.tm_tfhar = mfspr(SPRN_TFHAR); | |
2934 | current->thread.tm_tfiar = mfspr(SPRN_TFIAR); | |
2935 | current->thread.tm_texasr = mfspr(SPRN_TEXASR); | |
2936 | current->thread.regs->msr &= ~MSR_TM; | |
2937 | } | |
2938 | #endif | |
2939 | ||
25051b5a SW |
2940 | kvmppc_core_prepare_to_enter(vcpu); |
2941 | ||
19ccb76a PM |
2942 | /* No need to go into the guest when all we'll do is come back out */ |
2943 | if (signal_pending(current)) { | |
2944 | run->exit_reason = KVM_EXIT_INTR; | |
2945 | return -EINTR; | |
2946 | } | |
2947 | ||
32fad281 | 2948 | atomic_inc(&vcpu->kvm->arch.vcpus_running); |
31037eca | 2949 | /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */ |
32fad281 PM |
2950 | smp_mb(); |
2951 | ||
c17b98cf | 2952 | /* On the first time here, set up HTAB and VRMA */ |
8cf4ecc0 | 2953 | if (!kvm_is_radix(vcpu->kvm) && !vcpu->kvm->arch.hpte_setup_done) { |
32fad281 | 2954 | r = kvmppc_hv_setup_htab_rma(vcpu); |
c77162de | 2955 | if (r) |
32fad281 | 2956 | goto out; |
c77162de | 2957 | } |
19ccb76a | 2958 | |
579e633e AB |
2959 | flush_all_to_thread(current); |
2960 | ||
4c3bb4cc | 2961 | /* Save userspace EBB and other register values */ |
ca8efa1d PM |
2962 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
2963 | ebb_regs[0] = mfspr(SPRN_EBBHR); | |
2964 | ebb_regs[1] = mfspr(SPRN_EBBRR); | |
2965 | ebb_regs[2] = mfspr(SPRN_BESCR); | |
4c3bb4cc | 2966 | user_tar = mfspr(SPRN_TAR); |
ca8efa1d | 2967 | } |
4c3bb4cc | 2968 | user_vrsave = mfspr(SPRN_VRSAVE); |
ca8efa1d | 2969 | |
19ccb76a | 2970 | vcpu->arch.wqp = &vcpu->arch.vcore->wq; |
342d3db7 | 2971 | vcpu->arch.pgdir = current->mm->pgd; |
c7b67670 | 2972 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
19ccb76a | 2973 | |
a8606e20 PM |
2974 | do { |
2975 | r = kvmppc_run_vcpu(run, vcpu); | |
2976 | ||
2977 | if (run->exit_reason == KVM_EXIT_PAPR_HCALL && | |
2978 | !(vcpu->arch.shregs.msr & MSR_PR)) { | |
3c78f78a | 2979 | trace_kvm_hcall_enter(vcpu); |
a8606e20 | 2980 | r = kvmppc_pseries_do_hcall(vcpu); |
3c78f78a | 2981 | trace_kvm_hcall_exit(vcpu, r); |
7e28e60e | 2982 | kvmppc_core_prepare_to_enter(vcpu); |
913d3ff9 PM |
2983 | } else if (r == RESUME_PAGE_FAULT) { |
2984 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); | |
2985 | r = kvmppc_book3s_hv_page_fault(run, vcpu, | |
2986 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); | |
2987 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); | |
5af50993 BH |
2988 | } else if (r == RESUME_PASSTHROUGH) { |
2989 | if (WARN_ON(xive_enabled())) | |
2990 | r = H_SUCCESS; | |
2991 | else | |
2992 | r = kvmppc_xics_rm_complete(vcpu, 0); | |
2993 | } | |
e59d24e6 | 2994 | } while (is_kvmppc_resume_guest(r)); |
32fad281 | 2995 | |
4c3bb4cc | 2996 | /* Restore userspace EBB and other register values */ |
ca8efa1d PM |
2997 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
2998 | mtspr(SPRN_EBBHR, ebb_regs[0]); | |
2999 | mtspr(SPRN_EBBRR, ebb_regs[1]); | |
3000 | mtspr(SPRN_BESCR, ebb_regs[2]); | |
4c3bb4cc PM |
3001 | mtspr(SPRN_TAR, user_tar); |
3002 | mtspr(SPRN_FSCR, current->thread.fscr); | |
ca8efa1d | 3003 | } |
4c3bb4cc | 3004 | mtspr(SPRN_VRSAVE, user_vrsave); |
ca8efa1d | 3005 | |
32fad281 | 3006 | out: |
c7b67670 | 3007 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
32fad281 | 3008 | atomic_dec(&vcpu->kvm->arch.vcpus_running); |
a8606e20 PM |
3009 | return r; |
3010 | } | |
3011 | ||
5b74716e BH |
3012 | static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps, |
3013 | int linux_psize) | |
3014 | { | |
3015 | struct mmu_psize_def *def = &mmu_psize_defs[linux_psize]; | |
3016 | ||
3017 | if (!def->shift) | |
3018 | return; | |
3019 | (*sps)->page_shift = def->shift; | |
3020 | (*sps)->slb_enc = def->sllp; | |
3021 | (*sps)->enc[0].page_shift = def->shift; | |
b1022fbd | 3022 | (*sps)->enc[0].pte_enc = def->penc[linux_psize]; |
1f365bb0 AK |
3023 | /* |
3024 | * Add 16MB MPSS support if host supports it | |
3025 | */ | |
3026 | if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) { | |
3027 | (*sps)->enc[1].page_shift = 24; | |
3028 | (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M]; | |
3029 | } | |
5b74716e BH |
3030 | (*sps)++; |
3031 | } | |
3032 | ||
3a167bea AK |
3033 | static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, |
3034 | struct kvm_ppc_smmu_info *info) | |
5b74716e BH |
3035 | { |
3036 | struct kvm_ppc_one_seg_page_size *sps; | |
3037 | ||
8cf4ecc0 PM |
3038 | /* |
3039 | * Since we don't yet support HPT guests on a radix host, | |
3040 | * return an error if the host uses radix. | |
3041 | */ | |
3042 | if (radix_enabled()) | |
3043 | return -EINVAL; | |
3044 | ||
5b74716e BH |
3045 | info->flags = KVM_PPC_PAGE_SIZES_REAL; |
3046 | if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) | |
3047 | info->flags |= KVM_PPC_1T_SEGMENTS; | |
3048 | info->slb_size = mmu_slb_size; | |
3049 | ||
3050 | /* We only support these sizes for now, and no muti-size segments */ | |
3051 | sps = &info->sps[0]; | |
3052 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K); | |
3053 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K); | |
3054 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M); | |
3055 | ||
3056 | return 0; | |
3057 | } | |
3058 | ||
82ed3616 PM |
3059 | /* |
3060 | * Get (and clear) the dirty memory log for a memory slot. | |
3061 | */ | |
3a167bea AK |
3062 | static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm, |
3063 | struct kvm_dirty_log *log) | |
82ed3616 | 3064 | { |
9f6b8029 | 3065 | struct kvm_memslots *slots; |
82ed3616 | 3066 | struct kvm_memory_slot *memslot; |
8f7b79b8 | 3067 | int i, r; |
82ed3616 | 3068 | unsigned long n; |
8f7b79b8 PM |
3069 | unsigned long *buf; |
3070 | struct kvm_vcpu *vcpu; | |
82ed3616 PM |
3071 | |
3072 | mutex_lock(&kvm->slots_lock); | |
3073 | ||
3074 | r = -EINVAL; | |
bbacc0c1 | 3075 | if (log->slot >= KVM_USER_MEM_SLOTS) |
82ed3616 PM |
3076 | goto out; |
3077 | ||
9f6b8029 PB |
3078 | slots = kvm_memslots(kvm); |
3079 | memslot = id_to_memslot(slots, log->slot); | |
82ed3616 PM |
3080 | r = -ENOENT; |
3081 | if (!memslot->dirty_bitmap) | |
3082 | goto out; | |
3083 | ||
8f7b79b8 PM |
3084 | /* |
3085 | * Use second half of bitmap area because radix accumulates | |
3086 | * bits in the first half. | |
3087 | */ | |
82ed3616 | 3088 | n = kvm_dirty_bitmap_bytes(memslot); |
8f7b79b8 PM |
3089 | buf = memslot->dirty_bitmap + n / sizeof(long); |
3090 | memset(buf, 0, n); | |
82ed3616 | 3091 | |
8f7b79b8 PM |
3092 | if (kvm_is_radix(kvm)) |
3093 | r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf); | |
3094 | else | |
3095 | r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf); | |
82ed3616 PM |
3096 | if (r) |
3097 | goto out; | |
3098 | ||
8f7b79b8 PM |
3099 | /* Harvest dirty bits from VPA and DTL updates */ |
3100 | /* Note: we never modify the SLB shadow buffer areas */ | |
3101 | kvm_for_each_vcpu(i, vcpu, kvm) { | |
3102 | spin_lock(&vcpu->arch.vpa_update_lock); | |
3103 | kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf); | |
3104 | kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf); | |
3105 | spin_unlock(&vcpu->arch.vpa_update_lock); | |
3106 | } | |
3107 | ||
82ed3616 | 3108 | r = -EFAULT; |
8f7b79b8 | 3109 | if (copy_to_user(log->dirty_bitmap, buf, n)) |
82ed3616 PM |
3110 | goto out; |
3111 | ||
3112 | r = 0; | |
3113 | out: | |
3114 | mutex_unlock(&kvm->slots_lock); | |
3115 | return r; | |
3116 | } | |
3117 | ||
3a167bea AK |
3118 | static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, |
3119 | struct kvm_memory_slot *dont) | |
a66b48c3 PM |
3120 | { |
3121 | if (!dont || free->arch.rmap != dont->arch.rmap) { | |
3122 | vfree(free->arch.rmap); | |
3123 | free->arch.rmap = NULL; | |
b2b2f165 | 3124 | } |
a66b48c3 PM |
3125 | } |
3126 | ||
3a167bea AK |
3127 | static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot, |
3128 | unsigned long npages) | |
a66b48c3 | 3129 | { |
8cf4ecc0 PM |
3130 | /* |
3131 | * For now, if radix_enabled() then we only support radix guests, | |
3132 | * and in that case we don't need the rmap array. | |
3133 | */ | |
3134 | if (radix_enabled()) { | |
3135 | slot->arch.rmap = NULL; | |
3136 | return 0; | |
3137 | } | |
3138 | ||
a66b48c3 PM |
3139 | slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap)); |
3140 | if (!slot->arch.rmap) | |
3141 | return -ENOMEM; | |
aa04b4cc | 3142 | |
c77162de PM |
3143 | return 0; |
3144 | } | |
aa04b4cc | 3145 | |
3a167bea AK |
3146 | static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, |
3147 | struct kvm_memory_slot *memslot, | |
09170a49 | 3148 | const struct kvm_userspace_memory_region *mem) |
c77162de | 3149 | { |
a66b48c3 | 3150 | return 0; |
c77162de PM |
3151 | } |
3152 | ||
3a167bea | 3153 | static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, |
09170a49 | 3154 | const struct kvm_userspace_memory_region *mem, |
f36f3f28 PB |
3155 | const struct kvm_memory_slot *old, |
3156 | const struct kvm_memory_slot *new) | |
c77162de | 3157 | { |
dfe49dbd | 3158 | unsigned long npages = mem->memory_size >> PAGE_SHIFT; |
9f6b8029 | 3159 | struct kvm_memslots *slots; |
dfe49dbd PM |
3160 | struct kvm_memory_slot *memslot; |
3161 | ||
a56ee9f8 YX |
3162 | /* |
3163 | * If we are making a new memslot, it might make | |
3164 | * some address that was previously cached as emulated | |
3165 | * MMIO be no longer emulated MMIO, so invalidate | |
3166 | * all the caches of emulated MMIO translations. | |
3167 | */ | |
3168 | if (npages) | |
3169 | atomic64_inc(&kvm->arch.mmio_update); | |
3170 | ||
8f7b79b8 | 3171 | if (npages && old->npages && !kvm_is_radix(kvm)) { |
dfe49dbd PM |
3172 | /* |
3173 | * If modifying a memslot, reset all the rmap dirty bits. | |
3174 | * If this is a new memslot, we don't need to do anything | |
3175 | * since the rmap array starts out as all zeroes, | |
3176 | * i.e. no pages are dirty. | |
3177 | */ | |
9f6b8029 PB |
3178 | slots = kvm_memslots(kvm); |
3179 | memslot = id_to_memslot(slots, mem->slot); | |
8f7b79b8 | 3180 | kvmppc_hv_get_dirty_log_hpt(kvm, memslot, NULL); |
dfe49dbd | 3181 | } |
c77162de PM |
3182 | } |
3183 | ||
a0144e2a PM |
3184 | /* |
3185 | * Update LPCR values in kvm->arch and in vcores. | |
3186 | * Caller must hold kvm->lock. | |
3187 | */ | |
3188 | void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask) | |
3189 | { | |
3190 | long int i; | |
3191 | u32 cores_done = 0; | |
3192 | ||
3193 | if ((kvm->arch.lpcr & mask) == lpcr) | |
3194 | return; | |
3195 | ||
3196 | kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr; | |
3197 | ||
3198 | for (i = 0; i < KVM_MAX_VCORES; ++i) { | |
3199 | struct kvmppc_vcore *vc = kvm->arch.vcores[i]; | |
3200 | if (!vc) | |
3201 | continue; | |
3202 | spin_lock(&vc->lock); | |
3203 | vc->lpcr = (vc->lpcr & ~mask) | lpcr; | |
3204 | spin_unlock(&vc->lock); | |
3205 | if (++cores_done >= kvm->arch.online_vcores) | |
3206 | break; | |
3207 | } | |
3208 | } | |
3209 | ||
3a167bea AK |
3210 | static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu) |
3211 | { | |
3212 | return; | |
3213 | } | |
3214 | ||
7a84084c PM |
3215 | static void kvmppc_setup_partition_table(struct kvm *kvm) |
3216 | { | |
3217 | unsigned long dw0, dw1; | |
3218 | ||
8cf4ecc0 PM |
3219 | if (!kvm_is_radix(kvm)) { |
3220 | /* PS field - page size for VRMA */ | |
3221 | dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) | | |
3222 | ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1); | |
3223 | /* HTABSIZE and HTABORG fields */ | |
3224 | dw0 |= kvm->arch.sdr1; | |
7a84084c | 3225 | |
8cf4ecc0 PM |
3226 | /* Second dword as set by userspace */ |
3227 | dw1 = kvm->arch.process_table; | |
3228 | } else { | |
3229 | dw0 = PATB_HR | radix__get_tree_size() | | |
3230 | __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE; | |
3231 | dw1 = PATB_GR | kvm->arch.process_table; | |
3232 | } | |
7a84084c PM |
3233 | |
3234 | mmu_partition_table_set_entry(kvm->arch.lpid, dw0, dw1); | |
3235 | } | |
3236 | ||
32fad281 | 3237 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) |
c77162de PM |
3238 | { |
3239 | int err = 0; | |
3240 | struct kvm *kvm = vcpu->kvm; | |
c77162de PM |
3241 | unsigned long hva; |
3242 | struct kvm_memory_slot *memslot; | |
3243 | struct vm_area_struct *vma; | |
a0144e2a | 3244 | unsigned long lpcr = 0, senc; |
c77162de | 3245 | unsigned long psize, porder; |
2c9097e4 | 3246 | int srcu_idx; |
c77162de PM |
3247 | |
3248 | mutex_lock(&kvm->lock); | |
31037eca | 3249 | if (kvm->arch.hpte_setup_done) |
c77162de | 3250 | goto out; /* another vcpu beat us to it */ |
aa04b4cc | 3251 | |
32fad281 | 3252 | /* Allocate hashed page table (if not done already) and reset it */ |
3f9d4f5a | 3253 | if (!kvm->arch.hpt.virt) { |
aae0777f DG |
3254 | int order = KVM_DEFAULT_HPT_ORDER; |
3255 | struct kvm_hpt_info info; | |
3256 | ||
3257 | err = kvmppc_allocate_hpt(&info, order); | |
3258 | /* If we get here, it means userspace didn't specify a | |
3259 | * size explicitly. So, try successively smaller | |
3260 | * sizes if the default failed. */ | |
3261 | while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER) | |
3262 | err = kvmppc_allocate_hpt(&info, order); | |
3263 | ||
3264 | if (err < 0) { | |
32fad281 PM |
3265 | pr_err("KVM: Couldn't alloc HPT\n"); |
3266 | goto out; | |
3267 | } | |
aae0777f DG |
3268 | |
3269 | kvmppc_set_hpt(kvm, &info); | |
32fad281 PM |
3270 | } |
3271 | ||
c77162de | 3272 | /* Look up the memslot for guest physical address 0 */ |
2c9097e4 | 3273 | srcu_idx = srcu_read_lock(&kvm->srcu); |
c77162de | 3274 | memslot = gfn_to_memslot(kvm, 0); |
aa04b4cc | 3275 | |
c77162de PM |
3276 | /* We must have some memory at 0 by now */ |
3277 | err = -EINVAL; | |
3278 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) | |
2c9097e4 | 3279 | goto out_srcu; |
c77162de PM |
3280 | |
3281 | /* Look up the VMA for the start of this memory slot */ | |
3282 | hva = memslot->userspace_addr; | |
3283 | down_read(¤t->mm->mmap_sem); | |
3284 | vma = find_vma(current->mm, hva); | |
3285 | if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO)) | |
3286 | goto up_out; | |
3287 | ||
3288 | psize = vma_kernel_pagesize(vma); | |
da9d1d7f | 3289 | porder = __ilog2(psize); |
c77162de | 3290 | |
c77162de PM |
3291 | up_read(¤t->mm->mmap_sem); |
3292 | ||
c17b98cf PM |
3293 | /* We can handle 4k, 64k or 16M pages in the VRMA */ |
3294 | err = -EINVAL; | |
3295 | if (!(psize == 0x1000 || psize == 0x10000 || | |
3296 | psize == 0x1000000)) | |
3297 | goto out_srcu; | |
c77162de | 3298 | |
c17b98cf PM |
3299 | senc = slb_pgsize_encoding(psize); |
3300 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | | |
3301 | (VRMA_VSID << SLB_VSID_SHIFT_1T); | |
c17b98cf PM |
3302 | /* Create HPTEs in the hash page table for the VRMA */ |
3303 | kvmppc_map_vrma(vcpu, memslot, porder); | |
aa04b4cc | 3304 | |
7a84084c PM |
3305 | /* Update VRMASD field in the LPCR */ |
3306 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) { | |
3307 | /* the -4 is to account for senc values starting at 0x10 */ | |
3308 | lpcr = senc << (LPCR_VRMASD_SH - 4); | |
3309 | kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD); | |
3310 | } else { | |
3311 | kvmppc_setup_partition_table(kvm); | |
3312 | } | |
a0144e2a | 3313 | |
31037eca | 3314 | /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */ |
c77162de | 3315 | smp_wmb(); |
31037eca | 3316 | kvm->arch.hpte_setup_done = 1; |
c77162de | 3317 | err = 0; |
2c9097e4 PM |
3318 | out_srcu: |
3319 | srcu_read_unlock(&kvm->srcu, srcu_idx); | |
c77162de PM |
3320 | out: |
3321 | mutex_unlock(&kvm->lock); | |
3322 | return err; | |
b2b2f165 | 3323 | |
c77162de PM |
3324 | up_out: |
3325 | up_read(¤t->mm->mmap_sem); | |
505d6421 | 3326 | goto out_srcu; |
de56a948 PM |
3327 | } |
3328 | ||
79b6c247 SW |
3329 | #ifdef CONFIG_KVM_XICS |
3330 | /* | |
3331 | * Allocate a per-core structure for managing state about which cores are | |
3332 | * running in the host versus the guest and for exchanging data between | |
3333 | * real mode KVM and CPU running in the host. | |
3334 | * This is only done for the first VM. | |
3335 | * The allocated structure stays even if all VMs have stopped. | |
3336 | * It is only freed when the kvm-hv module is unloaded. | |
3337 | * It's OK for this routine to fail, we just don't support host | |
3338 | * core operations like redirecting H_IPI wakeups. | |
3339 | */ | |
3340 | void kvmppc_alloc_host_rm_ops(void) | |
3341 | { | |
3342 | struct kvmppc_host_rm_ops *ops; | |
3343 | unsigned long l_ops; | |
3344 | int cpu, core; | |
3345 | int size; | |
3346 | ||
3347 | /* Not the first time here ? */ | |
3348 | if (kvmppc_host_rm_ops_hv != NULL) | |
3349 | return; | |
3350 | ||
3351 | ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL); | |
3352 | if (!ops) | |
3353 | return; | |
3354 | ||
3355 | size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core); | |
3356 | ops->rm_core = kzalloc(size, GFP_KERNEL); | |
3357 | ||
3358 | if (!ops->rm_core) { | |
3359 | kfree(ops); | |
3360 | return; | |
3361 | } | |
3362 | ||
6f3bb809 SW |
3363 | get_online_cpus(); |
3364 | ||
79b6c247 SW |
3365 | for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) { |
3366 | if (!cpu_online(cpu)) | |
3367 | continue; | |
3368 | ||
3369 | core = cpu >> threads_shift; | |
3370 | ops->rm_core[core].rm_state.in_host = 1; | |
3371 | } | |
3372 | ||
0c2a6606 SW |
3373 | ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv; |
3374 | ||
79b6c247 SW |
3375 | /* |
3376 | * Make the contents of the kvmppc_host_rm_ops structure visible | |
3377 | * to other CPUs before we assign it to the global variable. | |
3378 | * Do an atomic assignment (no locks used here), but if someone | |
3379 | * beats us to it, just free our copy and return. | |
3380 | */ | |
3381 | smp_wmb(); | |
3382 | l_ops = (unsigned long) ops; | |
3383 | ||
3384 | if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) { | |
6f3bb809 | 3385 | put_online_cpus(); |
79b6c247 SW |
3386 | kfree(ops->rm_core); |
3387 | kfree(ops); | |
6f3bb809 | 3388 | return; |
79b6c247 | 3389 | } |
6f3bb809 | 3390 | |
3f7cd919 AMG |
3391 | cpuhp_setup_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE, |
3392 | "ppc/kvm_book3s:prepare", | |
3393 | kvmppc_set_host_core, | |
3394 | kvmppc_clear_host_core); | |
6f3bb809 | 3395 | put_online_cpus(); |
79b6c247 SW |
3396 | } |
3397 | ||
3398 | void kvmppc_free_host_rm_ops(void) | |
3399 | { | |
3400 | if (kvmppc_host_rm_ops_hv) { | |
3f7cd919 | 3401 | cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE); |
79b6c247 SW |
3402 | kfree(kvmppc_host_rm_ops_hv->rm_core); |
3403 | kfree(kvmppc_host_rm_ops_hv); | |
3404 | kvmppc_host_rm_ops_hv = NULL; | |
3405 | } | |
3406 | } | |
3407 | #endif | |
3408 | ||
3a167bea | 3409 | static int kvmppc_core_init_vm_hv(struct kvm *kvm) |
de56a948 | 3410 | { |
32fad281 | 3411 | unsigned long lpcr, lpid; |
e23a808b | 3412 | char buf[32]; |
8cf4ecc0 | 3413 | int ret; |
de56a948 | 3414 | |
32fad281 PM |
3415 | /* Allocate the guest's logical partition ID */ |
3416 | ||
3417 | lpid = kvmppc_alloc_lpid(); | |
5d226ae5 | 3418 | if ((long)lpid < 0) |
32fad281 PM |
3419 | return -ENOMEM; |
3420 | kvm->arch.lpid = lpid; | |
de56a948 | 3421 | |
79b6c247 SW |
3422 | kvmppc_alloc_host_rm_ops(); |
3423 | ||
1b400ba0 PM |
3424 | /* |
3425 | * Since we don't flush the TLB when tearing down a VM, | |
3426 | * and this lpid might have previously been used, | |
3427 | * make sure we flush on each core before running the new VM. | |
7c5b06ca PM |
3428 | * On POWER9, the tlbie in mmu_partition_table_set_entry() |
3429 | * does this flush for us. | |
1b400ba0 | 3430 | */ |
7c5b06ca PM |
3431 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
3432 | cpumask_setall(&kvm->arch.need_tlb_flush); | |
1b400ba0 | 3433 | |
699a0ea0 PM |
3434 | /* Start out with the default set of hcalls enabled */ |
3435 | memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls, | |
3436 | sizeof(kvm->arch.enabled_hcalls)); | |
3437 | ||
7a84084c PM |
3438 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
3439 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); | |
aa04b4cc | 3440 | |
c17b98cf PM |
3441 | /* Init LPCR for virtual RMA mode */ |
3442 | kvm->arch.host_lpid = mfspr(SPRN_LPID); | |
3443 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); | |
3444 | lpcr &= LPCR_PECE | LPCR_LPES; | |
3445 | lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | | |
3446 | LPCR_VPM0 | LPCR_VPM1; | |
3447 | kvm->arch.vrma_slb_v = SLB_VSID_B_1T | | |
3448 | (VRMA_VSID << SLB_VSID_SHIFT_1T); | |
3449 | /* On POWER8 turn on online bit to enable PURR/SPURR */ | |
3450 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) | |
3451 | lpcr |= LPCR_ONL; | |
84f7139c PM |
3452 | /* |
3453 | * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed) | |
3454 | * Set HVICE bit to enable hypervisor virtualization interrupts. | |
5af50993 BH |
3455 | * Set HEIC to prevent OS interrupts to go to hypervisor (should |
3456 | * be unnecessary but better safe than sorry in case we re-enable | |
3457 | * EE in HV mode with this LPCR still set) | |
84f7139c PM |
3458 | */ |
3459 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { | |
7a84084c | 3460 | lpcr &= ~LPCR_VPM0; |
5af50993 BH |
3461 | lpcr |= LPCR_HVICE | LPCR_HEIC; |
3462 | ||
3463 | /* | |
3464 | * If xive is enabled, we route 0x500 interrupts directly | |
3465 | * to the guest. | |
3466 | */ | |
3467 | if (xive_enabled()) | |
3468 | lpcr |= LPCR_LPES; | |
84f7139c PM |
3469 | } |
3470 | ||
8cf4ecc0 PM |
3471 | /* |
3472 | * For now, if the host uses radix, the guest must be radix. | |
3473 | */ | |
3474 | if (radix_enabled()) { | |
3475 | kvm->arch.radix = 1; | |
3476 | lpcr &= ~LPCR_VPM1; | |
3477 | lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR; | |
3478 | ret = kvmppc_init_vm_radix(kvm); | |
3479 | if (ret) { | |
3480 | kvmppc_free_lpid(kvm->arch.lpid); | |
3481 | return ret; | |
3482 | } | |
3483 | kvmppc_setup_partition_table(kvm); | |
3484 | } | |
3485 | ||
9e368f29 | 3486 | kvm->arch.lpcr = lpcr; |
aa04b4cc | 3487 | |
5e985969 DG |
3488 | /* Initialization for future HPT resizes */ |
3489 | kvm->arch.resize_hpt = NULL; | |
3490 | ||
7c5b06ca PM |
3491 | /* |
3492 | * Work out how many sets the TLB has, for the use of | |
3493 | * the TLB invalidation loop in book3s_hv_rmhandlers.S. | |
3494 | */ | |
8cf4ecc0 PM |
3495 | if (kvm_is_radix(kvm)) |
3496 | kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */ | |
3497 | else if (cpu_has_feature(CPU_FTR_ARCH_300)) | |
7c5b06ca PM |
3498 | kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */ |
3499 | else if (cpu_has_feature(CPU_FTR_ARCH_207S)) | |
3500 | kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */ | |
3501 | else | |
3502 | kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */ | |
3503 | ||
512691d4 | 3504 | /* |
441c19c8 ME |
3505 | * Track that we now have a HV mode VM active. This blocks secondary |
3506 | * CPU threads from coming online. | |
8cf4ecc0 PM |
3507 | * On POWER9, we only need to do this for HPT guests on a radix |
3508 | * host, which is not yet supported. | |
512691d4 | 3509 | */ |
8cf4ecc0 PM |
3510 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
3511 | kvm_hv_vm_activated(); | |
512691d4 | 3512 | |
e23a808b PM |
3513 | /* |
3514 | * Create a debugfs directory for the VM | |
3515 | */ | |
3516 | snprintf(buf, sizeof(buf), "vm%d", current->pid); | |
3517 | kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir); | |
3518 | if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir)) | |
3519 | kvmppc_mmu_debugfs_init(kvm); | |
3520 | ||
54738c09 | 3521 | return 0; |
de56a948 PM |
3522 | } |
3523 | ||
f1378b1c PM |
3524 | static void kvmppc_free_vcores(struct kvm *kvm) |
3525 | { | |
3526 | long int i; | |
3527 | ||
23316316 | 3528 | for (i = 0; i < KVM_MAX_VCORES; ++i) |
f1378b1c PM |
3529 | kfree(kvm->arch.vcores[i]); |
3530 | kvm->arch.online_vcores = 0; | |
3531 | } | |
3532 | ||
3a167bea | 3533 | static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) |
de56a948 | 3534 | { |
e23a808b PM |
3535 | debugfs_remove_recursive(kvm->arch.debugfs_dir); |
3536 | ||
8cf4ecc0 PM |
3537 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
3538 | kvm_hv_vm_deactivated(); | |
512691d4 | 3539 | |
f1378b1c | 3540 | kvmppc_free_vcores(kvm); |
aa04b4cc | 3541 | |
8cf4ecc0 PM |
3542 | kvmppc_free_lpid(kvm->arch.lpid); |
3543 | ||
5a319350 PM |
3544 | if (kvm_is_radix(kvm)) |
3545 | kvmppc_free_radix(kvm); | |
3546 | else | |
aae0777f | 3547 | kvmppc_free_hpt(&kvm->arch.hpt); |
c57875f5 SW |
3548 | |
3549 | kvmppc_free_pimap(kvm); | |
de56a948 PM |
3550 | } |
3551 | ||
3a167bea AK |
3552 | /* We don't need to emulate any privileged instructions or dcbz */ |
3553 | static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, | |
3554 | unsigned int inst, int *advance) | |
de56a948 | 3555 | { |
3a167bea | 3556 | return EMULATE_FAIL; |
de56a948 PM |
3557 | } |
3558 | ||
3a167bea AK |
3559 | static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn, |
3560 | ulong spr_val) | |
de56a948 PM |
3561 | { |
3562 | return EMULATE_FAIL; | |
3563 | } | |
3564 | ||
3a167bea AK |
3565 | static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, |
3566 | ulong *spr_val) | |
de56a948 PM |
3567 | { |
3568 | return EMULATE_FAIL; | |
3569 | } | |
3570 | ||
3a167bea | 3571 | static int kvmppc_core_check_processor_compat_hv(void) |
de56a948 | 3572 | { |
c17b98cf PM |
3573 | if (!cpu_has_feature(CPU_FTR_HVMODE) || |
3574 | !cpu_has_feature(CPU_FTR_ARCH_206)) | |
3a167bea | 3575 | return -EIO; |
50de596d | 3576 | |
3a167bea | 3577 | return 0; |
de56a948 PM |
3578 | } |
3579 | ||
8daaafc8 SW |
3580 | #ifdef CONFIG_KVM_XICS |
3581 | ||
3582 | void kvmppc_free_pimap(struct kvm *kvm) | |
3583 | { | |
3584 | kfree(kvm->arch.pimap); | |
3585 | } | |
3586 | ||
c57875f5 | 3587 | static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void) |
8daaafc8 SW |
3588 | { |
3589 | return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL); | |
3590 | } | |
c57875f5 SW |
3591 | |
3592 | static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi) | |
3593 | { | |
3594 | struct irq_desc *desc; | |
3595 | struct kvmppc_irq_map *irq_map; | |
3596 | struct kvmppc_passthru_irqmap *pimap; | |
3597 | struct irq_chip *chip; | |
5af50993 | 3598 | int i, rc = 0; |
c57875f5 | 3599 | |
644abbb2 SW |
3600 | if (!kvm_irq_bypass) |
3601 | return 1; | |
3602 | ||
c57875f5 SW |
3603 | desc = irq_to_desc(host_irq); |
3604 | if (!desc) | |
3605 | return -EIO; | |
3606 | ||
3607 | mutex_lock(&kvm->lock); | |
3608 | ||
3609 | pimap = kvm->arch.pimap; | |
3610 | if (pimap == NULL) { | |
3611 | /* First call, allocate structure to hold IRQ map */ | |
3612 | pimap = kvmppc_alloc_pimap(); | |
3613 | if (pimap == NULL) { | |
3614 | mutex_unlock(&kvm->lock); | |
3615 | return -ENOMEM; | |
3616 | } | |
3617 | kvm->arch.pimap = pimap; | |
3618 | } | |
3619 | ||
3620 | /* | |
3621 | * For now, we only support interrupts for which the EOI operation | |
3622 | * is an OPAL call followed by a write to XIRR, since that's | |
5af50993 | 3623 | * what our real-mode EOI code does, or a XIVE interrupt |
c57875f5 SW |
3624 | */ |
3625 | chip = irq_data_get_irq_chip(&desc->irq_data); | |
5af50993 | 3626 | if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) { |
c57875f5 SW |
3627 | pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n", |
3628 | host_irq, guest_gsi); | |
3629 | mutex_unlock(&kvm->lock); | |
3630 | return -ENOENT; | |
3631 | } | |
3632 | ||
3633 | /* | |
3634 | * See if we already have an entry for this guest IRQ number. | |
3635 | * If it's mapped to a hardware IRQ number, that's an error, | |
3636 | * otherwise re-use this entry. | |
3637 | */ | |
3638 | for (i = 0; i < pimap->n_mapped; i++) { | |
3639 | if (guest_gsi == pimap->mapped[i].v_hwirq) { | |
3640 | if (pimap->mapped[i].r_hwirq) { | |
3641 | mutex_unlock(&kvm->lock); | |
3642 | return -EINVAL; | |
3643 | } | |
3644 | break; | |
3645 | } | |
3646 | } | |
3647 | ||
3648 | if (i == KVMPPC_PIRQ_MAPPED) { | |
3649 | mutex_unlock(&kvm->lock); | |
3650 | return -EAGAIN; /* table is full */ | |
3651 | } | |
3652 | ||
3653 | irq_map = &pimap->mapped[i]; | |
3654 | ||
3655 | irq_map->v_hwirq = guest_gsi; | |
c57875f5 SW |
3656 | irq_map->desc = desc; |
3657 | ||
e3c13e56 SW |
3658 | /* |
3659 | * Order the above two stores before the next to serialize with | |
3660 | * the KVM real mode handler. | |
3661 | */ | |
3662 | smp_wmb(); | |
3663 | irq_map->r_hwirq = desc->irq_data.hwirq; | |
3664 | ||
c57875f5 SW |
3665 | if (i == pimap->n_mapped) |
3666 | pimap->n_mapped++; | |
3667 | ||
5af50993 BH |
3668 | if (xive_enabled()) |
3669 | rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc); | |
3670 | else | |
3671 | kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq); | |
3672 | if (rc) | |
3673 | irq_map->r_hwirq = 0; | |
5d375199 | 3674 | |
c57875f5 SW |
3675 | mutex_unlock(&kvm->lock); |
3676 | ||
3677 | return 0; | |
3678 | } | |
3679 | ||
3680 | static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi) | |
3681 | { | |
3682 | struct irq_desc *desc; | |
3683 | struct kvmppc_passthru_irqmap *pimap; | |
5af50993 | 3684 | int i, rc = 0; |
c57875f5 | 3685 | |
644abbb2 SW |
3686 | if (!kvm_irq_bypass) |
3687 | return 0; | |
3688 | ||
c57875f5 SW |
3689 | desc = irq_to_desc(host_irq); |
3690 | if (!desc) | |
3691 | return -EIO; | |
3692 | ||
3693 | mutex_lock(&kvm->lock); | |
a1c52e1c ME |
3694 | if (!kvm->arch.pimap) |
3695 | goto unlock; | |
c57875f5 | 3696 | |
c57875f5 SW |
3697 | pimap = kvm->arch.pimap; |
3698 | ||
3699 | for (i = 0; i < pimap->n_mapped; i++) { | |
3700 | if (guest_gsi == pimap->mapped[i].v_hwirq) | |
3701 | break; | |
3702 | } | |
3703 | ||
3704 | if (i == pimap->n_mapped) { | |
3705 | mutex_unlock(&kvm->lock); | |
3706 | return -ENODEV; | |
3707 | } | |
3708 | ||
5af50993 BH |
3709 | if (xive_enabled()) |
3710 | rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc); | |
3711 | else | |
3712 | kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq); | |
5d375199 | 3713 | |
5af50993 | 3714 | /* invalidate the entry (what do do on error from the above ?) */ |
c57875f5 SW |
3715 | pimap->mapped[i].r_hwirq = 0; |
3716 | ||
3717 | /* | |
3718 | * We don't free this structure even when the count goes to | |
3719 | * zero. The structure is freed when we destroy the VM. | |
3720 | */ | |
a1c52e1c | 3721 | unlock: |
c57875f5 | 3722 | mutex_unlock(&kvm->lock); |
5af50993 | 3723 | return rc; |
c57875f5 SW |
3724 | } |
3725 | ||
3726 | static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons, | |
3727 | struct irq_bypass_producer *prod) | |
3728 | { | |
3729 | int ret = 0; | |
3730 | struct kvm_kernel_irqfd *irqfd = | |
3731 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
3732 | ||
3733 | irqfd->producer = prod; | |
3734 | ||
3735 | ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi); | |
3736 | if (ret) | |
3737 | pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n", | |
3738 | prod->irq, irqfd->gsi, ret); | |
3739 | ||
3740 | return ret; | |
3741 | } | |
3742 | ||
3743 | static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons, | |
3744 | struct irq_bypass_producer *prod) | |
3745 | { | |
3746 | int ret; | |
3747 | struct kvm_kernel_irqfd *irqfd = | |
3748 | container_of(cons, struct kvm_kernel_irqfd, consumer); | |
3749 | ||
3750 | irqfd->producer = NULL; | |
3751 | ||
3752 | /* | |
3753 | * When producer of consumer is unregistered, we change back to | |
3754 | * default external interrupt handling mode - KVM real mode | |
3755 | * will switch back to host. | |
3756 | */ | |
3757 | ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi); | |
3758 | if (ret) | |
3759 | pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n", | |
3760 | prod->irq, irqfd->gsi, ret); | |
3761 | } | |
8daaafc8 SW |
3762 | #endif |
3763 | ||
3a167bea AK |
3764 | static long kvm_arch_vm_ioctl_hv(struct file *filp, |
3765 | unsigned int ioctl, unsigned long arg) | |
3766 | { | |
3767 | struct kvm *kvm __maybe_unused = filp->private_data; | |
3768 | void __user *argp = (void __user *)arg; | |
3769 | long r; | |
3770 | ||
3771 | switch (ioctl) { | |
3772 | ||
3a167bea AK |
3773 | case KVM_PPC_ALLOCATE_HTAB: { |
3774 | u32 htab_order; | |
3775 | ||
3776 | r = -EFAULT; | |
3777 | if (get_user(htab_order, (u32 __user *)argp)) | |
3778 | break; | |
f98a8bf9 | 3779 | r = kvmppc_alloc_reset_hpt(kvm, htab_order); |
3a167bea AK |
3780 | if (r) |
3781 | break; | |
3a167bea AK |
3782 | r = 0; |
3783 | break; | |
3784 | } | |
3785 | ||
3786 | case KVM_PPC_GET_HTAB_FD: { | |
3787 | struct kvm_get_htab_fd ghf; | |
3788 | ||
3789 | r = -EFAULT; | |
3790 | if (copy_from_user(&ghf, argp, sizeof(ghf))) | |
3791 | break; | |
3792 | r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf); | |
3793 | break; | |
3794 | } | |
3795 | ||
5e985969 DG |
3796 | case KVM_PPC_RESIZE_HPT_PREPARE: { |
3797 | struct kvm_ppc_resize_hpt rhpt; | |
3798 | ||
3799 | r = -EFAULT; | |
3800 | if (copy_from_user(&rhpt, argp, sizeof(rhpt))) | |
3801 | break; | |
3802 | ||
3803 | r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt); | |
3804 | break; | |
3805 | } | |
3806 | ||
3807 | case KVM_PPC_RESIZE_HPT_COMMIT: { | |
3808 | struct kvm_ppc_resize_hpt rhpt; | |
3809 | ||
3810 | r = -EFAULT; | |
3811 | if (copy_from_user(&rhpt, argp, sizeof(rhpt))) | |
3812 | break; | |
3813 | ||
3814 | r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt); | |
3815 | break; | |
3816 | } | |
3817 | ||
3a167bea AK |
3818 | default: |
3819 | r = -ENOTTY; | |
3820 | } | |
3821 | ||
3822 | return r; | |
3823 | } | |
3824 | ||
699a0ea0 PM |
3825 | /* |
3826 | * List of hcall numbers to enable by default. | |
3827 | * For compatibility with old userspace, we enable by default | |
3828 | * all hcalls that were implemented before the hcall-enabling | |
3829 | * facility was added. Note this list should not include H_RTAS. | |
3830 | */ | |
3831 | static unsigned int default_hcall_list[] = { | |
3832 | H_REMOVE, | |
3833 | H_ENTER, | |
3834 | H_READ, | |
3835 | H_PROTECT, | |
3836 | H_BULK_REMOVE, | |
3837 | H_GET_TCE, | |
3838 | H_PUT_TCE, | |
3839 | H_SET_DABR, | |
3840 | H_SET_XDABR, | |
3841 | H_CEDE, | |
3842 | H_PROD, | |
3843 | H_CONFER, | |
3844 | H_REGISTER_VPA, | |
3845 | #ifdef CONFIG_KVM_XICS | |
3846 | H_EOI, | |
3847 | H_CPPR, | |
3848 | H_IPI, | |
3849 | H_IPOLL, | |
3850 | H_XIRR, | |
3851 | H_XIRR_X, | |
3852 | #endif | |
3853 | 0 | |
3854 | }; | |
3855 | ||
3856 | static void init_default_hcalls(void) | |
3857 | { | |
3858 | int i; | |
ae2113a4 | 3859 | unsigned int hcall; |
699a0ea0 | 3860 | |
ae2113a4 PM |
3861 | for (i = 0; default_hcall_list[i]; ++i) { |
3862 | hcall = default_hcall_list[i]; | |
3863 | WARN_ON(!kvmppc_hcall_impl_hv(hcall)); | |
3864 | __set_bit(hcall / 4, default_enabled_hcalls); | |
3865 | } | |
699a0ea0 PM |
3866 | } |
3867 | ||
c9270132 PM |
3868 | static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg) |
3869 | { | |
468808bd | 3870 | unsigned long lpcr; |
8cf4ecc0 | 3871 | int radix; |
468808bd PM |
3872 | |
3873 | /* If not on a POWER9, reject it */ | |
3874 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) | |
3875 | return -ENODEV; | |
3876 | ||
3877 | /* If any unknown flags set, reject it */ | |
3878 | if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE)) | |
3879 | return -EINVAL; | |
3880 | ||
8cf4ecc0 PM |
3881 | /* We can't change a guest to/from radix yet */ |
3882 | radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX); | |
3883 | if (radix != kvm_is_radix(kvm)) | |
468808bd PM |
3884 | return -EINVAL; |
3885 | ||
3886 | /* GR (guest radix) bit in process_table field must match */ | |
8cf4ecc0 | 3887 | if (!!(cfg->process_table & PATB_GR) != radix) |
468808bd PM |
3888 | return -EINVAL; |
3889 | ||
3890 | /* Process table size field must be reasonable, i.e. <= 24 */ | |
3891 | if ((cfg->process_table & PRTS_MASK) > 24) | |
3892 | return -EINVAL; | |
3893 | ||
3894 | kvm->arch.process_table = cfg->process_table; | |
3895 | kvmppc_setup_partition_table(kvm); | |
3896 | ||
3897 | lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0; | |
3898 | kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE); | |
3899 | ||
3900 | return 0; | |
c9270132 PM |
3901 | } |
3902 | ||
cbbc58d4 | 3903 | static struct kvmppc_ops kvm_ops_hv = { |
3a167bea AK |
3904 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, |
3905 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, | |
3906 | .get_one_reg = kvmppc_get_one_reg_hv, | |
3907 | .set_one_reg = kvmppc_set_one_reg_hv, | |
3908 | .vcpu_load = kvmppc_core_vcpu_load_hv, | |
3909 | .vcpu_put = kvmppc_core_vcpu_put_hv, | |
3910 | .set_msr = kvmppc_set_msr_hv, | |
3911 | .vcpu_run = kvmppc_vcpu_run_hv, | |
3912 | .vcpu_create = kvmppc_core_vcpu_create_hv, | |
3913 | .vcpu_free = kvmppc_core_vcpu_free_hv, | |
3914 | .check_requests = kvmppc_core_check_requests_hv, | |
3915 | .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv, | |
3916 | .flush_memslot = kvmppc_core_flush_memslot_hv, | |
3917 | .prepare_memory_region = kvmppc_core_prepare_memory_region_hv, | |
3918 | .commit_memory_region = kvmppc_core_commit_memory_region_hv, | |
3919 | .unmap_hva = kvm_unmap_hva_hv, | |
3920 | .unmap_hva_range = kvm_unmap_hva_range_hv, | |
3921 | .age_hva = kvm_age_hva_hv, | |
3922 | .test_age_hva = kvm_test_age_hva_hv, | |
3923 | .set_spte_hva = kvm_set_spte_hva_hv, | |
3924 | .mmu_destroy = kvmppc_mmu_destroy_hv, | |
3925 | .free_memslot = kvmppc_core_free_memslot_hv, | |
3926 | .create_memslot = kvmppc_core_create_memslot_hv, | |
3927 | .init_vm = kvmppc_core_init_vm_hv, | |
3928 | .destroy_vm = kvmppc_core_destroy_vm_hv, | |
3a167bea AK |
3929 | .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv, |
3930 | .emulate_op = kvmppc_core_emulate_op_hv, | |
3931 | .emulate_mtspr = kvmppc_core_emulate_mtspr_hv, | |
3932 | .emulate_mfspr = kvmppc_core_emulate_mfspr_hv, | |
3933 | .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv, | |
3934 | .arch_vm_ioctl = kvm_arch_vm_ioctl_hv, | |
ae2113a4 | 3935 | .hcall_implemented = kvmppc_hcall_impl_hv, |
c57875f5 SW |
3936 | #ifdef CONFIG_KVM_XICS |
3937 | .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv, | |
3938 | .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv, | |
3939 | #endif | |
c9270132 PM |
3940 | .configure_mmu = kvmhv_configure_mmu, |
3941 | .get_rmmu_info = kvmhv_get_rmmu_info, | |
3a167bea AK |
3942 | }; |
3943 | ||
fd7bacbc MS |
3944 | static int kvm_init_subcore_bitmap(void) |
3945 | { | |
3946 | int i, j; | |
3947 | int nr_cores = cpu_nr_cores(); | |
3948 | struct sibling_subcore_state *sibling_subcore_state; | |
3949 | ||
3950 | for (i = 0; i < nr_cores; i++) { | |
3951 | int first_cpu = i * threads_per_core; | |
3952 | int node = cpu_to_node(first_cpu); | |
3953 | ||
3954 | /* Ignore if it is already allocated. */ | |
3955 | if (paca[first_cpu].sibling_subcore_state) | |
3956 | continue; | |
3957 | ||
3958 | sibling_subcore_state = | |
3959 | kmalloc_node(sizeof(struct sibling_subcore_state), | |
3960 | GFP_KERNEL, node); | |
3961 | if (!sibling_subcore_state) | |
3962 | return -ENOMEM; | |
3963 | ||
3964 | memset(sibling_subcore_state, 0, | |
3965 | sizeof(struct sibling_subcore_state)); | |
3966 | ||
3967 | for (j = 0; j < threads_per_core; j++) { | |
3968 | int cpu = first_cpu + j; | |
3969 | ||
3970 | paca[cpu].sibling_subcore_state = sibling_subcore_state; | |
3971 | } | |
3972 | } | |
3973 | return 0; | |
3974 | } | |
3975 | ||
5a319350 PM |
3976 | static int kvmppc_radix_possible(void) |
3977 | { | |
3978 | return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled(); | |
3979 | } | |
3980 | ||
3a167bea | 3981 | static int kvmppc_book3s_init_hv(void) |
de56a948 PM |
3982 | { |
3983 | int r; | |
cbbc58d4 AK |
3984 | /* |
3985 | * FIXME!! Do we need to check on all cpus ? | |
3986 | */ | |
3987 | r = kvmppc_core_check_processor_compat_hv(); | |
3988 | if (r < 0) | |
739e2425 | 3989 | return -ENODEV; |
de56a948 | 3990 | |
fd7bacbc MS |
3991 | r = kvm_init_subcore_bitmap(); |
3992 | if (r) | |
3993 | return r; | |
3994 | ||
f725758b PM |
3995 | /* |
3996 | * We need a way of accessing the XICS interrupt controller, | |
3997 | * either directly, via paca[cpu].kvm_hstate.xics_phys, or | |
3998 | * indirectly, via OPAL. | |
3999 | */ | |
4000 | #ifdef CONFIG_SMP | |
fb7dcf72 | 4001 | if (!xive_enabled() && !local_paca->kvm_hstate.xics_phys) { |
f725758b PM |
4002 | struct device_node *np; |
4003 | ||
4004 | np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc"); | |
4005 | if (!np) { | |
4006 | pr_err("KVM-HV: Cannot determine method for accessing XICS\n"); | |
4007 | return -ENODEV; | |
4008 | } | |
4009 | } | |
4010 | #endif | |
4011 | ||
cbbc58d4 AK |
4012 | kvm_ops_hv.owner = THIS_MODULE; |
4013 | kvmppc_hv_ops = &kvm_ops_hv; | |
de56a948 | 4014 | |
699a0ea0 PM |
4015 | init_default_hcalls(); |
4016 | ||
ec257165 PM |
4017 | init_vcore_lists(); |
4018 | ||
cbbc58d4 | 4019 | r = kvmppc_mmu_hv_init(); |
5a319350 PM |
4020 | if (r) |
4021 | return r; | |
4022 | ||
4023 | if (kvmppc_radix_possible()) | |
4024 | r = kvmppc_radix_init(); | |
de56a948 PM |
4025 | return r; |
4026 | } | |
4027 | ||
3a167bea | 4028 | static void kvmppc_book3s_exit_hv(void) |
de56a948 | 4029 | { |
79b6c247 | 4030 | kvmppc_free_host_rm_ops(); |
5a319350 PM |
4031 | if (kvmppc_radix_possible()) |
4032 | kvmppc_radix_exit(); | |
cbbc58d4 | 4033 | kvmppc_hv_ops = NULL; |
de56a948 PM |
4034 | } |
4035 | ||
3a167bea AK |
4036 | module_init(kvmppc_book3s_init_hv); |
4037 | module_exit(kvmppc_book3s_exit_hv); | |
2ba9f0d8 | 4038 | MODULE_LICENSE("GPL"); |
398a76c6 AG |
4039 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
4040 | MODULE_ALIAS("devname:kvm"); | |
7c5b06ca | 4041 |