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