1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
4 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
5 *
6 * Authors:
7 * Paul Mackerras <paulus@au1.ibm.com>
8 * Alexander Graf <agraf@suse.de>
9 * Kevin Wolf <mail@kevin-wolf.de>
10 *
11 * Description: KVM functions specific to running on Book 3S
12 * processors in hypervisor mode (specifically POWER7 and later).
13 *
14 * This file is derived from arch/powerpc/kvm/book3s.c,
15 * by Alexander Graf <agraf@suse.de>.
16 */
17
18 #include <linux/kvm_host.h>
19 #include <linux/kernel.h>
20 #include <linux/err.h>
21 #include <linux/slab.h>
22 #include <linux/preempt.h>
23 #include <linux/sched/signal.h>
24 #include <linux/sched/stat.h>
25 #include <linux/delay.h>
26 #include <linux/export.h>
27 #include <linux/fs.h>
28 #include <linux/anon_inodes.h>
29 #include <linux/cpu.h>
30 #include <linux/cpumask.h>
31 #include <linux/spinlock.h>
32 #include <linux/page-flags.h>
33 #include <linux/srcu.h>
34 #include <linux/miscdevice.h>
35 #include <linux/debugfs.h>
36 #include <linux/gfp.h>
37 #include <linux/vmalloc.h>
38 #include <linux/highmem.h>
39 #include <linux/hugetlb.h>
40 #include <linux/kvm_irqfd.h>
41 #include <linux/irqbypass.h>
42 #include <linux/module.h>
43 #include <linux/compiler.h>
44 #include <linux/of.h>
45
46 #include <asm/ftrace.h>
47 #include <asm/reg.h>
48 #include <asm/ppc-opcode.h>
49 #include <asm/asm-prototypes.h>
50 #include <asm/archrandom.h>
51 #include <asm/debug.h>
52 #include <asm/disassemble.h>
53 #include <asm/cputable.h>
54 #include <asm/cacheflush.h>
55 #include <linux/uaccess.h>
56 #include <asm/interrupt.h>
57 #include <asm/io.h>
58 #include <asm/kvm_ppc.h>
59 #include <asm/kvm_book3s.h>
60 #include <asm/mmu_context.h>
61 #include <asm/lppaca.h>
62 #include <asm/pmc.h>
63 #include <asm/processor.h>
64 #include <asm/cputhreads.h>
65 #include <asm/page.h>
66 #include <asm/hvcall.h>
67 #include <asm/switch_to.h>
68 #include <asm/smp.h>
69 #include <asm/dbell.h>
70 #include <asm/hmi.h>
71 #include <asm/pnv-pci.h>
72 #include <asm/mmu.h>
73 #include <asm/opal.h>
74 #include <asm/xics.h>
75 #include <asm/xive.h>
76 #include <asm/hw_breakpoint.h>
77 #include <asm/kvm_book3s_uvmem.h>
78 #include <asm/ultravisor.h>
79 #include <asm/dtl.h>
80 #include <asm/plpar_wrappers.h>
81
82 #include "book3s.h"
83
84 #define CREATE_TRACE_POINTS
85 #include "trace_hv.h"
86
87 /* #define EXIT_DEBUG */
88 /* #define EXIT_DEBUG_SIMPLE */
89 /* #define EXIT_DEBUG_INT */
90
91 /* Used to indicate that a guest page fault needs to be handled */
92 #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
93 /* Used to indicate that a guest passthrough interrupt needs to be handled */
94 #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
95
96 /* Used as a "null" value for timebase values */
97 #define TB_NIL (~(u64)0)
98
99 static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
100
101 static int dynamic_mt_modes = 6;
102 module_param(dynamic_mt_modes, int, 0644);
103 MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
104 static int target_smt_mode;
105 module_param(target_smt_mode, int, 0644);
106 MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
107
108 static bool one_vm_per_core;
109 module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR);
110 MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires POWER8 or older)");
111
112 #ifdef CONFIG_KVM_XICS
113 static const struct kernel_param_ops module_param_ops = {
114 .set = param_set_int,
115 .get = param_get_int,
116 };
117
118 module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644);
119 MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
120
121 module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
122 MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
123 #endif
124
125 /* If set, guests are allowed to create and control nested guests */
126 static bool nested = true;
127 module_param(nested, bool, S_IRUGO | S_IWUSR);
128 MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
129
nesting_enabled(struct kvm * kvm)130 static inline bool nesting_enabled(struct kvm *kvm)
131 {
132 return kvm->arch.nested_enable && kvm_is_radix(kvm);
133 }
134
135 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
136
137 /*
138 * RWMR values for POWER8. These control the rate at which PURR
139 * and SPURR count and should be set according to the number of
140 * online threads in the vcore being run.
141 */
142 #define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL
143 #define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL
144 #define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL
145 #define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL
146 #define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL
147 #define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL
148 #define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL
149 #define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL
150
151 static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = {
152 RWMR_RPA_P8_1THREAD,
153 RWMR_RPA_P8_1THREAD,
154 RWMR_RPA_P8_2THREAD,
155 RWMR_RPA_P8_3THREAD,
156 RWMR_RPA_P8_4THREAD,
157 RWMR_RPA_P8_5THREAD,
158 RWMR_RPA_P8_6THREAD,
159 RWMR_RPA_P8_7THREAD,
160 RWMR_RPA_P8_8THREAD,
161 };
162
next_runnable_thread(struct kvmppc_vcore * vc,int * ip)163 static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
164 int *ip)
165 {
166 int i = *ip;
167 struct kvm_vcpu *vcpu;
168
169 while (++i < MAX_SMT_THREADS) {
170 vcpu = READ_ONCE(vc->runnable_threads[i]);
171 if (vcpu) {
172 *ip = i;
173 return vcpu;
174 }
175 }
176 return NULL;
177 }
178
179 /* Used to traverse the list of runnable threads for a given vcore */
180 #define for_each_runnable_thread(i, vcpu, vc) \
181 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
182
kvmppc_ipi_thread(int cpu)183 static bool kvmppc_ipi_thread(int cpu)
184 {
185 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
186
187 /* If we're a nested hypervisor, fall back to ordinary IPIs for now */
188 if (kvmhv_on_pseries())
189 return false;
190
191 /* On POWER9 we can use msgsnd to IPI any cpu */
192 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
193 msg |= get_hard_smp_processor_id(cpu);
194 smp_mb();
195 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
196 return true;
197 }
198
199 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
200 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
201 preempt_disable();
202 if (cpu_first_thread_sibling(cpu) ==
203 cpu_first_thread_sibling(smp_processor_id())) {
204 msg |= cpu_thread_in_core(cpu);
205 smp_mb();
206 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
207 preempt_enable();
208 return true;
209 }
210 preempt_enable();
211 }
212
213 #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
214 if (cpu >= 0 && cpu < nr_cpu_ids) {
215 if (paca_ptrs[cpu]->kvm_hstate.xics_phys) {
216 xics_wake_cpu(cpu);
217 return true;
218 }
219 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
220 return true;
221 }
222 #endif
223
224 return false;
225 }
226
kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu * vcpu)227 static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
228 {
229 int cpu;
230 struct rcuwait *waitp;
231
232 waitp = kvm_arch_vcpu_get_wait(vcpu);
233 if (rcuwait_wake_up(waitp))
234 ++vcpu->stat.generic.halt_wakeup;
235
236 cpu = READ_ONCE(vcpu->arch.thread_cpu);
237 if (cpu >= 0 && kvmppc_ipi_thread(cpu))
238 return;
239
240 /* CPU points to the first thread of the core */
241 cpu = vcpu->cpu;
242 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
243 smp_send_reschedule(cpu);
244 }
245
246 /*
247 * We use the vcpu_load/put functions to measure stolen time.
248 * Stolen time is counted as time when either the vcpu is able to
249 * run as part of a virtual core, but the task running the vcore
250 * is preempted or sleeping, or when the vcpu needs something done
251 * in the kernel by the task running the vcpu, but that task is
252 * preempted or sleeping. Those two things have to be counted
253 * separately, since one of the vcpu tasks will take on the job
254 * of running the core, and the other vcpu tasks in the vcore will
255 * sleep waiting for it to do that, but that sleep shouldn't count
256 * as stolen time.
257 *
258 * Hence we accumulate stolen time when the vcpu can run as part of
259 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
260 * needs its task to do other things in the kernel (for example,
261 * service a page fault) in busy_stolen. We don't accumulate
262 * stolen time for a vcore when it is inactive, or for a vcpu
263 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
264 * a misnomer; it means that the vcpu task is not executing in
265 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
266 * the kernel. We don't have any way of dividing up that time
267 * between time that the vcpu is genuinely stopped, time that
268 * the task is actively working on behalf of the vcpu, and time
269 * that the task is preempted, so we don't count any of it as
270 * stolen.
271 *
272 * Updates to busy_stolen are protected by arch.tbacct_lock;
273 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
274 * lock. The stolen times are measured in units of timebase ticks.
275 * (Note that the != TB_NIL checks below are purely defensive;
276 * they should never fail.)
277 */
278
kvmppc_core_start_stolen(struct kvmppc_vcore * vc)279 static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
280 {
281 unsigned long flags;
282
283 spin_lock_irqsave(&vc->stoltb_lock, flags);
284 vc->preempt_tb = mftb();
285 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
286 }
287
kvmppc_core_end_stolen(struct kvmppc_vcore * vc)288 static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
289 {
290 unsigned long flags;
291
292 spin_lock_irqsave(&vc->stoltb_lock, flags);
293 if (vc->preempt_tb != TB_NIL) {
294 vc->stolen_tb += mftb() - vc->preempt_tb;
295 vc->preempt_tb = TB_NIL;
296 }
297 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
298 }
299
kvmppc_core_vcpu_load_hv(struct kvm_vcpu * vcpu,int cpu)300 static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
301 {
302 struct kvmppc_vcore *vc = vcpu->arch.vcore;
303 unsigned long flags;
304
305 /*
306 * We can test vc->runner without taking the vcore lock,
307 * because only this task ever sets vc->runner to this
308 * vcpu, and once it is set to this vcpu, only this task
309 * ever sets it to NULL.
310 */
311 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
312 kvmppc_core_end_stolen(vc);
313
314 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
315 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
316 vcpu->arch.busy_preempt != TB_NIL) {
317 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
318 vcpu->arch.busy_preempt = TB_NIL;
319 }
320 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
321 }
322
kvmppc_core_vcpu_put_hv(struct kvm_vcpu * vcpu)323 static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
324 {
325 struct kvmppc_vcore *vc = vcpu->arch.vcore;
326 unsigned long flags;
327
328 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
329 kvmppc_core_start_stolen(vc);
330
331 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
332 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
333 vcpu->arch.busy_preempt = mftb();
334 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
335 }
336
kvmppc_set_pvr_hv(struct kvm_vcpu * vcpu,u32 pvr)337 static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
338 {
339 vcpu->arch.pvr = pvr;
340 }
341
342 /* Dummy value used in computing PCR value below */
343 #define PCR_ARCH_31 (PCR_ARCH_300 << 1)
344
kvmppc_set_arch_compat(struct kvm_vcpu * vcpu,u32 arch_compat)345 static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
346 {
347 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
348 struct kvmppc_vcore *vc = vcpu->arch.vcore;
349
350 /* We can (emulate) our own architecture version and anything older */
351 if (cpu_has_feature(CPU_FTR_ARCH_31))
352 host_pcr_bit = PCR_ARCH_31;
353 else if (cpu_has_feature(CPU_FTR_ARCH_300))
354 host_pcr_bit = PCR_ARCH_300;
355 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
356 host_pcr_bit = PCR_ARCH_207;
357 else if (cpu_has_feature(CPU_FTR_ARCH_206))
358 host_pcr_bit = PCR_ARCH_206;
359 else
360 host_pcr_bit = PCR_ARCH_205;
361
362 /* Determine lowest PCR bit needed to run guest in given PVR level */
363 guest_pcr_bit = host_pcr_bit;
364 if (arch_compat) {
365 switch (arch_compat) {
366 case PVR_ARCH_205:
367 guest_pcr_bit = PCR_ARCH_205;
368 break;
369 case PVR_ARCH_206:
370 case PVR_ARCH_206p:
371 guest_pcr_bit = PCR_ARCH_206;
372 break;
373 case PVR_ARCH_207:
374 guest_pcr_bit = PCR_ARCH_207;
375 break;
376 case PVR_ARCH_300:
377 guest_pcr_bit = PCR_ARCH_300;
378 break;
379 case PVR_ARCH_31:
380 guest_pcr_bit = PCR_ARCH_31;
381 break;
382 default:
383 return -EINVAL;
384 }
385 }
386
387 /* Check requested PCR bits don't exceed our capabilities */
388 if (guest_pcr_bit > host_pcr_bit)
389 return -EINVAL;
390
391 spin_lock(&vc->lock);
392 vc->arch_compat = arch_compat;
393 /*
394 * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit
395 * Also set all reserved PCR bits
396 */
397 vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK;
398 spin_unlock(&vc->lock);
399
400 return 0;
401 }
402
kvmppc_dump_regs(struct kvm_vcpu * vcpu)403 static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
404 {
405 int r;
406
407 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
408 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
409 vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap);
410 for (r = 0; r < 16; ++r)
411 pr_err("r%2d = %.16lx r%d = %.16lx\n",
412 r, kvmppc_get_gpr(vcpu, r),
413 r+16, kvmppc_get_gpr(vcpu, r+16));
414 pr_err("ctr = %.16lx lr = %.16lx\n",
415 vcpu->arch.regs.ctr, vcpu->arch.regs.link);
416 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
417 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
418 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
419 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
420 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
421 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
422 pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n",
423 vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);
424 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
425 pr_err("fault dar = %.16lx dsisr = %.8x\n",
426 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
427 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
428 for (r = 0; r < vcpu->arch.slb_max; ++r)
429 pr_err(" ESID = %.16llx VSID = %.16llx\n",
430 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
431 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
432 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
433 vcpu->arch.last_inst);
434 }
435
kvmppc_find_vcpu(struct kvm * kvm,int id)436 static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
437 {
438 return kvm_get_vcpu_by_id(kvm, id);
439 }
440
init_vpa(struct kvm_vcpu * vcpu,struct lppaca * vpa)441 static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
442 {
443 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
444 vpa->yield_count = cpu_to_be32(1);
445 }
446
set_vpa(struct kvm_vcpu * vcpu,struct kvmppc_vpa * v,unsigned long addr,unsigned long len)447 static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
448 unsigned long addr, unsigned long len)
449 {
450 /* check address is cacheline aligned */
451 if (addr & (L1_CACHE_BYTES - 1))
452 return -EINVAL;
453 spin_lock(&vcpu->arch.vpa_update_lock);
454 if (v->next_gpa != addr || v->len != len) {
455 v->next_gpa = addr;
456 v->len = addr ? len : 0;
457 v->update_pending = 1;
458 }
459 spin_unlock(&vcpu->arch.vpa_update_lock);
460 return 0;
461 }
462
463 /* Length for a per-processor buffer is passed in at offset 4 in the buffer */
464 struct reg_vpa {
465 u32 dummy;
466 union {
467 __be16 hword;
468 __be32 word;
469 } length;
470 };
471
vpa_is_registered(struct kvmppc_vpa * vpap)472 static int vpa_is_registered(struct kvmppc_vpa *vpap)
473 {
474 if (vpap->update_pending)
475 return vpap->next_gpa != 0;
476 return vpap->pinned_addr != NULL;
477 }
478
do_h_register_vpa(struct kvm_vcpu * vcpu,unsigned long flags,unsigned long vcpuid,unsigned long vpa)479 static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
480 unsigned long flags,
481 unsigned long vcpuid, unsigned long vpa)
482 {
483 struct kvm *kvm = vcpu->kvm;
484 unsigned long len, nb;
485 void *va;
486 struct kvm_vcpu *tvcpu;
487 int err;
488 int subfunc;
489 struct kvmppc_vpa *vpap;
490
491 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
492 if (!tvcpu)
493 return H_PARAMETER;
494
495 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
496 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
497 subfunc == H_VPA_REG_SLB) {
498 /* Registering new area - address must be cache-line aligned */
499 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
500 return H_PARAMETER;
501
502 /* convert logical addr to kernel addr and read length */
503 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
504 if (va == NULL)
505 return H_PARAMETER;
506 if (subfunc == H_VPA_REG_VPA)
507 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
508 else
509 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
510 kvmppc_unpin_guest_page(kvm, va, vpa, false);
511
512 /* Check length */
513 if (len > nb || len < sizeof(struct reg_vpa))
514 return H_PARAMETER;
515 } else {
516 vpa = 0;
517 len = 0;
518 }
519
520 err = H_PARAMETER;
521 vpap = NULL;
522 spin_lock(&tvcpu->arch.vpa_update_lock);
523
524 switch (subfunc) {
525 case H_VPA_REG_VPA: /* register VPA */
526 /*
527 * The size of our lppaca is 1kB because of the way we align
528 * it for the guest to avoid crossing a 4kB boundary. We only
529 * use 640 bytes of the structure though, so we should accept
530 * clients that set a size of 640.
531 */
532 BUILD_BUG_ON(sizeof(struct lppaca) != 640);
533 if (len < sizeof(struct lppaca))
534 break;
535 vpap = &tvcpu->arch.vpa;
536 err = 0;
537 break;
538
539 case H_VPA_REG_DTL: /* register DTL */
540 if (len < sizeof(struct dtl_entry))
541 break;
542 len -= len % sizeof(struct dtl_entry);
543
544 /* Check that they have previously registered a VPA */
545 err = H_RESOURCE;
546 if (!vpa_is_registered(&tvcpu->arch.vpa))
547 break;
548
549 vpap = &tvcpu->arch.dtl;
550 err = 0;
551 break;
552
553 case H_VPA_REG_SLB: /* register SLB shadow buffer */
554 /* Check that they have previously registered a VPA */
555 err = H_RESOURCE;
556 if (!vpa_is_registered(&tvcpu->arch.vpa))
557 break;
558
559 vpap = &tvcpu->arch.slb_shadow;
560 err = 0;
561 break;
562
563 case H_VPA_DEREG_VPA: /* deregister VPA */
564 /* Check they don't still have a DTL or SLB buf registered */
565 err = H_RESOURCE;
566 if (vpa_is_registered(&tvcpu->arch.dtl) ||
567 vpa_is_registered(&tvcpu->arch.slb_shadow))
568 break;
569
570 vpap = &tvcpu->arch.vpa;
571 err = 0;
572 break;
573
574 case H_VPA_DEREG_DTL: /* deregister DTL */
575 vpap = &tvcpu->arch.dtl;
576 err = 0;
577 break;
578
579 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
580 vpap = &tvcpu->arch.slb_shadow;
581 err = 0;
582 break;
583 }
584
585 if (vpap) {
586 vpap->next_gpa = vpa;
587 vpap->len = len;
588 vpap->update_pending = 1;
589 }
590
591 spin_unlock(&tvcpu->arch.vpa_update_lock);
592
593 return err;
594 }
595
kvmppc_update_vpa(struct kvm_vcpu * vcpu,struct kvmppc_vpa * vpap)596 static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
597 {
598 struct kvm *kvm = vcpu->kvm;
599 void *va;
600 unsigned long nb;
601 unsigned long gpa;
602
603 /*
604 * We need to pin the page pointed to by vpap->next_gpa,
605 * but we can't call kvmppc_pin_guest_page under the lock
606 * as it does get_user_pages() and down_read(). So we
607 * have to drop the lock, pin the page, then get the lock
608 * again and check that a new area didn't get registered
609 * in the meantime.
610 */
611 for (;;) {
612 gpa = vpap->next_gpa;
613 spin_unlock(&vcpu->arch.vpa_update_lock);
614 va = NULL;
615 nb = 0;
616 if (gpa)
617 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
618 spin_lock(&vcpu->arch.vpa_update_lock);
619 if (gpa == vpap->next_gpa)
620 break;
621 /* sigh... unpin that one and try again */
622 if (va)
623 kvmppc_unpin_guest_page(kvm, va, gpa, false);
624 }
625
626 vpap->update_pending = 0;
627 if (va && nb < vpap->len) {
628 /*
629 * If it's now too short, it must be that userspace
630 * has changed the mappings underlying guest memory,
631 * so unregister the region.
632 */
633 kvmppc_unpin_guest_page(kvm, va, gpa, false);
634 va = NULL;
635 }
636 if (vpap->pinned_addr)
637 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
638 vpap->dirty);
639 vpap->gpa = gpa;
640 vpap->pinned_addr = va;
641 vpap->dirty = false;
642 if (va)
643 vpap->pinned_end = va + vpap->len;
644 }
645
kvmppc_update_vpas(struct kvm_vcpu * vcpu)646 static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
647 {
648 if (!(vcpu->arch.vpa.update_pending ||
649 vcpu->arch.slb_shadow.update_pending ||
650 vcpu->arch.dtl.update_pending))
651 return;
652
653 spin_lock(&vcpu->arch.vpa_update_lock);
654 if (vcpu->arch.vpa.update_pending) {
655 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
656 if (vcpu->arch.vpa.pinned_addr)
657 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
658 }
659 if (vcpu->arch.dtl.update_pending) {
660 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
661 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
662 vcpu->arch.dtl_index = 0;
663 }
664 if (vcpu->arch.slb_shadow.update_pending)
665 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
666 spin_unlock(&vcpu->arch.vpa_update_lock);
667 }
668
669 /*
670 * Return the accumulated stolen time for the vcore up until `now'.
671 * The caller should hold the vcore lock.
672 */
vcore_stolen_time(struct kvmppc_vcore * vc,u64 now)673 static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
674 {
675 u64 p;
676 unsigned long flags;
677
678 spin_lock_irqsave(&vc->stoltb_lock, flags);
679 p = vc->stolen_tb;
680 if (vc->vcore_state != VCORE_INACTIVE &&
681 vc->preempt_tb != TB_NIL)
682 p += now - vc->preempt_tb;
683 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
684 return p;
685 }
686
kvmppc_create_dtl_entry(struct kvm_vcpu * vcpu,struct kvmppc_vcore * vc)687 static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
688 struct kvmppc_vcore *vc)
689 {
690 struct dtl_entry *dt;
691 struct lppaca *vpa;
692 unsigned long stolen;
693 unsigned long core_stolen;
694 u64 now;
695 unsigned long flags;
696
697 dt = vcpu->arch.dtl_ptr;
698 vpa = vcpu->arch.vpa.pinned_addr;
699 now = mftb();
700 core_stolen = vcore_stolen_time(vc, now);
701 stolen = core_stolen - vcpu->arch.stolen_logged;
702 vcpu->arch.stolen_logged = core_stolen;
703 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
704 stolen += vcpu->arch.busy_stolen;
705 vcpu->arch.busy_stolen = 0;
706 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
707 if (!dt || !vpa)
708 return;
709 memset(dt, 0, sizeof(struct dtl_entry));
710 dt->dispatch_reason = 7;
711 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
712 dt->timebase = cpu_to_be64(now + vc->tb_offset);
713 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
714 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
715 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
716 ++dt;
717 if (dt == vcpu->arch.dtl.pinned_end)
718 dt = vcpu->arch.dtl.pinned_addr;
719 vcpu->arch.dtl_ptr = dt;
720 /* order writing *dt vs. writing vpa->dtl_idx */
721 smp_wmb();
722 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
723 vcpu->arch.dtl.dirty = true;
724 }
725
726 /* See if there is a doorbell interrupt pending for a vcpu */
kvmppc_doorbell_pending(struct kvm_vcpu * vcpu)727 static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
728 {
729 int thr;
730 struct kvmppc_vcore *vc;
731
732 if (vcpu->arch.doorbell_request)
733 return true;
734 /*
735 * Ensure that the read of vcore->dpdes comes after the read
736 * of vcpu->doorbell_request. This barrier matches the
737 * smp_wmb() in kvmppc_guest_entry_inject().
738 */
739 smp_rmb();
740 vc = vcpu->arch.vcore;
741 thr = vcpu->vcpu_id - vc->first_vcpuid;
742 return !!(vc->dpdes & (1 << thr));
743 }
744
kvmppc_power8_compatible(struct kvm_vcpu * vcpu)745 static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
746 {
747 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
748 return true;
749 if ((!vcpu->arch.vcore->arch_compat) &&
750 cpu_has_feature(CPU_FTR_ARCH_207S))
751 return true;
752 return false;
753 }
754
kvmppc_h_set_mode(struct kvm_vcpu * vcpu,unsigned long mflags,unsigned long resource,unsigned long value1,unsigned long value2)755 static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
756 unsigned long resource, unsigned long value1,
757 unsigned long value2)
758 {
759 switch (resource) {
760 case H_SET_MODE_RESOURCE_SET_CIABR:
761 if (!kvmppc_power8_compatible(vcpu))
762 return H_P2;
763 if (value2)
764 return H_P4;
765 if (mflags)
766 return H_UNSUPPORTED_FLAG_START;
767 /* Guests can't breakpoint the hypervisor */
768 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
769 return H_P3;
770 vcpu->arch.ciabr = value1;
771 return H_SUCCESS;
772 case H_SET_MODE_RESOURCE_SET_DAWR0:
773 if (!kvmppc_power8_compatible(vcpu))
774 return H_P2;
775 if (!ppc_breakpoint_available())
776 return H_P2;
777 if (mflags)
778 return H_UNSUPPORTED_FLAG_START;
779 if (value2 & DABRX_HYP)
780 return H_P4;
781 vcpu->arch.dawr0 = value1;
782 vcpu->arch.dawrx0 = value2;
783 return H_SUCCESS;
784 case H_SET_MODE_RESOURCE_SET_DAWR1:
785 if (!kvmppc_power8_compatible(vcpu))
786 return H_P2;
787 if (!ppc_breakpoint_available())
788 return H_P2;
789 if (!cpu_has_feature(CPU_FTR_DAWR1))
790 return H_P2;
791 if (!vcpu->kvm->arch.dawr1_enabled)
792 return H_FUNCTION;
793 if (mflags)
794 return H_UNSUPPORTED_FLAG_START;
795 if (value2 & DABRX_HYP)
796 return H_P4;
797 vcpu->arch.dawr1 = value1;
798 vcpu->arch.dawrx1 = value2;
799 return H_SUCCESS;
800 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
801 /*
802 * KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved.
803 * Keep this in synch with kvmppc_filter_guest_lpcr_hv.
804 */
805 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
806 kvmhv_vcpu_is_radix(vcpu) && mflags == 3)
807 return H_UNSUPPORTED_FLAG_START;
808 return H_TOO_HARD;
809 default:
810 return H_TOO_HARD;
811 }
812 }
813
814 /* Copy guest memory in place - must reside within a single memslot */
kvmppc_copy_guest(struct kvm * kvm,gpa_t to,gpa_t from,unsigned long len)815 static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from,
816 unsigned long len)
817 {
818 struct kvm_memory_slot *to_memslot = NULL;
819 struct kvm_memory_slot *from_memslot = NULL;
820 unsigned long to_addr, from_addr;
821 int r;
822
823 /* Get HPA for from address */
824 from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT);
825 if (!from_memslot)
826 return -EFAULT;
827 if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages)
828 << PAGE_SHIFT))
829 return -EINVAL;
830 from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT);
831 if (kvm_is_error_hva(from_addr))
832 return -EFAULT;
833 from_addr |= (from & (PAGE_SIZE - 1));
834
835 /* Get HPA for to address */
836 to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT);
837 if (!to_memslot)
838 return -EFAULT;
839 if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages)
840 << PAGE_SHIFT))
841 return -EINVAL;
842 to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT);
843 if (kvm_is_error_hva(to_addr))
844 return -EFAULT;
845 to_addr |= (to & (PAGE_SIZE - 1));
846
847 /* Perform copy */
848 r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr,
849 len);
850 if (r)
851 return -EFAULT;
852 mark_page_dirty(kvm, to >> PAGE_SHIFT);
853 return 0;
854 }
855
kvmppc_h_page_init(struct kvm_vcpu * vcpu,unsigned long flags,unsigned long dest,unsigned long src)856 static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
857 unsigned long dest, unsigned long src)
858 {
859 u64 pg_sz = SZ_4K; /* 4K page size */
860 u64 pg_mask = SZ_4K - 1;
861 int ret;
862
863 /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */
864 if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE |
865 H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED))
866 return H_PARAMETER;
867
868 /* dest (and src if copy_page flag set) must be page aligned */
869 if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask)))
870 return H_PARAMETER;
871
872 /* zero and/or copy the page as determined by the flags */
873 if (flags & H_COPY_PAGE) {
874 ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz);
875 if (ret < 0)
876 return H_PARAMETER;
877 } else if (flags & H_ZERO_PAGE) {
878 ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz);
879 if (ret < 0)
880 return H_PARAMETER;
881 }
882
883 /* We can ignore the remaining flags */
884
885 return H_SUCCESS;
886 }
887
kvm_arch_vcpu_yield_to(struct kvm_vcpu * target)888 static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
889 {
890 struct kvmppc_vcore *vcore = target->arch.vcore;
891
892 /*
893 * We expect to have been called by the real mode handler
894 * (kvmppc_rm_h_confer()) which would have directly returned
895 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
896 * have useful work to do and should not confer) so we don't
897 * recheck that here.
898 *
899 * In the case of the P9 single vcpu per vcore case, the real
900 * mode handler is not called but no other threads are in the
901 * source vcore.
902 */
903
904 spin_lock(&vcore->lock);
905 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
906 vcore->vcore_state != VCORE_INACTIVE &&
907 vcore->runner)
908 target = vcore->runner;
909 spin_unlock(&vcore->lock);
910
911 return kvm_vcpu_yield_to(target);
912 }
913
kvmppc_get_yield_count(struct kvm_vcpu * vcpu)914 static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
915 {
916 int yield_count = 0;
917 struct lppaca *lppaca;
918
919 spin_lock(&vcpu->arch.vpa_update_lock);
920 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
921 if (lppaca)
922 yield_count = be32_to_cpu(lppaca->yield_count);
923 spin_unlock(&vcpu->arch.vpa_update_lock);
924 return yield_count;
925 }
926
927 /*
928 * H_RPT_INVALIDATE hcall handler for nested guests.
929 *
930 * Handles only nested process-scoped invalidation requests in L0.
931 */
kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu * vcpu)932 static int kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu *vcpu)
933 {
934 unsigned long type = kvmppc_get_gpr(vcpu, 6);
935 unsigned long pid, pg_sizes, start, end;
936
937 /*
938 * The partition-scoped invalidations aren't handled here in L0.
939 */
940 if (type & H_RPTI_TYPE_NESTED)
941 return RESUME_HOST;
942
943 pid = kvmppc_get_gpr(vcpu, 4);
944 pg_sizes = kvmppc_get_gpr(vcpu, 7);
945 start = kvmppc_get_gpr(vcpu, 8);
946 end = kvmppc_get_gpr(vcpu, 9);
947
948 do_h_rpt_invalidate_prt(pid, vcpu->arch.nested->shadow_lpid,
949 type, pg_sizes, start, end);
950
951 kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
952 return RESUME_GUEST;
953 }
954
kvmppc_h_rpt_invalidate(struct kvm_vcpu * vcpu,unsigned long id,unsigned long target,unsigned long type,unsigned long pg_sizes,unsigned long start,unsigned long end)955 static long kvmppc_h_rpt_invalidate(struct kvm_vcpu *vcpu,
956 unsigned long id, unsigned long target,
957 unsigned long type, unsigned long pg_sizes,
958 unsigned long start, unsigned long end)
959 {
960 if (!kvm_is_radix(vcpu->kvm))
961 return H_UNSUPPORTED;
962
963 if (end < start)
964 return H_P5;
965
966 /*
967 * Partition-scoped invalidation for nested guests.
968 */
969 if (type & H_RPTI_TYPE_NESTED) {
970 if (!nesting_enabled(vcpu->kvm))
971 return H_FUNCTION;
972
973 /* Support only cores as target */
974 if (target != H_RPTI_TARGET_CMMU)
975 return H_P2;
976
977 return do_h_rpt_invalidate_pat(vcpu, id, type, pg_sizes,
978 start, end);
979 }
980
981 /*
982 * Process-scoped invalidation for L1 guests.
983 */
984 do_h_rpt_invalidate_prt(id, vcpu->kvm->arch.lpid,
985 type, pg_sizes, start, end);
986 return H_SUCCESS;
987 }
988
kvmppc_pseries_do_hcall(struct kvm_vcpu * vcpu)989 int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
990 {
991 struct kvm *kvm = vcpu->kvm;
992 unsigned long req = kvmppc_get_gpr(vcpu, 3);
993 unsigned long target, ret = H_SUCCESS;
994 int yield_count;
995 struct kvm_vcpu *tvcpu;
996 int idx, rc;
997
998 if (req <= MAX_HCALL_OPCODE &&
999 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
1000 return RESUME_HOST;
1001
1002 switch (req) {
1003 case H_REMOVE:
1004 ret = kvmppc_h_remove(vcpu, kvmppc_get_gpr(vcpu, 4),
1005 kvmppc_get_gpr(vcpu, 5),
1006 kvmppc_get_gpr(vcpu, 6));
1007 if (ret == H_TOO_HARD)
1008 return RESUME_HOST;
1009 break;
1010 case H_ENTER:
1011 ret = kvmppc_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
1012 kvmppc_get_gpr(vcpu, 5),
1013 kvmppc_get_gpr(vcpu, 6),
1014 kvmppc_get_gpr(vcpu, 7));
1015 if (ret == H_TOO_HARD)
1016 return RESUME_HOST;
1017 break;
1018 case H_READ:
1019 ret = kvmppc_h_read(vcpu, kvmppc_get_gpr(vcpu, 4),
1020 kvmppc_get_gpr(vcpu, 5));
1021 if (ret == H_TOO_HARD)
1022 return RESUME_HOST;
1023 break;
1024 case H_CLEAR_MOD:
1025 ret = kvmppc_h_clear_mod(vcpu, kvmppc_get_gpr(vcpu, 4),
1026 kvmppc_get_gpr(vcpu, 5));
1027 if (ret == H_TOO_HARD)
1028 return RESUME_HOST;
1029 break;
1030 case H_CLEAR_REF:
1031 ret = kvmppc_h_clear_ref(vcpu, kvmppc_get_gpr(vcpu, 4),
1032 kvmppc_get_gpr(vcpu, 5));
1033 if (ret == H_TOO_HARD)
1034 return RESUME_HOST;
1035 break;
1036 case H_PROTECT:
1037 ret = kvmppc_h_protect(vcpu, kvmppc_get_gpr(vcpu, 4),
1038 kvmppc_get_gpr(vcpu, 5),
1039 kvmppc_get_gpr(vcpu, 6));
1040 if (ret == H_TOO_HARD)
1041 return RESUME_HOST;
1042 break;
1043 case H_BULK_REMOVE:
1044 ret = kvmppc_h_bulk_remove(vcpu);
1045 if (ret == H_TOO_HARD)
1046 return RESUME_HOST;
1047 break;
1048
1049 case H_CEDE:
1050 break;
1051 case H_PROD:
1052 target = kvmppc_get_gpr(vcpu, 4);
1053 tvcpu = kvmppc_find_vcpu(kvm, target);
1054 if (!tvcpu) {
1055 ret = H_PARAMETER;
1056 break;
1057 }
1058 tvcpu->arch.prodded = 1;
1059 smp_mb();
1060 if (tvcpu->arch.ceded)
1061 kvmppc_fast_vcpu_kick_hv(tvcpu);
1062 break;
1063 case H_CONFER:
1064 target = kvmppc_get_gpr(vcpu, 4);
1065 if (target == -1)
1066 break;
1067 tvcpu = kvmppc_find_vcpu(kvm, target);
1068 if (!tvcpu) {
1069 ret = H_PARAMETER;
1070 break;
1071 }
1072 yield_count = kvmppc_get_gpr(vcpu, 5);
1073 if (kvmppc_get_yield_count(tvcpu) != yield_count)
1074 break;
1075 kvm_arch_vcpu_yield_to(tvcpu);
1076 break;
1077 case H_REGISTER_VPA:
1078 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
1079 kvmppc_get_gpr(vcpu, 5),
1080 kvmppc_get_gpr(vcpu, 6));
1081 break;
1082 case H_RTAS:
1083 if (list_empty(&kvm->arch.rtas_tokens))
1084 return RESUME_HOST;
1085
1086 idx = srcu_read_lock(&kvm->srcu);
1087 rc = kvmppc_rtas_hcall(vcpu);
1088 srcu_read_unlock(&kvm->srcu, idx);
1089
1090 if (rc == -ENOENT)
1091 return RESUME_HOST;
1092 else if (rc == 0)
1093 break;
1094
1095 /* Send the error out to userspace via KVM_RUN */
1096 return rc;
1097 case H_LOGICAL_CI_LOAD:
1098 ret = kvmppc_h_logical_ci_load(vcpu);
1099 if (ret == H_TOO_HARD)
1100 return RESUME_HOST;
1101 break;
1102 case H_LOGICAL_CI_STORE:
1103 ret = kvmppc_h_logical_ci_store(vcpu);
1104 if (ret == H_TOO_HARD)
1105 return RESUME_HOST;
1106 break;
1107 case H_SET_MODE:
1108 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
1109 kvmppc_get_gpr(vcpu, 5),
1110 kvmppc_get_gpr(vcpu, 6),
1111 kvmppc_get_gpr(vcpu, 7));
1112 if (ret == H_TOO_HARD)
1113 return RESUME_HOST;
1114 break;
1115 case H_XIRR:
1116 case H_CPPR:
1117 case H_EOI:
1118 case H_IPI:
1119 case H_IPOLL:
1120 case H_XIRR_X:
1121 if (kvmppc_xics_enabled(vcpu)) {
1122 if (xics_on_xive()) {
1123 ret = H_NOT_AVAILABLE;
1124 return RESUME_GUEST;
1125 }
1126 ret = kvmppc_xics_hcall(vcpu, req);
1127 break;
1128 }
1129 return RESUME_HOST;
1130 case H_SET_DABR:
1131 ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4));
1132 break;
1133 case H_SET_XDABR:
1134 ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4),
1135 kvmppc_get_gpr(vcpu, 5));
1136 break;
1137 #ifdef CONFIG_SPAPR_TCE_IOMMU
1138 case H_GET_TCE:
1139 ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1140 kvmppc_get_gpr(vcpu, 5));
1141 if (ret == H_TOO_HARD)
1142 return RESUME_HOST;
1143 break;
1144 case H_PUT_TCE:
1145 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1146 kvmppc_get_gpr(vcpu, 5),
1147 kvmppc_get_gpr(vcpu, 6));
1148 if (ret == H_TOO_HARD)
1149 return RESUME_HOST;
1150 break;
1151 case H_PUT_TCE_INDIRECT:
1152 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
1153 kvmppc_get_gpr(vcpu, 5),
1154 kvmppc_get_gpr(vcpu, 6),
1155 kvmppc_get_gpr(vcpu, 7));
1156 if (ret == H_TOO_HARD)
1157 return RESUME_HOST;
1158 break;
1159 case H_STUFF_TCE:
1160 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1161 kvmppc_get_gpr(vcpu, 5),
1162 kvmppc_get_gpr(vcpu, 6),
1163 kvmppc_get_gpr(vcpu, 7));
1164 if (ret == H_TOO_HARD)
1165 return RESUME_HOST;
1166 break;
1167 #endif
1168 case H_RANDOM:
1169 if (!arch_get_random_seed_long(&vcpu->arch.regs.gpr[4]))
1170 ret = H_HARDWARE;
1171 break;
1172 case H_RPT_INVALIDATE:
1173 ret = kvmppc_h_rpt_invalidate(vcpu, kvmppc_get_gpr(vcpu, 4),
1174 kvmppc_get_gpr(vcpu, 5),
1175 kvmppc_get_gpr(vcpu, 6),
1176 kvmppc_get_gpr(vcpu, 7),
1177 kvmppc_get_gpr(vcpu, 8),
1178 kvmppc_get_gpr(vcpu, 9));
1179 break;
1180
1181 case H_SET_PARTITION_TABLE:
1182 ret = H_FUNCTION;
1183 if (nesting_enabled(kvm))
1184 ret = kvmhv_set_partition_table(vcpu);
1185 break;
1186 case H_ENTER_NESTED:
1187 ret = H_FUNCTION;
1188 if (!nesting_enabled(kvm))
1189 break;
1190 ret = kvmhv_enter_nested_guest(vcpu);
1191 if (ret == H_INTERRUPT) {
1192 kvmppc_set_gpr(vcpu, 3, 0);
1193 vcpu->arch.hcall_needed = 0;
1194 return -EINTR;
1195 } else if (ret == H_TOO_HARD) {
1196 kvmppc_set_gpr(vcpu, 3, 0);
1197 vcpu->arch.hcall_needed = 0;
1198 return RESUME_HOST;
1199 }
1200 break;
1201 case H_TLB_INVALIDATE:
1202 ret = H_FUNCTION;
1203 if (nesting_enabled(kvm))
1204 ret = kvmhv_do_nested_tlbie(vcpu);
1205 break;
1206 case H_COPY_TOFROM_GUEST:
1207 ret = H_FUNCTION;
1208 if (nesting_enabled(kvm))
1209 ret = kvmhv_copy_tofrom_guest_nested(vcpu);
1210 break;
1211 case H_PAGE_INIT:
1212 ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4),
1213 kvmppc_get_gpr(vcpu, 5),
1214 kvmppc_get_gpr(vcpu, 6));
1215 break;
1216 case H_SVM_PAGE_IN:
1217 ret = H_UNSUPPORTED;
1218 if (kvmppc_get_srr1(vcpu) & MSR_S)
1219 ret = kvmppc_h_svm_page_in(kvm,
1220 kvmppc_get_gpr(vcpu, 4),
1221 kvmppc_get_gpr(vcpu, 5),
1222 kvmppc_get_gpr(vcpu, 6));
1223 break;
1224 case H_SVM_PAGE_OUT:
1225 ret = H_UNSUPPORTED;
1226 if (kvmppc_get_srr1(vcpu) & MSR_S)
1227 ret = kvmppc_h_svm_page_out(kvm,
1228 kvmppc_get_gpr(vcpu, 4),
1229 kvmppc_get_gpr(vcpu, 5),
1230 kvmppc_get_gpr(vcpu, 6));
1231 break;
1232 case H_SVM_INIT_START:
1233 ret = H_UNSUPPORTED;
1234 if (kvmppc_get_srr1(vcpu) & MSR_S)
1235 ret = kvmppc_h_svm_init_start(kvm);
1236 break;
1237 case H_SVM_INIT_DONE:
1238 ret = H_UNSUPPORTED;
1239 if (kvmppc_get_srr1(vcpu) & MSR_S)
1240 ret = kvmppc_h_svm_init_done(kvm);
1241 break;
1242 case H_SVM_INIT_ABORT:
1243 /*
1244 * Even if that call is made by the Ultravisor, the SSR1 value
1245 * is the guest context one, with the secure bit clear as it has
1246 * not yet been secured. So we can't check it here.
1247 * Instead the kvm->arch.secure_guest flag is checked inside
1248 * kvmppc_h_svm_init_abort().
1249 */
1250 ret = kvmppc_h_svm_init_abort(kvm);
1251 break;
1252
1253 default:
1254 return RESUME_HOST;
1255 }
1256 WARN_ON_ONCE(ret == H_TOO_HARD);
1257 kvmppc_set_gpr(vcpu, 3, ret);
1258 vcpu->arch.hcall_needed = 0;
1259 return RESUME_GUEST;
1260 }
1261
1262 /*
1263 * Handle H_CEDE in the P9 path where we don't call the real-mode hcall
1264 * handlers in book3s_hv_rmhandlers.S.
1265 *
1266 * This has to be done early, not in kvmppc_pseries_do_hcall(), so
1267 * that the cede logic in kvmppc_run_single_vcpu() works properly.
1268 */
kvmppc_cede(struct kvm_vcpu * vcpu)1269 static void kvmppc_cede(struct kvm_vcpu *vcpu)
1270 {
1271 vcpu->arch.shregs.msr |= MSR_EE;
1272 vcpu->arch.ceded = 1;
1273 smp_mb();
1274 if (vcpu->arch.prodded) {
1275 vcpu->arch.prodded = 0;
1276 smp_mb();
1277 vcpu->arch.ceded = 0;
1278 }
1279 }
1280
kvmppc_hcall_impl_hv(unsigned long cmd)1281 static int kvmppc_hcall_impl_hv(unsigned long cmd)
1282 {
1283 switch (cmd) {
1284 case H_CEDE:
1285 case H_PROD:
1286 case H_CONFER:
1287 case H_REGISTER_VPA:
1288 case H_SET_MODE:
1289 case H_LOGICAL_CI_LOAD:
1290 case H_LOGICAL_CI_STORE:
1291 #ifdef CONFIG_KVM_XICS
1292 case H_XIRR:
1293 case H_CPPR:
1294 case H_EOI:
1295 case H_IPI:
1296 case H_IPOLL:
1297 case H_XIRR_X:
1298 #endif
1299 case H_PAGE_INIT:
1300 case H_RPT_INVALIDATE:
1301 return 1;
1302 }
1303
1304 /* See if it's in the real-mode table */
1305 return kvmppc_hcall_impl_hv_realmode(cmd);
1306 }
1307
kvmppc_emulate_debug_inst(struct kvm_vcpu * vcpu)1308 static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu)
1309 {
1310 u32 last_inst;
1311
1312 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
1313 EMULATE_DONE) {
1314 /*
1315 * Fetch failed, so return to guest and
1316 * try executing it again.
1317 */
1318 return RESUME_GUEST;
1319 }
1320
1321 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
1322 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
1323 vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu);
1324 return RESUME_HOST;
1325 } else {
1326 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1327 return RESUME_GUEST;
1328 }
1329 }
1330
do_nothing(void * x)1331 static void do_nothing(void *x)
1332 {
1333 }
1334
kvmppc_read_dpdes(struct kvm_vcpu * vcpu)1335 static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
1336 {
1337 int thr, cpu, pcpu, nthreads;
1338 struct kvm_vcpu *v;
1339 unsigned long dpdes;
1340
1341 nthreads = vcpu->kvm->arch.emul_smt_mode;
1342 dpdes = 0;
1343 cpu = vcpu->vcpu_id & ~(nthreads - 1);
1344 for (thr = 0; thr < nthreads; ++thr, ++cpu) {
1345 v = kvmppc_find_vcpu(vcpu->kvm, cpu);
1346 if (!v)
1347 continue;
1348 /*
1349 * If the vcpu is currently running on a physical cpu thread,
1350 * interrupt it in order to pull it out of the guest briefly,
1351 * which will update its vcore->dpdes value.
1352 */
1353 pcpu = READ_ONCE(v->cpu);
1354 if (pcpu >= 0)
1355 smp_call_function_single(pcpu, do_nothing, NULL, 1);
1356 if (kvmppc_doorbell_pending(v))
1357 dpdes |= 1 << thr;
1358 }
1359 return dpdes;
1360 }
1361
1362 /*
1363 * On POWER9, emulate doorbell-related instructions in order to
1364 * give the guest the illusion of running on a multi-threaded core.
1365 * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
1366 * and mfspr DPDES.
1367 */
kvmppc_emulate_doorbell_instr(struct kvm_vcpu * vcpu)1368 static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
1369 {
1370 u32 inst, rb, thr;
1371 unsigned long arg;
1372 struct kvm *kvm = vcpu->kvm;
1373 struct kvm_vcpu *tvcpu;
1374
1375 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE)
1376 return RESUME_GUEST;
1377 if (get_op(inst) != 31)
1378 return EMULATE_FAIL;
1379 rb = get_rb(inst);
1380 thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
1381 switch (get_xop(inst)) {
1382 case OP_31_XOP_MSGSNDP:
1383 arg = kvmppc_get_gpr(vcpu, rb);
1384 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
1385 break;
1386 arg &= 0x7f;
1387 if (arg >= kvm->arch.emul_smt_mode)
1388 break;
1389 tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
1390 if (!tvcpu)
1391 break;
1392 if (!tvcpu->arch.doorbell_request) {
1393 tvcpu->arch.doorbell_request = 1;
1394 kvmppc_fast_vcpu_kick_hv(tvcpu);
1395 }
1396 break;
1397 case OP_31_XOP_MSGCLRP:
1398 arg = kvmppc_get_gpr(vcpu, rb);
1399 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
1400 break;
1401 vcpu->arch.vcore->dpdes = 0;
1402 vcpu->arch.doorbell_request = 0;
1403 break;
1404 case OP_31_XOP_MFSPR:
1405 switch (get_sprn(inst)) {
1406 case SPRN_TIR:
1407 arg = thr;
1408 break;
1409 case SPRN_DPDES:
1410 arg = kvmppc_read_dpdes(vcpu);
1411 break;
1412 default:
1413 return EMULATE_FAIL;
1414 }
1415 kvmppc_set_gpr(vcpu, get_rt(inst), arg);
1416 break;
1417 default:
1418 return EMULATE_FAIL;
1419 }
1420 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
1421 return RESUME_GUEST;
1422 }
1423
kvmppc_handle_exit_hv(struct kvm_vcpu * vcpu,struct task_struct * tsk)1424 static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
1425 struct task_struct *tsk)
1426 {
1427 struct kvm_run *run = vcpu->run;
1428 int r = RESUME_HOST;
1429
1430 vcpu->stat.sum_exits++;
1431
1432 /*
1433 * This can happen if an interrupt occurs in the last stages
1434 * of guest entry or the first stages of guest exit (i.e. after
1435 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1436 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1437 * That can happen due to a bug, or due to a machine check
1438 * occurring at just the wrong time.
1439 */
1440 if (vcpu->arch.shregs.msr & MSR_HV) {
1441 printk(KERN_EMERG "KVM trap in HV mode!\n");
1442 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1443 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1444 vcpu->arch.shregs.msr);
1445 kvmppc_dump_regs(vcpu);
1446 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1447 run->hw.hardware_exit_reason = vcpu->arch.trap;
1448 return RESUME_HOST;
1449 }
1450 run->exit_reason = KVM_EXIT_UNKNOWN;
1451 run->ready_for_interrupt_injection = 1;
1452 switch (vcpu->arch.trap) {
1453 /* We're good on these - the host merely wanted to get our attention */
1454 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1455 vcpu->stat.dec_exits++;
1456 r = RESUME_GUEST;
1457 break;
1458 case BOOK3S_INTERRUPT_EXTERNAL:
1459 case BOOK3S_INTERRUPT_H_DOORBELL:
1460 case BOOK3S_INTERRUPT_H_VIRT:
1461 vcpu->stat.ext_intr_exits++;
1462 r = RESUME_GUEST;
1463 break;
1464 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
1465 case BOOK3S_INTERRUPT_HMI:
1466 case BOOK3S_INTERRUPT_PERFMON:
1467 case BOOK3S_INTERRUPT_SYSTEM_RESET:
1468 r = RESUME_GUEST;
1469 break;
1470 case BOOK3S_INTERRUPT_MACHINE_CHECK: {
1471 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1472 DEFAULT_RATELIMIT_BURST);
1473 /*
1474 * Print the MCE event to host console. Ratelimit so the guest
1475 * can't flood the host log.
1476 */
1477 if (__ratelimit(&rs))
1478 machine_check_print_event_info(&vcpu->arch.mce_evt,false, true);
1479
1480 /*
1481 * If the guest can do FWNMI, exit to userspace so it can
1482 * deliver a FWNMI to the guest.
1483 * Otherwise we synthesize a machine check for the guest
1484 * so that it knows that the machine check occurred.
1485 */
1486 if (!vcpu->kvm->arch.fwnmi_enabled) {
1487 ulong flags = vcpu->arch.shregs.msr & 0x083c0000;
1488 kvmppc_core_queue_machine_check(vcpu, flags);
1489 r = RESUME_GUEST;
1490 break;
1491 }
1492
1493 /* Exit to guest with KVM_EXIT_NMI as exit reason */
1494 run->exit_reason = KVM_EXIT_NMI;
1495 run->hw.hardware_exit_reason = vcpu->arch.trap;
1496 /* Clear out the old NMI status from run->flags */
1497 run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
1498 /* Now set the NMI status */
1499 if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
1500 run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
1501 else
1502 run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
1503
1504 r = RESUME_HOST;
1505 break;
1506 }
1507 case BOOK3S_INTERRUPT_PROGRAM:
1508 {
1509 ulong flags;
1510 /*
1511 * Normally program interrupts are delivered directly
1512 * to the guest by the hardware, but we can get here
1513 * as a result of a hypervisor emulation interrupt
1514 * (e40) getting turned into a 700 by BML RTAS.
1515 */
1516 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
1517 kvmppc_core_queue_program(vcpu, flags);
1518 r = RESUME_GUEST;
1519 break;
1520 }
1521 case BOOK3S_INTERRUPT_SYSCALL:
1522 {
1523 int i;
1524
1525 if (unlikely(vcpu->arch.shregs.msr & MSR_PR)) {
1526 /*
1527 * Guest userspace executed sc 1. This can only be
1528 * reached by the P9 path because the old path
1529 * handles this case in realmode hcall handlers.
1530 */
1531 if (!kvmhv_vcpu_is_radix(vcpu)) {
1532 /*
1533 * A guest could be running PR KVM, so this
1534 * may be a PR KVM hcall. It must be reflected
1535 * to the guest kernel as a sc interrupt.
1536 */
1537 kvmppc_core_queue_syscall(vcpu);
1538 } else {
1539 /*
1540 * Radix guests can not run PR KVM or nested HV
1541 * hash guests which might run PR KVM, so this
1542 * is always a privilege fault. Send a program
1543 * check to guest kernel.
1544 */
1545 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
1546 }
1547 r = RESUME_GUEST;
1548 break;
1549 }
1550
1551 /*
1552 * hcall - gather args and set exit_reason. This will next be
1553 * handled by kvmppc_pseries_do_hcall which may be able to deal
1554 * with it and resume guest, or may punt to userspace.
1555 */
1556 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1557 for (i = 0; i < 9; ++i)
1558 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1559 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1560 vcpu->arch.hcall_needed = 1;
1561 r = RESUME_HOST;
1562 break;
1563 }
1564 /*
1565 * We get these next two if the guest accesses a page which it thinks
1566 * it has mapped but which is not actually present, either because
1567 * it is for an emulated I/O device or because the corresonding
1568 * host page has been paged out.
1569 *
1570 * Any other HDSI/HISI interrupts have been handled already for P7/8
1571 * guests. For POWER9 hash guests not using rmhandlers, basic hash
1572 * fault handling is done here.
1573 */
1574 case BOOK3S_INTERRUPT_H_DATA_STORAGE: {
1575 unsigned long vsid;
1576 long err;
1577
1578 if (vcpu->arch.fault_dsisr == HDSISR_CANARY) {
1579 r = RESUME_GUEST; /* Just retry if it's the canary */
1580 break;
1581 }
1582
1583 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1584 /*
1585 * Radix doesn't require anything, and pre-ISAv3.0 hash
1586 * already attempted to handle this in rmhandlers. The
1587 * hash fault handling below is v3 only (it uses ASDR
1588 * via fault_gpa).
1589 */
1590 r = RESUME_PAGE_FAULT;
1591 break;
1592 }
1593
1594 if (!(vcpu->arch.fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT))) {
1595 kvmppc_core_queue_data_storage(vcpu,
1596 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1597 r = RESUME_GUEST;
1598 break;
1599 }
1600
1601 if (!(vcpu->arch.shregs.msr & MSR_DR))
1602 vsid = vcpu->kvm->arch.vrma_slb_v;
1603 else
1604 vsid = vcpu->arch.fault_gpa;
1605
1606 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1607 vsid, vcpu->arch.fault_dsisr, true);
1608 if (err == 0) {
1609 r = RESUME_GUEST;
1610 } else if (err == -1 || err == -2) {
1611 r = RESUME_PAGE_FAULT;
1612 } else {
1613 kvmppc_core_queue_data_storage(vcpu,
1614 vcpu->arch.fault_dar, err);
1615 r = RESUME_GUEST;
1616 }
1617 break;
1618 }
1619 case BOOK3S_INTERRUPT_H_INST_STORAGE: {
1620 unsigned long vsid;
1621 long err;
1622
1623 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1624 vcpu->arch.fault_dsisr = vcpu->arch.shregs.msr &
1625 DSISR_SRR1_MATCH_64S;
1626 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1627 /*
1628 * Radix doesn't require anything, and pre-ISAv3.0 hash
1629 * already attempted to handle this in rmhandlers. The
1630 * hash fault handling below is v3 only (it uses ASDR
1631 * via fault_gpa).
1632 */
1633 if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
1634 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1635 r = RESUME_PAGE_FAULT;
1636 break;
1637 }
1638
1639 if (!(vcpu->arch.fault_dsisr & SRR1_ISI_NOPT)) {
1640 kvmppc_core_queue_inst_storage(vcpu,
1641 vcpu->arch.fault_dsisr);
1642 r = RESUME_GUEST;
1643 break;
1644 }
1645
1646 if (!(vcpu->arch.shregs.msr & MSR_IR))
1647 vsid = vcpu->kvm->arch.vrma_slb_v;
1648 else
1649 vsid = vcpu->arch.fault_gpa;
1650
1651 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1652 vsid, vcpu->arch.fault_dsisr, false);
1653 if (err == 0) {
1654 r = RESUME_GUEST;
1655 } else if (err == -1) {
1656 r = RESUME_PAGE_FAULT;
1657 } else {
1658 kvmppc_core_queue_inst_storage(vcpu, err);
1659 r = RESUME_GUEST;
1660 }
1661 break;
1662 }
1663
1664 /*
1665 * This occurs if the guest executes an illegal instruction.
1666 * If the guest debug is disabled, generate a program interrupt
1667 * to the guest. If guest debug is enabled, we need to check
1668 * whether the instruction is a software breakpoint instruction.
1669 * Accordingly return to Guest or Host.
1670 */
1671 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
1672 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1673 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1674 swab32(vcpu->arch.emul_inst) :
1675 vcpu->arch.emul_inst;
1676 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
1677 r = kvmppc_emulate_debug_inst(vcpu);
1678 } else {
1679 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1680 r = RESUME_GUEST;
1681 }
1682 break;
1683
1684 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1685 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1686 /*
1687 * This occurs for various TM-related instructions that
1688 * we need to emulate on POWER9 DD2.2. We have already
1689 * handled the cases where the guest was in real-suspend
1690 * mode and was transitioning to transactional state.
1691 */
1692 r = kvmhv_p9_tm_emulation(vcpu);
1693 if (r != -1)
1694 break;
1695 fallthrough; /* go to facility unavailable handler */
1696 #endif
1697
1698 /*
1699 * This occurs if the guest (kernel or userspace), does something that
1700 * is prohibited by HFSCR.
1701 * On POWER9, this could be a doorbell instruction that we need
1702 * to emulate.
1703 * Otherwise, we just generate a program interrupt to the guest.
1704 */
1705 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
1706 r = EMULATE_FAIL;
1707 if (((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG) &&
1708 cpu_has_feature(CPU_FTR_ARCH_300))
1709 r = kvmppc_emulate_doorbell_instr(vcpu);
1710 if (r == EMULATE_FAIL) {
1711 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1712 r = RESUME_GUEST;
1713 }
1714 break;
1715
1716 case BOOK3S_INTERRUPT_HV_RM_HARD:
1717 r = RESUME_PASSTHROUGH;
1718 break;
1719 default:
1720 kvmppc_dump_regs(vcpu);
1721 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1722 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1723 vcpu->arch.shregs.msr);
1724 run->hw.hardware_exit_reason = vcpu->arch.trap;
1725 r = RESUME_HOST;
1726 break;
1727 }
1728
1729 return r;
1730 }
1731
kvmppc_handle_nested_exit(struct kvm_vcpu * vcpu)1732 static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
1733 {
1734 int r;
1735 int srcu_idx;
1736
1737 vcpu->stat.sum_exits++;
1738
1739 /*
1740 * This can happen if an interrupt occurs in the last stages
1741 * of guest entry or the first stages of guest exit (i.e. after
1742 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1743 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1744 * That can happen due to a bug, or due to a machine check
1745 * occurring at just the wrong time.
1746 */
1747 if (vcpu->arch.shregs.msr & MSR_HV) {
1748 pr_emerg("KVM trap in HV mode while nested!\n");
1749 pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1750 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1751 vcpu->arch.shregs.msr);
1752 kvmppc_dump_regs(vcpu);
1753 return RESUME_HOST;
1754 }
1755 switch (vcpu->arch.trap) {
1756 /* We're good on these - the host merely wanted to get our attention */
1757 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1758 vcpu->stat.dec_exits++;
1759 r = RESUME_GUEST;
1760 break;
1761 case BOOK3S_INTERRUPT_EXTERNAL:
1762 vcpu->stat.ext_intr_exits++;
1763 r = RESUME_HOST;
1764 break;
1765 case BOOK3S_INTERRUPT_H_DOORBELL:
1766 case BOOK3S_INTERRUPT_H_VIRT:
1767 vcpu->stat.ext_intr_exits++;
1768 r = RESUME_GUEST;
1769 break;
1770 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
1771 case BOOK3S_INTERRUPT_HMI:
1772 case BOOK3S_INTERRUPT_PERFMON:
1773 case BOOK3S_INTERRUPT_SYSTEM_RESET:
1774 r = RESUME_GUEST;
1775 break;
1776 case BOOK3S_INTERRUPT_MACHINE_CHECK:
1777 {
1778 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1779 DEFAULT_RATELIMIT_BURST);
1780 /* Pass the machine check to the L1 guest */
1781 r = RESUME_HOST;
1782 /* Print the MCE event to host console. */
1783 if (__ratelimit(&rs))
1784 machine_check_print_event_info(&vcpu->arch.mce_evt, false, true);
1785 break;
1786 }
1787 /*
1788 * We get these next two if the guest accesses a page which it thinks
1789 * it has mapped but which is not actually present, either because
1790 * it is for an emulated I/O device or because the corresonding
1791 * host page has been paged out.
1792 */
1793 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
1794 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1795 r = kvmhv_nested_page_fault(vcpu);
1796 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1797 break;
1798 case BOOK3S_INTERRUPT_H_INST_STORAGE:
1799 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1800 vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) &
1801 DSISR_SRR1_MATCH_64S;
1802 if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
1803 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1804 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
1805 r = kvmhv_nested_page_fault(vcpu);
1806 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1807 break;
1808
1809 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1810 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1811 /*
1812 * This occurs for various TM-related instructions that
1813 * we need to emulate on POWER9 DD2.2. We have already
1814 * handled the cases where the guest was in real-suspend
1815 * mode and was transitioning to transactional state.
1816 */
1817 r = kvmhv_p9_tm_emulation(vcpu);
1818 if (r != -1)
1819 break;
1820 fallthrough; /* go to facility unavailable handler */
1821 #endif
1822
1823 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
1824 u64 cause = vcpu->arch.hfscr >> 56;
1825
1826 /*
1827 * Only pass HFU interrupts to the L1 if the facility is
1828 * permitted but disabled by the L1's HFSCR, otherwise
1829 * the interrupt does not make sense to the L1 so turn
1830 * it into a HEAI.
1831 */
1832 if (!(vcpu->arch.hfscr_permitted & (1UL << cause)) ||
1833 (vcpu->arch.nested_hfscr & (1UL << cause))) {
1834 vcpu->arch.trap = BOOK3S_INTERRUPT_H_EMUL_ASSIST;
1835
1836 /*
1837 * If the fetch failed, return to guest and
1838 * try executing it again.
1839 */
1840 r = kvmppc_get_last_inst(vcpu, INST_GENERIC,
1841 &vcpu->arch.emul_inst);
1842 if (r != EMULATE_DONE)
1843 r = RESUME_GUEST;
1844 else
1845 r = RESUME_HOST;
1846 } else {
1847 r = RESUME_HOST;
1848 }
1849
1850 break;
1851 }
1852
1853 case BOOK3S_INTERRUPT_HV_RM_HARD:
1854 vcpu->arch.trap = 0;
1855 r = RESUME_GUEST;
1856 if (!xics_on_xive())
1857 kvmppc_xics_rm_complete(vcpu, 0);
1858 break;
1859 case BOOK3S_INTERRUPT_SYSCALL:
1860 {
1861 unsigned long req = kvmppc_get_gpr(vcpu, 3);
1862
1863 /*
1864 * The H_RPT_INVALIDATE hcalls issued by nested
1865 * guests for process-scoped invalidations when
1866 * GTSE=0, are handled here in L0.
1867 */
1868 if (req == H_RPT_INVALIDATE) {
1869 r = kvmppc_nested_h_rpt_invalidate(vcpu);
1870 break;
1871 }
1872
1873 r = RESUME_HOST;
1874 break;
1875 }
1876 default:
1877 r = RESUME_HOST;
1878 break;
1879 }
1880
1881 return r;
1882 }
1883
kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)1884 static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1885 struct kvm_sregs *sregs)
1886 {
1887 int i;
1888
1889 memset(sregs, 0, sizeof(struct kvm_sregs));
1890 sregs->pvr = vcpu->arch.pvr;
1891 for (i = 0; i < vcpu->arch.slb_max; i++) {
1892 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1893 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1894 }
1895
1896 return 0;
1897 }
1898
kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu * vcpu,struct kvm_sregs * sregs)1899 static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1900 struct kvm_sregs *sregs)
1901 {
1902 int i, j;
1903
1904 /* Only accept the same PVR as the host's, since we can't spoof it */
1905 if (sregs->pvr != vcpu->arch.pvr)
1906 return -EINVAL;
1907
1908 j = 0;
1909 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1910 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1911 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1912 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1913 ++j;
1914 }
1915 }
1916 vcpu->arch.slb_max = j;
1917
1918 return 0;
1919 }
1920
1921 /*
1922 * Enforce limits on guest LPCR values based on hardware availability,
1923 * guest configuration, and possibly hypervisor support and security
1924 * concerns.
1925 */
kvmppc_filter_lpcr_hv(struct kvm * kvm,unsigned long lpcr)1926 unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr)
1927 {
1928 /* LPCR_TC only applies to HPT guests */
1929 if (kvm_is_radix(kvm))
1930 lpcr &= ~LPCR_TC;
1931
1932 /* On POWER8 and above, userspace can modify AIL */
1933 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
1934 lpcr &= ~LPCR_AIL;
1935 if ((lpcr & LPCR_AIL) != LPCR_AIL_3)
1936 lpcr &= ~LPCR_AIL; /* LPCR[AIL]=1/2 is disallowed */
1937 /*
1938 * On some POWER9s we force AIL off for radix guests to prevent
1939 * executing in MSR[HV]=1 mode with the MMU enabled and PIDR set to
1940 * guest, which can result in Q0 translations with LPID=0 PID=PIDR to
1941 * be cached, which the host TLB management does not expect.
1942 */
1943 if (kvm_is_radix(kvm) && cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))
1944 lpcr &= ~LPCR_AIL;
1945
1946 /*
1947 * On POWER9, allow userspace to enable large decrementer for the
1948 * guest, whether or not the host has it enabled.
1949 */
1950 if (!cpu_has_feature(CPU_FTR_ARCH_300))
1951 lpcr &= ~LPCR_LD;
1952
1953 return lpcr;
1954 }
1955
verify_lpcr(struct kvm * kvm,unsigned long lpcr)1956 static void verify_lpcr(struct kvm *kvm, unsigned long lpcr)
1957 {
1958 if (lpcr != kvmppc_filter_lpcr_hv(kvm, lpcr)) {
1959 WARN_ONCE(1, "lpcr 0x%lx differs from filtered 0x%lx\n",
1960 lpcr, kvmppc_filter_lpcr_hv(kvm, lpcr));
1961 }
1962 }
1963
kvmppc_set_lpcr(struct kvm_vcpu * vcpu,u64 new_lpcr,bool preserve_top32)1964 static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1965 bool preserve_top32)
1966 {
1967 struct kvm *kvm = vcpu->kvm;
1968 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1969 u64 mask;
1970
1971 spin_lock(&vc->lock);
1972
1973 /*
1974 * Userspace can only modify
1975 * DPFD (default prefetch depth), ILE (interrupt little-endian),
1976 * TC (translation control), AIL (alternate interrupt location),
1977 * LD (large decrementer).
1978 * These are subject to restrictions from kvmppc_filter_lcpr_hv().
1979 */
1980 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD;
1981
1982 /* Broken 32-bit version of LPCR must not clear top bits */
1983 if (preserve_top32)
1984 mask &= 0xFFFFFFFF;
1985
1986 new_lpcr = kvmppc_filter_lpcr_hv(kvm,
1987 (vc->lpcr & ~mask) | (new_lpcr & mask));
1988
1989 /*
1990 * If ILE (interrupt little-endian) has changed, update the
1991 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1992 */
1993 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
1994 struct kvm_vcpu *vcpu;
1995 int i;
1996
1997 kvm_for_each_vcpu(i, vcpu, kvm) {
1998 if (vcpu->arch.vcore != vc)
1999 continue;
2000 if (new_lpcr & LPCR_ILE)
2001 vcpu->arch.intr_msr |= MSR_LE;
2002 else
2003 vcpu->arch.intr_msr &= ~MSR_LE;
2004 }
2005 }
2006
2007 vc->lpcr = new_lpcr;
2008
2009 spin_unlock(&vc->lock);
2010 }
2011
kvmppc_get_one_reg_hv(struct kvm_vcpu * vcpu,u64 id,union kvmppc_one_reg * val)2012 static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2013 union kvmppc_one_reg *val)
2014 {
2015 int r = 0;
2016 long int i;
2017
2018 switch (id) {
2019 case KVM_REG_PPC_DEBUG_INST:
2020 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
2021 break;
2022 case KVM_REG_PPC_HIOR:
2023 *val = get_reg_val(id, 0);
2024 break;
2025 case KVM_REG_PPC_DABR:
2026 *val = get_reg_val(id, vcpu->arch.dabr);
2027 break;
2028 case KVM_REG_PPC_DABRX:
2029 *val = get_reg_val(id, vcpu->arch.dabrx);
2030 break;
2031 case KVM_REG_PPC_DSCR:
2032 *val = get_reg_val(id, vcpu->arch.dscr);
2033 break;
2034 case KVM_REG_PPC_PURR:
2035 *val = get_reg_val(id, vcpu->arch.purr);
2036 break;
2037 case KVM_REG_PPC_SPURR:
2038 *val = get_reg_val(id, vcpu->arch.spurr);
2039 break;
2040 case KVM_REG_PPC_AMR:
2041 *val = get_reg_val(id, vcpu->arch.amr);
2042 break;
2043 case KVM_REG_PPC_UAMOR:
2044 *val = get_reg_val(id, vcpu->arch.uamor);
2045 break;
2046 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
2047 i = id - KVM_REG_PPC_MMCR0;
2048 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
2049 break;
2050 case KVM_REG_PPC_MMCR2:
2051 *val = get_reg_val(id, vcpu->arch.mmcr[2]);
2052 break;
2053 case KVM_REG_PPC_MMCRA:
2054 *val = get_reg_val(id, vcpu->arch.mmcra);
2055 break;
2056 case KVM_REG_PPC_MMCRS:
2057 *val = get_reg_val(id, vcpu->arch.mmcrs);
2058 break;
2059 case KVM_REG_PPC_MMCR3:
2060 *val = get_reg_val(id, vcpu->arch.mmcr[3]);
2061 break;
2062 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2063 i = id - KVM_REG_PPC_PMC1;
2064 *val = get_reg_val(id, vcpu->arch.pmc[i]);
2065 break;
2066 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2067 i = id - KVM_REG_PPC_SPMC1;
2068 *val = get_reg_val(id, vcpu->arch.spmc[i]);
2069 break;
2070 case KVM_REG_PPC_SIAR:
2071 *val = get_reg_val(id, vcpu->arch.siar);
2072 break;
2073 case KVM_REG_PPC_SDAR:
2074 *val = get_reg_val(id, vcpu->arch.sdar);
2075 break;
2076 case KVM_REG_PPC_SIER:
2077 *val = get_reg_val(id, vcpu->arch.sier[0]);
2078 break;
2079 case KVM_REG_PPC_SIER2:
2080 *val = get_reg_val(id, vcpu->arch.sier[1]);
2081 break;
2082 case KVM_REG_PPC_SIER3:
2083 *val = get_reg_val(id, vcpu->arch.sier[2]);
2084 break;
2085 case KVM_REG_PPC_IAMR:
2086 *val = get_reg_val(id, vcpu->arch.iamr);
2087 break;
2088 case KVM_REG_PPC_PSPB:
2089 *val = get_reg_val(id, vcpu->arch.pspb);
2090 break;
2091 case KVM_REG_PPC_DPDES:
2092 /*
2093 * On POWER9, where we are emulating msgsndp etc.,
2094 * we return 1 bit for each vcpu, which can come from
2095 * either vcore->dpdes or doorbell_request.
2096 * On POWER8, doorbell_request is 0.
2097 */
2098 *val = get_reg_val(id, vcpu->arch.vcore->dpdes |
2099 vcpu->arch.doorbell_request);
2100 break;
2101 case KVM_REG_PPC_VTB:
2102 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
2103 break;
2104 case KVM_REG_PPC_DAWR:
2105 *val = get_reg_val(id, vcpu->arch.dawr0);
2106 break;
2107 case KVM_REG_PPC_DAWRX:
2108 *val = get_reg_val(id, vcpu->arch.dawrx0);
2109 break;
2110 case KVM_REG_PPC_DAWR1:
2111 *val = get_reg_val(id, vcpu->arch.dawr1);
2112 break;
2113 case KVM_REG_PPC_DAWRX1:
2114 *val = get_reg_val(id, vcpu->arch.dawrx1);
2115 break;
2116 case KVM_REG_PPC_CIABR:
2117 *val = get_reg_val(id, vcpu->arch.ciabr);
2118 break;
2119 case KVM_REG_PPC_CSIGR:
2120 *val = get_reg_val(id, vcpu->arch.csigr);
2121 break;
2122 case KVM_REG_PPC_TACR:
2123 *val = get_reg_val(id, vcpu->arch.tacr);
2124 break;
2125 case KVM_REG_PPC_TCSCR:
2126 *val = get_reg_val(id, vcpu->arch.tcscr);
2127 break;
2128 case KVM_REG_PPC_PID:
2129 *val = get_reg_val(id, vcpu->arch.pid);
2130 break;
2131 case KVM_REG_PPC_ACOP:
2132 *val = get_reg_val(id, vcpu->arch.acop);
2133 break;
2134 case KVM_REG_PPC_WORT:
2135 *val = get_reg_val(id, vcpu->arch.wort);
2136 break;
2137 case KVM_REG_PPC_TIDR:
2138 *val = get_reg_val(id, vcpu->arch.tid);
2139 break;
2140 case KVM_REG_PPC_PSSCR:
2141 *val = get_reg_val(id, vcpu->arch.psscr);
2142 break;
2143 case KVM_REG_PPC_VPA_ADDR:
2144 spin_lock(&vcpu->arch.vpa_update_lock);
2145 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
2146 spin_unlock(&vcpu->arch.vpa_update_lock);
2147 break;
2148 case KVM_REG_PPC_VPA_SLB:
2149 spin_lock(&vcpu->arch.vpa_update_lock);
2150 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
2151 val->vpaval.length = vcpu->arch.slb_shadow.len;
2152 spin_unlock(&vcpu->arch.vpa_update_lock);
2153 break;
2154 case KVM_REG_PPC_VPA_DTL:
2155 spin_lock(&vcpu->arch.vpa_update_lock);
2156 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
2157 val->vpaval.length = vcpu->arch.dtl.len;
2158 spin_unlock(&vcpu->arch.vpa_update_lock);
2159 break;
2160 case KVM_REG_PPC_TB_OFFSET:
2161 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
2162 break;
2163 case KVM_REG_PPC_LPCR:
2164 case KVM_REG_PPC_LPCR_64:
2165 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
2166 break;
2167 case KVM_REG_PPC_PPR:
2168 *val = get_reg_val(id, vcpu->arch.ppr);
2169 break;
2170 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2171 case KVM_REG_PPC_TFHAR:
2172 *val = get_reg_val(id, vcpu->arch.tfhar);
2173 break;
2174 case KVM_REG_PPC_TFIAR:
2175 *val = get_reg_val(id, vcpu->arch.tfiar);
2176 break;
2177 case KVM_REG_PPC_TEXASR:
2178 *val = get_reg_val(id, vcpu->arch.texasr);
2179 break;
2180 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2181 i = id - KVM_REG_PPC_TM_GPR0;
2182 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
2183 break;
2184 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2185 {
2186 int j;
2187 i = id - KVM_REG_PPC_TM_VSR0;
2188 if (i < 32)
2189 for (j = 0; j < TS_FPRWIDTH; j++)
2190 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
2191 else {
2192 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2193 val->vval = vcpu->arch.vr_tm.vr[i-32];
2194 else
2195 r = -ENXIO;
2196 }
2197 break;
2198 }
2199 case KVM_REG_PPC_TM_CR:
2200 *val = get_reg_val(id, vcpu->arch.cr_tm);
2201 break;
2202 case KVM_REG_PPC_TM_XER:
2203 *val = get_reg_val(id, vcpu->arch.xer_tm);
2204 break;
2205 case KVM_REG_PPC_TM_LR:
2206 *val = get_reg_val(id, vcpu->arch.lr_tm);
2207 break;
2208 case KVM_REG_PPC_TM_CTR:
2209 *val = get_reg_val(id, vcpu->arch.ctr_tm);
2210 break;
2211 case KVM_REG_PPC_TM_FPSCR:
2212 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
2213 break;
2214 case KVM_REG_PPC_TM_AMR:
2215 *val = get_reg_val(id, vcpu->arch.amr_tm);
2216 break;
2217 case KVM_REG_PPC_TM_PPR:
2218 *val = get_reg_val(id, vcpu->arch.ppr_tm);
2219 break;
2220 case KVM_REG_PPC_TM_VRSAVE:
2221 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
2222 break;
2223 case KVM_REG_PPC_TM_VSCR:
2224 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2225 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
2226 else
2227 r = -ENXIO;
2228 break;
2229 case KVM_REG_PPC_TM_DSCR:
2230 *val = get_reg_val(id, vcpu->arch.dscr_tm);
2231 break;
2232 case KVM_REG_PPC_TM_TAR:
2233 *val = get_reg_val(id, vcpu->arch.tar_tm);
2234 break;
2235 #endif
2236 case KVM_REG_PPC_ARCH_COMPAT:
2237 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
2238 break;
2239 case KVM_REG_PPC_DEC_EXPIRY:
2240 *val = get_reg_val(id, vcpu->arch.dec_expires +
2241 vcpu->arch.vcore->tb_offset);
2242 break;
2243 case KVM_REG_PPC_ONLINE:
2244 *val = get_reg_val(id, vcpu->arch.online);
2245 break;
2246 case KVM_REG_PPC_PTCR:
2247 *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr);
2248 break;
2249 default:
2250 r = -EINVAL;
2251 break;
2252 }
2253
2254 return r;
2255 }
2256
kvmppc_set_one_reg_hv(struct kvm_vcpu * vcpu,u64 id,union kvmppc_one_reg * val)2257 static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2258 union kvmppc_one_reg *val)
2259 {
2260 int r = 0;
2261 long int i;
2262 unsigned long addr, len;
2263
2264 switch (id) {
2265 case KVM_REG_PPC_HIOR:
2266 /* Only allow this to be set to zero */
2267 if (set_reg_val(id, *val))
2268 r = -EINVAL;
2269 break;
2270 case KVM_REG_PPC_DABR:
2271 vcpu->arch.dabr = set_reg_val(id, *val);
2272 break;
2273 case KVM_REG_PPC_DABRX:
2274 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
2275 break;
2276 case KVM_REG_PPC_DSCR:
2277 vcpu->arch.dscr = set_reg_val(id, *val);
2278 break;
2279 case KVM_REG_PPC_PURR:
2280 vcpu->arch.purr = set_reg_val(id, *val);
2281 break;
2282 case KVM_REG_PPC_SPURR:
2283 vcpu->arch.spurr = set_reg_val(id, *val);
2284 break;
2285 case KVM_REG_PPC_AMR:
2286 vcpu->arch.amr = set_reg_val(id, *val);
2287 break;
2288 case KVM_REG_PPC_UAMOR:
2289 vcpu->arch.uamor = set_reg_val(id, *val);
2290 break;
2291 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
2292 i = id - KVM_REG_PPC_MMCR0;
2293 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
2294 break;
2295 case KVM_REG_PPC_MMCR2:
2296 vcpu->arch.mmcr[2] = set_reg_val(id, *val);
2297 break;
2298 case KVM_REG_PPC_MMCRA:
2299 vcpu->arch.mmcra = set_reg_val(id, *val);
2300 break;
2301 case KVM_REG_PPC_MMCRS:
2302 vcpu->arch.mmcrs = set_reg_val(id, *val);
2303 break;
2304 case KVM_REG_PPC_MMCR3:
2305 *val = get_reg_val(id, vcpu->arch.mmcr[3]);
2306 break;
2307 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2308 i = id - KVM_REG_PPC_PMC1;
2309 vcpu->arch.pmc[i] = set_reg_val(id, *val);
2310 break;
2311 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2312 i = id - KVM_REG_PPC_SPMC1;
2313 vcpu->arch.spmc[i] = set_reg_val(id, *val);
2314 break;
2315 case KVM_REG_PPC_SIAR:
2316 vcpu->arch.siar = set_reg_val(id, *val);
2317 break;
2318 case KVM_REG_PPC_SDAR:
2319 vcpu->arch.sdar = set_reg_val(id, *val);
2320 break;
2321 case KVM_REG_PPC_SIER:
2322 vcpu->arch.sier[0] = set_reg_val(id, *val);
2323 break;
2324 case KVM_REG_PPC_SIER2:
2325 vcpu->arch.sier[1] = set_reg_val(id, *val);
2326 break;
2327 case KVM_REG_PPC_SIER3:
2328 vcpu->arch.sier[2] = set_reg_val(id, *val);
2329 break;
2330 case KVM_REG_PPC_IAMR:
2331 vcpu->arch.iamr = set_reg_val(id, *val);
2332 break;
2333 case KVM_REG_PPC_PSPB:
2334 vcpu->arch.pspb = set_reg_val(id, *val);
2335 break;
2336 case KVM_REG_PPC_DPDES:
2337 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
2338 break;
2339 case KVM_REG_PPC_VTB:
2340 vcpu->arch.vcore->vtb = set_reg_val(id, *val);
2341 break;
2342 case KVM_REG_PPC_DAWR:
2343 vcpu->arch.dawr0 = set_reg_val(id, *val);
2344 break;
2345 case KVM_REG_PPC_DAWRX:
2346 vcpu->arch.dawrx0 = set_reg_val(id, *val) & ~DAWRX_HYP;
2347 break;
2348 case KVM_REG_PPC_DAWR1:
2349 vcpu->arch.dawr1 = set_reg_val(id, *val);
2350 break;
2351 case KVM_REG_PPC_DAWRX1:
2352 vcpu->arch.dawrx1 = set_reg_val(id, *val) & ~DAWRX_HYP;
2353 break;
2354 case KVM_REG_PPC_CIABR:
2355 vcpu->arch.ciabr = set_reg_val(id, *val);
2356 /* Don't allow setting breakpoints in hypervisor code */
2357 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
2358 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
2359 break;
2360 case KVM_REG_PPC_CSIGR:
2361 vcpu->arch.csigr = set_reg_val(id, *val);
2362 break;
2363 case KVM_REG_PPC_TACR:
2364 vcpu->arch.tacr = set_reg_val(id, *val);
2365 break;
2366 case KVM_REG_PPC_TCSCR:
2367 vcpu->arch.tcscr = set_reg_val(id, *val);
2368 break;
2369 case KVM_REG_PPC_PID:
2370 vcpu->arch.pid = set_reg_val(id, *val);
2371 break;
2372 case KVM_REG_PPC_ACOP:
2373 vcpu->arch.acop = set_reg_val(id, *val);
2374 break;
2375 case KVM_REG_PPC_WORT:
2376 vcpu->arch.wort = set_reg_val(id, *val);
2377 break;
2378 case KVM_REG_PPC_TIDR:
2379 vcpu->arch.tid = set_reg_val(id, *val);
2380 break;
2381 case KVM_REG_PPC_PSSCR:
2382 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
2383 break;
2384 case KVM_REG_PPC_VPA_ADDR:
2385 addr = set_reg_val(id, *val);
2386 r = -EINVAL;
2387 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
2388 vcpu->arch.dtl.next_gpa))
2389 break;
2390 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
2391 break;
2392 case KVM_REG_PPC_VPA_SLB:
2393 addr = val->vpaval.addr;
2394 len = val->vpaval.length;
2395 r = -EINVAL;
2396 if (addr && !vcpu->arch.vpa.next_gpa)
2397 break;
2398 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
2399 break;
2400 case KVM_REG_PPC_VPA_DTL:
2401 addr = val->vpaval.addr;
2402 len = val->vpaval.length;
2403 r = -EINVAL;
2404 if (addr && (len < sizeof(struct dtl_entry) ||
2405 !vcpu->arch.vpa.next_gpa))
2406 break;
2407 len -= len % sizeof(struct dtl_entry);
2408 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
2409 break;
2410 case KVM_REG_PPC_TB_OFFSET:
2411 /* round up to multiple of 2^24 */
2412 vcpu->arch.vcore->tb_offset =
2413 ALIGN(set_reg_val(id, *val), 1UL << 24);
2414 break;
2415 case KVM_REG_PPC_LPCR:
2416 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
2417 break;
2418 case KVM_REG_PPC_LPCR_64:
2419 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
2420 break;
2421 case KVM_REG_PPC_PPR:
2422 vcpu->arch.ppr = set_reg_val(id, *val);
2423 break;
2424 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2425 case KVM_REG_PPC_TFHAR:
2426 vcpu->arch.tfhar = set_reg_val(id, *val);
2427 break;
2428 case KVM_REG_PPC_TFIAR:
2429 vcpu->arch.tfiar = set_reg_val(id, *val);
2430 break;
2431 case KVM_REG_PPC_TEXASR:
2432 vcpu->arch.texasr = set_reg_val(id, *val);
2433 break;
2434 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2435 i = id - KVM_REG_PPC_TM_GPR0;
2436 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
2437 break;
2438 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2439 {
2440 int j;
2441 i = id - KVM_REG_PPC_TM_VSR0;
2442 if (i < 32)
2443 for (j = 0; j < TS_FPRWIDTH; j++)
2444 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
2445 else
2446 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2447 vcpu->arch.vr_tm.vr[i-32] = val->vval;
2448 else
2449 r = -ENXIO;
2450 break;
2451 }
2452 case KVM_REG_PPC_TM_CR:
2453 vcpu->arch.cr_tm = set_reg_val(id, *val);
2454 break;
2455 case KVM_REG_PPC_TM_XER:
2456 vcpu->arch.xer_tm = set_reg_val(id, *val);
2457 break;
2458 case KVM_REG_PPC_TM_LR:
2459 vcpu->arch.lr_tm = set_reg_val(id, *val);
2460 break;
2461 case KVM_REG_PPC_TM_CTR:
2462 vcpu->arch.ctr_tm = set_reg_val(id, *val);
2463 break;
2464 case KVM_REG_PPC_TM_FPSCR:
2465 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
2466 break;
2467 case KVM_REG_PPC_TM_AMR:
2468 vcpu->arch.amr_tm = set_reg_val(id, *val);
2469 break;
2470 case KVM_REG_PPC_TM_PPR:
2471 vcpu->arch.ppr_tm = set_reg_val(id, *val);
2472 break;
2473 case KVM_REG_PPC_TM_VRSAVE:
2474 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
2475 break;
2476 case KVM_REG_PPC_TM_VSCR:
2477 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2478 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
2479 else
2480 r = - ENXIO;
2481 break;
2482 case KVM_REG_PPC_TM_DSCR:
2483 vcpu->arch.dscr_tm = set_reg_val(id, *val);
2484 break;
2485 case KVM_REG_PPC_TM_TAR:
2486 vcpu->arch.tar_tm = set_reg_val(id, *val);
2487 break;
2488 #endif
2489 case KVM_REG_PPC_ARCH_COMPAT:
2490 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
2491 break;
2492 case KVM_REG_PPC_DEC_EXPIRY:
2493 vcpu->arch.dec_expires = set_reg_val(id, *val) -
2494 vcpu->arch.vcore->tb_offset;
2495 break;
2496 case KVM_REG_PPC_ONLINE:
2497 i = set_reg_val(id, *val);
2498 if (i && !vcpu->arch.online)
2499 atomic_inc(&vcpu->arch.vcore->online_count);
2500 else if (!i && vcpu->arch.online)
2501 atomic_dec(&vcpu->arch.vcore->online_count);
2502 vcpu->arch.online = i;
2503 break;
2504 case KVM_REG_PPC_PTCR:
2505 vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val);
2506 break;
2507 default:
2508 r = -EINVAL;
2509 break;
2510 }
2511
2512 return r;
2513 }
2514
2515 /*
2516 * On POWER9, threads are independent and can be in different partitions.
2517 * Therefore we consider each thread to be a subcore.
2518 * There is a restriction that all threads have to be in the same
2519 * MMU mode (radix or HPT), unfortunately, but since we only support
2520 * HPT guests on a HPT host so far, that isn't an impediment yet.
2521 */
threads_per_vcore(struct kvm * kvm)2522 static int threads_per_vcore(struct kvm *kvm)
2523 {
2524 if (cpu_has_feature(CPU_FTR_ARCH_300))
2525 return 1;
2526 return threads_per_subcore;
2527 }
2528
kvmppc_vcore_create(struct kvm * kvm,int id)2529 static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
2530 {
2531 struct kvmppc_vcore *vcore;
2532
2533 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
2534
2535 if (vcore == NULL)
2536 return NULL;
2537
2538 spin_lock_init(&vcore->lock);
2539 spin_lock_init(&vcore->stoltb_lock);
2540 rcuwait_init(&vcore->wait);
2541 vcore->preempt_tb = TB_NIL;
2542 vcore->lpcr = kvm->arch.lpcr;
2543 vcore->first_vcpuid = id;
2544 vcore->kvm = kvm;
2545 INIT_LIST_HEAD(&vcore->preempt_list);
2546
2547 return vcore;
2548 }
2549
2550 #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2551 static struct debugfs_timings_element {
2552 const char *name;
2553 size_t offset;
2554 } timings[] = {
2555 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
2556 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
2557 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
2558 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
2559 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
2560 };
2561
2562 #define N_TIMINGS (ARRAY_SIZE(timings))
2563
2564 struct debugfs_timings_state {
2565 struct kvm_vcpu *vcpu;
2566 unsigned int buflen;
2567 char buf[N_TIMINGS * 100];
2568 };
2569
debugfs_timings_open(struct inode * inode,struct file * file)2570 static int debugfs_timings_open(struct inode *inode, struct file *file)
2571 {
2572 struct kvm_vcpu *vcpu = inode->i_private;
2573 struct debugfs_timings_state *p;
2574
2575 p = kzalloc(sizeof(*p), GFP_KERNEL);
2576 if (!p)
2577 return -ENOMEM;
2578
2579 kvm_get_kvm(vcpu->kvm);
2580 p->vcpu = vcpu;
2581 file->private_data = p;
2582
2583 return nonseekable_open(inode, file);
2584 }
2585
debugfs_timings_release(struct inode * inode,struct file * file)2586 static int debugfs_timings_release(struct inode *inode, struct file *file)
2587 {
2588 struct debugfs_timings_state *p = file->private_data;
2589
2590 kvm_put_kvm(p->vcpu->kvm);
2591 kfree(p);
2592 return 0;
2593 }
2594
debugfs_timings_read(struct file * file,char __user * buf,size_t len,loff_t * ppos)2595 static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
2596 size_t len, loff_t *ppos)
2597 {
2598 struct debugfs_timings_state *p = file->private_data;
2599 struct kvm_vcpu *vcpu = p->vcpu;
2600 char *s, *buf_end;
2601 struct kvmhv_tb_accumulator tb;
2602 u64 count;
2603 loff_t pos;
2604 ssize_t n;
2605 int i, loops;
2606 bool ok;
2607
2608 if (!p->buflen) {
2609 s = p->buf;
2610 buf_end = s + sizeof(p->buf);
2611 for (i = 0; i < N_TIMINGS; ++i) {
2612 struct kvmhv_tb_accumulator *acc;
2613
2614 acc = (struct kvmhv_tb_accumulator *)
2615 ((unsigned long)vcpu + timings[i].offset);
2616 ok = false;
2617 for (loops = 0; loops < 1000; ++loops) {
2618 count = acc->seqcount;
2619 if (!(count & 1)) {
2620 smp_rmb();
2621 tb = *acc;
2622 smp_rmb();
2623 if (count == acc->seqcount) {
2624 ok = true;
2625 break;
2626 }
2627 }
2628 udelay(1);
2629 }
2630 if (!ok)
2631 snprintf(s, buf_end - s, "%s: stuck\n",
2632 timings[i].name);
2633 else
2634 snprintf(s, buf_end - s,
2635 "%s: %llu %llu %llu %llu\n",
2636 timings[i].name, count / 2,
2637 tb_to_ns(tb.tb_total),
2638 tb_to_ns(tb.tb_min),
2639 tb_to_ns(tb.tb_max));
2640 s += strlen(s);
2641 }
2642 p->buflen = s - p->buf;
2643 }
2644
2645 pos = *ppos;
2646 if (pos >= p->buflen)
2647 return 0;
2648 if (len > p->buflen - pos)
2649 len = p->buflen - pos;
2650 n = copy_to_user(buf, p->buf + pos, len);
2651 if (n) {
2652 if (n == len)
2653 return -EFAULT;
2654 len -= n;
2655 }
2656 *ppos = pos + len;
2657 return len;
2658 }
2659
debugfs_timings_write(struct file * file,const char __user * buf,size_t len,loff_t * ppos)2660 static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
2661 size_t len, loff_t *ppos)
2662 {
2663 return -EACCES;
2664 }
2665
2666 static const struct file_operations debugfs_timings_ops = {
2667 .owner = THIS_MODULE,
2668 .open = debugfs_timings_open,
2669 .release = debugfs_timings_release,
2670 .read = debugfs_timings_read,
2671 .write = debugfs_timings_write,
2672 .llseek = generic_file_llseek,
2673 };
2674
2675 /* Create a debugfs directory for the vcpu */
debugfs_vcpu_init(struct kvm_vcpu * vcpu,unsigned int id)2676 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
2677 {
2678 char buf[16];
2679 struct kvm *kvm = vcpu->kvm;
2680
2681 snprintf(buf, sizeof(buf), "vcpu%u", id);
2682 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
2683 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir, vcpu,
2684 &debugfs_timings_ops);
2685 }
2686
2687 #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
debugfs_vcpu_init(struct kvm_vcpu * vcpu,unsigned int id)2688 static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
2689 {
2690 }
2691 #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2692
kvmppc_core_vcpu_create_hv(struct kvm_vcpu * vcpu)2693 static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)
2694 {
2695 int err;
2696 int core;
2697 struct kvmppc_vcore *vcore;
2698 struct kvm *kvm;
2699 unsigned int id;
2700
2701 kvm = vcpu->kvm;
2702 id = vcpu->vcpu_id;
2703
2704 vcpu->arch.shared = &vcpu->arch.shregs;
2705 #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
2706 /*
2707 * The shared struct is never shared on HV,
2708 * so we can always use host endianness
2709 */
2710 #ifdef __BIG_ENDIAN__
2711 vcpu->arch.shared_big_endian = true;
2712 #else
2713 vcpu->arch.shared_big_endian = false;
2714 #endif
2715 #endif
2716 vcpu->arch.mmcr[0] = MMCR0_FC;
2717 vcpu->arch.ctrl = CTRL_RUNLATCH;
2718 /* default to host PVR, since we can't spoof it */
2719 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
2720 spin_lock_init(&vcpu->arch.vpa_update_lock);
2721 spin_lock_init(&vcpu->arch.tbacct_lock);
2722 vcpu->arch.busy_preempt = TB_NIL;
2723 vcpu->arch.shregs.msr = MSR_ME;
2724 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
2725
2726 /*
2727 * Set the default HFSCR for the guest from the host value.
2728 * This value is only used on POWER9.
2729 * On POWER9, we want to virtualize the doorbell facility, so we
2730 * don't set the HFSCR_MSGP bit, and that causes those instructions
2731 * to trap and then we emulate them.
2732 */
2733 vcpu->arch.hfscr = HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB |
2734 HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP | HFSCR_PREFIX;
2735 if (cpu_has_feature(CPU_FTR_HVMODE)) {
2736 vcpu->arch.hfscr &= mfspr(SPRN_HFSCR);
2737 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2738 if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
2739 vcpu->arch.hfscr |= HFSCR_TM;
2740 #endif
2741 }
2742 if (cpu_has_feature(CPU_FTR_TM_COMP))
2743 vcpu->arch.hfscr |= HFSCR_TM;
2744
2745 vcpu->arch.hfscr_permitted = vcpu->arch.hfscr;
2746
2747 kvmppc_mmu_book3s_hv_init(vcpu);
2748
2749 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
2750
2751 init_waitqueue_head(&vcpu->arch.cpu_run);
2752
2753 mutex_lock(&kvm->lock);
2754 vcore = NULL;
2755 err = -EINVAL;
2756 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
2757 if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
2758 pr_devel("KVM: VCPU ID too high\n");
2759 core = KVM_MAX_VCORES;
2760 } else {
2761 BUG_ON(kvm->arch.smt_mode != 1);
2762 core = kvmppc_pack_vcpu_id(kvm, id);
2763 }
2764 } else {
2765 core = id / kvm->arch.smt_mode;
2766 }
2767 if (core < KVM_MAX_VCORES) {
2768 vcore = kvm->arch.vcores[core];
2769 if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) {
2770 pr_devel("KVM: collision on id %u", id);
2771 vcore = NULL;
2772 } else if (!vcore) {
2773 /*
2774 * Take mmu_setup_lock for mutual exclusion
2775 * with kvmppc_update_lpcr().
2776 */
2777 err = -ENOMEM;
2778 vcore = kvmppc_vcore_create(kvm,
2779 id & ~(kvm->arch.smt_mode - 1));
2780 mutex_lock(&kvm->arch.mmu_setup_lock);
2781 kvm->arch.vcores[core] = vcore;
2782 kvm->arch.online_vcores++;
2783 mutex_unlock(&kvm->arch.mmu_setup_lock);
2784 }
2785 }
2786 mutex_unlock(&kvm->lock);
2787
2788 if (!vcore)
2789 return err;
2790
2791 spin_lock(&vcore->lock);
2792 ++vcore->num_threads;
2793 spin_unlock(&vcore->lock);
2794 vcpu->arch.vcore = vcore;
2795 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
2796 vcpu->arch.thread_cpu = -1;
2797 vcpu->arch.prev_cpu = -1;
2798
2799 vcpu->arch.cpu_type = KVM_CPU_3S_64;
2800 kvmppc_sanity_check(vcpu);
2801
2802 debugfs_vcpu_init(vcpu, id);
2803
2804 return 0;
2805 }
2806
kvmhv_set_smt_mode(struct kvm * kvm,unsigned long smt_mode,unsigned long flags)2807 static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
2808 unsigned long flags)
2809 {
2810 int err;
2811 int esmt = 0;
2812
2813 if (flags)
2814 return -EINVAL;
2815 if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
2816 return -EINVAL;
2817 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
2818 /*
2819 * On POWER8 (or POWER7), the threading mode is "strict",
2820 * so we pack smt_mode vcpus per vcore.
2821 */
2822 if (smt_mode > threads_per_subcore)
2823 return -EINVAL;
2824 } else {
2825 /*
2826 * On POWER9, the threading mode is "loose",
2827 * so each vcpu gets its own vcore.
2828 */
2829 esmt = smt_mode;
2830 smt_mode = 1;
2831 }
2832 mutex_lock(&kvm->lock);
2833 err = -EBUSY;
2834 if (!kvm->arch.online_vcores) {
2835 kvm->arch.smt_mode = smt_mode;
2836 kvm->arch.emul_smt_mode = esmt;
2837 err = 0;
2838 }
2839 mutex_unlock(&kvm->lock);
2840
2841 return err;
2842 }
2843
unpin_vpa(struct kvm * kvm,struct kvmppc_vpa * vpa)2844 static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
2845 {
2846 if (vpa->pinned_addr)
2847 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
2848 vpa->dirty);
2849 }
2850
kvmppc_core_vcpu_free_hv(struct kvm_vcpu * vcpu)2851 static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
2852 {
2853 spin_lock(&vcpu->arch.vpa_update_lock);
2854 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
2855 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
2856 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
2857 spin_unlock(&vcpu->arch.vpa_update_lock);
2858 }
2859
kvmppc_core_check_requests_hv(struct kvm_vcpu * vcpu)2860 static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
2861 {
2862 /* Indicate we want to get back into the guest */
2863 return 1;
2864 }
2865
kvmppc_set_timer(struct kvm_vcpu * vcpu)2866 static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
2867 {
2868 unsigned long dec_nsec, now;
2869
2870 now = get_tb();
2871 if (now > vcpu->arch.dec_expires) {
2872 /* decrementer has already gone negative */
2873 kvmppc_core_queue_dec(vcpu);
2874 kvmppc_core_prepare_to_enter(vcpu);
2875 return;
2876 }
2877 dec_nsec = tb_to_ns(vcpu->arch.dec_expires - now);
2878 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
2879 vcpu->arch.timer_running = 1;
2880 }
2881
2882 extern int __kvmppc_vcore_entry(void);
2883
kvmppc_remove_runnable(struct kvmppc_vcore * vc,struct kvm_vcpu * vcpu)2884 static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
2885 struct kvm_vcpu *vcpu)
2886 {
2887 u64 now;
2888
2889 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2890 return;
2891 spin_lock_irq(&vcpu->arch.tbacct_lock);
2892 now = mftb();
2893 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
2894 vcpu->arch.stolen_logged;
2895 vcpu->arch.busy_preempt = now;
2896 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
2897 spin_unlock_irq(&vcpu->arch.tbacct_lock);
2898 --vc->n_runnable;
2899 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
2900 }
2901
kvmppc_grab_hwthread(int cpu)2902 static int kvmppc_grab_hwthread(int cpu)
2903 {
2904 struct paca_struct *tpaca;
2905 long timeout = 10000;
2906
2907 tpaca = paca_ptrs[cpu];
2908
2909 /* Ensure the thread won't go into the kernel if it wakes */
2910 tpaca->kvm_hstate.kvm_vcpu = NULL;
2911 tpaca->kvm_hstate.kvm_vcore = NULL;
2912 tpaca->kvm_hstate.napping = 0;
2913 smp_wmb();
2914 tpaca->kvm_hstate.hwthread_req = 1;
2915
2916 /*
2917 * If the thread is already executing in the kernel (e.g. handling
2918 * a stray interrupt), wait for it to get back to nap mode.
2919 * The smp_mb() is to ensure that our setting of hwthread_req
2920 * is visible before we look at hwthread_state, so if this
2921 * races with the code at system_reset_pSeries and the thread
2922 * misses our setting of hwthread_req, we are sure to see its
2923 * setting of hwthread_state, and vice versa.
2924 */
2925 smp_mb();
2926 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
2927 if (--timeout <= 0) {
2928 pr_err("KVM: couldn't grab cpu %d\n", cpu);
2929 return -EBUSY;
2930 }
2931 udelay(1);
2932 }
2933 return 0;
2934 }
2935
kvmppc_release_hwthread(int cpu)2936 static void kvmppc_release_hwthread(int cpu)
2937 {
2938 struct paca_struct *tpaca;
2939
2940 tpaca = paca_ptrs[cpu];
2941 tpaca->kvm_hstate.hwthread_req = 0;
2942 tpaca->kvm_hstate.kvm_vcpu = NULL;
2943 tpaca->kvm_hstate.kvm_vcore = NULL;
2944 tpaca->kvm_hstate.kvm_split_mode = NULL;
2945 }
2946
radix_flush_cpu(struct kvm * kvm,int cpu,struct kvm_vcpu * vcpu)2947 static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
2948 {
2949 struct kvm_nested_guest *nested = vcpu->arch.nested;
2950 cpumask_t *cpu_in_guest;
2951 int i;
2952
2953 cpu = cpu_first_tlb_thread_sibling(cpu);
2954 if (nested) {
2955 cpumask_set_cpu(cpu, &nested->need_tlb_flush);
2956 cpu_in_guest = &nested->cpu_in_guest;
2957 } else {
2958 cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush);
2959 cpu_in_guest = &kvm->arch.cpu_in_guest;
2960 }
2961 /*
2962 * Make sure setting of bit in need_tlb_flush precedes
2963 * testing of cpu_in_guest bits. The matching barrier on
2964 * the other side is the first smp_mb() in kvmppc_run_core().
2965 */
2966 smp_mb();
2967 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
2968 i += cpu_tlb_thread_sibling_step())
2969 if (cpumask_test_cpu(i, cpu_in_guest))
2970 smp_call_function_single(i, do_nothing, NULL, 1);
2971 }
2972
kvmppc_prepare_radix_vcpu(struct kvm_vcpu * vcpu,int pcpu)2973 static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
2974 {
2975 struct kvm_nested_guest *nested = vcpu->arch.nested;
2976 struct kvm *kvm = vcpu->kvm;
2977 int prev_cpu;
2978
2979 if (!cpu_has_feature(CPU_FTR_HVMODE))
2980 return;
2981
2982 if (nested)
2983 prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id];
2984 else
2985 prev_cpu = vcpu->arch.prev_cpu;
2986
2987 /*
2988 * With radix, the guest can do TLB invalidations itself,
2989 * and it could choose to use the local form (tlbiel) if
2990 * it is invalidating a translation that has only ever been
2991 * used on one vcpu. However, that doesn't mean it has
2992 * only ever been used on one physical cpu, since vcpus
2993 * can move around between pcpus. To cope with this, when
2994 * a vcpu moves from one pcpu to another, we need to tell
2995 * any vcpus running on the same core as this vcpu previously
2996 * ran to flush the TLB. The TLB is shared between threads,
2997 * so we use a single bit in .need_tlb_flush for all 4 threads.
2998 */
2999 if (prev_cpu != pcpu) {
3000 if (prev_cpu >= 0 &&
3001 cpu_first_tlb_thread_sibling(prev_cpu) !=
3002 cpu_first_tlb_thread_sibling(pcpu))
3003 radix_flush_cpu(kvm, prev_cpu, vcpu);
3004 if (nested)
3005 nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu;
3006 else
3007 vcpu->arch.prev_cpu = pcpu;
3008 }
3009 }
3010
kvmppc_start_thread(struct kvm_vcpu * vcpu,struct kvmppc_vcore * vc)3011 static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
3012 {
3013 int cpu;
3014 struct paca_struct *tpaca;
3015 struct kvm *kvm = vc->kvm;
3016
3017 cpu = vc->pcpu;
3018 if (vcpu) {
3019 if (vcpu->arch.timer_running) {
3020 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
3021 vcpu->arch.timer_running = 0;
3022 }
3023 cpu += vcpu->arch.ptid;
3024 vcpu->cpu = vc->pcpu;
3025 vcpu->arch.thread_cpu = cpu;
3026 cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest);
3027 }
3028 tpaca = paca_ptrs[cpu];
3029 tpaca->kvm_hstate.kvm_vcpu = vcpu;
3030 tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
3031 tpaca->kvm_hstate.fake_suspend = 0;
3032 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
3033 smp_wmb();
3034 tpaca->kvm_hstate.kvm_vcore = vc;
3035 if (cpu != smp_processor_id())
3036 kvmppc_ipi_thread(cpu);
3037 }
3038
kvmppc_wait_for_nap(int n_threads)3039 static void kvmppc_wait_for_nap(int n_threads)
3040 {
3041 int cpu = smp_processor_id();
3042 int i, loops;
3043
3044 if (n_threads <= 1)
3045 return;
3046 for (loops = 0; loops < 1000000; ++loops) {
3047 /*
3048 * Check if all threads are finished.
3049 * We set the vcore pointer when starting a thread
3050 * and the thread clears it when finished, so we look
3051 * for any threads that still have a non-NULL vcore ptr.
3052 */
3053 for (i = 1; i < n_threads; ++i)
3054 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
3055 break;
3056 if (i == n_threads) {
3057 HMT_medium();
3058 return;
3059 }
3060 HMT_low();
3061 }
3062 HMT_medium();
3063 for (i = 1; i < n_threads; ++i)
3064 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
3065 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
3066 }
3067
3068 /*
3069 * Check that we are on thread 0 and that any other threads in
3070 * this core are off-line. Then grab the threads so they can't
3071 * enter the kernel.
3072 */
on_primary_thread(void)3073 static int on_primary_thread(void)
3074 {
3075 int cpu = smp_processor_id();
3076 int thr;
3077
3078 /* Are we on a primary subcore? */
3079 if (cpu_thread_in_subcore(cpu))
3080 return 0;
3081
3082 thr = 0;
3083 while (++thr < threads_per_subcore)
3084 if (cpu_online(cpu + thr))
3085 return 0;
3086
3087 /* Grab all hw threads so they can't go into the kernel */
3088 for (thr = 1; thr < threads_per_subcore; ++thr) {
3089 if (kvmppc_grab_hwthread(cpu + thr)) {
3090 /* Couldn't grab one; let the others go */
3091 do {
3092 kvmppc_release_hwthread(cpu + thr);
3093 } while (--thr > 0);
3094 return 0;
3095 }
3096 }
3097 return 1;
3098 }
3099
3100 /*
3101 * A list of virtual cores for each physical CPU.
3102 * These are vcores that could run but their runner VCPU tasks are
3103 * (or may be) preempted.
3104 */
3105 struct preempted_vcore_list {
3106 struct list_head list;
3107 spinlock_t lock;
3108 };
3109
3110 static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
3111
init_vcore_lists(void)3112 static void init_vcore_lists(void)
3113 {
3114 int cpu;
3115
3116 for_each_possible_cpu(cpu) {
3117 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
3118 spin_lock_init(&lp->lock);
3119 INIT_LIST_HEAD(&lp->list);
3120 }
3121 }
3122
kvmppc_vcore_preempt(struct kvmppc_vcore * vc)3123 static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
3124 {
3125 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3126
3127 vc->vcore_state = VCORE_PREEMPT;
3128 vc->pcpu = smp_processor_id();
3129 if (vc->num_threads < threads_per_vcore(vc->kvm)) {
3130 spin_lock(&lp->lock);
3131 list_add_tail(&vc->preempt_list, &lp->list);
3132 spin_unlock(&lp->lock);
3133 }
3134
3135 /* Start accumulating stolen time */
3136 kvmppc_core_start_stolen(vc);
3137 }
3138
kvmppc_vcore_end_preempt(struct kvmppc_vcore * vc)3139 static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
3140 {
3141 struct preempted_vcore_list *lp;
3142
3143 kvmppc_core_end_stolen(vc);
3144 if (!list_empty(&vc->preempt_list)) {
3145 lp = &per_cpu(preempted_vcores, vc->pcpu);
3146 spin_lock(&lp->lock);
3147 list_del_init(&vc->preempt_list);
3148 spin_unlock(&lp->lock);
3149 }
3150 vc->vcore_state = VCORE_INACTIVE;
3151 }
3152
3153 /*
3154 * This stores information about the virtual cores currently
3155 * assigned to a physical core.
3156 */
3157 struct core_info {
3158 int n_subcores;
3159 int max_subcore_threads;
3160 int total_threads;
3161 int subcore_threads[MAX_SUBCORES];
3162 struct kvmppc_vcore *vc[MAX_SUBCORES];
3163 };
3164
3165 /*
3166 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
3167 * respectively in 2-way micro-threading (split-core) mode on POWER8.
3168 */
3169 static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
3170
init_core_info(struct core_info * cip,struct kvmppc_vcore * vc)3171 static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
3172 {
3173 memset(cip, 0, sizeof(*cip));
3174 cip->n_subcores = 1;
3175 cip->max_subcore_threads = vc->num_threads;
3176 cip->total_threads = vc->num_threads;
3177 cip->subcore_threads[0] = vc->num_threads;
3178 cip->vc[0] = vc;
3179 }
3180
subcore_config_ok(int n_subcores,int n_threads)3181 static bool subcore_config_ok(int n_subcores, int n_threads)
3182 {
3183 /*
3184 * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
3185 * split-core mode, with one thread per subcore.
3186 */
3187 if (cpu_has_feature(CPU_FTR_ARCH_300))
3188 return n_subcores <= 4 && n_threads == 1;
3189
3190 /* On POWER8, can only dynamically split if unsplit to begin with */
3191 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
3192 return false;
3193 if (n_subcores > MAX_SUBCORES)
3194 return false;
3195 if (n_subcores > 1) {
3196 if (!(dynamic_mt_modes & 2))
3197 n_subcores = 4;
3198 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
3199 return false;
3200 }
3201
3202 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
3203 }
3204
init_vcore_to_run(struct kvmppc_vcore * vc)3205 static void init_vcore_to_run(struct kvmppc_vcore *vc)
3206 {
3207 vc->entry_exit_map = 0;
3208 vc->in_guest = 0;
3209 vc->napping_threads = 0;
3210 vc->conferring_threads = 0;
3211 vc->tb_offset_applied = 0;
3212 }
3213
can_dynamic_split(struct kvmppc_vcore * vc,struct core_info * cip)3214 static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
3215 {
3216 int n_threads = vc->num_threads;
3217 int sub;
3218
3219 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
3220 return false;
3221
3222 /* In one_vm_per_core mode, require all vcores to be from the same vm */
3223 if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm)
3224 return false;
3225
3226 if (n_threads < cip->max_subcore_threads)
3227 n_threads = cip->max_subcore_threads;
3228 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
3229 return false;
3230 cip->max_subcore_threads = n_threads;
3231
3232 sub = cip->n_subcores;
3233 ++cip->n_subcores;
3234 cip->total_threads += vc->num_threads;
3235 cip->subcore_threads[sub] = vc->num_threads;
3236 cip->vc[sub] = vc;
3237 init_vcore_to_run(vc);
3238 list_del_init(&vc->preempt_list);
3239
3240 return true;
3241 }
3242
3243 /*
3244 * Work out whether it is possible to piggyback the execution of
3245 * vcore *pvc onto the execution of the other vcores described in *cip.
3246 */
can_piggyback(struct kvmppc_vcore * pvc,struct core_info * cip,int target_threads)3247 static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
3248 int target_threads)
3249 {
3250 if (cip->total_threads + pvc->num_threads > target_threads)
3251 return false;
3252
3253 return can_dynamic_split(pvc, cip);
3254 }
3255
prepare_threads(struct kvmppc_vcore * vc)3256 static void prepare_threads(struct kvmppc_vcore *vc)
3257 {
3258 int i;
3259 struct kvm_vcpu *vcpu;
3260
3261 for_each_runnable_thread(i, vcpu, vc) {
3262 if (signal_pending(vcpu->arch.run_task))
3263 vcpu->arch.ret = -EINTR;
3264 else if (vcpu->arch.vpa.update_pending ||
3265 vcpu->arch.slb_shadow.update_pending ||
3266 vcpu->arch.dtl.update_pending)
3267 vcpu->arch.ret = RESUME_GUEST;
3268 else
3269 continue;
3270 kvmppc_remove_runnable(vc, vcpu);
3271 wake_up(&vcpu->arch.cpu_run);
3272 }
3273 }
3274
collect_piggybacks(struct core_info * cip,int target_threads)3275 static void collect_piggybacks(struct core_info *cip, int target_threads)
3276 {
3277 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3278 struct kvmppc_vcore *pvc, *vcnext;
3279
3280 spin_lock(&lp->lock);
3281 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
3282 if (!spin_trylock(&pvc->lock))
3283 continue;
3284 prepare_threads(pvc);
3285 if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
3286 list_del_init(&pvc->preempt_list);
3287 if (pvc->runner == NULL) {
3288 pvc->vcore_state = VCORE_INACTIVE;
3289 kvmppc_core_end_stolen(pvc);
3290 }
3291 spin_unlock(&pvc->lock);
3292 continue;
3293 }
3294 if (!can_piggyback(pvc, cip, target_threads)) {
3295 spin_unlock(&pvc->lock);
3296 continue;
3297 }
3298 kvmppc_core_end_stolen(pvc);
3299 pvc->vcore_state = VCORE_PIGGYBACK;
3300 if (cip->total_threads >= target_threads)
3301 break;
3302 }
3303 spin_unlock(&lp->lock);
3304 }
3305
recheck_signals_and_mmu(struct core_info * cip)3306 static bool recheck_signals_and_mmu(struct core_info *cip)
3307 {
3308 int sub, i;
3309 struct kvm_vcpu *vcpu;
3310 struct kvmppc_vcore *vc;
3311
3312 for (sub = 0; sub < cip->n_subcores; ++sub) {
3313 vc = cip->vc[sub];
3314 if (!vc->kvm->arch.mmu_ready)
3315 return true;
3316 for_each_runnable_thread(i, vcpu, vc)
3317 if (signal_pending(vcpu->arch.run_task))
3318 return true;
3319 }
3320 return false;
3321 }
3322
post_guest_process(struct kvmppc_vcore * vc,bool is_master)3323 static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
3324 {
3325 int still_running = 0, i;
3326 u64 now;
3327 long ret;
3328 struct kvm_vcpu *vcpu;
3329
3330 spin_lock(&vc->lock);
3331 now = get_tb();
3332 for_each_runnable_thread(i, vcpu, vc) {
3333 /*
3334 * It's safe to unlock the vcore in the loop here, because
3335 * for_each_runnable_thread() is safe against removal of
3336 * the vcpu, and the vcore state is VCORE_EXITING here,
3337 * so any vcpus becoming runnable will have their arch.trap
3338 * set to zero and can't actually run in the guest.
3339 */
3340 spin_unlock(&vc->lock);
3341 /* cancel pending dec exception if dec is positive */
3342 if (now < vcpu->arch.dec_expires &&
3343 kvmppc_core_pending_dec(vcpu))
3344 kvmppc_core_dequeue_dec(vcpu);
3345
3346 trace_kvm_guest_exit(vcpu);
3347
3348 ret = RESUME_GUEST;
3349 if (vcpu->arch.trap)
3350 ret = kvmppc_handle_exit_hv(vcpu,
3351 vcpu->arch.run_task);
3352
3353 vcpu->arch.ret = ret;
3354 vcpu->arch.trap = 0;
3355
3356 spin_lock(&vc->lock);
3357 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
3358 if (vcpu->arch.pending_exceptions)
3359 kvmppc_core_prepare_to_enter(vcpu);
3360 if (vcpu->arch.ceded)
3361 kvmppc_set_timer(vcpu);
3362 else
3363 ++still_running;
3364 } else {
3365 kvmppc_remove_runnable(vc, vcpu);
3366 wake_up(&vcpu->arch.cpu_run);
3367 }
3368 }
3369 if (!is_master) {
3370 if (still_running > 0) {
3371 kvmppc_vcore_preempt(vc);
3372 } else if (vc->runner) {
3373 vc->vcore_state = VCORE_PREEMPT;
3374 kvmppc_core_start_stolen(vc);
3375 } else {
3376 vc->vcore_state = VCORE_INACTIVE;
3377 }
3378 if (vc->n_runnable > 0 && vc->runner == NULL) {
3379 /* make sure there's a candidate runner awake */
3380 i = -1;
3381 vcpu = next_runnable_thread(vc, &i);
3382 wake_up(&vcpu->arch.cpu_run);
3383 }
3384 }
3385 spin_unlock(&vc->lock);
3386 }
3387
3388 /*
3389 * Clear core from the list of active host cores as we are about to
3390 * enter the guest. Only do this if it is the primary thread of the
3391 * core (not if a subcore) that is entering the guest.
3392 */
kvmppc_clear_host_core(unsigned int cpu)3393 static inline int kvmppc_clear_host_core(unsigned int cpu)
3394 {
3395 int core;
3396
3397 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
3398 return 0;
3399 /*
3400 * Memory barrier can be omitted here as we will do a smp_wmb()
3401 * later in kvmppc_start_thread and we need ensure that state is
3402 * visible to other CPUs only after we enter guest.
3403 */
3404 core = cpu >> threads_shift;
3405 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
3406 return 0;
3407 }
3408
3409 /*
3410 * Advertise this core as an active host core since we exited the guest
3411 * Only need to do this if it is the primary thread of the core that is
3412 * exiting.
3413 */
kvmppc_set_host_core(unsigned int cpu)3414 static inline int kvmppc_set_host_core(unsigned int cpu)
3415 {
3416 int core;
3417
3418 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
3419 return 0;
3420
3421 /*
3422 * Memory barrier can be omitted here because we do a spin_unlock
3423 * immediately after this which provides the memory barrier.
3424 */
3425 core = cpu >> threads_shift;
3426 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
3427 return 0;
3428 }
3429
set_irq_happened(int trap)3430 static void set_irq_happened(int trap)
3431 {
3432 switch (trap) {
3433 case BOOK3S_INTERRUPT_EXTERNAL:
3434 local_paca->irq_happened |= PACA_IRQ_EE;
3435 break;
3436 case BOOK3S_INTERRUPT_H_DOORBELL:
3437 local_paca->irq_happened |= PACA_IRQ_DBELL;
3438 break;
3439 case BOOK3S_INTERRUPT_HMI:
3440 local_paca->irq_happened |= PACA_IRQ_HMI;
3441 break;
3442 case BOOK3S_INTERRUPT_SYSTEM_RESET:
3443 replay_system_reset();
3444 break;
3445 }
3446 }
3447
3448 /*
3449 * Run a set of guest threads on a physical core.
3450 * Called with vc->lock held.
3451 */
kvmppc_run_core(struct kvmppc_vcore * vc)3452 static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
3453 {
3454 struct kvm_vcpu *vcpu;
3455 int i;
3456 int srcu_idx;
3457 struct core_info core_info;
3458 struct kvmppc_vcore *pvc;
3459 struct kvm_split_mode split_info, *sip;
3460 int split, subcore_size, active;
3461 int sub;
3462 bool thr0_done;
3463 unsigned long cmd_bit, stat_bit;
3464 int pcpu, thr;
3465 int target_threads;
3466 int controlled_threads;
3467 int trap;
3468 bool is_power8;
3469
3470 if (WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)))
3471 return;
3472
3473 /*
3474 * Remove from the list any threads that have a signal pending
3475 * or need a VPA update done
3476 */
3477 prepare_threads(vc);
3478
3479 /* if the runner is no longer runnable, let the caller pick a new one */
3480 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
3481 return;
3482
3483 /*
3484 * Initialize *vc.
3485 */
3486 init_vcore_to_run(vc);
3487 vc->preempt_tb = TB_NIL;
3488
3489 /*
3490 * Number of threads that we will be controlling: the same as
3491 * the number of threads per subcore, except on POWER9,
3492 * where it's 1 because the threads are (mostly) independent.
3493 */
3494 controlled_threads = threads_per_vcore(vc->kvm);
3495
3496 /*
3497 * Make sure we are running on primary threads, and that secondary
3498 * threads are offline. Also check if the number of threads in this
3499 * guest are greater than the current system threads per guest.
3500 */
3501 if ((controlled_threads > 1) &&
3502 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
3503 for_each_runnable_thread(i, vcpu, vc) {
3504 vcpu->arch.ret = -EBUSY;
3505 kvmppc_remove_runnable(vc, vcpu);
3506 wake_up(&vcpu->arch.cpu_run);
3507 }
3508 goto out;
3509 }
3510
3511 /*
3512 * See if we could run any other vcores on the physical core
3513 * along with this one.
3514 */
3515 init_core_info(&core_info, vc);
3516 pcpu = smp_processor_id();
3517 target_threads = controlled_threads;
3518 if (target_smt_mode && target_smt_mode < target_threads)
3519 target_threads = target_smt_mode;
3520 if (vc->num_threads < target_threads)
3521 collect_piggybacks(&core_info, target_threads);
3522
3523 /*
3524 * Hard-disable interrupts, and check resched flag and signals.
3525 * If we need to reschedule or deliver a signal, clean up
3526 * and return without going into the guest(s).
3527 * If the mmu_ready flag has been cleared, don't go into the
3528 * guest because that means a HPT resize operation is in progress.
3529 */
3530 local_irq_disable();
3531 hard_irq_disable();
3532 if (lazy_irq_pending() || need_resched() ||
3533 recheck_signals_and_mmu(&core_info)) {
3534 local_irq_enable();
3535 vc->vcore_state = VCORE_INACTIVE;
3536 /* Unlock all except the primary vcore */
3537 for (sub = 1; sub < core_info.n_subcores; ++sub) {
3538 pvc = core_info.vc[sub];
3539 /* Put back on to the preempted vcores list */
3540 kvmppc_vcore_preempt(pvc);
3541 spin_unlock(&pvc->lock);
3542 }
3543 for (i = 0; i < controlled_threads; ++i)
3544 kvmppc_release_hwthread(pcpu + i);
3545 return;
3546 }
3547
3548 kvmppc_clear_host_core(pcpu);
3549
3550 /* Decide on micro-threading (split-core) mode */
3551 subcore_size = threads_per_subcore;
3552 cmd_bit = stat_bit = 0;
3553 split = core_info.n_subcores;
3554 sip = NULL;
3555 is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S);
3556
3557 if (split > 1) {
3558 sip = &split_info;
3559 memset(&split_info, 0, sizeof(split_info));
3560 for (sub = 0; sub < core_info.n_subcores; ++sub)
3561 split_info.vc[sub] = core_info.vc[sub];
3562
3563 if (is_power8) {
3564 if (split == 2 && (dynamic_mt_modes & 2)) {
3565 cmd_bit = HID0_POWER8_1TO2LPAR;
3566 stat_bit = HID0_POWER8_2LPARMODE;
3567 } else {
3568 split = 4;
3569 cmd_bit = HID0_POWER8_1TO4LPAR;
3570 stat_bit = HID0_POWER8_4LPARMODE;
3571 }
3572 subcore_size = MAX_SMT_THREADS / split;
3573 split_info.rpr = mfspr(SPRN_RPR);
3574 split_info.pmmar = mfspr(SPRN_PMMAR);
3575 split_info.ldbar = mfspr(SPRN_LDBAR);
3576 split_info.subcore_size = subcore_size;
3577 } else {
3578 split_info.subcore_size = 1;
3579 }
3580
3581 /* order writes to split_info before kvm_split_mode pointer */
3582 smp_wmb();
3583 }
3584
3585 for (thr = 0; thr < controlled_threads; ++thr) {
3586 struct paca_struct *paca = paca_ptrs[pcpu + thr];
3587
3588 paca->kvm_hstate.napping = 0;
3589 paca->kvm_hstate.kvm_split_mode = sip;
3590 }
3591
3592 /* Initiate micro-threading (split-core) on POWER8 if required */
3593 if (cmd_bit) {
3594 unsigned long hid0 = mfspr(SPRN_HID0);
3595
3596 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
3597 mb();
3598 mtspr(SPRN_HID0, hid0);
3599 isync();
3600 for (;;) {
3601 hid0 = mfspr(SPRN_HID0);
3602 if (hid0 & stat_bit)
3603 break;
3604 cpu_relax();
3605 }
3606 }
3607
3608 /*
3609 * On POWER8, set RWMR register.
3610 * Since it only affects PURR and SPURR, it doesn't affect
3611 * the host, so we don't save/restore the host value.
3612 */
3613 if (is_power8) {
3614 unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
3615 int n_online = atomic_read(&vc->online_count);
3616
3617 /*
3618 * Use the 8-thread value if we're doing split-core
3619 * or if the vcore's online count looks bogus.
3620 */
3621 if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
3622 n_online >= 1 && n_online <= MAX_SMT_THREADS)
3623 rwmr_val = p8_rwmr_values[n_online];
3624 mtspr(SPRN_RWMR, rwmr_val);
3625 }
3626
3627 /* Start all the threads */
3628 active = 0;
3629 for (sub = 0; sub < core_info.n_subcores; ++sub) {
3630 thr = is_power8 ? subcore_thread_map[sub] : sub;
3631 thr0_done = false;
3632 active |= 1 << thr;
3633 pvc = core_info.vc[sub];
3634 pvc->pcpu = pcpu + thr;
3635 for_each_runnable_thread(i, vcpu, pvc) {
3636 kvmppc_start_thread(vcpu, pvc);
3637 kvmppc_create_dtl_entry(vcpu, pvc);
3638 trace_kvm_guest_enter(vcpu);
3639 if (!vcpu->arch.ptid)
3640 thr0_done = true;
3641 active |= 1 << (thr + vcpu->arch.ptid);
3642 }
3643 /*
3644 * We need to start the first thread of each subcore
3645 * even if it doesn't have a vcpu.
3646 */
3647 if (!thr0_done)
3648 kvmppc_start_thread(NULL, pvc);
3649 }
3650
3651 /*
3652 * Ensure that split_info.do_nap is set after setting
3653 * the vcore pointer in the PACA of the secondaries.
3654 */
3655 smp_mb();
3656
3657 /*
3658 * When doing micro-threading, poke the inactive threads as well.
3659 * This gets them to the nap instruction after kvm_do_nap,
3660 * which reduces the time taken to unsplit later.
3661 */
3662 if (cmd_bit) {
3663 split_info.do_nap = 1; /* ask secondaries to nap when done */
3664 for (thr = 1; thr < threads_per_subcore; ++thr)
3665 if (!(active & (1 << thr)))
3666 kvmppc_ipi_thread(pcpu + thr);
3667 }
3668
3669 vc->vcore_state = VCORE_RUNNING;
3670 preempt_disable();
3671
3672 trace_kvmppc_run_core(vc, 0);
3673
3674 for (sub = 0; sub < core_info.n_subcores; ++sub)
3675 spin_unlock(&core_info.vc[sub]->lock);
3676
3677 guest_enter_irqoff();
3678
3679 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
3680
3681 this_cpu_disable_ftrace();
3682
3683 /*
3684 * Interrupts will be enabled once we get into the guest,
3685 * so tell lockdep that we're about to enable interrupts.
3686 */
3687 trace_hardirqs_on();
3688
3689 trap = __kvmppc_vcore_entry();
3690
3691 trace_hardirqs_off();
3692
3693 this_cpu_enable_ftrace();
3694
3695 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
3696
3697 set_irq_happened(trap);
3698
3699 spin_lock(&vc->lock);
3700 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
3701 vc->vcore_state = VCORE_EXITING;
3702
3703 /* wait for secondary threads to finish writing their state to memory */
3704 kvmppc_wait_for_nap(controlled_threads);
3705
3706 /* Return to whole-core mode if we split the core earlier */
3707 if (cmd_bit) {
3708 unsigned long hid0 = mfspr(SPRN_HID0);
3709 unsigned long loops = 0;
3710
3711 hid0 &= ~HID0_POWER8_DYNLPARDIS;
3712 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
3713 mb();
3714 mtspr(SPRN_HID0, hid0);
3715 isync();
3716 for (;;) {
3717 hid0 = mfspr(SPRN_HID0);
3718 if (!(hid0 & stat_bit))
3719 break;
3720 cpu_relax();
3721 ++loops;
3722 }
3723 split_info.do_nap = 0;
3724 }
3725
3726 kvmppc_set_host_core(pcpu);
3727
3728 context_tracking_guest_exit();
3729 if (!vtime_accounting_enabled_this_cpu()) {
3730 local_irq_enable();
3731 /*
3732 * Service IRQs here before vtime_account_guest_exit() so any
3733 * ticks that occurred while running the guest are accounted to
3734 * the guest. If vtime accounting is enabled, accounting uses
3735 * TB rather than ticks, so it can be done without enabling
3736 * interrupts here, which has the problem that it accounts
3737 * interrupt processing overhead to the host.
3738 */
3739 local_irq_disable();
3740 }
3741 vtime_account_guest_exit();
3742
3743 local_irq_enable();
3744
3745 /* Let secondaries go back to the offline loop */
3746 for (i = 0; i < controlled_threads; ++i) {
3747 kvmppc_release_hwthread(pcpu + i);
3748 if (sip && sip->napped[i])
3749 kvmppc_ipi_thread(pcpu + i);
3750 cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest);
3751 }
3752
3753 spin_unlock(&vc->lock);
3754
3755 /* make sure updates to secondary vcpu structs are visible now */
3756 smp_mb();
3757
3758 preempt_enable();
3759
3760 for (sub = 0; sub < core_info.n_subcores; ++sub) {
3761 pvc = core_info.vc[sub];
3762 post_guest_process(pvc, pvc == vc);
3763 }
3764
3765 spin_lock(&vc->lock);
3766
3767 out:
3768 vc->vcore_state = VCORE_INACTIVE;
3769 trace_kvmppc_run_core(vc, 1);
3770 }
3771
load_spr_state(struct kvm_vcpu * vcpu)3772 static void load_spr_state(struct kvm_vcpu *vcpu)
3773 {
3774 mtspr(SPRN_DSCR, vcpu->arch.dscr);
3775 mtspr(SPRN_IAMR, vcpu->arch.iamr);
3776 mtspr(SPRN_PSPB, vcpu->arch.pspb);
3777 mtspr(SPRN_FSCR, vcpu->arch.fscr);
3778 mtspr(SPRN_TAR, vcpu->arch.tar);
3779 mtspr(SPRN_EBBHR, vcpu->arch.ebbhr);
3780 mtspr(SPRN_EBBRR, vcpu->arch.ebbrr);
3781 mtspr(SPRN_BESCR, vcpu->arch.bescr);
3782 mtspr(SPRN_TIDR, vcpu->arch.tid);
3783 mtspr(SPRN_AMR, vcpu->arch.amr);
3784 mtspr(SPRN_UAMOR, vcpu->arch.uamor);
3785
3786 /*
3787 * DAR, DSISR, and for nested HV, SPRGs must be set with MSR[RI]
3788 * clear (or hstate set appropriately to catch those registers
3789 * being clobbered if we take a MCE or SRESET), so those are done
3790 * later.
3791 */
3792
3793 if (!(vcpu->arch.ctrl & 1))
3794 mtspr(SPRN_CTRLT, mfspr(SPRN_CTRLF) & ~1);
3795 }
3796
store_spr_state(struct kvm_vcpu * vcpu)3797 static void store_spr_state(struct kvm_vcpu *vcpu)
3798 {
3799 vcpu->arch.ctrl = mfspr(SPRN_CTRLF);
3800
3801 vcpu->arch.iamr = mfspr(SPRN_IAMR);
3802 vcpu->arch.pspb = mfspr(SPRN_PSPB);
3803 vcpu->arch.fscr = mfspr(SPRN_FSCR);
3804 vcpu->arch.tar = mfspr(SPRN_TAR);
3805 vcpu->arch.ebbhr = mfspr(SPRN_EBBHR);
3806 vcpu->arch.ebbrr = mfspr(SPRN_EBBRR);
3807 vcpu->arch.bescr = mfspr(SPRN_BESCR);
3808 vcpu->arch.tid = mfspr(SPRN_TIDR);
3809 vcpu->arch.amr = mfspr(SPRN_AMR);
3810 vcpu->arch.uamor = mfspr(SPRN_UAMOR);
3811 vcpu->arch.dscr = mfspr(SPRN_DSCR);
3812 }
3813
3814 /*
3815 * Privileged (non-hypervisor) host registers to save.
3816 */
3817 struct p9_host_os_sprs {
3818 unsigned long dscr;
3819 unsigned long tidr;
3820 unsigned long iamr;
3821 unsigned long amr;
3822 unsigned long fscr;
3823 };
3824
save_p9_host_os_sprs(struct p9_host_os_sprs * host_os_sprs)3825 static void save_p9_host_os_sprs(struct p9_host_os_sprs *host_os_sprs)
3826 {
3827 host_os_sprs->dscr = mfspr(SPRN_DSCR);
3828 host_os_sprs->tidr = mfspr(SPRN_TIDR);
3829 host_os_sprs->iamr = mfspr(SPRN_IAMR);
3830 host_os_sprs->amr = mfspr(SPRN_AMR);
3831 host_os_sprs->fscr = mfspr(SPRN_FSCR);
3832 }
3833
3834 /* vcpu guest regs must already be saved */
restore_p9_host_os_sprs(struct kvm_vcpu * vcpu,struct p9_host_os_sprs * host_os_sprs)3835 static void restore_p9_host_os_sprs(struct kvm_vcpu *vcpu,
3836 struct p9_host_os_sprs *host_os_sprs)
3837 {
3838 mtspr(SPRN_PSPB, 0);
3839 mtspr(SPRN_UAMOR, 0);
3840
3841 mtspr(SPRN_DSCR, host_os_sprs->dscr);
3842 mtspr(SPRN_TIDR, host_os_sprs->tidr);
3843 mtspr(SPRN_IAMR, host_os_sprs->iamr);
3844
3845 if (host_os_sprs->amr != vcpu->arch.amr)
3846 mtspr(SPRN_AMR, host_os_sprs->amr);
3847
3848 if (host_os_sprs->fscr != vcpu->arch.fscr)
3849 mtspr(SPRN_FSCR, host_os_sprs->fscr);
3850
3851 /* Save guest CTRL register, set runlatch to 1 */
3852 if (!(vcpu->arch.ctrl & 1))
3853 mtspr(SPRN_CTRLT, 1);
3854 }
3855
hcall_is_xics(unsigned long req)3856 static inline bool hcall_is_xics(unsigned long req)
3857 {
3858 return req == H_EOI || req == H_CPPR || req == H_IPI ||
3859 req == H_IPOLL || req == H_XIRR || req == H_XIRR_X;
3860 }
3861
3862 /*
3863 * Guest entry for POWER9 and later CPUs.
3864 */
kvmhv_p9_guest_entry(struct kvm_vcpu * vcpu,u64 time_limit,unsigned long lpcr)3865 static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,
3866 unsigned long lpcr)
3867 {
3868 struct kvmppc_vcore *vc = vcpu->arch.vcore;
3869 struct p9_host_os_sprs host_os_sprs;
3870 s64 dec;
3871 u64 tb;
3872 int trap, save_pmu;
3873
3874 WARN_ON_ONCE(vcpu->arch.ceded);
3875
3876 dec = mfspr(SPRN_DEC);
3877 tb = mftb();
3878 if (dec < 0)
3879 return BOOK3S_INTERRUPT_HV_DECREMENTER;
3880 local_paca->kvm_hstate.dec_expires = dec + tb;
3881 if (local_paca->kvm_hstate.dec_expires < time_limit)
3882 time_limit = local_paca->kvm_hstate.dec_expires;
3883
3884 save_p9_host_os_sprs(&host_os_sprs);
3885
3886 kvmhv_save_host_pmu(); /* saves it to PACA kvm_hstate */
3887
3888 kvmppc_subcore_enter_guest();
3889
3890 vc->entry_exit_map = 1;
3891 vc->in_guest = 1;
3892
3893 if (vcpu->arch.vpa.pinned_addr) {
3894 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
3895 u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
3896 lp->yield_count = cpu_to_be32(yield_count);
3897 vcpu->arch.vpa.dirty = 1;
3898 }
3899
3900 if (cpu_has_feature(CPU_FTR_TM) ||
3901 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
3902 kvmppc_restore_tm_hv(vcpu, vcpu->arch.shregs.msr, true);
3903
3904 #ifdef CONFIG_PPC_PSERIES
3905 if (kvmhv_on_pseries()) {
3906 barrier();
3907 if (vcpu->arch.vpa.pinned_addr) {
3908 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
3909 get_lppaca()->pmcregs_in_use = lp->pmcregs_in_use;
3910 } else {
3911 get_lppaca()->pmcregs_in_use = 1;
3912 }
3913 barrier();
3914 }
3915 #endif
3916 kvmhv_load_guest_pmu(vcpu);
3917
3918 msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX);
3919 load_fp_state(&vcpu->arch.fp);
3920 #ifdef CONFIG_ALTIVEC
3921 load_vr_state(&vcpu->arch.vr);
3922 #endif
3923 mtspr(SPRN_VRSAVE, vcpu->arch.vrsave);
3924
3925 load_spr_state(vcpu);
3926
3927 /*
3928 * When setting DEC, we must always deal with irq_work_raise via NMI vs
3929 * setting DEC. The problem occurs right as we switch into guest mode
3930 * if a NMI hits and sets pending work and sets DEC, then that will
3931 * apply to the guest and not bring us back to the host.
3932 *
3933 * irq_work_raise could check a flag (or possibly LPCR[HDICE] for
3934 * example) and set HDEC to 1? That wouldn't solve the nested hv
3935 * case which needs to abort the hcall or zero the time limit.
3936 *
3937 * XXX: Another day's problem.
3938 */
3939 mtspr(SPRN_DEC, vcpu->arch.dec_expires - mftb());
3940
3941 if (kvmhv_on_pseries()) {
3942 /*
3943 * We need to save and restore the guest visible part of the
3944 * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor
3945 * doesn't do this for us. Note only required if pseries since
3946 * this is done in kvmhv_vcpu_entry_p9() below otherwise.
3947 */
3948 unsigned long host_psscr;
3949 /* call our hypervisor to load up HV regs and go */
3950 struct hv_guest_state hvregs;
3951
3952 host_psscr = mfspr(SPRN_PSSCR_PR);
3953 mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr);
3954 kvmhv_save_hv_regs(vcpu, &hvregs);
3955 hvregs.lpcr = lpcr;
3956 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
3957 hvregs.version = HV_GUEST_STATE_VERSION;
3958 if (vcpu->arch.nested) {
3959 hvregs.lpid = vcpu->arch.nested->shadow_lpid;
3960 hvregs.vcpu_token = vcpu->arch.nested_vcpu_id;
3961 } else {
3962 hvregs.lpid = vcpu->kvm->arch.lpid;
3963 hvregs.vcpu_token = vcpu->vcpu_id;
3964 }
3965 hvregs.hdec_expiry = time_limit;
3966 mtspr(SPRN_DAR, vcpu->arch.shregs.dar);
3967 mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);
3968 trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs),
3969 __pa(&vcpu->arch.regs));
3970 kvmhv_restore_hv_return_state(vcpu, &hvregs);
3971 vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
3972 vcpu->arch.shregs.dar = mfspr(SPRN_DAR);
3973 vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);
3974 vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR);
3975 mtspr(SPRN_PSSCR_PR, host_psscr);
3976
3977 /* H_CEDE has to be handled now, not later */
3978 if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested &&
3979 kvmppc_get_gpr(vcpu, 3) == H_CEDE) {
3980 kvmppc_cede(vcpu);
3981 kvmppc_set_gpr(vcpu, 3, 0);
3982 trap = 0;
3983 }
3984 } else {
3985 kvmppc_xive_push_vcpu(vcpu);
3986 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr);
3987 if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested &&
3988 !(vcpu->arch.shregs.msr & MSR_PR)) {
3989 unsigned long req = kvmppc_get_gpr(vcpu, 3);
3990
3991 /* H_CEDE has to be handled now, not later */
3992 if (req == H_CEDE) {
3993 kvmppc_cede(vcpu);
3994 kvmppc_xive_rearm_escalation(vcpu); /* may un-cede */
3995 kvmppc_set_gpr(vcpu, 3, 0);
3996 trap = 0;
3997
3998 /* XICS hcalls must be handled before xive is pulled */
3999 } else if (hcall_is_xics(req)) {
4000 int ret;
4001
4002 ret = kvmppc_xive_xics_hcall(vcpu, req);
4003 if (ret != H_TOO_HARD) {
4004 kvmppc_set_gpr(vcpu, 3, ret);
4005 trap = 0;
4006 }
4007 }
4008 }
4009 kvmppc_xive_pull_vcpu(vcpu);
4010
4011 if (kvm_is_radix(vcpu->kvm))
4012 vcpu->arch.slb_max = 0;
4013 }
4014
4015 dec = mfspr(SPRN_DEC);
4016 if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */
4017 dec = (s32) dec;
4018 tb = mftb();
4019 vcpu->arch.dec_expires = dec + tb;
4020 vcpu->cpu = -1;
4021 vcpu->arch.thread_cpu = -1;
4022
4023 store_spr_state(vcpu);
4024
4025 restore_p9_host_os_sprs(vcpu, &host_os_sprs);
4026
4027 msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX);
4028 store_fp_state(&vcpu->arch.fp);
4029 #ifdef CONFIG_ALTIVEC
4030 store_vr_state(&vcpu->arch.vr);
4031 #endif
4032 vcpu->arch.vrsave = mfspr(SPRN_VRSAVE);
4033
4034 if (cpu_has_feature(CPU_FTR_TM) ||
4035 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
4036 kvmppc_save_tm_hv(vcpu, vcpu->arch.shregs.msr, true);
4037
4038 save_pmu = 1;
4039 if (vcpu->arch.vpa.pinned_addr) {
4040 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
4041 u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
4042 lp->yield_count = cpu_to_be32(yield_count);
4043 vcpu->arch.vpa.dirty = 1;
4044 save_pmu = lp->pmcregs_in_use;
4045 }
4046 /* Must save pmu if this guest is capable of running nested guests */
4047 save_pmu |= nesting_enabled(vcpu->kvm);
4048
4049 kvmhv_save_guest_pmu(vcpu, save_pmu);
4050 #ifdef CONFIG_PPC_PSERIES
4051 if (kvmhv_on_pseries()) {
4052 barrier();
4053 get_lppaca()->pmcregs_in_use = ppc_get_pmu_inuse();
4054 barrier();
4055 }
4056 #endif
4057
4058 vc->entry_exit_map = 0x101;
4059 vc->in_guest = 0;
4060
4061 mtspr(SPRN_DEC, local_paca->kvm_hstate.dec_expires - mftb());
4062 /* We may have raced with new irq work */
4063 if (test_irq_work_pending())
4064 set_dec(1);
4065 mtspr(SPRN_SPRG_VDSO_WRITE, local_paca->sprg_vdso);
4066
4067 kvmhv_load_host_pmu();
4068
4069 kvmppc_subcore_exit_guest();
4070
4071 return trap;
4072 }
4073
4074 /*
4075 * Wait for some other vcpu thread to execute us, and
4076 * wake us up when we need to handle something in the host.
4077 */
kvmppc_wait_for_exec(struct kvmppc_vcore * vc,struct kvm_vcpu * vcpu,int wait_state)4078 static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
4079 struct kvm_vcpu *vcpu, int wait_state)
4080 {
4081 DEFINE_WAIT(wait);
4082
4083 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
4084 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4085 spin_unlock(&vc->lock);
4086 schedule();
4087 spin_lock(&vc->lock);
4088 }
4089 finish_wait(&vcpu->arch.cpu_run, &wait);
4090 }
4091
grow_halt_poll_ns(struct kvmppc_vcore * vc)4092 static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
4093 {
4094 if (!halt_poll_ns_grow)
4095 return;
4096
4097 vc->halt_poll_ns *= halt_poll_ns_grow;
4098 if (vc->halt_poll_ns < halt_poll_ns_grow_start)
4099 vc->halt_poll_ns = halt_poll_ns_grow_start;
4100 }
4101
shrink_halt_poll_ns(struct kvmppc_vcore * vc)4102 static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
4103 {
4104 if (halt_poll_ns_shrink == 0)
4105 vc->halt_poll_ns = 0;
4106 else
4107 vc->halt_poll_ns /= halt_poll_ns_shrink;
4108 }
4109
4110 #ifdef CONFIG_KVM_XICS
xive_interrupt_pending(struct kvm_vcpu * vcpu)4111 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4112 {
4113 if (!xics_on_xive())
4114 return false;
4115 return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
4116 vcpu->arch.xive_saved_state.cppr;
4117 }
4118 #else
xive_interrupt_pending(struct kvm_vcpu * vcpu)4119 static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4120 {
4121 return false;
4122 }
4123 #endif /* CONFIG_KVM_XICS */
4124
kvmppc_vcpu_woken(struct kvm_vcpu * vcpu)4125 static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
4126 {
4127 if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
4128 kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
4129 return true;
4130
4131 return false;
4132 }
4133
4134 /*
4135 * Check to see if any of the runnable vcpus on the vcore have pending
4136 * exceptions or are no longer ceded
4137 */
kvmppc_vcore_check_block(struct kvmppc_vcore * vc)4138 static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
4139 {
4140 struct kvm_vcpu *vcpu;
4141 int i;
4142
4143 for_each_runnable_thread(i, vcpu, vc) {
4144 if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
4145 return 1;
4146 }
4147
4148 return 0;
4149 }
4150
4151 /*
4152 * All the vcpus in this vcore are idle, so wait for a decrementer
4153 * or external interrupt to one of the vcpus. vc->lock is held.
4154 */
kvmppc_vcore_blocked(struct kvmppc_vcore * vc)4155 static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
4156 {
4157 ktime_t cur, start_poll, start_wait;
4158 int do_sleep = 1;
4159 u64 block_ns;
4160
4161 /* Poll for pending exceptions and ceded state */
4162 cur = start_poll = ktime_get();
4163 if (vc->halt_poll_ns) {
4164 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
4165 ++vc->runner->stat.generic.halt_attempted_poll;
4166
4167 vc->vcore_state = VCORE_POLLING;
4168 spin_unlock(&vc->lock);
4169
4170 do {
4171 if (kvmppc_vcore_check_block(vc)) {
4172 do_sleep = 0;
4173 break;
4174 }
4175 cur = ktime_get();
4176 } while (kvm_vcpu_can_poll(cur, stop));
4177
4178 spin_lock(&vc->lock);
4179 vc->vcore_state = VCORE_INACTIVE;
4180
4181 if (!do_sleep) {
4182 ++vc->runner->stat.generic.halt_successful_poll;
4183 goto out;
4184 }
4185 }
4186
4187 prepare_to_rcuwait(&vc->wait);
4188 set_current_state(TASK_INTERRUPTIBLE);
4189 if (kvmppc_vcore_check_block(vc)) {
4190 finish_rcuwait(&vc->wait);
4191 do_sleep = 0;
4192 /* If we polled, count this as a successful poll */
4193 if (vc->halt_poll_ns)
4194 ++vc->runner->stat.generic.halt_successful_poll;
4195 goto out;
4196 }
4197
4198 start_wait = ktime_get();
4199
4200 vc->vcore_state = VCORE_SLEEPING;
4201 trace_kvmppc_vcore_blocked(vc, 0);
4202 spin_unlock(&vc->lock);
4203 schedule();
4204 finish_rcuwait(&vc->wait);
4205 spin_lock(&vc->lock);
4206 vc->vcore_state = VCORE_INACTIVE;
4207 trace_kvmppc_vcore_blocked(vc, 1);
4208 ++vc->runner->stat.halt_successful_wait;
4209
4210 cur = ktime_get();
4211
4212 out:
4213 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
4214
4215 /* Attribute wait time */
4216 if (do_sleep) {
4217 vc->runner->stat.generic.halt_wait_ns +=
4218 ktime_to_ns(cur) - ktime_to_ns(start_wait);
4219 KVM_STATS_LOG_HIST_UPDATE(
4220 vc->runner->stat.generic.halt_wait_hist,
4221 ktime_to_ns(cur) - ktime_to_ns(start_wait));
4222 /* Attribute failed poll time */
4223 if (vc->halt_poll_ns) {
4224 vc->runner->stat.generic.halt_poll_fail_ns +=
4225 ktime_to_ns(start_wait) -
4226 ktime_to_ns(start_poll);
4227 KVM_STATS_LOG_HIST_UPDATE(
4228 vc->runner->stat.generic.halt_poll_fail_hist,
4229 ktime_to_ns(start_wait) -
4230 ktime_to_ns(start_poll));
4231 }
4232 } else {
4233 /* Attribute successful poll time */
4234 if (vc->halt_poll_ns) {
4235 vc->runner->stat.generic.halt_poll_success_ns +=
4236 ktime_to_ns(cur) -
4237 ktime_to_ns(start_poll);
4238 KVM_STATS_LOG_HIST_UPDATE(
4239 vc->runner->stat.generic.halt_poll_success_hist,
4240 ktime_to_ns(cur) - ktime_to_ns(start_poll));
4241 }
4242 }
4243
4244 /* Adjust poll time */
4245 if (halt_poll_ns) {
4246 if (block_ns <= vc->halt_poll_ns)
4247 ;
4248 /* We slept and blocked for longer than the max halt time */
4249 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
4250 shrink_halt_poll_ns(vc);
4251 /* We slept and our poll time is too small */
4252 else if (vc->halt_poll_ns < halt_poll_ns &&
4253 block_ns < halt_poll_ns)
4254 grow_halt_poll_ns(vc);
4255 if (vc->halt_poll_ns > halt_poll_ns)
4256 vc->halt_poll_ns = halt_poll_ns;
4257 } else
4258 vc->halt_poll_ns = 0;
4259
4260 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
4261 }
4262
4263 /*
4264 * This never fails for a radix guest, as none of the operations it does
4265 * for a radix guest can fail or have a way to report failure.
4266 */
kvmhv_setup_mmu(struct kvm_vcpu * vcpu)4267 static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
4268 {
4269 int r = 0;
4270 struct kvm *kvm = vcpu->kvm;
4271
4272 mutex_lock(&kvm->arch.mmu_setup_lock);
4273 if (!kvm->arch.mmu_ready) {
4274 if (!kvm_is_radix(kvm))
4275 r = kvmppc_hv_setup_htab_rma(vcpu);
4276 if (!r) {
4277 if (cpu_has_feature(CPU_FTR_ARCH_300))
4278 kvmppc_setup_partition_table(kvm);
4279 kvm->arch.mmu_ready = 1;
4280 }
4281 }
4282 mutex_unlock(&kvm->arch.mmu_setup_lock);
4283 return r;
4284 }
4285
kvmppc_run_vcpu(struct kvm_vcpu * vcpu)4286 static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)
4287 {
4288 struct kvm_run *run = vcpu->run;
4289 int n_ceded, i, r;
4290 struct kvmppc_vcore *vc;
4291 struct kvm_vcpu *v;
4292
4293 trace_kvmppc_run_vcpu_enter(vcpu);
4294
4295 run->exit_reason = 0;
4296 vcpu->arch.ret = RESUME_GUEST;
4297 vcpu->arch.trap = 0;
4298 kvmppc_update_vpas(vcpu);
4299
4300 /*
4301 * Synchronize with other threads in this virtual core
4302 */
4303 vc = vcpu->arch.vcore;
4304 spin_lock(&vc->lock);
4305 vcpu->arch.ceded = 0;
4306 vcpu->arch.run_task = current;
4307 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
4308 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
4309 vcpu->arch.busy_preempt = TB_NIL;
4310 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
4311 ++vc->n_runnable;
4312
4313 /*
4314 * This happens the first time this is called for a vcpu.
4315 * If the vcore is already running, we may be able to start
4316 * this thread straight away and have it join in.
4317 */
4318 if (!signal_pending(current)) {
4319 if ((vc->vcore_state == VCORE_PIGGYBACK ||
4320 vc->vcore_state == VCORE_RUNNING) &&
4321 !VCORE_IS_EXITING(vc)) {
4322 kvmppc_create_dtl_entry(vcpu, vc);
4323 kvmppc_start_thread(vcpu, vc);
4324 trace_kvm_guest_enter(vcpu);
4325 } else if (vc->vcore_state == VCORE_SLEEPING) {
4326 rcuwait_wake_up(&vc->wait);
4327 }
4328
4329 }
4330
4331 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4332 !signal_pending(current)) {
4333 /* See if the MMU is ready to go */
4334 if (!vcpu->kvm->arch.mmu_ready) {
4335 spin_unlock(&vc->lock);
4336 r = kvmhv_setup_mmu(vcpu);
4337 spin_lock(&vc->lock);
4338 if (r) {
4339 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4340 run->fail_entry.
4341 hardware_entry_failure_reason = 0;
4342 vcpu->arch.ret = r;
4343 break;
4344 }
4345 }
4346
4347 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4348 kvmppc_vcore_end_preempt(vc);
4349
4350 if (vc->vcore_state != VCORE_INACTIVE) {
4351 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
4352 continue;
4353 }
4354 for_each_runnable_thread(i, v, vc) {
4355 kvmppc_core_prepare_to_enter(v);
4356 if (signal_pending(v->arch.run_task)) {
4357 kvmppc_remove_runnable(vc, v);
4358 v->stat.signal_exits++;
4359 v->run->exit_reason = KVM_EXIT_INTR;
4360 v->arch.ret = -EINTR;
4361 wake_up(&v->arch.cpu_run);
4362 }
4363 }
4364 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
4365 break;
4366 n_ceded = 0;
4367 for_each_runnable_thread(i, v, vc) {
4368 if (!kvmppc_vcpu_woken(v))
4369 n_ceded += v->arch.ceded;
4370 else
4371 v->arch.ceded = 0;
4372 }
4373 vc->runner = vcpu;
4374 if (n_ceded == vc->n_runnable) {
4375 kvmppc_vcore_blocked(vc);
4376 } else if (need_resched()) {
4377 kvmppc_vcore_preempt(vc);
4378 /* Let something else run */
4379 cond_resched_lock(&vc->lock);
4380 if (vc->vcore_state == VCORE_PREEMPT)
4381 kvmppc_vcore_end_preempt(vc);
4382 } else {
4383 kvmppc_run_core(vc);
4384 }
4385 vc->runner = NULL;
4386 }
4387
4388 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4389 (vc->vcore_state == VCORE_RUNNING ||
4390 vc->vcore_state == VCORE_EXITING ||
4391 vc->vcore_state == VCORE_PIGGYBACK))
4392 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
4393
4394 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4395 kvmppc_vcore_end_preempt(vc);
4396
4397 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4398 kvmppc_remove_runnable(vc, vcpu);
4399 vcpu->stat.signal_exits++;
4400 run->exit_reason = KVM_EXIT_INTR;
4401 vcpu->arch.ret = -EINTR;
4402 }
4403
4404 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
4405 /* Wake up some vcpu to run the core */
4406 i = -1;
4407 v = next_runnable_thread(vc, &i);
4408 wake_up(&v->arch.cpu_run);
4409 }
4410
4411 trace_kvmppc_run_vcpu_exit(vcpu);
4412 spin_unlock(&vc->lock);
4413 return vcpu->arch.ret;
4414 }
4415
kvmhv_run_single_vcpu(struct kvm_vcpu * vcpu,u64 time_limit,unsigned long lpcr)4416 int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
4417 unsigned long lpcr)
4418 {
4419 struct kvm_run *run = vcpu->run;
4420 int trap, r, pcpu;
4421 int srcu_idx;
4422 struct kvmppc_vcore *vc;
4423 struct kvm *kvm = vcpu->kvm;
4424 struct kvm_nested_guest *nested = vcpu->arch.nested;
4425
4426 trace_kvmppc_run_vcpu_enter(vcpu);
4427
4428 run->exit_reason = 0;
4429 vcpu->arch.ret = RESUME_GUEST;
4430 vcpu->arch.trap = 0;
4431
4432 vc = vcpu->arch.vcore;
4433 vcpu->arch.ceded = 0;
4434 vcpu->arch.run_task = current;
4435 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
4436 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
4437 vcpu->arch.busy_preempt = TB_NIL;
4438 vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;
4439 vc->runnable_threads[0] = vcpu;
4440 vc->n_runnable = 1;
4441 vc->runner = vcpu;
4442
4443 /* See if the MMU is ready to go */
4444 if (!kvm->arch.mmu_ready) {
4445 r = kvmhv_setup_mmu(vcpu);
4446 if (r) {
4447 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4448 run->fail_entry.hardware_entry_failure_reason = 0;
4449 vcpu->arch.ret = r;
4450 return r;
4451 }
4452 }
4453
4454 if (need_resched())
4455 cond_resched();
4456
4457 kvmppc_update_vpas(vcpu);
4458
4459 init_vcore_to_run(vc);
4460 vc->preempt_tb = TB_NIL;
4461
4462 preempt_disable();
4463 pcpu = smp_processor_id();
4464 vc->pcpu = pcpu;
4465 if (kvm_is_radix(kvm))
4466 kvmppc_prepare_radix_vcpu(vcpu, pcpu);
4467
4468 local_irq_disable();
4469 hard_irq_disable();
4470 if (signal_pending(current))
4471 goto sigpend;
4472 if (lazy_irq_pending() || need_resched() || !kvm->arch.mmu_ready)
4473 goto out;
4474
4475 if (!nested) {
4476 kvmppc_core_prepare_to_enter(vcpu);
4477 if (vcpu->arch.doorbell_request) {
4478 vc->dpdes = 1;
4479 smp_wmb();
4480 vcpu->arch.doorbell_request = 0;
4481 }
4482 if (test_bit(BOOK3S_IRQPRIO_EXTERNAL,
4483 &vcpu->arch.pending_exceptions))
4484 lpcr |= LPCR_MER;
4485 } else if (vcpu->arch.pending_exceptions ||
4486 vcpu->arch.doorbell_request ||
4487 xive_interrupt_pending(vcpu)) {
4488 vcpu->arch.ret = RESUME_HOST;
4489 goto out;
4490 }
4491
4492 kvmppc_clear_host_core(pcpu);
4493
4494 local_paca->kvm_hstate.napping = 0;
4495 local_paca->kvm_hstate.kvm_split_mode = NULL;
4496 kvmppc_start_thread(vcpu, vc);
4497 kvmppc_create_dtl_entry(vcpu, vc);
4498 trace_kvm_guest_enter(vcpu);
4499
4500 vc->vcore_state = VCORE_RUNNING;
4501 trace_kvmppc_run_core(vc, 0);
4502
4503 guest_enter_irqoff();
4504
4505 srcu_idx = srcu_read_lock(&kvm->srcu);
4506
4507 this_cpu_disable_ftrace();
4508
4509 /* Tell lockdep that we're about to enable interrupts */
4510 trace_hardirqs_on();
4511
4512 trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr);
4513 vcpu->arch.trap = trap;
4514
4515 trace_hardirqs_off();
4516
4517 this_cpu_enable_ftrace();
4518
4519 srcu_read_unlock(&kvm->srcu, srcu_idx);
4520
4521 set_irq_happened(trap);
4522
4523 kvmppc_set_host_core(pcpu);
4524
4525 context_tracking_guest_exit();
4526 if (!vtime_accounting_enabled_this_cpu()) {
4527 local_irq_enable();
4528 /*
4529 * Service IRQs here before vtime_account_guest_exit() so any
4530 * ticks that occurred while running the guest are accounted to
4531 * the guest. If vtime accounting is enabled, accounting uses
4532 * TB rather than ticks, so it can be done without enabling
4533 * interrupts here, which has the problem that it accounts
4534 * interrupt processing overhead to the host.
4535 */
4536 local_irq_disable();
4537 }
4538 vtime_account_guest_exit();
4539
4540 local_irq_enable();
4541
4542 cpumask_clear_cpu(pcpu, &kvm->arch.cpu_in_guest);
4543
4544 preempt_enable();
4545
4546 /*
4547 * cancel pending decrementer exception if DEC is now positive, or if
4548 * entering a nested guest in which case the decrementer is now owned
4549 * by L2 and the L1 decrementer is provided in hdec_expires
4550 */
4551 if (kvmppc_core_pending_dec(vcpu) &&
4552 ((get_tb() < vcpu->arch.dec_expires) ||
4553 (trap == BOOK3S_INTERRUPT_SYSCALL &&
4554 kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED)))
4555 kvmppc_core_dequeue_dec(vcpu);
4556
4557 trace_kvm_guest_exit(vcpu);
4558 r = RESUME_GUEST;
4559 if (trap) {
4560 if (!nested)
4561 r = kvmppc_handle_exit_hv(vcpu, current);
4562 else
4563 r = kvmppc_handle_nested_exit(vcpu);
4564 }
4565 vcpu->arch.ret = r;
4566
4567 if (is_kvmppc_resume_guest(r) && vcpu->arch.ceded &&
4568 !kvmppc_vcpu_woken(vcpu)) {
4569 kvmppc_set_timer(vcpu);
4570 while (vcpu->arch.ceded && !kvmppc_vcpu_woken(vcpu)) {
4571 if (signal_pending(current)) {
4572 vcpu->stat.signal_exits++;
4573 run->exit_reason = KVM_EXIT_INTR;
4574 vcpu->arch.ret = -EINTR;
4575 break;
4576 }
4577 spin_lock(&vc->lock);
4578 kvmppc_vcore_blocked(vc);
4579 spin_unlock(&vc->lock);
4580 }
4581 }
4582 vcpu->arch.ceded = 0;
4583
4584 vc->vcore_state = VCORE_INACTIVE;
4585 trace_kvmppc_run_core(vc, 1);
4586
4587 done:
4588 kvmppc_remove_runnable(vc, vcpu);
4589 trace_kvmppc_run_vcpu_exit(vcpu);
4590
4591 return vcpu->arch.ret;
4592
4593 sigpend:
4594 vcpu->stat.signal_exits++;
4595 run->exit_reason = KVM_EXIT_INTR;
4596 vcpu->arch.ret = -EINTR;
4597 out:
4598 local_irq_enable();
4599 preempt_enable();
4600 goto done;
4601 }
4602
kvmppc_vcpu_run_hv(struct kvm_vcpu * vcpu)4603 static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)
4604 {
4605 struct kvm_run *run = vcpu->run;
4606 int r;
4607 int srcu_idx;
4608 unsigned long ebb_regs[3] = {}; /* shut up GCC */
4609 unsigned long user_tar = 0;
4610 unsigned int user_vrsave;
4611 struct kvm *kvm;
4612
4613 if (!vcpu->arch.sane) {
4614 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4615 return -EINVAL;
4616 }
4617
4618 /*
4619 * Don't allow entry with a suspended transaction, because
4620 * the guest entry/exit code will lose it.
4621 * If the guest has TM enabled, save away their TM-related SPRs
4622 * (they will get restored by the TM unavailable interrupt).
4623 */
4624 #ifdef CONFIG_PPC_TRANSACTIONAL_MEM
4625 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
4626 (current->thread.regs->msr & MSR_TM)) {
4627 if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
4628 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4629 run->fail_entry.hardware_entry_failure_reason = 0;
4630 return -EINVAL;
4631 }
4632 /* Enable TM so we can read the TM SPRs */
4633 mtmsr(mfmsr() | MSR_TM);
4634 current->thread.tm_tfhar = mfspr(SPRN_TFHAR);
4635 current->thread.tm_tfiar = mfspr(SPRN_TFIAR);
4636 current->thread.tm_texasr = mfspr(SPRN_TEXASR);
4637 current->thread.regs->msr &= ~MSR_TM;
4638 }
4639 #endif
4640
4641 /*
4642 * Force online to 1 for the sake of old userspace which doesn't
4643 * set it.
4644 */
4645 if (!vcpu->arch.online) {
4646 atomic_inc(&vcpu->arch.vcore->online_count);
4647 vcpu->arch.online = 1;
4648 }
4649
4650 kvmppc_core_prepare_to_enter(vcpu);
4651
4652 /* No need to go into the guest when all we'll do is come back out */
4653 if (signal_pending(current)) {
4654 run->exit_reason = KVM_EXIT_INTR;
4655 return -EINTR;
4656 }
4657
4658 kvm = vcpu->kvm;
4659 atomic_inc(&kvm->arch.vcpus_running);
4660 /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
4661 smp_mb();
4662
4663 flush_all_to_thread(current);
4664
4665 /* Save userspace EBB and other register values */
4666 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
4667 ebb_regs[0] = mfspr(SPRN_EBBHR);
4668 ebb_regs[1] = mfspr(SPRN_EBBRR);
4669 ebb_regs[2] = mfspr(SPRN_BESCR);
4670 user_tar = mfspr(SPRN_TAR);
4671 }
4672 user_vrsave = mfspr(SPRN_VRSAVE);
4673
4674 vcpu->arch.waitp = &vcpu->arch.vcore->wait;
4675 vcpu->arch.pgdir = kvm->mm->pgd;
4676 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
4677
4678 do {
4679 if (cpu_has_feature(CPU_FTR_ARCH_300))
4680 r = kvmhv_run_single_vcpu(vcpu, ~(u64)0,
4681 vcpu->arch.vcore->lpcr);
4682 else
4683 r = kvmppc_run_vcpu(vcpu);
4684
4685 if (run->exit_reason == KVM_EXIT_PAPR_HCALL) {
4686 if (WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_PR)) {
4687 /*
4688 * These should have been caught reflected
4689 * into the guest by now. Final sanity check:
4690 * don't allow userspace to execute hcalls in
4691 * the hypervisor.
4692 */
4693 r = RESUME_GUEST;
4694 continue;
4695 }
4696 trace_kvm_hcall_enter(vcpu);
4697 r = kvmppc_pseries_do_hcall(vcpu);
4698 trace_kvm_hcall_exit(vcpu, r);
4699 kvmppc_core_prepare_to_enter(vcpu);
4700 } else if (r == RESUME_PAGE_FAULT) {
4701 srcu_idx = srcu_read_lock(&kvm->srcu);
4702 r = kvmppc_book3s_hv_page_fault(vcpu,
4703 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
4704 srcu_read_unlock(&kvm->srcu, srcu_idx);
4705 } else if (r == RESUME_PASSTHROUGH) {
4706 if (WARN_ON(xics_on_xive()))
4707 r = H_SUCCESS;
4708 else
4709 r = kvmppc_xics_rm_complete(vcpu, 0);
4710 }
4711 } while (is_kvmppc_resume_guest(r));
4712
4713 /* Restore userspace EBB and other register values */
4714 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
4715 mtspr(SPRN_EBBHR, ebb_regs[0]);
4716 mtspr(SPRN_EBBRR, ebb_regs[1]);
4717 mtspr(SPRN_BESCR, ebb_regs[2]);
4718 mtspr(SPRN_TAR, user_tar);
4719 }
4720 mtspr(SPRN_VRSAVE, user_vrsave);
4721
4722 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
4723 atomic_dec(&kvm->arch.vcpus_running);
4724
4725 srr_regs_clobbered();
4726
4727 return r;
4728 }
4729
kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size ** sps,int shift,int sllp)4730 static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
4731 int shift, int sllp)
4732 {
4733 (*sps)->page_shift = shift;
4734 (*sps)->slb_enc = sllp;
4735 (*sps)->enc[0].page_shift = shift;
4736 (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
4737 /*
4738 * Add 16MB MPSS support (may get filtered out by userspace)
4739 */
4740 if (shift != 24) {
4741 int penc = kvmppc_pgsize_lp_encoding(shift, 24);
4742 if (penc != -1) {
4743 (*sps)->enc[1].page_shift = 24;
4744 (*sps)->enc[1].pte_enc = penc;
4745 }
4746 }
4747 (*sps)++;
4748 }
4749
kvm_vm_ioctl_get_smmu_info_hv(struct kvm * kvm,struct kvm_ppc_smmu_info * info)4750 static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
4751 struct kvm_ppc_smmu_info *info)
4752 {
4753 struct kvm_ppc_one_seg_page_size *sps;
4754
4755 /*
4756 * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
4757 * POWER7 doesn't support keys for instruction accesses,
4758 * POWER8 and POWER9 do.
4759 */
4760 info->data_keys = 32;
4761 info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
4762
4763 /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
4764 info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
4765 info->slb_size = 32;
4766
4767 /* We only support these sizes for now, and no muti-size segments */
4768 sps = &info->sps[0];
4769 kvmppc_add_seg_page_size(&sps, 12, 0);
4770 kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
4771 kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
4772
4773 /* If running as a nested hypervisor, we don't support HPT guests */
4774 if (kvmhv_on_pseries())
4775 info->flags |= KVM_PPC_NO_HASH;
4776
4777 return 0;
4778 }
4779
4780 /*
4781 * Get (and clear) the dirty memory log for a memory slot.
4782 */
kvm_vm_ioctl_get_dirty_log_hv(struct kvm * kvm,struct kvm_dirty_log * log)4783 static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
4784 struct kvm_dirty_log *log)
4785 {
4786 struct kvm_memslots *slots;
4787 struct kvm_memory_slot *memslot;
4788 int i, r;
4789 unsigned long n;
4790 unsigned long *buf, *p;
4791 struct kvm_vcpu *vcpu;
4792
4793 mutex_lock(&kvm->slots_lock);
4794
4795 r = -EINVAL;
4796 if (log->slot >= KVM_USER_MEM_SLOTS)
4797 goto out;
4798
4799 slots = kvm_memslots(kvm);
4800 memslot = id_to_memslot(slots, log->slot);
4801 r = -ENOENT;
4802 if (!memslot || !memslot->dirty_bitmap)
4803 goto out;
4804
4805 /*
4806 * Use second half of bitmap area because both HPT and radix
4807 * accumulate bits in the first half.
4808 */
4809 n = kvm_dirty_bitmap_bytes(memslot);
4810 buf = memslot->dirty_bitmap + n / sizeof(long);
4811 memset(buf, 0, n);
4812
4813 if (kvm_is_radix(kvm))
4814 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
4815 else
4816 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
4817 if (r)
4818 goto out;
4819
4820 /*
4821 * We accumulate dirty bits in the first half of the
4822 * memslot's dirty_bitmap area, for when pages are paged
4823 * out or modified by the host directly. Pick up these
4824 * bits and add them to the map.
4825 */
4826 p = memslot->dirty_bitmap;
4827 for (i = 0; i < n / sizeof(long); ++i)
4828 buf[i] |= xchg(&p[i], 0);
4829
4830 /* Harvest dirty bits from VPA and DTL updates */
4831 /* Note: we never modify the SLB shadow buffer areas */
4832 kvm_for_each_vcpu(i, vcpu, kvm) {
4833 spin_lock(&vcpu->arch.vpa_update_lock);
4834 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
4835 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
4836 spin_unlock(&vcpu->arch.vpa_update_lock);
4837 }
4838
4839 r = -EFAULT;
4840 if (copy_to_user(log->dirty_bitmap, buf, n))
4841 goto out;
4842
4843 r = 0;
4844 out:
4845 mutex_unlock(&kvm->slots_lock);
4846 return r;
4847 }
4848
kvmppc_core_free_memslot_hv(struct kvm_memory_slot * slot)4849 static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot)
4850 {
4851 vfree(slot->arch.rmap);
4852 slot->arch.rmap = NULL;
4853 }
4854
kvmppc_core_prepare_memory_region_hv(struct kvm * kvm,struct kvm_memory_slot * slot,const struct kvm_userspace_memory_region * mem,enum kvm_mr_change change)4855 static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
4856 struct kvm_memory_slot *slot,
4857 const struct kvm_userspace_memory_region *mem,
4858 enum kvm_mr_change change)
4859 {
4860 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
4861
4862 if (change == KVM_MR_CREATE) {
4863 unsigned long size = array_size(npages, sizeof(*slot->arch.rmap));
4864
4865 if ((size >> PAGE_SHIFT) > totalram_pages())
4866 return -ENOMEM;
4867
4868 slot->arch.rmap = vzalloc(size);
4869 if (!slot->arch.rmap)
4870 return -ENOMEM;
4871 }
4872
4873 return 0;
4874 }
4875
kvmppc_core_commit_memory_region_hv(struct kvm * kvm,const struct kvm_userspace_memory_region * mem,const struct kvm_memory_slot * old,const struct kvm_memory_slot * new,enum kvm_mr_change change)4876 static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
4877 const struct kvm_userspace_memory_region *mem,
4878 const struct kvm_memory_slot *old,
4879 const struct kvm_memory_slot *new,
4880 enum kvm_mr_change change)
4881 {
4882 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
4883
4884 /*
4885 * If we are making a new memslot, it might make
4886 * some address that was previously cached as emulated
4887 * MMIO be no longer emulated MMIO, so invalidate
4888 * all the caches of emulated MMIO translations.
4889 */
4890 if (npages)
4891 atomic64_inc(&kvm->arch.mmio_update);
4892
4893 /*
4894 * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels
4895 * have already called kvm_arch_flush_shadow_memslot() to
4896 * flush shadow mappings. For KVM_MR_CREATE we have no
4897 * previous mappings. So the only case to handle is
4898 * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit
4899 * has been changed.
4900 * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES
4901 * to get rid of any THP PTEs in the partition-scoped page tables
4902 * so we can track dirtiness at the page level; we flush when
4903 * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to
4904 * using THP PTEs.
4905 */
4906 if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) &&
4907 ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES))
4908 kvmppc_radix_flush_memslot(kvm, old);
4909 /*
4910 * If UV hasn't yet called H_SVM_INIT_START, don't register memslots.
4911 */
4912 if (!kvm->arch.secure_guest)
4913 return;
4914
4915 switch (change) {
4916 case KVM_MR_CREATE:
4917 /*
4918 * @TODO kvmppc_uvmem_memslot_create() can fail and
4919 * return error. Fix this.
4920 */
4921 kvmppc_uvmem_memslot_create(kvm, new);
4922 break;
4923 case KVM_MR_DELETE:
4924 kvmppc_uvmem_memslot_delete(kvm, old);
4925 break;
4926 default:
4927 /* TODO: Handle KVM_MR_MOVE */
4928 break;
4929 }
4930 }
4931
4932 /*
4933 * Update LPCR values in kvm->arch and in vcores.
4934 * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion
4935 * of kvm->arch.lpcr update).
4936 */
kvmppc_update_lpcr(struct kvm * kvm,unsigned long lpcr,unsigned long mask)4937 void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
4938 {
4939 long int i;
4940 u32 cores_done = 0;
4941
4942 if ((kvm->arch.lpcr & mask) == lpcr)
4943 return;
4944
4945 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
4946
4947 for (i = 0; i < KVM_MAX_VCORES; ++i) {
4948 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
4949 if (!vc)
4950 continue;
4951
4952 spin_lock(&vc->lock);
4953 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
4954 verify_lpcr(kvm, vc->lpcr);
4955 spin_unlock(&vc->lock);
4956 if (++cores_done >= kvm->arch.online_vcores)
4957 break;
4958 }
4959 }
4960
kvmppc_setup_partition_table(struct kvm * kvm)4961 void kvmppc_setup_partition_table(struct kvm *kvm)
4962 {
4963 unsigned long dw0, dw1;
4964
4965 if (!kvm_is_radix(kvm)) {
4966 /* PS field - page size for VRMA */
4967 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
4968 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
4969 /* HTABSIZE and HTABORG fields */
4970 dw0 |= kvm->arch.sdr1;
4971
4972 /* Second dword as set by userspace */
4973 dw1 = kvm->arch.process_table;
4974 } else {
4975 dw0 = PATB_HR | radix__get_tree_size() |
4976 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
4977 dw1 = PATB_GR | kvm->arch.process_table;
4978 }
4979 kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1);
4980 }
4981
4982 /*
4983 * Set up HPT (hashed page table) and RMA (real-mode area).
4984 * Must be called with kvm->arch.mmu_setup_lock held.
4985 */
kvmppc_hv_setup_htab_rma(struct kvm_vcpu * vcpu)4986 static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
4987 {
4988 int err = 0;
4989 struct kvm *kvm = vcpu->kvm;
4990 unsigned long hva;
4991 struct kvm_memory_slot *memslot;
4992 struct vm_area_struct *vma;
4993 unsigned long lpcr = 0, senc;
4994 unsigned long psize, porder;
4995 int srcu_idx;
4996
4997 /* Allocate hashed page table (if not done already) and reset it */
4998 if (!kvm->arch.hpt.virt) {
4999 int order = KVM_DEFAULT_HPT_ORDER;
5000 struct kvm_hpt_info info;
5001
5002 err = kvmppc_allocate_hpt(&info, order);
5003 /* If we get here, it means userspace didn't specify a
5004 * size explicitly. So, try successively smaller
5005 * sizes if the default failed. */
5006 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
5007 err = kvmppc_allocate_hpt(&info, order);
5008
5009 if (err < 0) {
5010 pr_err("KVM: Couldn't alloc HPT\n");
5011 goto out;
5012 }
5013
5014 kvmppc_set_hpt(kvm, &info);
5015 }
5016
5017 /* Look up the memslot for guest physical address 0 */
5018 srcu_idx = srcu_read_lock(&kvm->srcu);
5019 memslot = gfn_to_memslot(kvm, 0);
5020
5021 /* We must have some memory at 0 by now */
5022 err = -EINVAL;
5023 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
5024 goto out_srcu;
5025
5026 /* Look up the VMA for the start of this memory slot */
5027 hva = memslot->userspace_addr;
5028 mmap_read_lock(kvm->mm);
5029 vma = vma_lookup(kvm->mm, hva);
5030 if (!vma || (vma->vm_flags & VM_IO))
5031 goto up_out;
5032
5033 psize = vma_kernel_pagesize(vma);
5034
5035 mmap_read_unlock(kvm->mm);
5036
5037 /* We can handle 4k, 64k or 16M pages in the VRMA */
5038 if (psize >= 0x1000000)
5039 psize = 0x1000000;
5040 else if (psize >= 0x10000)
5041 psize = 0x10000;
5042 else
5043 psize = 0x1000;
5044 porder = __ilog2(psize);
5045
5046 senc = slb_pgsize_encoding(psize);
5047 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
5048 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5049 /* Create HPTEs in the hash page table for the VRMA */
5050 kvmppc_map_vrma(vcpu, memslot, porder);
5051
5052 /* Update VRMASD field in the LPCR */
5053 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
5054 /* the -4 is to account for senc values starting at 0x10 */
5055 lpcr = senc << (LPCR_VRMASD_SH - 4);
5056 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
5057 }
5058
5059 /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
5060 smp_wmb();
5061 err = 0;
5062 out_srcu:
5063 srcu_read_unlock(&kvm->srcu, srcu_idx);
5064 out:
5065 return err;
5066
5067 up_out:
5068 mmap_read_unlock(kvm->mm);
5069 goto out_srcu;
5070 }
5071
5072 /*
5073 * Must be called with kvm->arch.mmu_setup_lock held and
5074 * mmu_ready = 0 and no vcpus running.
5075 */
kvmppc_switch_mmu_to_hpt(struct kvm * kvm)5076 int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
5077 {
5078 if (nesting_enabled(kvm))
5079 kvmhv_release_all_nested(kvm);
5080 kvmppc_rmap_reset(kvm);
5081 kvm->arch.process_table = 0;
5082 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
5083 spin_lock(&kvm->mmu_lock);
5084 kvm->arch.radix = 0;
5085 spin_unlock(&kvm->mmu_lock);
5086 kvmppc_free_radix(kvm);
5087 kvmppc_update_lpcr(kvm, LPCR_VPM1,
5088 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
5089 return 0;
5090 }
5091
5092 /*
5093 * Must be called with kvm->arch.mmu_setup_lock held and
5094 * mmu_ready = 0 and no vcpus running.
5095 */
kvmppc_switch_mmu_to_radix(struct kvm * kvm)5096 int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
5097 {
5098 int err;
5099
5100 err = kvmppc_init_vm_radix(kvm);
5101 if (err)
5102 return err;
5103 kvmppc_rmap_reset(kvm);
5104 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
5105 spin_lock(&kvm->mmu_lock);
5106 kvm->arch.radix = 1;
5107 spin_unlock(&kvm->mmu_lock);
5108 kvmppc_free_hpt(&kvm->arch.hpt);
5109 kvmppc_update_lpcr(kvm, LPCR_UPRT | LPCR_GTSE | LPCR_HR,
5110 LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR);
5111 return 0;
5112 }
5113
5114 #ifdef CONFIG_KVM_XICS
5115 /*
5116 * Allocate a per-core structure for managing state about which cores are
5117 * running in the host versus the guest and for exchanging data between
5118 * real mode KVM and CPU running in the host.
5119 * This is only done for the first VM.
5120 * The allocated structure stays even if all VMs have stopped.
5121 * It is only freed when the kvm-hv module is unloaded.
5122 * It's OK for this routine to fail, we just don't support host
5123 * core operations like redirecting H_IPI wakeups.
5124 */
kvmppc_alloc_host_rm_ops(void)5125 void kvmppc_alloc_host_rm_ops(void)
5126 {
5127 struct kvmppc_host_rm_ops *ops;
5128 unsigned long l_ops;
5129 int cpu, core;
5130 int size;
5131
5132 /* Not the first time here ? */
5133 if (kvmppc_host_rm_ops_hv != NULL)
5134 return;
5135
5136 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
5137 if (!ops)
5138 return;
5139
5140 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
5141 ops->rm_core = kzalloc(size, GFP_KERNEL);
5142
5143 if (!ops->rm_core) {
5144 kfree(ops);
5145 return;
5146 }
5147
5148 cpus_read_lock();
5149
5150 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
5151 if (!cpu_online(cpu))
5152 continue;
5153
5154 core = cpu >> threads_shift;
5155 ops->rm_core[core].rm_state.in_host = 1;
5156 }
5157
5158 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
5159
5160 /*
5161 * Make the contents of the kvmppc_host_rm_ops structure visible
5162 * to other CPUs before we assign it to the global variable.
5163 * Do an atomic assignment (no locks used here), but if someone
5164 * beats us to it, just free our copy and return.
5165 */
5166 smp_wmb();
5167 l_ops = (unsigned long) ops;
5168
5169 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
5170 cpus_read_unlock();
5171 kfree(ops->rm_core);
5172 kfree(ops);
5173 return;
5174 }
5175
5176 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
5177 "ppc/kvm_book3s:prepare",
5178 kvmppc_set_host_core,
5179 kvmppc_clear_host_core);
5180 cpus_read_unlock();
5181 }
5182
kvmppc_free_host_rm_ops(void)5183 void kvmppc_free_host_rm_ops(void)
5184 {
5185 if (kvmppc_host_rm_ops_hv) {
5186 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
5187 kfree(kvmppc_host_rm_ops_hv->rm_core);
5188 kfree(kvmppc_host_rm_ops_hv);
5189 kvmppc_host_rm_ops_hv = NULL;
5190 }
5191 }
5192 #endif
5193
kvmppc_core_init_vm_hv(struct kvm * kvm)5194 static int kvmppc_core_init_vm_hv(struct kvm *kvm)
5195 {
5196 unsigned long lpcr, lpid;
5197 char buf[32];
5198 int ret;
5199
5200 mutex_init(&kvm->arch.uvmem_lock);
5201 INIT_LIST_HEAD(&kvm->arch.uvmem_pfns);
5202 mutex_init(&kvm->arch.mmu_setup_lock);
5203
5204 /* Allocate the guest's logical partition ID */
5205
5206 lpid = kvmppc_alloc_lpid();
5207 if ((long)lpid < 0)
5208 return -ENOMEM;
5209 kvm->arch.lpid = lpid;
5210
5211 kvmppc_alloc_host_rm_ops();
5212
5213 kvmhv_vm_nested_init(kvm);
5214
5215 /*
5216 * Since we don't flush the TLB when tearing down a VM,
5217 * and this lpid might have previously been used,
5218 * make sure we flush on each core before running the new VM.
5219 * On POWER9, the tlbie in mmu_partition_table_set_entry()
5220 * does this flush for us.
5221 */
5222 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5223 cpumask_setall(&kvm->arch.need_tlb_flush);
5224
5225 /* Start out with the default set of hcalls enabled */
5226 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
5227 sizeof(kvm->arch.enabled_hcalls));
5228
5229 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5230 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
5231
5232 /* Init LPCR for virtual RMA mode */
5233 if (cpu_has_feature(CPU_FTR_HVMODE)) {
5234 kvm->arch.host_lpid = mfspr(SPRN_LPID);
5235 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
5236 lpcr &= LPCR_PECE | LPCR_LPES;
5237 } else {
5238 /*
5239 * The L2 LPES mode will be set by the L0 according to whether
5240 * or not it needs to take external interrupts in HV mode.
5241 */
5242 lpcr = 0;
5243 }
5244 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
5245 LPCR_VPM0 | LPCR_VPM1;
5246 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
5247 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5248 /* On POWER8 turn on online bit to enable PURR/SPURR */
5249 if (cpu_has_feature(CPU_FTR_ARCH_207S))
5250 lpcr |= LPCR_ONL;
5251 /*
5252 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
5253 * Set HVICE bit to enable hypervisor virtualization interrupts.
5254 * Set HEIC to prevent OS interrupts to go to hypervisor (should
5255 * be unnecessary but better safe than sorry in case we re-enable
5256 * EE in HV mode with this LPCR still set)
5257 */
5258 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
5259 lpcr &= ~LPCR_VPM0;
5260 lpcr |= LPCR_HVICE | LPCR_HEIC;
5261
5262 /*
5263 * If xive is enabled, we route 0x500 interrupts directly
5264 * to the guest.
5265 */
5266 if (xics_on_xive())
5267 lpcr |= LPCR_LPES;
5268 }
5269
5270 /*
5271 * If the host uses radix, the guest starts out as radix.
5272 */
5273 if (radix_enabled()) {
5274 kvm->arch.radix = 1;
5275 kvm->arch.mmu_ready = 1;
5276 lpcr &= ~LPCR_VPM1;
5277 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5278 ret = kvmppc_init_vm_radix(kvm);
5279 if (ret) {
5280 kvmppc_free_lpid(kvm->arch.lpid);
5281 return ret;
5282 }
5283 kvmppc_setup_partition_table(kvm);
5284 }
5285
5286 verify_lpcr(kvm, lpcr);
5287 kvm->arch.lpcr = lpcr;
5288
5289 /* Initialization for future HPT resizes */
5290 kvm->arch.resize_hpt = NULL;
5291
5292 /*
5293 * Work out how many sets the TLB has, for the use of
5294 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
5295 */
5296 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5297 /*
5298 * P10 will flush all the congruence class with a single tlbiel
5299 */
5300 kvm->arch.tlb_sets = 1;
5301 } else if (radix_enabled())
5302 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
5303 else if (cpu_has_feature(CPU_FTR_ARCH_300))
5304 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
5305 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
5306 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
5307 else
5308 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
5309
5310 /*
5311 * Track that we now have a HV mode VM active. This blocks secondary
5312 * CPU threads from coming online.
5313 */
5314 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5315 kvm_hv_vm_activated();
5316
5317 /*
5318 * Initialize smt_mode depending on processor.
5319 * POWER8 and earlier have to use "strict" threading, where
5320 * all vCPUs in a vcore have to run on the same (sub)core,
5321 * whereas on POWER9 the threads can each run a different
5322 * guest.
5323 */
5324 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5325 kvm->arch.smt_mode = threads_per_subcore;
5326 else
5327 kvm->arch.smt_mode = 1;
5328 kvm->arch.emul_smt_mode = 1;
5329
5330 /*
5331 * Create a debugfs directory for the VM
5332 */
5333 snprintf(buf, sizeof(buf), "vm%d", current->pid);
5334 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
5335 kvmppc_mmu_debugfs_init(kvm);
5336 if (radix_enabled())
5337 kvmhv_radix_debugfs_init(kvm);
5338
5339 return 0;
5340 }
5341
kvmppc_free_vcores(struct kvm * kvm)5342 static void kvmppc_free_vcores(struct kvm *kvm)
5343 {
5344 long int i;
5345
5346 for (i = 0; i < KVM_MAX_VCORES; ++i)
5347 kfree(kvm->arch.vcores[i]);
5348 kvm->arch.online_vcores = 0;
5349 }
5350
kvmppc_core_destroy_vm_hv(struct kvm * kvm)5351 static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
5352 {
5353 debugfs_remove_recursive(kvm->arch.debugfs_dir);
5354
5355 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5356 kvm_hv_vm_deactivated();
5357
5358 kvmppc_free_vcores(kvm);
5359
5360
5361 if (kvm_is_radix(kvm))
5362 kvmppc_free_radix(kvm);
5363 else
5364 kvmppc_free_hpt(&kvm->arch.hpt);
5365
5366 /* Perform global invalidation and return lpid to the pool */
5367 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
5368 if (nesting_enabled(kvm))
5369 kvmhv_release_all_nested(kvm);
5370 kvm->arch.process_table = 0;
5371 if (kvm->arch.secure_guest)
5372 uv_svm_terminate(kvm->arch.lpid);
5373 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
5374 }
5375
5376 kvmppc_free_lpid(kvm->arch.lpid);
5377
5378 kvmppc_free_pimap(kvm);
5379 }
5380
5381 /* We don't need to emulate any privileged instructions or dcbz */
kvmppc_core_emulate_op_hv(struct kvm_vcpu * vcpu,unsigned int inst,int * advance)5382 static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu,
5383 unsigned int inst, int *advance)
5384 {
5385 return EMULATE_FAIL;
5386 }
5387
kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu * vcpu,int sprn,ulong spr_val)5388 static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
5389 ulong spr_val)
5390 {
5391 return EMULATE_FAIL;
5392 }
5393
kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu * vcpu,int sprn,ulong * spr_val)5394 static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
5395 ulong *spr_val)
5396 {
5397 return EMULATE_FAIL;
5398 }
5399
kvmppc_core_check_processor_compat_hv(void)5400 static int kvmppc_core_check_processor_compat_hv(void)
5401 {
5402 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5403 cpu_has_feature(CPU_FTR_ARCH_206))
5404 return 0;
5405
5406 /* POWER9 in radix mode is capable of being a nested hypervisor. */
5407 if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
5408 return 0;
5409
5410 return -EIO;
5411 }
5412
5413 #ifdef CONFIG_KVM_XICS
5414
kvmppc_free_pimap(struct kvm * kvm)5415 void kvmppc_free_pimap(struct kvm *kvm)
5416 {
5417 kfree(kvm->arch.pimap);
5418 }
5419
kvmppc_alloc_pimap(void)5420 static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
5421 {
5422 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
5423 }
5424
kvmppc_set_passthru_irq(struct kvm * kvm,int host_irq,int guest_gsi)5425 static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5426 {
5427 struct irq_desc *desc;
5428 struct kvmppc_irq_map *irq_map;
5429 struct kvmppc_passthru_irqmap *pimap;
5430 struct irq_chip *chip;
5431 int i, rc = 0;
5432 struct irq_data *host_data;
5433
5434 if (!kvm_irq_bypass)
5435 return 1;
5436
5437 desc = irq_to_desc(host_irq);
5438 if (!desc)
5439 return -EIO;
5440
5441 mutex_lock(&kvm->lock);
5442
5443 pimap = kvm->arch.pimap;
5444 if (pimap == NULL) {
5445 /* First call, allocate structure to hold IRQ map */
5446 pimap = kvmppc_alloc_pimap();
5447 if (pimap == NULL) {
5448 mutex_unlock(&kvm->lock);
5449 return -ENOMEM;
5450 }
5451 kvm->arch.pimap = pimap;
5452 }
5453
5454 /*
5455 * For now, we only support interrupts for which the EOI operation
5456 * is an OPAL call followed by a write to XIRR, since that's
5457 * what our real-mode EOI code does, or a XIVE interrupt
5458 */
5459 chip = irq_data_get_irq_chip(&desc->irq_data);
5460 if (!chip || !is_pnv_opal_msi(chip)) {
5461 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
5462 host_irq, guest_gsi);
5463 mutex_unlock(&kvm->lock);
5464 return -ENOENT;
5465 }
5466
5467 /*
5468 * See if we already have an entry for this guest IRQ number.
5469 * If it's mapped to a hardware IRQ number, that's an error,
5470 * otherwise re-use this entry.
5471 */
5472 for (i = 0; i < pimap->n_mapped; i++) {
5473 if (guest_gsi == pimap->mapped[i].v_hwirq) {
5474 if (pimap->mapped[i].r_hwirq) {
5475 mutex_unlock(&kvm->lock);
5476 return -EINVAL;
5477 }
5478 break;
5479 }
5480 }
5481
5482 if (i == KVMPPC_PIRQ_MAPPED) {
5483 mutex_unlock(&kvm->lock);
5484 return -EAGAIN; /* table is full */
5485 }
5486
5487 irq_map = &pimap->mapped[i];
5488
5489 irq_map->v_hwirq = guest_gsi;
5490 irq_map->desc = desc;
5491
5492 /*
5493 * Order the above two stores before the next to serialize with
5494 * the KVM real mode handler.
5495 */
5496 smp_wmb();
5497
5498 /*
5499 * The 'host_irq' number is mapped in the PCI-MSI domain but
5500 * the underlying calls, which will EOI the interrupt in real
5501 * mode, need an HW IRQ number mapped in the XICS IRQ domain.
5502 */
5503 host_data = irq_domain_get_irq_data(irq_get_default_host(), host_irq);
5504 irq_map->r_hwirq = (unsigned int)irqd_to_hwirq(host_data);
5505
5506 if (i == pimap->n_mapped)
5507 pimap->n_mapped++;
5508
5509 if (xics_on_xive())
5510 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, host_irq);
5511 else
5512 kvmppc_xics_set_mapped(kvm, guest_gsi, irq_map->r_hwirq);
5513 if (rc)
5514 irq_map->r_hwirq = 0;
5515
5516 mutex_unlock(&kvm->lock);
5517
5518 return 0;
5519 }
5520
kvmppc_clr_passthru_irq(struct kvm * kvm,int host_irq,int guest_gsi)5521 static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5522 {
5523 struct irq_desc *desc;
5524 struct kvmppc_passthru_irqmap *pimap;
5525 int i, rc = 0;
5526
5527 if (!kvm_irq_bypass)
5528 return 0;
5529
5530 desc = irq_to_desc(host_irq);
5531 if (!desc)
5532 return -EIO;
5533
5534 mutex_lock(&kvm->lock);
5535 if (!kvm->arch.pimap)
5536 goto unlock;
5537
5538 pimap = kvm->arch.pimap;
5539
5540 for (i = 0; i < pimap->n_mapped; i++) {
5541 if (guest_gsi == pimap->mapped[i].v_hwirq)
5542 break;
5543 }
5544
5545 if (i == pimap->n_mapped) {
5546 mutex_unlock(&kvm->lock);
5547 return -ENODEV;
5548 }
5549
5550 if (xics_on_xive())
5551 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, host_irq);
5552 else
5553 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
5554
5555 /* invalidate the entry (what do do on error from the above ?) */
5556 pimap->mapped[i].r_hwirq = 0;
5557
5558 /*
5559 * We don't free this structure even when the count goes to
5560 * zero. The structure is freed when we destroy the VM.
5561 */
5562 unlock:
5563 mutex_unlock(&kvm->lock);
5564 return rc;
5565 }
5566
kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)5567 static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
5568 struct irq_bypass_producer *prod)
5569 {
5570 int ret = 0;
5571 struct kvm_kernel_irqfd *irqfd =
5572 container_of(cons, struct kvm_kernel_irqfd, consumer);
5573
5574 irqfd->producer = prod;
5575
5576 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
5577 if (ret)
5578 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
5579 prod->irq, irqfd->gsi, ret);
5580
5581 return ret;
5582 }
5583
kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer * cons,struct irq_bypass_producer * prod)5584 static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
5585 struct irq_bypass_producer *prod)
5586 {
5587 int ret;
5588 struct kvm_kernel_irqfd *irqfd =
5589 container_of(cons, struct kvm_kernel_irqfd, consumer);
5590
5591 irqfd->producer = NULL;
5592
5593 /*
5594 * When producer of consumer is unregistered, we change back to
5595 * default external interrupt handling mode - KVM real mode
5596 * will switch back to host.
5597 */
5598 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
5599 if (ret)
5600 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
5601 prod->irq, irqfd->gsi, ret);
5602 }
5603 #endif
5604
kvm_arch_vm_ioctl_hv(struct file * filp,unsigned int ioctl,unsigned long arg)5605 static long kvm_arch_vm_ioctl_hv(struct file *filp,
5606 unsigned int ioctl, unsigned long arg)
5607 {
5608 struct kvm *kvm __maybe_unused = filp->private_data;
5609 void __user *argp = (void __user *)arg;
5610 long r;
5611
5612 switch (ioctl) {
5613
5614 case KVM_PPC_ALLOCATE_HTAB: {
5615 u32 htab_order;
5616
5617 /* If we're a nested hypervisor, we currently only support radix */
5618 if (kvmhv_on_pseries()) {
5619 r = -EOPNOTSUPP;
5620 break;
5621 }
5622
5623 r = -EFAULT;
5624 if (get_user(htab_order, (u32 __user *)argp))
5625 break;
5626 r = kvmppc_alloc_reset_hpt(kvm, htab_order);
5627 if (r)
5628 break;
5629 r = 0;
5630 break;
5631 }
5632
5633 case KVM_PPC_GET_HTAB_FD: {
5634 struct kvm_get_htab_fd ghf;
5635
5636 r = -EFAULT;
5637 if (copy_from_user(&ghf, argp, sizeof(ghf)))
5638 break;
5639 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
5640 break;
5641 }
5642
5643 case KVM_PPC_RESIZE_HPT_PREPARE: {
5644 struct kvm_ppc_resize_hpt rhpt;
5645
5646 r = -EFAULT;
5647 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
5648 break;
5649
5650 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
5651 break;
5652 }
5653
5654 case KVM_PPC_RESIZE_HPT_COMMIT: {
5655 struct kvm_ppc_resize_hpt rhpt;
5656
5657 r = -EFAULT;
5658 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
5659 break;
5660
5661 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
5662 break;
5663 }
5664
5665 default:
5666 r = -ENOTTY;
5667 }
5668
5669 return r;
5670 }
5671
5672 /*
5673 * List of hcall numbers to enable by default.
5674 * For compatibility with old userspace, we enable by default
5675 * all hcalls that were implemented before the hcall-enabling
5676 * facility was added. Note this list should not include H_RTAS.
5677 */
5678 static unsigned int default_hcall_list[] = {
5679 H_REMOVE,
5680 H_ENTER,
5681 H_READ,
5682 H_PROTECT,
5683 H_BULK_REMOVE,
5684 #ifdef CONFIG_SPAPR_TCE_IOMMU
5685 H_GET_TCE,
5686 H_PUT_TCE,
5687 #endif
5688 H_SET_DABR,
5689 H_SET_XDABR,
5690 H_CEDE,
5691 H_PROD,
5692 H_CONFER,
5693 H_REGISTER_VPA,
5694 #ifdef CONFIG_KVM_XICS
5695 H_EOI,
5696 H_CPPR,
5697 H_IPI,
5698 H_IPOLL,
5699 H_XIRR,
5700 H_XIRR_X,
5701 #endif
5702 0
5703 };
5704
init_default_hcalls(void)5705 static void init_default_hcalls(void)
5706 {
5707 int i;
5708 unsigned int hcall;
5709
5710 for (i = 0; default_hcall_list[i]; ++i) {
5711 hcall = default_hcall_list[i];
5712 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
5713 __set_bit(hcall / 4, default_enabled_hcalls);
5714 }
5715 }
5716
kvmhv_configure_mmu(struct kvm * kvm,struct kvm_ppc_mmuv3_cfg * cfg)5717 static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
5718 {
5719 unsigned long lpcr;
5720 int radix;
5721 int err;
5722
5723 /* If not on a POWER9, reject it */
5724 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5725 return -ENODEV;
5726
5727 /* If any unknown flags set, reject it */
5728 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
5729 return -EINVAL;
5730
5731 /* GR (guest radix) bit in process_table field must match */
5732 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
5733 if (!!(cfg->process_table & PATB_GR) != radix)
5734 return -EINVAL;
5735
5736 /* Process table size field must be reasonable, i.e. <= 24 */
5737 if ((cfg->process_table & PRTS_MASK) > 24)
5738 return -EINVAL;
5739
5740 /* We can change a guest to/from radix now, if the host is radix */
5741 if (radix && !radix_enabled())
5742 return -EINVAL;
5743
5744 /* If we're a nested hypervisor, we currently only support radix */
5745 if (kvmhv_on_pseries() && !radix)
5746 return -EINVAL;
5747
5748 mutex_lock(&kvm->arch.mmu_setup_lock);
5749 if (radix != kvm_is_radix(kvm)) {
5750 if (kvm->arch.mmu_ready) {
5751 kvm->arch.mmu_ready = 0;
5752 /* order mmu_ready vs. vcpus_running */
5753 smp_mb();
5754 if (atomic_read(&kvm->arch.vcpus_running)) {
5755 kvm->arch.mmu_ready = 1;
5756 err = -EBUSY;
5757 goto out_unlock;
5758 }
5759 }
5760 if (radix)
5761 err = kvmppc_switch_mmu_to_radix(kvm);
5762 else
5763 err = kvmppc_switch_mmu_to_hpt(kvm);
5764 if (err)
5765 goto out_unlock;
5766 }
5767
5768 kvm->arch.process_table = cfg->process_table;
5769 kvmppc_setup_partition_table(kvm);
5770
5771 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
5772 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
5773 err = 0;
5774
5775 out_unlock:
5776 mutex_unlock(&kvm->arch.mmu_setup_lock);
5777 return err;
5778 }
5779
kvmhv_enable_nested(struct kvm * kvm)5780 static int kvmhv_enable_nested(struct kvm *kvm)
5781 {
5782 if (!nested)
5783 return -EPERM;
5784 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5785 return -ENODEV;
5786 if (!radix_enabled())
5787 return -ENODEV;
5788
5789 /* kvm == NULL means the caller is testing if the capability exists */
5790 if (kvm)
5791 kvm->arch.nested_enable = true;
5792 return 0;
5793 }
5794
kvmhv_load_from_eaddr(struct kvm_vcpu * vcpu,ulong * eaddr,void * ptr,int size)5795 static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
5796 int size)
5797 {
5798 int rc = -EINVAL;
5799
5800 if (kvmhv_vcpu_is_radix(vcpu)) {
5801 rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size);
5802
5803 if (rc > 0)
5804 rc = -EINVAL;
5805 }
5806
5807 /* For now quadrants are the only way to access nested guest memory */
5808 if (rc && vcpu->arch.nested)
5809 rc = -EAGAIN;
5810
5811 return rc;
5812 }
5813
kvmhv_store_to_eaddr(struct kvm_vcpu * vcpu,ulong * eaddr,void * ptr,int size)5814 static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
5815 int size)
5816 {
5817 int rc = -EINVAL;
5818
5819 if (kvmhv_vcpu_is_radix(vcpu)) {
5820 rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size);
5821
5822 if (rc > 0)
5823 rc = -EINVAL;
5824 }
5825
5826 /* For now quadrants are the only way to access nested guest memory */
5827 if (rc && vcpu->arch.nested)
5828 rc = -EAGAIN;
5829
5830 return rc;
5831 }
5832
unpin_vpa_reset(struct kvm * kvm,struct kvmppc_vpa * vpa)5833 static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa)
5834 {
5835 unpin_vpa(kvm, vpa);
5836 vpa->gpa = 0;
5837 vpa->pinned_addr = NULL;
5838 vpa->dirty = false;
5839 vpa->update_pending = 0;
5840 }
5841
5842 /*
5843 * Enable a guest to become a secure VM, or test whether
5844 * that could be enabled.
5845 * Called when the KVM_CAP_PPC_SECURE_GUEST capability is
5846 * tested (kvm == NULL) or enabled (kvm != NULL).
5847 */
kvmhv_enable_svm(struct kvm * kvm)5848 static int kvmhv_enable_svm(struct kvm *kvm)
5849 {
5850 if (!kvmppc_uvmem_available())
5851 return -EINVAL;
5852 if (kvm)
5853 kvm->arch.svm_enabled = 1;
5854 return 0;
5855 }
5856
5857 /*
5858 * IOCTL handler to turn off secure mode of guest
5859 *
5860 * - Release all device pages
5861 * - Issue ucall to terminate the guest on the UV side
5862 * - Unpin the VPA pages.
5863 * - Reinit the partition scoped page tables
5864 */
kvmhv_svm_off(struct kvm * kvm)5865 static int kvmhv_svm_off(struct kvm *kvm)
5866 {
5867 struct kvm_vcpu *vcpu;
5868 int mmu_was_ready;
5869 int srcu_idx;
5870 int ret = 0;
5871 int i;
5872
5873 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
5874 return ret;
5875
5876 mutex_lock(&kvm->arch.mmu_setup_lock);
5877 mmu_was_ready = kvm->arch.mmu_ready;
5878 if (kvm->arch.mmu_ready) {
5879 kvm->arch.mmu_ready = 0;
5880 /* order mmu_ready vs. vcpus_running */
5881 smp_mb();
5882 if (atomic_read(&kvm->arch.vcpus_running)) {
5883 kvm->arch.mmu_ready = 1;
5884 ret = -EBUSY;
5885 goto out;
5886 }
5887 }
5888
5889 srcu_idx = srcu_read_lock(&kvm->srcu);
5890 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5891 struct kvm_memory_slot *memslot;
5892 struct kvm_memslots *slots = __kvm_memslots(kvm, i);
5893
5894 if (!slots)
5895 continue;
5896
5897 kvm_for_each_memslot(memslot, slots) {
5898 kvmppc_uvmem_drop_pages(memslot, kvm, true);
5899 uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
5900 }
5901 }
5902 srcu_read_unlock(&kvm->srcu, srcu_idx);
5903
5904 ret = uv_svm_terminate(kvm->arch.lpid);
5905 if (ret != U_SUCCESS) {
5906 ret = -EINVAL;
5907 goto out;
5908 }
5909
5910 /*
5911 * When secure guest is reset, all the guest pages are sent
5912 * to UV via UV_PAGE_IN before the non-boot vcpus get a
5913 * chance to run and unpin their VPA pages. Unpinning of all
5914 * VPA pages is done here explicitly so that VPA pages
5915 * can be migrated to the secure side.
5916 *
5917 * This is required to for the secure SMP guest to reboot
5918 * correctly.
5919 */
5920 kvm_for_each_vcpu(i, vcpu, kvm) {
5921 spin_lock(&vcpu->arch.vpa_update_lock);
5922 unpin_vpa_reset(kvm, &vcpu->arch.dtl);
5923 unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow);
5924 unpin_vpa_reset(kvm, &vcpu->arch.vpa);
5925 spin_unlock(&vcpu->arch.vpa_update_lock);
5926 }
5927
5928 kvmppc_setup_partition_table(kvm);
5929 kvm->arch.secure_guest = 0;
5930 kvm->arch.mmu_ready = mmu_was_ready;
5931 out:
5932 mutex_unlock(&kvm->arch.mmu_setup_lock);
5933 return ret;
5934 }
5935
kvmhv_enable_dawr1(struct kvm * kvm)5936 static int kvmhv_enable_dawr1(struct kvm *kvm)
5937 {
5938 if (!cpu_has_feature(CPU_FTR_DAWR1))
5939 return -ENODEV;
5940
5941 /* kvm == NULL means the caller is testing if the capability exists */
5942 if (kvm)
5943 kvm->arch.dawr1_enabled = true;
5944 return 0;
5945 }
5946
kvmppc_hash_v3_possible(void)5947 static bool kvmppc_hash_v3_possible(void)
5948 {
5949 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5950 return false;
5951
5952 if (!cpu_has_feature(CPU_FTR_HVMODE))
5953 return false;
5954
5955 /*
5956 * POWER9 chips before version 2.02 can't have some threads in
5957 * HPT mode and some in radix mode on the same core.
5958 */
5959 if (radix_enabled()) {
5960 unsigned int pvr = mfspr(SPRN_PVR);
5961 if ((pvr >> 16) == PVR_POWER9 &&
5962 (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
5963 ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
5964 return false;
5965 }
5966
5967 return true;
5968 }
5969
5970 static struct kvmppc_ops kvm_ops_hv = {
5971 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
5972 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
5973 .get_one_reg = kvmppc_get_one_reg_hv,
5974 .set_one_reg = kvmppc_set_one_reg_hv,
5975 .vcpu_load = kvmppc_core_vcpu_load_hv,
5976 .vcpu_put = kvmppc_core_vcpu_put_hv,
5977 .inject_interrupt = kvmppc_inject_interrupt_hv,
5978 .set_msr = kvmppc_set_msr_hv,
5979 .vcpu_run = kvmppc_vcpu_run_hv,
5980 .vcpu_create = kvmppc_core_vcpu_create_hv,
5981 .vcpu_free = kvmppc_core_vcpu_free_hv,
5982 .check_requests = kvmppc_core_check_requests_hv,
5983 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
5984 .flush_memslot = kvmppc_core_flush_memslot_hv,
5985 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
5986 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
5987 .unmap_gfn_range = kvm_unmap_gfn_range_hv,
5988 .age_gfn = kvm_age_gfn_hv,
5989 .test_age_gfn = kvm_test_age_gfn_hv,
5990 .set_spte_gfn = kvm_set_spte_gfn_hv,
5991 .free_memslot = kvmppc_core_free_memslot_hv,
5992 .init_vm = kvmppc_core_init_vm_hv,
5993 .destroy_vm = kvmppc_core_destroy_vm_hv,
5994 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
5995 .emulate_op = kvmppc_core_emulate_op_hv,
5996 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
5997 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
5998 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
5999 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
6000 .hcall_implemented = kvmppc_hcall_impl_hv,
6001 #ifdef CONFIG_KVM_XICS
6002 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
6003 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
6004 #endif
6005 .configure_mmu = kvmhv_configure_mmu,
6006 .get_rmmu_info = kvmhv_get_rmmu_info,
6007 .set_smt_mode = kvmhv_set_smt_mode,
6008 .enable_nested = kvmhv_enable_nested,
6009 .load_from_eaddr = kvmhv_load_from_eaddr,
6010 .store_to_eaddr = kvmhv_store_to_eaddr,
6011 .enable_svm = kvmhv_enable_svm,
6012 .svm_off = kvmhv_svm_off,
6013 .enable_dawr1 = kvmhv_enable_dawr1,
6014 .hash_v3_possible = kvmppc_hash_v3_possible,
6015 };
6016
kvm_init_subcore_bitmap(void)6017 static int kvm_init_subcore_bitmap(void)
6018 {
6019 int i, j;
6020 int nr_cores = cpu_nr_cores();
6021 struct sibling_subcore_state *sibling_subcore_state;
6022
6023 for (i = 0; i < nr_cores; i++) {
6024 int first_cpu = i * threads_per_core;
6025 int node = cpu_to_node(first_cpu);
6026
6027 /* Ignore if it is already allocated. */
6028 if (paca_ptrs[first_cpu]->sibling_subcore_state)
6029 continue;
6030
6031 sibling_subcore_state =
6032 kzalloc_node(sizeof(struct sibling_subcore_state),
6033 GFP_KERNEL, node);
6034 if (!sibling_subcore_state)
6035 return -ENOMEM;
6036
6037
6038 for (j = 0; j < threads_per_core; j++) {
6039 int cpu = first_cpu + j;
6040
6041 paca_ptrs[cpu]->sibling_subcore_state =
6042 sibling_subcore_state;
6043 }
6044 }
6045 return 0;
6046 }
6047
kvmppc_radix_possible(void)6048 static int kvmppc_radix_possible(void)
6049 {
6050 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
6051 }
6052
kvmppc_book3s_init_hv(void)6053 static int kvmppc_book3s_init_hv(void)
6054 {
6055 int r;
6056
6057 if (!tlbie_capable) {
6058 pr_err("KVM-HV: Host does not support TLBIE\n");
6059 return -ENODEV;
6060 }
6061
6062 /*
6063 * FIXME!! Do we need to check on all cpus ?
6064 */
6065 r = kvmppc_core_check_processor_compat_hv();
6066 if (r < 0)
6067 return -ENODEV;
6068
6069 r = kvmhv_nested_init();
6070 if (r)
6071 return r;
6072
6073 r = kvm_init_subcore_bitmap();
6074 if (r)
6075 return r;
6076
6077 /*
6078 * We need a way of accessing the XICS interrupt controller,
6079 * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
6080 * indirectly, via OPAL.
6081 */
6082 #ifdef CONFIG_SMP
6083 if (!xics_on_xive() && !kvmhv_on_pseries() &&
6084 !local_paca->kvm_hstate.xics_phys) {
6085 struct device_node *np;
6086
6087 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
6088 if (!np) {
6089 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
6090 return -ENODEV;
6091 }
6092 /* presence of intc confirmed - node can be dropped again */
6093 of_node_put(np);
6094 }
6095 #endif
6096
6097 kvm_ops_hv.owner = THIS_MODULE;
6098 kvmppc_hv_ops = &kvm_ops_hv;
6099
6100 init_default_hcalls();
6101
6102 init_vcore_lists();
6103
6104 r = kvmppc_mmu_hv_init();
6105 if (r)
6106 return r;
6107
6108 if (kvmppc_radix_possible()) {
6109 r = kvmppc_radix_init();
6110 if (r)
6111 return r;
6112 }
6113
6114 r = kvmppc_uvmem_init();
6115 if (r < 0)
6116 pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r);
6117
6118 return r;
6119 }
6120
kvmppc_book3s_exit_hv(void)6121 static void kvmppc_book3s_exit_hv(void)
6122 {
6123 kvmppc_uvmem_free();
6124 kvmppc_free_host_rm_ops();
6125 if (kvmppc_radix_possible())
6126 kvmppc_radix_exit();
6127 kvmppc_hv_ops = NULL;
6128 kvmhv_nested_exit();
6129 }
6130
6131 module_init(kvmppc_book3s_init_hv);
6132 module_exit(kvmppc_book3s_exit_hv);
6133 MODULE_LICENSE("GPL");
6134 MODULE_ALIAS_MISCDEV(KVM_MINOR);
6135 MODULE_ALIAS("devname:kvm");
6136