blob: 84c89f08ae9aa97eb04b8de6b91f491dedf0592a [file] [log] [blame]
Thomas Gleixnerd2912cb2019-06-04 10:11:33 +02001// SPDX-License-Identifier: GPL-2.0-only
Paul Mackerrasde56a942011-06-29 00:21:34 +00002/*
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>.
Paul Mackerrasde56a942011-06-29 00:21:34 +000016 */
17
18#include <linux/kvm_host.h>
Thomas Meyer4bb817ed2017-09-03 14:19:31 +020019#include <linux/kernel.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000020#include <linux/err.h>
21#include <linux/slab.h>
22#include <linux/preempt.h>
Ingo Molnar174cd4b2017-02-02 19:15:33 +010023#include <linux/sched/signal.h>
Ingo Molnar03441a32017-02-08 18:51:35 +010024#include <linux/sched/stat.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000025#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040026#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000027#include <linux/fs.h>
28#include <linux/anon_inodes.h>
Gavin Shan07f8ab252016-05-11 11:15:55 +100029#include <linux/cpu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000030#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000031#include <linux/spinlock.h>
32#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000033#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010034#include <linux/miscdevice.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110035#include <linux/debugfs.h>
Benjamin Herrenschmidtd3989142017-04-05 17:54:51 +100036#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>
Paul Mackerrasde56a942011-06-29 00:21:34 +000045
Luke Dashjrd6ee76d2018-08-16 21:36:26 +000046#include <asm/ftrace.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000047#include <asm/reg.h>
Paul Mackerras57900692017-05-16 16:41:20 +100048#include <asm/ppc-opcode.h>
Nicholas Piggin6de66382017-11-05 23:33:55 +110049#include <asm/asm-prototypes.h>
Paul Mackerras4bad7772018-10-08 16:31:06 +110050#include <asm/archrandom.h>
Nicholas Piggine303c082018-04-01 15:50:35 +100051#include <asm/debug.h>
Paul Mackerras57900692017-05-16 16:41:20 +100052#include <asm/disassemble.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000053#include <asm/cputable.h>
54#include <asm/cacheflush.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080055#include <linux/uaccess.h>
Nicholas Piggin3a965702021-01-30 23:08:38 +100056#include <asm/interrupt.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000057#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>
Nicholas Piggin17826632021-08-12 02:00:43 +100062#include <asm/pmc.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000063#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000064#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000065#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000066#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010067#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000068#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110069#include <asm/dbell.h>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053070#include <asm/hmi.h>
Suresh Warrierc57875f2016-08-19 15:35:50 +100071#include <asm/pnv-pci.h>
Paul Mackerras7a840842016-11-16 22:25:20 +110072#include <asm/mmu.h>
Paul Mackerrasf7257582016-11-18 09:02:08 +110073#include <asm/opal.h>
74#include <asm/xics.h>
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +100075#include <asm/xive.h>
Michael Neulingc1fe1902019-04-01 17:03:12 +110076#include <asm/hw_breakpoint.h>
Bharata B Raoca9f4942019-11-25 08:36:26 +053077#include <asm/kvm_book3s_uvmem.h>
Bharata B Raoc3262252019-11-25 08:36:29 +053078#include <asm/ultravisor.h>
Peter Zijlstrad6bdceb2020-05-29 22:41:01 +020079#include <asm/dtl.h>
Bharata B Raof0c6fbb2021-06-21 14:20:00 +053080#include <asm/plpar_wrappers.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000081
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053082#include "book3s.h"
Nicholas Pigginaabcaf62021-11-23 19:52:07 +100083#include "book3s_hv.h"
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053084
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060085#define CREATE_TRACE_POINTS
86#include "trace_hv.h"
87
Paul Mackerrasde56a942011-06-29 00:21:34 +000088/* #define EXIT_DEBUG */
89/* #define EXIT_DEBUG_SIMPLE */
90/* #define EXIT_DEBUG_INT */
91
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000092/* Used to indicate that a guest page fault needs to be handled */
93#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
Suresh Warrierf7af5202016-08-19 15:35:52 +100094/* Used to indicate that a guest passthrough interrupt needs to be handled */
95#define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2)
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000096
Paul Mackerrasc7b67672012-10-15 01:18:07 +000097/* Used as a "null" value for timebase values */
98#define TB_NIL (~(u64)0)
99
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000100static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
101
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000102static int dynamic_mt_modes = 6;
Russell Currey57ad583f2017-01-12 14:54:13 +1100103module_param(dynamic_mt_modes, int, 0644);
Paul Mackerrasb4deba52015-07-02 20:38:16 +1000104MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
Paul Mackerrasec257162015-06-24 21:18:03 +1000105static int target_smt_mode;
Russell Currey57ad583f2017-01-12 14:54:13 +1100106module_param(target_smt_mode, int, 0644);
Paul Mackerrasec257162015-06-24 21:18:03 +1000107MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +1000108
Paul Mackerrasaa227862018-09-12 10:42:12 +1000109static bool one_vm_per_core;
110module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR);
Nicholas Pigginaaae8c72021-05-28 19:07:42 +1000111MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires POWER8 or older)");
Paul Mackerrasaa227862018-09-12 10:42:12 +1000112
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600113#ifdef CONFIG_KVM_XICS
Joe Perchesa4f1d942020-10-03 17:18:06 -0700114static const struct kernel_param_ops module_param_ops = {
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600115 .set = param_set_int,
116 .get = param_get_int,
117};
118
Russell Currey57ad583f2017-01-12 14:54:13 +1100119module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644);
Suresh Warrier644abbb2016-08-19 15:35:54 +1000120MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization");
121
Russell Currey57ad583f2017-01-12 14:54:13 +1100122module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644);
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -0600123MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
124#endif
125
Paul Mackerrasaa069a92018-09-21 20:02:01 +1000126/* If set, guests are allowed to create and control nested guests */
127static bool nested = true;
128module_param(nested, bool, S_IRUGO | S_IWUSR);
129MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)");
130
Paul Mackerras32fad282012-05-04 02:32:53 +0000131static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +1000132
Paul Mackerras7aa15842018-04-20 19:53:22 +1000133/*
134 * RWMR values for POWER8. These control the rate at which PURR
135 * and SPURR count and should be set according to the number of
136 * online threads in the vcore being run.
137 */
Nicholas Mc Guire0abb75b2018-07-07 11:07:25 +0200138#define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL
139#define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL
140#define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL
141#define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL
142#define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL
143#define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL
144#define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL
145#define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL
Paul Mackerras7aa15842018-04-20 19:53:22 +1000146
147static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = {
148 RWMR_RPA_P8_1THREAD,
149 RWMR_RPA_P8_1THREAD,
150 RWMR_RPA_P8_2THREAD,
151 RWMR_RPA_P8_3THREAD,
152 RWMR_RPA_P8_4THREAD,
153 RWMR_RPA_P8_5THREAD,
154 RWMR_RPA_P8_6THREAD,
155 RWMR_RPA_P8_7THREAD,
156 RWMR_RPA_P8_8THREAD,
157};
158
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +1000159static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc,
160 int *ip)
161{
162 int i = *ip;
163 struct kvm_vcpu *vcpu;
164
165 while (++i < MAX_SMT_THREADS) {
166 vcpu = READ_ONCE(vc->runnable_threads[i]);
167 if (vcpu) {
168 *ip = i;
169 return vcpu;
170 }
171 }
172 return NULL;
173}
174
175/* Used to traverse the list of runnable threads for a given vcore */
176#define for_each_runnable_thread(i, vcpu, vc) \
177 for (i = -1; (vcpu = next_runnable_thread(vc, &i)); )
178
Paul Mackerras66feed62015-03-28 14:21:12 +1100179static bool kvmppc_ipi_thread(int cpu)
180{
Paul Mackerras1704a812016-11-18 08:47:08 +1100181 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
182
Paul Mackerrasf3c18e92018-10-08 16:31:05 +1100183 /* If we're a nested hypervisor, fall back to ordinary IPIs for now */
184 if (kvmhv_on_pseries())
185 return false;
186
Paul Mackerras1704a812016-11-18 08:47:08 +1100187 /* On POWER9 we can use msgsnd to IPI any cpu */
188 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
189 msg |= get_hard_smp_processor_id(cpu);
190 smp_mb();
191 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
192 return true;
193 }
194
Paul Mackerras66feed62015-03-28 14:21:12 +1100195 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
196 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
197 preempt_disable();
198 if (cpu_first_thread_sibling(cpu) ==
199 cpu_first_thread_sibling(smp_processor_id())) {
Paul Mackerras66feed62015-03-28 14:21:12 +1100200 msg |= cpu_thread_in_core(cpu);
201 smp_mb();
202 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
203 preempt_enable();
204 return true;
205 }
206 preempt_enable();
207 }
208
209#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
Paul Mackerrasf7257582016-11-18 09:02:08 +1100210 if (cpu >= 0 && cpu < nr_cpu_ids) {
Nicholas Piggind2e60072018-02-14 01:08:12 +1000211 if (paca_ptrs[cpu]->kvm_hstate.xics_phys) {
Paul Mackerrasf7257582016-11-18 09:02:08 +1100212 xics_wake_cpu(cpu);
213 return true;
214 }
215 opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY);
Paul Mackerras66feed62015-03-28 14:21:12 +1100216 return true;
217 }
218#endif
219
220 return false;
221}
222
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530223static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000224{
Paul Mackerrasec257162015-06-24 21:18:03 +1000225 int cpu;
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700226 struct rcuwait *waitp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000227
Davidlohr Buesoda4ad882020-04-23 22:48:37 -0700228 waitp = kvm_arch_vcpu_get_wait(vcpu);
229 if (rcuwait_wake_up(waitp))
Jing Zhang0193cc92021-06-18 22:27:03 +0000230 ++vcpu->stat.generic.halt_wakeup;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000231
Paul Mackerras3deda5e2016-12-20 14:02:29 +1100232 cpu = READ_ONCE(vcpu->arch.thread_cpu);
233 if (cpu >= 0 && kvmppc_ipi_thread(cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100234 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000235
236 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000237 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100238 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
239 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000240}
241
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000242/*
243 * We use the vcpu_load/put functions to measure stolen time.
244 * Stolen time is counted as time when either the vcpu is able to
245 * run as part of a virtual core, but the task running the vcore
246 * is preempted or sleeping, or when the vcpu needs something done
247 * in the kernel by the task running the vcpu, but that task is
248 * preempted or sleeping. Those two things have to be counted
249 * separately, since one of the vcpu tasks will take on the job
250 * of running the core, and the other vcpu tasks in the vcore will
251 * sleep waiting for it to do that, but that sleep shouldn't count
252 * as stolen time.
253 *
254 * Hence we accumulate stolen time when the vcpu can run as part of
255 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
256 * needs its task to do other things in the kernel (for example,
257 * service a page fault) in busy_stolen. We don't accumulate
258 * stolen time for a vcore when it is inactive, or for a vcpu
259 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
260 * a misnomer; it means that the vcpu task is not executing in
261 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
262 * the kernel. We don't have any way of dividing up that time
263 * between time that the vcpu is genuinely stopped, time that
264 * the task is actively working on behalf of the vcpu, and time
265 * that the task is preempted, so we don't count any of it as
266 * stolen.
267 *
268 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100269 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
270 * lock. The stolen times are measured in units of timebase ticks.
271 * (Note that the != TB_NIL checks below are purely defensive;
272 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000273 */
274
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000275static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc, u64 tb)
Paul Mackerrasec257162015-06-24 21:18:03 +1000276{
277 unsigned long flags;
278
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000279 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
280
Paul Mackerrasec257162015-06-24 21:18:03 +1000281 spin_lock_irqsave(&vc->stoltb_lock, flags);
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000282 vc->preempt_tb = tb;
Paul Mackerrasec257162015-06-24 21:18:03 +1000283 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
284}
285
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000286static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc, u64 tb)
Paul Mackerrasec257162015-06-24 21:18:03 +1000287{
288 unsigned long flags;
289
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000290 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
291
Paul Mackerrasec257162015-06-24 21:18:03 +1000292 spin_lock_irqsave(&vc->stoltb_lock, flags);
293 if (vc->preempt_tb != TB_NIL) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000294 vc->stolen_tb += tb - vc->preempt_tb;
Paul Mackerrasec257162015-06-24 21:18:03 +1000295 vc->preempt_tb = TB_NIL;
296 }
297 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
298}
299
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530300static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000301{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000302 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100303 unsigned long flags;
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000304 u64 now;
305
306 if (cpu_has_feature(CPU_FTR_ARCH_300))
307 return;
308
309 now = mftb();
Paul Mackerras0456ec42012-02-03 00:56:21 +0000310
Paul Mackerras2711e242014-12-04 16:43:28 +1100311 /*
312 * We can test vc->runner without taking the vcore lock,
313 * because only this task ever sets vc->runner to this
314 * vcpu, and once it is set to this vcpu, only this task
315 * ever sets it to NULL.
316 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000317 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000318 kvmppc_core_end_stolen(vc, now);
Paul Mackerrasec257162015-06-24 21:18:03 +1000319
Paul Mackerras2711e242014-12-04 16:43:28 +1100320 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000321 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
322 vcpu->arch.busy_preempt != TB_NIL) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000323 vcpu->arch.busy_stolen += now - vcpu->arch.busy_preempt;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000324 vcpu->arch.busy_preempt = TB_NIL;
325 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100326 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000327}
328
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530329static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000330{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000331 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100332 unsigned long flags;
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000333 u64 now;
334
335 if (cpu_has_feature(CPU_FTR_ARCH_300))
336 return;
337
338 now = mftb();
Paul Mackerras0456ec42012-02-03 00:56:21 +0000339
Paul Mackerrasec257162015-06-24 21:18:03 +1000340 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000341 kvmppc_core_start_stolen(vc, now);
Paul Mackerrasec257162015-06-24 21:18:03 +1000342
Paul Mackerras2711e242014-12-04 16:43:28 +1100343 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000344 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
Nicholas Piggincb2553a2021-11-23 19:52:02 +1000345 vcpu->arch.busy_preempt = now;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100346 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000347}
348
Thomas Huth5358a962015-05-22 09:25:02 +0200349static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000350{
351 vcpu->arch.pvr = pvr;
352}
353
Alistair Popple4cb4ade2020-06-02 15:53:25 +1000354/* Dummy value used in computing PCR value below */
355#define PCR_ARCH_31 (PCR_ARCH_300 << 1)
356
Thomas Huth5358a962015-05-22 09:25:02 +0200357static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000358{
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100359 unsigned long host_pcr_bit = 0, guest_pcr_bit = 0;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000360 struct kvmppc_vcore *vc = vcpu->arch.vcore;
361
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100362 /* We can (emulate) our own architecture version and anything older */
Alistair Popple4cb4ade2020-06-02 15:53:25 +1000363 if (cpu_has_feature(CPU_FTR_ARCH_31))
364 host_pcr_bit = PCR_ARCH_31;
365 else if (cpu_has_feature(CPU_FTR_ARCH_300))
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100366 host_pcr_bit = PCR_ARCH_300;
367 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
368 host_pcr_bit = PCR_ARCH_207;
369 else if (cpu_has_feature(CPU_FTR_ARCH_206))
370 host_pcr_bit = PCR_ARCH_206;
371 else
372 host_pcr_bit = PCR_ARCH_205;
373
374 /* Determine lowest PCR bit needed to run guest in given PVR level */
375 guest_pcr_bit = host_pcr_bit;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000376 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000377 switch (arch_compat) {
378 case PVR_ARCH_205:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100379 guest_pcr_bit = PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000380 break;
381 case PVR_ARCH_206:
382 case PVR_ARCH_206p:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100383 guest_pcr_bit = PCR_ARCH_206;
Paul Mackerras5557ae02014-01-08 21:25:24 +1100384 break;
385 case PVR_ARCH_207:
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100386 guest_pcr_bit = PCR_ARCH_207;
387 break;
388 case PVR_ARCH_300:
389 guest_pcr_bit = PCR_ARCH_300;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000390 break;
Alistair Popple4cb4ade2020-06-02 15:53:25 +1000391 case PVR_ARCH_31:
392 guest_pcr_bit = PCR_ARCH_31;
393 break;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000394 default:
395 return -EINVAL;
396 }
397 }
398
Suraj Jitindar Singh2ee13be32016-11-14 11:35:08 +1100399 /* Check requested PCR bits don't exceed our capabilities */
400 if (guest_pcr_bit > host_pcr_bit)
401 return -EINVAL;
402
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000403 spin_lock(&vc->lock);
404 vc->arch_compat = arch_compat;
Jordan Niethe13c7bb32019-09-17 10:46:05 +1000405 /*
406 * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit
407 * Also set all reserved PCR bits
408 */
409 vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000410 spin_unlock(&vc->lock);
411
412 return 0;
413}
414
Thomas Huth5358a962015-05-22 09:25:02 +0200415static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000416{
417 int r;
418
419 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
420 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
Simon Guo173c5202018-05-07 14:20:08 +0800421 vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000422 for (r = 0; r < 16; ++r)
423 pr_err("r%2d = %.16lx r%d = %.16lx\n",
424 r, kvmppc_get_gpr(vcpu, r),
425 r+16, kvmppc_get_gpr(vcpu, r+16));
426 pr_err("ctr = %.16lx lr = %.16lx\n",
Simon Guo173c5202018-05-07 14:20:08 +0800427 vcpu->arch.regs.ctr, vcpu->arch.regs.link);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000428 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
429 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
430 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
431 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
432 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
433 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
Paul Mackerrasfd0944b2018-10-08 16:30:58 +1100434 pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n",
435 vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000436 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
437 pr_err("fault dar = %.16lx dsisr = %.8x\n",
438 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
439 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
440 for (r = 0; r < vcpu->arch.slb_max; ++r)
441 pr_err(" ESID = %.16llx VSID = %.16llx\n",
442 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
443 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000444 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000445 vcpu->arch.last_inst);
446}
447
Thomas Huth5358a962015-05-22 09:25:02 +0200448static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000449{
Paul Mackerras5a3f4932019-05-23 16:36:32 +1000450 return kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000451}
452
453static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
454{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000455 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200456 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000457}
458
Paul Mackerras55b665b2012-09-25 20:33:06 +0000459static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
460 unsigned long addr, unsigned long len)
461{
462 /* check address is cacheline aligned */
463 if (addr & (L1_CACHE_BYTES - 1))
464 return -EINVAL;
465 spin_lock(&vcpu->arch.vpa_update_lock);
466 if (v->next_gpa != addr || v->len != len) {
467 v->next_gpa = addr;
468 v->len = addr ? len : 0;
469 v->update_pending = 1;
470 }
471 spin_unlock(&vcpu->arch.vpa_update_lock);
472 return 0;
473}
474
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000475/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
476struct reg_vpa {
477 u32 dummy;
478 union {
Alexander Graf02407552014-06-11 10:34:19 +0200479 __be16 hword;
480 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000481 } length;
482};
483
484static int vpa_is_registered(struct kvmppc_vpa *vpap)
485{
486 if (vpap->update_pending)
487 return vpap->next_gpa != 0;
488 return vpap->pinned_addr != NULL;
489}
490
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000491static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
492 unsigned long flags,
493 unsigned long vcpuid, unsigned long vpa)
494{
495 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000496 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000497 void *va;
498 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000499 int err;
500 int subfunc;
501 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000502
503 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
504 if (!tvcpu)
505 return H_PARAMETER;
506
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000507 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
508 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
509 subfunc == H_VPA_REG_SLB) {
510 /* Registering new area - address must be cache-line aligned */
511 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000512 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000513
514 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000515 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
516 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000517 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000518 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200519 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000520 else
Alexander Graf02407552014-06-11 10:34:19 +0200521 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000522 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000523
524 /* Check length */
525 if (len > nb || len < sizeof(struct reg_vpa))
526 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000527 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000528 vpa = 0;
529 len = 0;
530 }
531
532 err = H_PARAMETER;
533 vpap = NULL;
534 spin_lock(&tvcpu->arch.vpa_update_lock);
535
536 switch (subfunc) {
537 case H_VPA_REG_VPA: /* register VPA */
Nicholas Piggineaac112e2017-08-13 11:33:38 +1000538 /*
539 * The size of our lppaca is 1kB because of the way we align
540 * it for the guest to avoid crossing a 4kB boundary. We only
541 * use 640 bytes of the structure though, so we should accept
542 * clients that set a size of 640.
543 */
Nicholas Piggin499dcd42018-02-14 01:08:13 +1000544 BUILD_BUG_ON(sizeof(struct lppaca) != 640);
545 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000546 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000547 vpap = &tvcpu->arch.vpa;
548 err = 0;
549 break;
550
551 case H_VPA_REG_DTL: /* register DTL */
552 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000553 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000554 len -= len % sizeof(struct dtl_entry);
555
556 /* Check that they have previously registered a VPA */
557 err = H_RESOURCE;
558 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000559 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000560
561 vpap = &tvcpu->arch.dtl;
562 err = 0;
563 break;
564
565 case H_VPA_REG_SLB: /* register SLB shadow buffer */
566 /* Check that they have previously registered a VPA */
567 err = H_RESOURCE;
568 if (!vpa_is_registered(&tvcpu->arch.vpa))
569 break;
570
571 vpap = &tvcpu->arch.slb_shadow;
572 err = 0;
573 break;
574
575 case H_VPA_DEREG_VPA: /* deregister VPA */
576 /* Check they don't still have a DTL or SLB buf registered */
577 err = H_RESOURCE;
578 if (vpa_is_registered(&tvcpu->arch.dtl) ||
579 vpa_is_registered(&tvcpu->arch.slb_shadow))
580 break;
581
582 vpap = &tvcpu->arch.vpa;
583 err = 0;
584 break;
585
586 case H_VPA_DEREG_DTL: /* deregister DTL */
587 vpap = &tvcpu->arch.dtl;
588 err = 0;
589 break;
590
591 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
592 vpap = &tvcpu->arch.slb_shadow;
593 err = 0;
594 break;
595 }
596
597 if (vpap) {
598 vpap->next_gpa = vpa;
599 vpap->len = len;
600 vpap->update_pending = 1;
601 }
602
603 spin_unlock(&tvcpu->arch.vpa_update_lock);
604
605 return err;
606}
607
Paul Mackerras081f3232012-06-01 20:20:24 +1000608static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000609{
Paul Mackerras081f3232012-06-01 20:20:24 +1000610 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000611 void *va;
612 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000613 unsigned long gpa;
614
615 /*
616 * We need to pin the page pointed to by vpap->next_gpa,
617 * but we can't call kvmppc_pin_guest_page under the lock
618 * as it does get_user_pages() and down_read(). So we
619 * have to drop the lock, pin the page, then get the lock
620 * again and check that a new area didn't get registered
621 * in the meantime.
622 */
623 for (;;) {
624 gpa = vpap->next_gpa;
625 spin_unlock(&vcpu->arch.vpa_update_lock);
626 va = NULL;
627 nb = 0;
628 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000629 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000630 spin_lock(&vcpu->arch.vpa_update_lock);
631 if (gpa == vpap->next_gpa)
632 break;
633 /* sigh... unpin that one and try again */
634 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000635 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000636 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000637
638 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000639 if (va && nb < vpap->len) {
640 /*
641 * If it's now too short, it must be that userspace
642 * has changed the mappings underlying guest memory,
643 * so unregister the region.
644 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000645 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000646 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000647 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000648 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000649 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
650 vpap->dirty);
651 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000652 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000653 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000654 if (va)
655 vpap->pinned_end = va + vpap->len;
656}
Paul Mackerras93e60242011-12-12 12:28:55 +0000657
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000658static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
659{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000660 if (!(vcpu->arch.vpa.update_pending ||
661 vcpu->arch.slb_shadow.update_pending ||
662 vcpu->arch.dtl.update_pending))
663 return;
664
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000665 spin_lock(&vcpu->arch.vpa_update_lock);
666 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000667 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000668 if (vcpu->arch.vpa.pinned_addr)
669 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000670 }
671 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000672 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000673 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
674 vcpu->arch.dtl_index = 0;
675 }
676 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000677 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000678 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000679}
680
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000681/*
682 * Return the accumulated stolen time for the vcore up until `now'.
683 * The caller should hold the vcore lock.
684 */
685static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
686{
687 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100688 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000689
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000690 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
691
Paul Mackerras2711e242014-12-04 16:43:28 +1100692 spin_lock_irqsave(&vc->stoltb_lock, flags);
693 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000694 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100695 vc->preempt_tb != TB_NIL)
696 p += now - vc->preempt_tb;
697 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000698 return p;
699}
700
Nicholas Piggin617326f2021-11-23 19:52:29 +1000701static void __kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
702 unsigned int pcpu, u64 now,
703 unsigned long stolen)
Paul Mackerras0456ec42012-02-03 00:56:21 +0000704{
705 struct dtl_entry *dt;
706 struct lppaca *vpa;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000707
708 dt = vcpu->arch.dtl_ptr;
709 vpa = vcpu->arch.vpa.pinned_addr;
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000710
Paul Mackerras0456ec42012-02-03 00:56:21 +0000711 if (!dt || !vpa)
712 return;
Nicholas Piggin617326f2021-11-23 19:52:29 +1000713
Paul Mackerras0456ec42012-02-03 00:56:21 +0000714 dt->dispatch_reason = 7;
Nicholas Piggin617326f2021-11-23 19:52:29 +1000715 dt->preempt_reason = 0;
716 dt->processor_id = cpu_to_be16(pcpu + vcpu->arch.ptid);
Alexander Graf02407552014-06-11 10:34:19 +0200717 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
Nicholas Piggin617326f2021-11-23 19:52:29 +1000718 dt->ready_to_enqueue_time = 0;
719 dt->waiting_to_ready_time = 0;
720 dt->timebase = cpu_to_be64(now);
721 dt->fault_addr = 0;
Alexander Graf02407552014-06-11 10:34:19 +0200722 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
723 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Nicholas Piggin617326f2021-11-23 19:52:29 +1000724
Paul Mackerras0456ec42012-02-03 00:56:21 +0000725 ++dt;
726 if (dt == vcpu->arch.dtl.pinned_end)
727 dt = vcpu->arch.dtl.pinned_addr;
728 vcpu->arch.dtl_ptr = dt;
729 /* order writing *dt vs. writing vpa->dtl_idx */
730 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200731 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000732 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000733}
734
Nicholas Piggin617326f2021-11-23 19:52:29 +1000735static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
736 struct kvmppc_vcore *vc)
737{
738 unsigned long stolen;
739 unsigned long core_stolen;
740 u64 now;
741 unsigned long flags;
742
743 now = mftb();
744
745 core_stolen = vcore_stolen_time(vc, now);
746 stolen = core_stolen - vcpu->arch.stolen_logged;
747 vcpu->arch.stolen_logged = core_stolen;
748 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
749 stolen += vcpu->arch.busy_stolen;
750 vcpu->arch.busy_stolen = 0;
751 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
752
753 __kvmppc_create_dtl_entry(vcpu, vc->pcpu, now + vc->tb_offset, stolen);
754}
755
Paul Mackerras1da4e2f2017-05-19 16:26:16 +1000756/* See if there is a doorbell interrupt pending for a vcpu */
757static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu)
758{
759 int thr;
760 struct kvmppc_vcore *vc;
761
Paul Mackerras57900692017-05-16 16:41:20 +1000762 if (vcpu->arch.doorbell_request)
763 return true;
Nicholas Piggin63983262021-11-23 19:52:30 +1000764 if (cpu_has_feature(CPU_FTR_ARCH_300))
765 return false;
Paul Mackerras57900692017-05-16 16:41:20 +1000766 /*
767 * Ensure that the read of vcore->dpdes comes after the read
768 * of vcpu->doorbell_request. This barrier matches the
Palmer Dabbelt6fabc9f2019-04-25 12:53:39 -0700769 * smp_wmb() in kvmppc_guest_entry_inject().
Paul Mackerras57900692017-05-16 16:41:20 +1000770 */
771 smp_rmb();
Paul Mackerras1da4e2f2017-05-19 16:26:16 +1000772 vc = vcpu->arch.vcore;
773 thr = vcpu->vcpu_id - vc->first_vcpuid;
774 return !!(vc->dpdes & (1 << thr));
775}
776
Michael Neuling96423822014-06-02 11:03:01 +1000777static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
778{
779 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
780 return true;
781 if ((!vcpu->arch.vcore->arch_compat) &&
782 cpu_has_feature(CPU_FTR_ARCH_207S))
783 return true;
784 return false;
785}
786
787static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
788 unsigned long resource, unsigned long value1,
789 unsigned long value2)
790{
791 switch (resource) {
792 case H_SET_MODE_RESOURCE_SET_CIABR:
793 if (!kvmppc_power8_compatible(vcpu))
794 return H_P2;
795 if (value2)
796 return H_P4;
797 if (mflags)
798 return H_UNSUPPORTED_FLAG_START;
799 /* Guests can't breakpoint the hypervisor */
800 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
801 return H_P3;
802 vcpu->arch.ciabr = value1;
803 return H_SUCCESS;
Ravi Bangoria6f3fe292020-07-23 14:38:10 +0530804 case H_SET_MODE_RESOURCE_SET_DAWR0:
Michael Neuling96423822014-06-02 11:03:01 +1000805 if (!kvmppc_power8_compatible(vcpu))
806 return H_P2;
Michael Neuling398e7122018-03-27 15:37:20 +1100807 if (!ppc_breakpoint_available())
808 return H_P2;
Michael Neuling96423822014-06-02 11:03:01 +1000809 if (mflags)
810 return H_UNSUPPORTED_FLAG_START;
811 if (value2 & DABRX_HYP)
812 return H_P4;
Ravi Bangoria122954ed72020-12-16 16:12:17 +0530813 vcpu->arch.dawr0 = value1;
814 vcpu->arch.dawrx0 = value2;
Michael Neuling96423822014-06-02 11:03:01 +1000815 return H_SUCCESS;
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +0530816 case H_SET_MODE_RESOURCE_SET_DAWR1:
817 if (!kvmppc_power8_compatible(vcpu))
818 return H_P2;
819 if (!ppc_breakpoint_available())
820 return H_P2;
821 if (!cpu_has_feature(CPU_FTR_DAWR1))
822 return H_P2;
823 if (!vcpu->kvm->arch.dawr1_enabled)
824 return H_FUNCTION;
825 if (mflags)
826 return H_UNSUPPORTED_FLAG_START;
827 if (value2 & DABRX_HYP)
828 return H_P4;
829 vcpu->arch.dawr1 = value1;
830 vcpu->arch.dawrx1 = value2;
831 return H_SUCCESS;
Nicholas Piggin55d70042019-10-02 16:00:25 +1000832 case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE:
Nicholas Pigginbcc92a02021-04-12 11:48:37 +1000833 /*
834 * KVM does not support mflags=2 (AIL=2) and AIL=1 is reserved.
835 * Keep this in synch with kvmppc_filter_guest_lpcr_hv.
836 */
Nicholas Piggin2e1ae9cd2021-05-28 19:07:41 +1000837 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
838 kvmhv_vcpu_is_radix(vcpu) && mflags == 3)
Nicholas Piggin55d70042019-10-02 16:00:25 +1000839 return H_UNSUPPORTED_FLAG_START;
840 return H_TOO_HARD;
Michael Neuling96423822014-06-02 11:03:01 +1000841 default:
842 return H_TOO_HARD;
843 }
844}
845
Suraj Jitindar Singh2d34d1c2019-03-22 17:05:44 +1100846/* Copy guest memory in place - must reside within a single memslot */
847static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from,
848 unsigned long len)
849{
850 struct kvm_memory_slot *to_memslot = NULL;
851 struct kvm_memory_slot *from_memslot = NULL;
852 unsigned long to_addr, from_addr;
853 int r;
854
855 /* Get HPA for from address */
856 from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT);
857 if (!from_memslot)
858 return -EFAULT;
859 if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages)
860 << PAGE_SHIFT))
861 return -EINVAL;
862 from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT);
863 if (kvm_is_error_hva(from_addr))
864 return -EFAULT;
865 from_addr |= (from & (PAGE_SIZE - 1));
866
867 /* Get HPA for to address */
868 to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT);
869 if (!to_memslot)
870 return -EFAULT;
871 if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages)
872 << PAGE_SHIFT))
873 return -EINVAL;
874 to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT);
875 if (kvm_is_error_hva(to_addr))
876 return -EFAULT;
877 to_addr |= (to & (PAGE_SIZE - 1));
878
879 /* Perform copy */
880 r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr,
881 len);
882 if (r)
883 return -EFAULT;
884 mark_page_dirty(kvm, to >> PAGE_SHIFT);
885 return 0;
886}
887
888static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags,
889 unsigned long dest, unsigned long src)
890{
891 u64 pg_sz = SZ_4K; /* 4K page size */
892 u64 pg_mask = SZ_4K - 1;
893 int ret;
894
895 /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */
896 if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE |
897 H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED))
898 return H_PARAMETER;
899
900 /* dest (and src if copy_page flag set) must be page aligned */
901 if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask)))
902 return H_PARAMETER;
903
904 /* zero and/or copy the page as determined by the flags */
905 if (flags & H_COPY_PAGE) {
906 ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz);
907 if (ret < 0)
908 return H_PARAMETER;
909 } else if (flags & H_ZERO_PAGE) {
910 ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz);
911 if (ret < 0)
912 return H_PARAMETER;
913 }
914
915 /* We can ignore the remaining flags */
916
917 return H_SUCCESS;
918}
919
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100920static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
921{
922 struct kvmppc_vcore *vcore = target->arch.vcore;
923
924 /*
925 * We expect to have been called by the real mode handler
926 * (kvmppc_rm_h_confer()) which would have directly returned
927 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
928 * have useful work to do and should not confer) so we don't
929 * recheck that here.
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +1000930 *
931 * In the case of the P9 single vcpu per vcore case, the real
932 * mode handler is not called but no other threads are in the
933 * source vcore.
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100934 */
Nicholas Pigginecb6a722021-11-23 19:52:28 +1000935 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
936 spin_lock(&vcore->lock);
937 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
938 vcore->vcore_state != VCORE_INACTIVE &&
939 vcore->runner)
940 target = vcore->runner;
941 spin_unlock(&vcore->lock);
942 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100943
944 return kvm_vcpu_yield_to(target);
945}
946
947static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
948{
949 int yield_count = 0;
950 struct lppaca *lppaca;
951
952 spin_lock(&vcpu->arch.vpa_update_lock);
953 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
954 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100955 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100956 spin_unlock(&vcpu->arch.vpa_update_lock);
957 return yield_count;
958}
959
Bharata B Rao53324b52021-06-21 14:20:01 +0530960/*
961 * H_RPT_INVALIDATE hcall handler for nested guests.
962 *
963 * Handles only nested process-scoped invalidation requests in L0.
964 */
965static int kvmppc_nested_h_rpt_invalidate(struct kvm_vcpu *vcpu)
966{
967 unsigned long type = kvmppc_get_gpr(vcpu, 6);
968 unsigned long pid, pg_sizes, start, end;
969
970 /*
971 * The partition-scoped invalidations aren't handled here in L0.
972 */
973 if (type & H_RPTI_TYPE_NESTED)
974 return RESUME_HOST;
975
976 pid = kvmppc_get_gpr(vcpu, 4);
977 pg_sizes = kvmppc_get_gpr(vcpu, 7);
978 start = kvmppc_get_gpr(vcpu, 8);
979 end = kvmppc_get_gpr(vcpu, 9);
980
981 do_h_rpt_invalidate_prt(pid, vcpu->arch.nested->shadow_lpid,
982 type, pg_sizes, start, end);
983
984 kvmppc_set_gpr(vcpu, 3, H_SUCCESS);
985 return RESUME_GUEST;
986}
987
Bharata B Raof0c6fbb2021-06-21 14:20:00 +0530988static long kvmppc_h_rpt_invalidate(struct kvm_vcpu *vcpu,
989 unsigned long id, unsigned long target,
990 unsigned long type, unsigned long pg_sizes,
991 unsigned long start, unsigned long end)
992{
993 if (!kvm_is_radix(vcpu->kvm))
994 return H_UNSUPPORTED;
995
996 if (end < start)
997 return H_P5;
998
999 /*
1000 * Partition-scoped invalidation for nested guests.
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301001 */
Bharata B Rao53324b52021-06-21 14:20:01 +05301002 if (type & H_RPTI_TYPE_NESTED) {
1003 if (!nesting_enabled(vcpu->kvm))
1004 return H_FUNCTION;
1005
1006 /* Support only cores as target */
1007 if (target != H_RPTI_TARGET_CMMU)
1008 return H_P2;
1009
1010 return do_h_rpt_invalidate_pat(vcpu, id, type, pg_sizes,
1011 start, end);
1012 }
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301013
1014 /*
1015 * Process-scoped invalidation for L1 guests.
1016 */
1017 do_h_rpt_invalidate_prt(id, vcpu->kvm->arch.lpid,
1018 type, pg_sizes, start, end);
1019 return H_SUCCESS;
1020}
1021
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001022int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
1023{
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001024 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001025 unsigned long req = kvmppc_get_gpr(vcpu, 3);
1026 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001027 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001028 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001029 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001030
Paul Mackerras699a0ea2014-06-02 11:02:59 +10001031 if (req <= MAX_HCALL_OPCODE &&
1032 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
1033 return RESUME_HOST;
1034
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001035 switch (req) {
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001036 case H_REMOVE:
1037 ret = kvmppc_h_remove(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_ENTER:
1044 ret = kvmppc_h_enter(vcpu, kvmppc_get_gpr(vcpu, 4),
1045 kvmppc_get_gpr(vcpu, 5),
1046 kvmppc_get_gpr(vcpu, 6),
1047 kvmppc_get_gpr(vcpu, 7));
1048 if (ret == H_TOO_HARD)
1049 return RESUME_HOST;
1050 break;
1051 case H_READ:
1052 ret = kvmppc_h_read(vcpu, kvmppc_get_gpr(vcpu, 4),
1053 kvmppc_get_gpr(vcpu, 5));
1054 if (ret == H_TOO_HARD)
1055 return RESUME_HOST;
1056 break;
1057 case H_CLEAR_MOD:
1058 ret = kvmppc_h_clear_mod(vcpu, kvmppc_get_gpr(vcpu, 4),
1059 kvmppc_get_gpr(vcpu, 5));
1060 if (ret == H_TOO_HARD)
1061 return RESUME_HOST;
1062 break;
1063 case H_CLEAR_REF:
1064 ret = kvmppc_h_clear_ref(vcpu, kvmppc_get_gpr(vcpu, 4),
1065 kvmppc_get_gpr(vcpu, 5));
1066 if (ret == H_TOO_HARD)
1067 return RESUME_HOST;
1068 break;
1069 case H_PROTECT:
1070 ret = kvmppc_h_protect(vcpu, kvmppc_get_gpr(vcpu, 4),
1071 kvmppc_get_gpr(vcpu, 5),
1072 kvmppc_get_gpr(vcpu, 6));
1073 if (ret == H_TOO_HARD)
1074 return RESUME_HOST;
1075 break;
1076 case H_BULK_REMOVE:
1077 ret = kvmppc_h_bulk_remove(vcpu);
1078 if (ret == H_TOO_HARD)
1079 return RESUME_HOST;
1080 break;
1081
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001082 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001083 break;
1084 case H_PROD:
1085 target = kvmppc_get_gpr(vcpu, 4);
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001086 tvcpu = kvmppc_find_vcpu(kvm, target);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001087 if (!tvcpu) {
1088 ret = H_PARAMETER;
1089 break;
1090 }
1091 tvcpu->arch.prodded = 1;
1092 smp_mb();
Paul Mackerras8464c882016-12-06 20:42:05 +11001093 if (tvcpu->arch.ceded)
1094 kvmppc_fast_vcpu_kick_hv(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001095 break;
1096 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +10001097 target = kvmppc_get_gpr(vcpu, 4);
1098 if (target == -1)
1099 break;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001100 tvcpu = kvmppc_find_vcpu(kvm, target);
Paul Mackerras42d76042013-09-06 13:23:21 +10001101 if (!tvcpu) {
1102 ret = H_PARAMETER;
1103 break;
1104 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +11001105 yield_count = kvmppc_get_gpr(vcpu, 5);
1106 if (kvmppc_get_yield_count(tvcpu) != yield_count)
1107 break;
1108 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001109 break;
1110 case H_REGISTER_VPA:
1111 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
1112 kvmppc_get_gpr(vcpu, 5),
1113 kvmppc_get_gpr(vcpu, 6));
1114 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001115 case H_RTAS:
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001116 if (list_empty(&kvm->arch.rtas_tokens))
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001117 return RESUME_HOST;
1118
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001119 idx = srcu_read_lock(&kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001120 rc = kvmppc_rtas_hcall(vcpu);
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001121 srcu_read_unlock(&kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +00001122
1123 if (rc == -ENOENT)
1124 return RESUME_HOST;
1125 else if (rc == 0)
1126 break;
1127
1128 /* Send the error out to userspace via KVM_RUN */
1129 return rc;
David Gibson99342cf82015-02-05 11:53:25 +11001130 case H_LOGICAL_CI_LOAD:
1131 ret = kvmppc_h_logical_ci_load(vcpu);
1132 if (ret == H_TOO_HARD)
1133 return RESUME_HOST;
1134 break;
1135 case H_LOGICAL_CI_STORE:
1136 ret = kvmppc_h_logical_ci_store(vcpu);
1137 if (ret == H_TOO_HARD)
1138 return RESUME_HOST;
1139 break;
Michael Neuling96423822014-06-02 11:03:01 +10001140 case H_SET_MODE:
1141 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
1142 kvmppc_get_gpr(vcpu, 5),
1143 kvmppc_get_gpr(vcpu, 6),
1144 kvmppc_get_gpr(vcpu, 7));
1145 if (ret == H_TOO_HARD)
1146 return RESUME_HOST;
1147 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001148 case H_XIRR:
1149 case H_CPPR:
1150 case H_EOI:
1151 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +00001152 case H_IPOLL:
1153 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001154 if (kvmppc_xics_enabled(vcpu)) {
Paul Mackerras03f95332019-02-04 22:07:20 +11001155 if (xics_on_xive()) {
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10001156 ret = H_NOT_AVAILABLE;
1157 return RESUME_GUEST;
1158 }
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +00001159 ret = kvmppc_xics_hcall(vcpu, req);
1160 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11001161 }
1162 return RESUME_HOST;
Paul Mackerras4bad7772018-10-08 16:31:06 +11001163 case H_SET_DABR:
1164 ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4));
1165 break;
1166 case H_SET_XDABR:
1167 ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4),
1168 kvmppc_get_gpr(vcpu, 5));
1169 break;
Jordan Niethee40542a2019-02-21 14:28:48 +11001170#ifdef CONFIG_SPAPR_TCE_IOMMU
Paul Mackerras4bad7772018-10-08 16:31:06 +11001171 case H_GET_TCE:
1172 ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1173 kvmppc_get_gpr(vcpu, 5));
1174 if (ret == H_TOO_HARD)
1175 return RESUME_HOST;
1176 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +11001177 case H_PUT_TCE:
1178 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1179 kvmppc_get_gpr(vcpu, 5),
1180 kvmppc_get_gpr(vcpu, 6));
1181 if (ret == H_TOO_HARD)
1182 return RESUME_HOST;
1183 break;
1184 case H_PUT_TCE_INDIRECT:
1185 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
1186 kvmppc_get_gpr(vcpu, 5),
1187 kvmppc_get_gpr(vcpu, 6),
1188 kvmppc_get_gpr(vcpu, 7));
1189 if (ret == H_TOO_HARD)
1190 return RESUME_HOST;
1191 break;
1192 case H_STUFF_TCE:
1193 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
1194 kvmppc_get_gpr(vcpu, 5),
1195 kvmppc_get_gpr(vcpu, 6),
1196 kvmppc_get_gpr(vcpu, 7));
1197 if (ret == H_TOO_HARD)
1198 return RESUME_HOST;
1199 break;
Jordan Niethee40542a2019-02-21 14:28:48 +11001200#endif
Paul Mackerras4bad7772018-10-08 16:31:06 +11001201 case H_RANDOM:
Alexey Kardashevskiy2ac78e02021-08-05 17:56:49 +10001202 if (!arch_get_random_seed_long(&vcpu->arch.regs.gpr[4]))
Paul Mackerras4bad7772018-10-08 16:31:06 +11001203 ret = H_HARDWARE;
1204 break;
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301205 case H_RPT_INVALIDATE:
1206 ret = kvmppc_h_rpt_invalidate(vcpu, kvmppc_get_gpr(vcpu, 4),
1207 kvmppc_get_gpr(vcpu, 5),
1208 kvmppc_get_gpr(vcpu, 6),
1209 kvmppc_get_gpr(vcpu, 7),
1210 kvmppc_get_gpr(vcpu, 8),
1211 kvmppc_get_gpr(vcpu, 9));
1212 break;
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001213
1214 case H_SET_PARTITION_TABLE:
1215 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001216 if (nesting_enabled(kvm))
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001217 ret = kvmhv_set_partition_table(vcpu);
1218 break;
1219 case H_ENTER_NESTED:
1220 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001221 if (!nesting_enabled(kvm))
Paul Mackerras360cae32018-10-08 16:31:04 +11001222 break;
1223 ret = kvmhv_enter_nested_guest(vcpu);
1224 if (ret == H_INTERRUPT) {
1225 kvmppc_set_gpr(vcpu, 3, 0);
Michael Roth6c08ec12018-11-08 21:27:23 -06001226 vcpu->arch.hcall_needed = 0;
Paul Mackerras360cae32018-10-08 16:31:04 +11001227 return -EINTR;
Suraj Jitindar Singh873db2c2018-12-14 16:29:08 +11001228 } else if (ret == H_TOO_HARD) {
1229 kvmppc_set_gpr(vcpu, 3, 0);
1230 vcpu->arch.hcall_needed = 0;
1231 return RESUME_HOST;
Paul Mackerras360cae32018-10-08 16:31:04 +11001232 }
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001233 break;
1234 case H_TLB_INVALIDATE:
1235 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001236 if (nesting_enabled(kvm))
Paul Mackerrasaa069a92018-09-21 20:02:01 +10001237 ret = kvmhv_do_nested_tlbie(vcpu);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11001238 break;
Suraj Jitindar Singh6ff887b2018-12-14 16:29:09 +11001239 case H_COPY_TOFROM_GUEST:
1240 ret = H_FUNCTION;
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001241 if (nesting_enabled(kvm))
Suraj Jitindar Singh6ff887b2018-12-14 16:29:09 +11001242 ret = kvmhv_copy_tofrom_guest_nested(vcpu);
1243 break;
Suraj Jitindar Singh2d34d1c2019-03-22 17:05:44 +11001244 case H_PAGE_INIT:
1245 ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4),
1246 kvmppc_get_gpr(vcpu, 5),
1247 kvmppc_get_gpr(vcpu, 6));
1248 break;
Bharata B Raoca9f4942019-11-25 08:36:26 +05301249 case H_SVM_PAGE_IN:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001250 ret = H_UNSUPPORTED;
1251 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001252 ret = kvmppc_h_svm_page_in(kvm,
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001253 kvmppc_get_gpr(vcpu, 4),
1254 kvmppc_get_gpr(vcpu, 5),
1255 kvmppc_get_gpr(vcpu, 6));
Bharata B Raoca9f4942019-11-25 08:36:26 +05301256 break;
1257 case H_SVM_PAGE_OUT:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001258 ret = H_UNSUPPORTED;
1259 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001260 ret = kvmppc_h_svm_page_out(kvm,
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001261 kvmppc_get_gpr(vcpu, 4),
1262 kvmppc_get_gpr(vcpu, 5),
1263 kvmppc_get_gpr(vcpu, 6));
Bharata B Raoca9f4942019-11-25 08:36:26 +05301264 break;
1265 case H_SVM_INIT_START:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001266 ret = H_UNSUPPORTED;
1267 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001268 ret = kvmppc_h_svm_init_start(kvm);
Bharata B Raoca9f4942019-11-25 08:36:26 +05301269 break;
1270 case H_SVM_INIT_DONE:
Laurent Dufour8c47b6ff2020-03-20 11:26:42 +01001271 ret = H_UNSUPPORTED;
1272 if (kvmppc_get_srr1(vcpu) & MSR_S)
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001273 ret = kvmppc_h_svm_init_done(kvm);
Bharata B Raoca9f4942019-11-25 08:36:26 +05301274 break;
Sukadev Bhattiprolu3a439702020-01-06 18:02:37 -08001275 case H_SVM_INIT_ABORT:
Laurent Dufoure3326ae2020-05-20 19:43:08 +02001276 /*
1277 * Even if that call is made by the Ultravisor, the SSR1 value
1278 * is the guest context one, with the secure bit clear as it has
1279 * not yet been secured. So we can't check it here.
1280 * Instead the kvm->arch.secure_guest flag is checked inside
1281 * kvmppc_h_svm_init_abort().
1282 */
Nicholas Piggina9aa86e2021-05-28 19:07:47 +10001283 ret = kvmppc_h_svm_init_abort(kvm);
Sukadev Bhattiprolu3a439702020-01-06 18:02:37 -08001284 break;
Bharata B Raoca9f4942019-11-25 08:36:26 +05301285
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001286 default:
1287 return RESUME_HOST;
1288 }
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001289 WARN_ON_ONCE(ret == H_TOO_HARD);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00001290 kvmppc_set_gpr(vcpu, 3, ret);
1291 vcpu->arch.hcall_needed = 0;
1292 return RESUME_GUEST;
1293}
1294
Paul Mackerras4bad7772018-10-08 16:31:06 +11001295/*
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001296 * Handle H_CEDE in the P9 path where we don't call the real-mode hcall
1297 * handlers in book3s_hv_rmhandlers.S.
1298 *
Paul Mackerras4bad7772018-10-08 16:31:06 +11001299 * This has to be done early, not in kvmppc_pseries_do_hcall(), so
1300 * that the cede logic in kvmppc_run_single_vcpu() works properly.
1301 */
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001302static void kvmppc_cede(struct kvm_vcpu *vcpu)
Paul Mackerras4bad7772018-10-08 16:31:06 +11001303{
1304 vcpu->arch.shregs.msr |= MSR_EE;
1305 vcpu->arch.ceded = 1;
1306 smp_mb();
1307 if (vcpu->arch.prodded) {
1308 vcpu->arch.prodded = 0;
1309 smp_mb();
1310 vcpu->arch.ceded = 0;
1311 }
1312}
1313
Paul Mackerrasae2113a2014-06-02 11:03:00 +10001314static int kvmppc_hcall_impl_hv(unsigned long cmd)
1315{
1316 switch (cmd) {
1317 case H_CEDE:
1318 case H_PROD:
1319 case H_CONFER:
1320 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +10001321 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +11001322 case H_LOGICAL_CI_LOAD:
1323 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +10001324#ifdef CONFIG_KVM_XICS
1325 case H_XIRR:
1326 case H_CPPR:
1327 case H_EOI:
1328 case H_IPI:
1329 case H_IPOLL:
1330 case H_XIRR_X:
1331#endif
Suraj Jitindar Singh2d34d1c2019-03-22 17:05:44 +11001332 case H_PAGE_INIT:
Bharata B Raof0c6fbb2021-06-21 14:20:00 +05301333 case H_RPT_INVALIDATE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +10001334 return 1;
1335 }
1336
1337 /* See if it's in the real-mode table */
1338 return kvmppc_hcall_impl_hv_realmode(cmd);
1339}
1340
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001341static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu)
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301342{
1343 u32 last_inst;
1344
1345 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
1346 EMULATE_DONE) {
1347 /*
1348 * Fetch failed, so return to guest and
1349 * try executing it again.
1350 */
1351 return RESUME_GUEST;
1352 }
1353
1354 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001355 vcpu->run->exit_reason = KVM_EXIT_DEBUG;
1356 vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu);
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301357 return RESUME_HOST;
1358 } else {
1359 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1360 return RESUME_GUEST;
1361 }
1362}
1363
Paul Mackerras57900692017-05-16 16:41:20 +10001364static void do_nothing(void *x)
1365{
1366}
1367
1368static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu)
1369{
1370 int thr, cpu, pcpu, nthreads;
1371 struct kvm_vcpu *v;
1372 unsigned long dpdes;
1373
1374 nthreads = vcpu->kvm->arch.emul_smt_mode;
1375 dpdes = 0;
1376 cpu = vcpu->vcpu_id & ~(nthreads - 1);
1377 for (thr = 0; thr < nthreads; ++thr, ++cpu) {
1378 v = kvmppc_find_vcpu(vcpu->kvm, cpu);
1379 if (!v)
1380 continue;
1381 /*
1382 * If the vcpu is currently running on a physical cpu thread,
1383 * interrupt it in order to pull it out of the guest briefly,
1384 * which will update its vcore->dpdes value.
1385 */
1386 pcpu = READ_ONCE(v->cpu);
1387 if (pcpu >= 0)
1388 smp_call_function_single(pcpu, do_nothing, NULL, 1);
1389 if (kvmppc_doorbell_pending(v))
1390 dpdes |= 1 << thr;
1391 }
1392 return dpdes;
1393}
1394
1395/*
1396 * On POWER9, emulate doorbell-related instructions in order to
1397 * give the guest the illusion of running on a multi-threaded core.
1398 * The instructions emulated are msgsndp, msgclrp, mfspr TIR,
1399 * and mfspr DPDES.
1400 */
1401static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu)
1402{
1403 u32 inst, rb, thr;
1404 unsigned long arg;
1405 struct kvm *kvm = vcpu->kvm;
1406 struct kvm_vcpu *tvcpu;
1407
Paul Mackerras57900692017-05-16 16:41:20 +10001408 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE)
1409 return RESUME_GUEST;
1410 if (get_op(inst) != 31)
1411 return EMULATE_FAIL;
1412 rb = get_rb(inst);
1413 thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1);
1414 switch (get_xop(inst)) {
1415 case OP_31_XOP_MSGSNDP:
1416 arg = kvmppc_get_gpr(vcpu, rb);
Leonardo Bras87fb4972020-12-08 18:57:08 -03001417 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
Paul Mackerras57900692017-05-16 16:41:20 +10001418 break;
Leonardo Bras87fb4972020-12-08 18:57:08 -03001419 arg &= 0x7f;
Paul Mackerras57900692017-05-16 16:41:20 +10001420 if (arg >= kvm->arch.emul_smt_mode)
1421 break;
1422 tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg);
1423 if (!tvcpu)
1424 break;
1425 if (!tvcpu->arch.doorbell_request) {
1426 tvcpu->arch.doorbell_request = 1;
1427 kvmppc_fast_vcpu_kick_hv(tvcpu);
1428 }
1429 break;
1430 case OP_31_XOP_MSGCLRP:
1431 arg = kvmppc_get_gpr(vcpu, rb);
Leonardo Bras87fb4972020-12-08 18:57:08 -03001432 if (((arg >> 27) & 0x1f) != PPC_DBELL_SERVER)
Paul Mackerras57900692017-05-16 16:41:20 +10001433 break;
1434 vcpu->arch.vcore->dpdes = 0;
1435 vcpu->arch.doorbell_request = 0;
1436 break;
1437 case OP_31_XOP_MFSPR:
1438 switch (get_sprn(inst)) {
1439 case SPRN_TIR:
1440 arg = thr;
1441 break;
1442 case SPRN_DPDES:
1443 arg = kvmppc_read_dpdes(vcpu);
1444 break;
1445 default:
1446 return EMULATE_FAIL;
1447 }
1448 kvmppc_set_gpr(vcpu, get_rt(inst), arg);
1449 break;
1450 default:
1451 return EMULATE_FAIL;
1452 }
1453 kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4);
1454 return RESUME_GUEST;
1455}
1456
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001457/*
1458 * If the lppaca had pmcregs_in_use clear when we exited the guest, then
1459 * HFSCR_PM is cleared for next entry. If the guest then tries to access
1460 * the PMU SPRs, we get this facility unavailable interrupt. Putting HFSCR_PM
1461 * back in the guest HFSCR will cause the next entry to load the PMU SPRs and
1462 * allow the guest access to continue.
1463 */
1464static int kvmppc_pmu_unavailable(struct kvm_vcpu *vcpu)
1465{
1466 if (!(vcpu->arch.hfscr_permitted & HFSCR_PM))
1467 return EMULATE_FAIL;
1468
1469 vcpu->arch.hfscr |= HFSCR_PM;
1470
1471 return RESUME_GUEST;
1472}
1473
Nicholas Piggina3e18ca2021-11-23 19:52:14 +10001474static int kvmppc_ebb_unavailable(struct kvm_vcpu *vcpu)
1475{
1476 if (!(vcpu->arch.hfscr_permitted & HFSCR_EBB))
1477 return EMULATE_FAIL;
1478
1479 vcpu->arch.hfscr |= HFSCR_EBB;
1480
1481 return RESUME_GUEST;
1482}
1483
Nicholas Piggin022ecb92021-11-23 19:52:15 +10001484static int kvmppc_tm_unavailable(struct kvm_vcpu *vcpu)
1485{
1486 if (!(vcpu->arch.hfscr_permitted & HFSCR_TM))
1487 return EMULATE_FAIL;
1488
1489 vcpu->arch.hfscr |= HFSCR_TM;
1490
1491 return RESUME_GUEST;
1492}
1493
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001494static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301495 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001496{
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001497 struct kvm_run *run = vcpu->run;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001498 int r = RESUME_HOST;
1499
1500 vcpu->stat.sum_exits++;
1501
Paul Mackerras1c9e3d52015-11-12 16:43:48 +11001502 /*
1503 * This can happen if an interrupt occurs in the last stages
1504 * of guest entry or the first stages of guest exit (i.e. after
1505 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1506 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1507 * That can happen due to a bug, or due to a machine check
1508 * occurring at just the wrong time.
1509 */
1510 if (vcpu->arch.shregs.msr & MSR_HV) {
1511 printk(KERN_EMERG "KVM trap in HV mode!\n");
1512 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1513 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1514 vcpu->arch.shregs.msr);
1515 kvmppc_dump_regs(vcpu);
1516 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
1517 run->hw.hardware_exit_reason = vcpu->arch.trap;
1518 return RESUME_HOST;
1519 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001520 run->exit_reason = KVM_EXIT_UNKNOWN;
1521 run->ready_for_interrupt_injection = 1;
1522 switch (vcpu->arch.trap) {
1523 /* We're good on these - the host merely wanted to get our attention */
Nicholas Piggin46dea772021-11-23 19:52:22 +10001524 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
1525 WARN_ON_ONCE(1); /* Should never happen */
1526 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
1527 fallthrough;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001528 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1529 vcpu->stat.dec_exits++;
1530 r = RESUME_GUEST;
1531 break;
1532 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +11001533 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerras84f71392016-11-22 14:30:14 +11001534 case BOOK3S_INTERRUPT_H_VIRT:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001535 vcpu->stat.ext_intr_exits++;
1536 r = RESUME_GUEST;
1537 break;
Nicholas Piggin6de66382017-11-05 23:33:55 +11001538 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +11001539 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001540 case BOOK3S_INTERRUPT_PERFMON:
Nicholas Piggin6de66382017-11-05 23:33:55 +11001541 case BOOK3S_INTERRUPT_SYSTEM_RESET:
Paul Mackerrasde56a942011-06-29 00:21:34 +00001542 r = RESUME_GUEST;
1543 break;
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10001544 case BOOK3S_INTERRUPT_MACHINE_CHECK: {
1545 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1546 DEFAULT_RATELIMIT_BURST);
1547 /*
1548 * Print the MCE event to host console. Ratelimit so the guest
1549 * can't flood the host log.
1550 */
1551 if (__ratelimit(&rs))
1552 machine_check_print_event_info(&vcpu->arch.mce_evt,false, true);
Paul Mackerras884dfb72019-02-21 13:38:49 +11001553
1554 /*
1555 * If the guest can do FWNMI, exit to userspace so it can
1556 * deliver a FWNMI to the guest.
1557 * Otherwise we synthesize a machine check for the guest
1558 * so that it knows that the machine check occurred.
1559 */
1560 if (!vcpu->kvm->arch.fwnmi_enabled) {
1561 ulong flags = vcpu->arch.shregs.msr & 0x083c0000;
1562 kvmppc_core_queue_machine_check(vcpu, flags);
1563 r = RESUME_GUEST;
1564 break;
1565 }
1566
Aravinda Prasade20bbd32017-05-11 16:33:37 +05301567 /* Exit to guest with KVM_EXIT_NMI as exit reason */
1568 run->exit_reason = KVM_EXIT_NMI;
1569 run->hw.hardware_exit_reason = vcpu->arch.trap;
1570 /* Clear out the old NMI status from run->flags */
1571 run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK;
1572 /* Now set the NMI status */
1573 if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED)
1574 run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV;
1575 else
1576 run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV;
1577
1578 r = RESUME_HOST;
Paul Mackerrasb4072df2012-11-23 22:37:50 +00001579 break;
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10001580 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001581 case BOOK3S_INTERRUPT_PROGRAM:
1582 {
1583 ulong flags;
1584 /*
1585 * Normally program interrupts are delivered directly
1586 * to the guest by the hardware, but we can get here
1587 * as a result of a hypervisor emulation interrupt
1588 * (e40) getting turned into a 700 by BML RTAS.
1589 */
1590 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
1591 kvmppc_core_queue_program(vcpu, flags);
1592 r = RESUME_GUEST;
1593 break;
1594 }
1595 case BOOK3S_INTERRUPT_SYSCALL:
1596 {
Paul Mackerrasde56a942011-06-29 00:21:34 +00001597 int i;
1598
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001599 if (unlikely(vcpu->arch.shregs.msr & MSR_PR)) {
1600 /*
1601 * Guest userspace executed sc 1. This can only be
1602 * reached by the P9 path because the old path
1603 * handles this case in realmode hcall handlers.
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001604 */
Nicholas Pigginac3c8b42021-05-28 19:07:49 +10001605 if (!kvmhv_vcpu_is_radix(vcpu)) {
1606 /*
1607 * A guest could be running PR KVM, so this
1608 * may be a PR KVM hcall. It must be reflected
1609 * to the guest kernel as a sc interrupt.
1610 */
1611 kvmppc_core_queue_syscall(vcpu);
1612 } else {
1613 /*
1614 * Radix guests can not run PR KVM or nested HV
1615 * hash guests which might run PR KVM, so this
1616 * is always a privilege fault. Send a program
1617 * check to guest kernel.
1618 */
1619 kvmppc_core_queue_program(vcpu, SRR1_PROGPRIV);
1620 }
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001621 r = RESUME_GUEST;
1622 break;
1623 }
Liu Ping Fan27025a62013-11-19 14:12:48 +08001624
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10001625 /*
1626 * hcall - gather args and set exit_reason. This will next be
1627 * handled by kvmppc_pseries_do_hcall which may be able to deal
1628 * with it and resume guest, or may punt to userspace.
1629 */
Paul Mackerrasde56a942011-06-29 00:21:34 +00001630 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
1631 for (i = 0; i < 9; ++i)
1632 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
1633 run->exit_reason = KVM_EXIT_PAPR_HCALL;
1634 vcpu->arch.hcall_needed = 1;
1635 r = RESUME_HOST;
1636 break;
1637 }
1638 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +00001639 * We get these next two if the guest accesses a page which it thinks
1640 * it has mapped but which is not actually present, either because
1641 * it is for an emulated I/O device or because the corresonding
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001642 * host page has been paged out.
1643 *
1644 * Any other HDSI/HISI interrupts have been handled already for P7/8
1645 * guests. For POWER9 hash guests not using rmhandlers, basic hash
1646 * fault handling is done here.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001647 */
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001648 case BOOK3S_INTERRUPT_H_DATA_STORAGE: {
1649 unsigned long vsid;
1650 long err;
1651
Nicholas Piggind55b1ec2021-11-23 19:52:12 +10001652 if (cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG) &&
1653 unlikely(vcpu->arch.fault_dsisr == HDSISR_CANARY)) {
Nicholas Piggin89d35b22021-05-28 19:07:34 +10001654 r = RESUME_GUEST; /* Just retry if it's the canary */
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001655 break;
1656 }
1657
1658 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1659 /*
1660 * Radix doesn't require anything, and pre-ISAv3.0 hash
1661 * already attempted to handle this in rmhandlers. The
1662 * hash fault handling below is v3 only (it uses ASDR
1663 * via fault_gpa).
1664 */
1665 r = RESUME_PAGE_FAULT;
1666 break;
1667 }
1668
1669 if (!(vcpu->arch.fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT))) {
1670 kvmppc_core_queue_data_storage(vcpu,
1671 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
1672 r = RESUME_GUEST;
1673 break;
1674 }
1675
1676 if (!(vcpu->arch.shregs.msr & MSR_DR))
1677 vsid = vcpu->kvm->arch.vrma_slb_v;
1678 else
1679 vsid = vcpu->arch.fault_gpa;
1680
1681 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1682 vsid, vcpu->arch.fault_dsisr, true);
1683 if (err == 0) {
1684 r = RESUME_GUEST;
1685 } else if (err == -1 || err == -2) {
1686 r = RESUME_PAGE_FAULT;
1687 } else {
1688 kvmppc_core_queue_data_storage(vcpu,
1689 vcpu->arch.fault_dar, err);
1690 r = RESUME_GUEST;
1691 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001692 break;
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001693 }
1694 case BOOK3S_INTERRUPT_H_INST_STORAGE: {
1695 unsigned long vsid;
1696 long err;
1697
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00001698 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
Paul Mackerras32eb1502018-10-08 16:30:56 +11001699 vcpu->arch.fault_dsisr = vcpu->arch.shregs.msr &
1700 DSISR_SRR1_MATCH_64S;
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001701 if (kvm_is_radix(vcpu->kvm) || !cpu_has_feature(CPU_FTR_ARCH_300)) {
1702 /*
1703 * Radix doesn't require anything, and pre-ISAv3.0 hash
1704 * already attempted to handle this in rmhandlers. The
1705 * hash fault handling below is v3 only (it uses ASDR
1706 * via fault_gpa).
1707 */
1708 if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
1709 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1710 r = RESUME_PAGE_FAULT;
1711 break;
1712 }
1713
1714 if (!(vcpu->arch.fault_dsisr & SRR1_ISI_NOPT)) {
1715 kvmppc_core_queue_inst_storage(vcpu,
1716 vcpu->arch.fault_dsisr);
1717 r = RESUME_GUEST;
1718 break;
1719 }
1720
1721 if (!(vcpu->arch.shregs.msr & MSR_IR))
1722 vsid = vcpu->kvm->arch.vrma_slb_v;
1723 else
1724 vsid = vcpu->arch.fault_gpa;
1725
1726 err = kvmppc_hpte_hv_fault(vcpu, vcpu->arch.fault_dar,
1727 vsid, vcpu->arch.fault_dsisr, false);
1728 if (err == 0) {
1729 r = RESUME_GUEST;
1730 } else if (err == -1) {
1731 r = RESUME_PAGE_FAULT;
1732 } else {
1733 kvmppc_core_queue_inst_storage(vcpu, err);
1734 r = RESUME_GUEST;
1735 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001736 break;
Nicholas Piggin6165d5d2021-05-28 19:07:48 +10001737 }
1738
Paul Mackerrasde56a942011-06-29 00:21:34 +00001739 /*
1740 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301741 * If the guest debug is disabled, generate a program interrupt
1742 * to the guest. If guest debug is enabled, we need to check
1743 * whether the instruction is a software breakpoint instruction.
1744 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +00001745 */
1746 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +11001747 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
1748 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
1749 swab32(vcpu->arch.emul_inst) :
1750 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301751 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001752 r = kvmppc_emulate_debug_inst(vcpu);
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301753 } else {
1754 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1755 r = RESUME_GUEST;
1756 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001757 break;
Nicholas Piggind82b3922021-08-12 02:00:38 +10001758
1759#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1760 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1761 /*
1762 * This occurs for various TM-related instructions that
1763 * we need to emulate on POWER9 DD2.2. We have already
1764 * handled the cases where the guest was in real-suspend
1765 * mode and was transitioning to transactional state.
1766 */
1767 r = kvmhv_p9_tm_emulation(vcpu);
1768 if (r != -1)
1769 break;
1770 fallthrough; /* go to facility unavailable handler */
1771#endif
1772
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001773 /*
1774 * This occurs if the guest (kernel or userspace), does something that
Paul Mackerras57900692017-05-16 16:41:20 +10001775 * is prohibited by HFSCR.
1776 * On POWER9, this could be a doorbell instruction that we need
1777 * to emulate.
1778 * Otherwise, we just generate a program interrupt to the guest.
Michael Ellermanbd3048b2014-01-08 21:25:23 +11001779 */
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001780 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
1781 u64 cause = vcpu->arch.hfscr >> 56;
1782
Paul Mackerras57900692017-05-16 16:41:20 +10001783 r = EMULATE_FAIL;
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001784 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
1785 if (cause == FSCR_MSGP_LG)
1786 r = kvmppc_emulate_doorbell_instr(vcpu);
1787 if (cause == FSCR_PM_LG)
1788 r = kvmppc_pmu_unavailable(vcpu);
Nicholas Piggina3e18ca2021-11-23 19:52:14 +10001789 if (cause == FSCR_EBB_LG)
1790 r = kvmppc_ebb_unavailable(vcpu);
Nicholas Piggin022ecb92021-11-23 19:52:15 +10001791 if (cause == FSCR_TM_LG)
1792 r = kvmppc_tm_unavailable(vcpu);
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001793 }
Paul Mackerras57900692017-05-16 16:41:20 +10001794 if (r == EMULATE_FAIL) {
1795 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
1796 r = RESUME_GUEST;
1797 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00001798 break;
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10001799 }
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11001800
Suresh Warrierf7af5202016-08-19 15:35:52 +10001801 case BOOK3S_INTERRUPT_HV_RM_HARD:
1802 r = RESUME_PASSTHROUGH;
1803 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001804 default:
1805 kvmppc_dump_regs(vcpu);
1806 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1807 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1808 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +10001809 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001810 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001811 break;
1812 }
1813
Paul Mackerrasde56a942011-06-29 00:21:34 +00001814 return r;
1815}
1816
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001817static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu)
Paul Mackerras360cae32018-10-08 16:31:04 +11001818{
1819 int r;
1820 int srcu_idx;
1821
1822 vcpu->stat.sum_exits++;
1823
1824 /*
1825 * This can happen if an interrupt occurs in the last stages
1826 * of guest entry or the first stages of guest exit (i.e. after
1827 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
1828 * and before setting it to KVM_GUEST_MODE_HOST_HV).
1829 * That can happen due to a bug, or due to a machine check
1830 * occurring at just the wrong time.
1831 */
1832 if (vcpu->arch.shregs.msr & MSR_HV) {
1833 pr_emerg("KVM trap in HV mode while nested!\n");
1834 pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n",
1835 vcpu->arch.trap, kvmppc_get_pc(vcpu),
1836 vcpu->arch.shregs.msr);
1837 kvmppc_dump_regs(vcpu);
1838 return RESUME_HOST;
1839 }
1840 switch (vcpu->arch.trap) {
1841 /* We're good on these - the host merely wanted to get our attention */
1842 case BOOK3S_INTERRUPT_HV_DECREMENTER:
1843 vcpu->stat.dec_exits++;
1844 r = RESUME_GUEST;
1845 break;
1846 case BOOK3S_INTERRUPT_EXTERNAL:
1847 vcpu->stat.ext_intr_exits++;
1848 r = RESUME_HOST;
1849 break;
1850 case BOOK3S_INTERRUPT_H_DOORBELL:
1851 case BOOK3S_INTERRUPT_H_VIRT:
1852 vcpu->stat.ext_intr_exits++;
1853 r = RESUME_GUEST;
1854 break;
Nicholas Piggin46dea772021-11-23 19:52:22 +10001855 /* These need to go to the nested HV */
1856 case BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER:
1857 vcpu->arch.trap = BOOK3S_INTERRUPT_HV_DECREMENTER;
1858 vcpu->stat.dec_exits++;
1859 r = RESUME_HOST;
1860 break;
Paul Mackerras360cae32018-10-08 16:31:04 +11001861 /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/
1862 case BOOK3S_INTERRUPT_HMI:
1863 case BOOK3S_INTERRUPT_PERFMON:
1864 case BOOK3S_INTERRUPT_SYSTEM_RESET:
1865 r = RESUME_GUEST;
1866 break;
1867 case BOOK3S_INTERRUPT_MACHINE_CHECK:
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10001868 {
1869 static DEFINE_RATELIMIT_STATE(rs, DEFAULT_RATELIMIT_INTERVAL,
1870 DEFAULT_RATELIMIT_BURST);
Paul Mackerras360cae32018-10-08 16:31:04 +11001871 /* Pass the machine check to the L1 guest */
1872 r = RESUME_HOST;
1873 /* Print the MCE event to host console. */
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10001874 if (__ratelimit(&rs))
1875 machine_check_print_event_info(&vcpu->arch.mce_evt, false, true);
Paul Mackerras360cae32018-10-08 16:31:04 +11001876 break;
Nicholas Piggin1d15ffd2020-11-28 17:07:24 +10001877 }
Paul Mackerras360cae32018-10-08 16:31:04 +11001878 /*
1879 * We get these next two if the guest accesses a page which it thinks
1880 * it has mapped but which is not actually present, either because
1881 * it is for an emulated I/O device or because the corresonding
1882 * host page has been paged out.
1883 */
1884 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
1885 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001886 r = kvmhv_nested_page_fault(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11001887 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1888 break;
1889 case BOOK3S_INTERRUPT_H_INST_STORAGE:
1890 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
1891 vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) &
1892 DSISR_SRR1_MATCH_64S;
1893 if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE)
1894 vcpu->arch.fault_dsisr |= DSISR_ISSTORE;
1895 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08001896 r = kvmhv_nested_page_fault(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11001897 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
1898 break;
1899
1900#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1901 case BOOK3S_INTERRUPT_HV_SOFTPATCH:
1902 /*
1903 * This occurs for various TM-related instructions that
1904 * we need to emulate on POWER9 DD2.2. We have already
1905 * handled the cases where the guest was in real-suspend
1906 * mode and was transitioning to transactional state.
1907 */
1908 r = kvmhv_p9_tm_emulation(vcpu);
Nicholas Piggind82b3922021-08-12 02:00:38 +10001909 if (r != -1)
1910 break;
1911 fallthrough; /* go to facility unavailable handler */
Paul Mackerras360cae32018-10-08 16:31:04 +11001912#endif
1913
Fabiano Rosas7c3ded52021-08-12 02:00:41 +10001914 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: {
1915 u64 cause = vcpu->arch.hfscr >> 56;
1916
1917 /*
1918 * Only pass HFU interrupts to the L1 if the facility is
1919 * permitted but disabled by the L1's HFSCR, otherwise
1920 * the interrupt does not make sense to the L1 so turn
1921 * it into a HEAI.
1922 */
1923 if (!(vcpu->arch.hfscr_permitted & (1UL << cause)) ||
Nicholas Piggin22f7ff02022-01-22 20:55:30 +10001924 (vcpu->arch.nested_hfscr & (1UL << cause))) {
Fabiano Rosas7c3ded52021-08-12 02:00:41 +10001925 vcpu->arch.trap = BOOK3S_INTERRUPT_H_EMUL_ASSIST;
1926
1927 /*
1928 * If the fetch failed, return to guest and
1929 * try executing it again.
1930 */
1931 r = kvmppc_get_last_inst(vcpu, INST_GENERIC,
1932 &vcpu->arch.emul_inst);
1933 if (r != EMULATE_DONE)
1934 r = RESUME_GUEST;
1935 else
1936 r = RESUME_HOST;
1937 } else {
1938 r = RESUME_HOST;
1939 }
1940
Nicholas Piggind82b3922021-08-12 02:00:38 +10001941 break;
Fabiano Rosas7c3ded52021-08-12 02:00:41 +10001942 }
Nicholas Piggind82b3922021-08-12 02:00:38 +10001943
Paul Mackerras360cae32018-10-08 16:31:04 +11001944 case BOOK3S_INTERRUPT_HV_RM_HARD:
1945 vcpu->arch.trap = 0;
1946 r = RESUME_GUEST;
Paul Mackerras03f95332019-02-04 22:07:20 +11001947 if (!xics_on_xive())
Paul Mackerras360cae32018-10-08 16:31:04 +11001948 kvmppc_xics_rm_complete(vcpu, 0);
1949 break;
Bharata B Rao53324b52021-06-21 14:20:01 +05301950 case BOOK3S_INTERRUPT_SYSCALL:
1951 {
1952 unsigned long req = kvmppc_get_gpr(vcpu, 3);
1953
1954 /*
1955 * The H_RPT_INVALIDATE hcalls issued by nested
1956 * guests for process-scoped invalidations when
1957 * GTSE=0, are handled here in L0.
1958 */
1959 if (req == H_RPT_INVALIDATE) {
1960 r = kvmppc_nested_h_rpt_invalidate(vcpu);
1961 break;
1962 }
1963
1964 r = RESUME_HOST;
1965 break;
1966 }
Paul Mackerras360cae32018-10-08 16:31:04 +11001967 default:
1968 r = RESUME_HOST;
1969 break;
1970 }
1971
1972 return r;
1973}
1974
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301975static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1976 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001977{
1978 int i;
1979
Paul Mackerrasde56a942011-06-29 00:21:34 +00001980 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301981 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001982 for (i = 0; i < vcpu->arch.slb_max; i++) {
1983 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1984 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1985 }
1986
1987 return 0;
1988}
1989
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301990static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1991 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001992{
1993 int i, j;
1994
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001995 /* Only accept the same PVR as the host's, since we can't spoof it */
1996 if (sregs->pvr != vcpu->arch.pvr)
1997 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001998
1999 j = 0;
2000 for (i = 0; i < vcpu->arch.slb_nr; i++) {
2001 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
2002 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
2003 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
2004 ++j;
2005 }
2006 }
2007 vcpu->arch.slb_max = j;
2008
2009 return 0;
2010}
2011
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002012/*
2013 * Enforce limits on guest LPCR values based on hardware availability,
2014 * guest configuration, and possibly hypervisor support and security
2015 * concerns.
2016 */
2017unsigned long kvmppc_filter_lpcr_hv(struct kvm *kvm, unsigned long lpcr)
2018{
Nicholas Piggin72c15282021-04-12 11:48:38 +10002019 /* LPCR_TC only applies to HPT guests */
2020 if (kvm_is_radix(kvm))
2021 lpcr &= ~LPCR_TC;
2022
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002023 /* On POWER8 and above, userspace can modify AIL */
2024 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2025 lpcr &= ~LPCR_AIL;
Nicholas Pigginbcc92a02021-04-12 11:48:37 +10002026 if ((lpcr & LPCR_AIL) != LPCR_AIL_3)
2027 lpcr &= ~LPCR_AIL; /* LPCR[AIL]=1/2 is disallowed */
Nicholas Piggin2e1ae9cd2021-05-28 19:07:41 +10002028 /*
2029 * On some POWER9s we force AIL off for radix guests to prevent
2030 * executing in MSR[HV]=1 mode with the MMU enabled and PIDR set to
2031 * guest, which can result in Q0 translations with LPID=0 PID=PIDR to
2032 * be cached, which the host TLB management does not expect.
2033 */
2034 if (kvm_is_radix(kvm) && cpu_has_feature(CPU_FTR_P9_RADIX_PREFETCH_BUG))
2035 lpcr &= ~LPCR_AIL;
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002036
2037 /*
2038 * On POWER9, allow userspace to enable large decrementer for the
2039 * guest, whether or not the host has it enabled.
2040 */
2041 if (!cpu_has_feature(CPU_FTR_ARCH_300))
2042 lpcr &= ~LPCR_LD;
2043
2044 return lpcr;
2045}
2046
2047static void verify_lpcr(struct kvm *kvm, unsigned long lpcr)
2048{
2049 if (lpcr != kvmppc_filter_lpcr_hv(kvm, lpcr)) {
2050 WARN_ONCE(1, "lpcr 0x%lx differs from filtered 0x%lx\n",
2051 lpcr, kvmppc_filter_lpcr_hv(kvm, lpcr));
2052 }
2053}
2054
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002055static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
2056 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002057{
Paul Mackerras8f902b02015-03-20 20:39:38 +11002058 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002059 struct kvmppc_vcore *vc = vcpu->arch.vcore;
2060 u64 mask;
2061
2062 spin_lock(&vc->lock);
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002063
2064 /*
2065 * Userspace can only modify
2066 * DPFD (default prefetch depth), ILE (interrupt little-endian),
2067 * TC (translation control), AIL (alternate interrupt location),
2068 * LD (large decrementer).
2069 * These are subject to restrictions from kvmppc_filter_lcpr_hv().
2070 */
2071 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC | LPCR_AIL | LPCR_LD;
2072
2073 /* Broken 32-bit version of LPCR must not clear top bits */
2074 if (preserve_top32)
2075 mask &= 0xFFFFFFFF;
2076
2077 new_lpcr = kvmppc_filter_lpcr_hv(kvm,
2078 (vc->lpcr & ~mask) | (new_lpcr & mask));
2079
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002080 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11002081 * If ILE (interrupt little-endian) has changed, update the
2082 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
2083 */
2084 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11002085 struct kvm_vcpu *vcpu;
Marc Zyngier46808a42021-11-16 16:04:02 +00002086 unsigned long i;
Anton Blanchardd6829162014-01-08 21:25:30 +11002087
Anton Blanchardd6829162014-01-08 21:25:30 +11002088 kvm_for_each_vcpu(i, vcpu, kvm) {
2089 if (vcpu->arch.vcore != vc)
2090 continue;
2091 if (new_lpcr & LPCR_ILE)
2092 vcpu->arch.intr_msr |= MSR_LE;
2093 else
2094 vcpu->arch.intr_msr &= ~MSR_LE;
2095 }
Anton Blanchardd6829162014-01-08 21:25:30 +11002096 }
2097
Nicholas Piggin67145ef2021-04-12 11:48:36 +10002098 vc->lpcr = new_lpcr;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002099
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002100 spin_unlock(&vc->lock);
2101}
2102
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302103static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2104 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00002105{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002106 int r = 0;
2107 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00002108
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002109 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05302110 case KVM_REG_PPC_DEBUG_INST:
2111 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
2112 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00002113 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002114 *val = get_reg_val(id, 0);
2115 break;
2116 case KVM_REG_PPC_DABR:
2117 *val = get_reg_val(id, vcpu->arch.dabr);
2118 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11002119 case KVM_REG_PPC_DABRX:
2120 *val = get_reg_val(id, vcpu->arch.dabrx);
2121 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002122 case KVM_REG_PPC_DSCR:
2123 *val = get_reg_val(id, vcpu->arch.dscr);
2124 break;
2125 case KVM_REG_PPC_PURR:
2126 *val = get_reg_val(id, vcpu->arch.purr);
2127 break;
2128 case KVM_REG_PPC_SPURR:
2129 *val = get_reg_val(id, vcpu->arch.spurr);
2130 break;
2131 case KVM_REG_PPC_AMR:
2132 *val = get_reg_val(id, vcpu->arch.amr);
2133 break;
2134 case KVM_REG_PPC_UAMOR:
2135 *val = get_reg_val(id, vcpu->arch.uamor);
2136 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002137 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002138 i = id - KVM_REG_PPC_MMCR0;
2139 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
2140 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002141 case KVM_REG_PPC_MMCR2:
2142 *val = get_reg_val(id, vcpu->arch.mmcr[2]);
2143 break;
2144 case KVM_REG_PPC_MMCRA:
2145 *val = get_reg_val(id, vcpu->arch.mmcra);
2146 break;
2147 case KVM_REG_PPC_MMCRS:
2148 *val = get_reg_val(id, vcpu->arch.mmcrs);
2149 break;
Athira Rajeev5752fe02020-07-17 10:38:17 -04002150 case KVM_REG_PPC_MMCR3:
2151 *val = get_reg_val(id, vcpu->arch.mmcr[3]);
2152 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002153 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2154 i = id - KVM_REG_PPC_PMC1;
2155 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00002156 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002157 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2158 i = id - KVM_REG_PPC_SPMC1;
2159 *val = get_reg_val(id, vcpu->arch.spmc[i]);
2160 break;
Paul Mackerras14941782013-09-06 13:11:18 +10002161 case KVM_REG_PPC_SIAR:
2162 *val = get_reg_val(id, vcpu->arch.siar);
2163 break;
2164 case KVM_REG_PPC_SDAR:
2165 *val = get_reg_val(id, vcpu->arch.sdar);
2166 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002167 case KVM_REG_PPC_SIER:
Athira Rajeev5752fe02020-07-17 10:38:17 -04002168 *val = get_reg_val(id, vcpu->arch.sier[0]);
2169 break;
2170 case KVM_REG_PPC_SIER2:
2171 *val = get_reg_val(id, vcpu->arch.sier[1]);
2172 break;
2173 case KVM_REG_PPC_SIER3:
2174 *val = get_reg_val(id, vcpu->arch.sier[2]);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002175 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002176 case KVM_REG_PPC_IAMR:
2177 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002178 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002179 case KVM_REG_PPC_PSPB:
2180 *val = get_reg_val(id, vcpu->arch.pspb);
2181 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002182 case KVM_REG_PPC_DPDES:
Paul Mackerrasff42df42019-08-27 11:35:40 +10002183 /*
2184 * On POWER9, where we are emulating msgsndp etc.,
2185 * we return 1 bit for each vcpu, which can come from
2186 * either vcore->dpdes or doorbell_request.
2187 * On POWER8, doorbell_request is 0.
2188 */
Nicholas Piggin63983262021-11-23 19:52:30 +10002189 if (cpu_has_feature(CPU_FTR_ARCH_300))
2190 *val = get_reg_val(id, vcpu->arch.doorbell_request);
2191 else
2192 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
Michael Neulingb005255e2014-01-08 21:25:21 +11002193 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10002194 case KVM_REG_PPC_VTB:
2195 *val = get_reg_val(id, vcpu->arch.vcore->vtb);
2196 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002197 case KVM_REG_PPC_DAWR:
Ravi Bangoria122954ed72020-12-16 16:12:17 +05302198 *val = get_reg_val(id, vcpu->arch.dawr0);
Michael Neulingb005255e2014-01-08 21:25:21 +11002199 break;
2200 case KVM_REG_PPC_DAWRX:
Ravi Bangoria122954ed72020-12-16 16:12:17 +05302201 *val = get_reg_val(id, vcpu->arch.dawrx0);
Michael Neulingb005255e2014-01-08 21:25:21 +11002202 break;
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302203 case KVM_REG_PPC_DAWR1:
2204 *val = get_reg_val(id, vcpu->arch.dawr1);
2205 break;
2206 case KVM_REG_PPC_DAWRX1:
2207 *val = get_reg_val(id, vcpu->arch.dawrx1);
2208 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002209 case KVM_REG_PPC_CIABR:
2210 *val = get_reg_val(id, vcpu->arch.ciabr);
2211 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002212 case KVM_REG_PPC_CSIGR:
2213 *val = get_reg_val(id, vcpu->arch.csigr);
2214 break;
2215 case KVM_REG_PPC_TACR:
2216 *val = get_reg_val(id, vcpu->arch.tacr);
2217 break;
2218 case KVM_REG_PPC_TCSCR:
2219 *val = get_reg_val(id, vcpu->arch.tcscr);
2220 break;
2221 case KVM_REG_PPC_PID:
2222 *val = get_reg_val(id, vcpu->arch.pid);
2223 break;
2224 case KVM_REG_PPC_ACOP:
2225 *val = get_reg_val(id, vcpu->arch.acop);
2226 break;
2227 case KVM_REG_PPC_WORT:
2228 *val = get_reg_val(id, vcpu->arch.wort);
2229 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11002230 case KVM_REG_PPC_TIDR:
2231 *val = get_reg_val(id, vcpu->arch.tid);
2232 break;
2233 case KVM_REG_PPC_PSSCR:
2234 *val = get_reg_val(id, vcpu->arch.psscr);
2235 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00002236 case KVM_REG_PPC_VPA_ADDR:
2237 spin_lock(&vcpu->arch.vpa_update_lock);
2238 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
2239 spin_unlock(&vcpu->arch.vpa_update_lock);
2240 break;
2241 case KVM_REG_PPC_VPA_SLB:
2242 spin_lock(&vcpu->arch.vpa_update_lock);
2243 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
2244 val->vpaval.length = vcpu->arch.slb_shadow.len;
2245 spin_unlock(&vcpu->arch.vpa_update_lock);
2246 break;
2247 case KVM_REG_PPC_VPA_DTL:
2248 spin_lock(&vcpu->arch.vpa_update_lock);
2249 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
2250 val->vpaval.length = vcpu->arch.dtl.len;
2251 spin_unlock(&vcpu->arch.vpa_update_lock);
2252 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10002253 case KVM_REG_PPC_TB_OFFSET:
2254 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
2255 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002256 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002257 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002258 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
2259 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10002260 case KVM_REG_PPC_PPR:
2261 *val = get_reg_val(id, vcpu->arch.ppr);
2262 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002263#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2264 case KVM_REG_PPC_TFHAR:
2265 *val = get_reg_val(id, vcpu->arch.tfhar);
2266 break;
2267 case KVM_REG_PPC_TFIAR:
2268 *val = get_reg_val(id, vcpu->arch.tfiar);
2269 break;
2270 case KVM_REG_PPC_TEXASR:
2271 *val = get_reg_val(id, vcpu->arch.texasr);
2272 break;
2273 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2274 i = id - KVM_REG_PPC_TM_GPR0;
2275 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
2276 break;
2277 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2278 {
2279 int j;
2280 i = id - KVM_REG_PPC_TM_VSR0;
2281 if (i < 32)
2282 for (j = 0; j < TS_FPRWIDTH; j++)
2283 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
2284 else {
2285 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2286 val->vval = vcpu->arch.vr_tm.vr[i-32];
2287 else
2288 r = -ENXIO;
2289 }
2290 break;
2291 }
2292 case KVM_REG_PPC_TM_CR:
2293 *val = get_reg_val(id, vcpu->arch.cr_tm);
2294 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11002295 case KVM_REG_PPC_TM_XER:
2296 *val = get_reg_val(id, vcpu->arch.xer_tm);
2297 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002298 case KVM_REG_PPC_TM_LR:
2299 *val = get_reg_val(id, vcpu->arch.lr_tm);
2300 break;
2301 case KVM_REG_PPC_TM_CTR:
2302 *val = get_reg_val(id, vcpu->arch.ctr_tm);
2303 break;
2304 case KVM_REG_PPC_TM_FPSCR:
2305 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
2306 break;
2307 case KVM_REG_PPC_TM_AMR:
2308 *val = get_reg_val(id, vcpu->arch.amr_tm);
2309 break;
2310 case KVM_REG_PPC_TM_PPR:
2311 *val = get_reg_val(id, vcpu->arch.ppr_tm);
2312 break;
2313 case KVM_REG_PPC_TM_VRSAVE:
2314 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
2315 break;
2316 case KVM_REG_PPC_TM_VSCR:
2317 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2318 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
2319 else
2320 r = -ENXIO;
2321 break;
2322 case KVM_REG_PPC_TM_DSCR:
2323 *val = get_reg_val(id, vcpu->arch.dscr_tm);
2324 break;
2325 case KVM_REG_PPC_TM_TAR:
2326 *val = get_reg_val(id, vcpu->arch.tar_tm);
2327 break;
2328#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10002329 case KVM_REG_PPC_ARCH_COMPAT:
2330 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
2331 break;
Paul Mackerras58555642018-01-12 20:55:20 +11002332 case KVM_REG_PPC_DEC_EXPIRY:
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10002333 *val = get_reg_val(id, vcpu->arch.dec_expires);
Paul Mackerras58555642018-01-12 20:55:20 +11002334 break;
Paul Mackerrasa1f15822018-04-20 15:33:21 +10002335 case KVM_REG_PPC_ONLINE:
2336 *val = get_reg_val(id, vcpu->arch.online);
2337 break;
Paul Mackerras30323412018-10-08 16:31:13 +11002338 case KVM_REG_PPC_PTCR:
2339 *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr);
2340 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00002341 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002342 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00002343 break;
2344 }
2345
2346 return r;
2347}
2348
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302349static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
2350 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00002351{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002352 int r = 0;
2353 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00002354 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00002355
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002356 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00002357 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00002358 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002359 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00002360 r = -EINVAL;
2361 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002362 case KVM_REG_PPC_DABR:
2363 vcpu->arch.dabr = set_reg_val(id, *val);
2364 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11002365 case KVM_REG_PPC_DABRX:
2366 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
2367 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002368 case KVM_REG_PPC_DSCR:
2369 vcpu->arch.dscr = set_reg_val(id, *val);
2370 break;
2371 case KVM_REG_PPC_PURR:
2372 vcpu->arch.purr = set_reg_val(id, *val);
2373 break;
2374 case KVM_REG_PPC_SPURR:
2375 vcpu->arch.spurr = set_reg_val(id, *val);
2376 break;
2377 case KVM_REG_PPC_AMR:
2378 vcpu->arch.amr = set_reg_val(id, *val);
2379 break;
2380 case KVM_REG_PPC_UAMOR:
2381 vcpu->arch.uamor = set_reg_val(id, *val);
2382 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002383 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002384 i = id - KVM_REG_PPC_MMCR0;
2385 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
2386 break;
Athira Rajeev7e4a1452020-07-17 10:38:14 -04002387 case KVM_REG_PPC_MMCR2:
2388 vcpu->arch.mmcr[2] = set_reg_val(id, *val);
2389 break;
2390 case KVM_REG_PPC_MMCRA:
2391 vcpu->arch.mmcra = set_reg_val(id, *val);
2392 break;
2393 case KVM_REG_PPC_MMCRS:
2394 vcpu->arch.mmcrs = set_reg_val(id, *val);
2395 break;
Athira Rajeev5752fe02020-07-17 10:38:17 -04002396 case KVM_REG_PPC_MMCR3:
2397 *val = get_reg_val(id, vcpu->arch.mmcr[3]);
2398 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002399 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
2400 i = id - KVM_REG_PPC_PMC1;
2401 vcpu->arch.pmc[i] = set_reg_val(id, *val);
2402 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002403 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
2404 i = id - KVM_REG_PPC_SPMC1;
2405 vcpu->arch.spmc[i] = set_reg_val(id, *val);
2406 break;
Paul Mackerras14941782013-09-06 13:11:18 +10002407 case KVM_REG_PPC_SIAR:
2408 vcpu->arch.siar = set_reg_val(id, *val);
2409 break;
2410 case KVM_REG_PPC_SDAR:
2411 vcpu->arch.sdar = set_reg_val(id, *val);
2412 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002413 case KVM_REG_PPC_SIER:
Athira Rajeev5752fe02020-07-17 10:38:17 -04002414 vcpu->arch.sier[0] = set_reg_val(id, *val);
2415 break;
2416 case KVM_REG_PPC_SIER2:
2417 vcpu->arch.sier[1] = set_reg_val(id, *val);
2418 break;
2419 case KVM_REG_PPC_SIER3:
2420 vcpu->arch.sier[2] = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002421 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002422 case KVM_REG_PPC_IAMR:
2423 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00002424 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002425 case KVM_REG_PPC_PSPB:
2426 vcpu->arch.pspb = set_reg_val(id, *val);
2427 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002428 case KVM_REG_PPC_DPDES:
Nicholas Piggin63983262021-11-23 19:52:30 +10002429 if (cpu_has_feature(CPU_FTR_ARCH_300))
2430 vcpu->arch.doorbell_request = set_reg_val(id, *val) & 1;
2431 else
2432 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
Michael Neulingb005255e2014-01-08 21:25:21 +11002433 break;
Paul Mackerras88b02cf92016-09-15 13:42:52 +10002434 case KVM_REG_PPC_VTB:
2435 vcpu->arch.vcore->vtb = set_reg_val(id, *val);
2436 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002437 case KVM_REG_PPC_DAWR:
Ravi Bangoria122954ed72020-12-16 16:12:17 +05302438 vcpu->arch.dawr0 = set_reg_val(id, *val);
Michael Neulingb005255e2014-01-08 21:25:21 +11002439 break;
2440 case KVM_REG_PPC_DAWRX:
Ravi Bangoria122954ed72020-12-16 16:12:17 +05302441 vcpu->arch.dawrx0 = set_reg_val(id, *val) & ~DAWRX_HYP;
Michael Neulingb005255e2014-01-08 21:25:21 +11002442 break;
Ravi Bangoriabd1de1a2020-12-16 16:12:18 +05302443 case KVM_REG_PPC_DAWR1:
2444 vcpu->arch.dawr1 = set_reg_val(id, *val);
2445 break;
2446 case KVM_REG_PPC_DAWRX1:
2447 vcpu->arch.dawrx1 = set_reg_val(id, *val) & ~DAWRX_HYP;
2448 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002449 case KVM_REG_PPC_CIABR:
2450 vcpu->arch.ciabr = set_reg_val(id, *val);
2451 /* Don't allow setting breakpoints in hypervisor code */
2452 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
2453 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
2454 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11002455 case KVM_REG_PPC_CSIGR:
2456 vcpu->arch.csigr = set_reg_val(id, *val);
2457 break;
2458 case KVM_REG_PPC_TACR:
2459 vcpu->arch.tacr = set_reg_val(id, *val);
2460 break;
2461 case KVM_REG_PPC_TCSCR:
2462 vcpu->arch.tcscr = set_reg_val(id, *val);
2463 break;
2464 case KVM_REG_PPC_PID:
2465 vcpu->arch.pid = set_reg_val(id, *val);
2466 break;
2467 case KVM_REG_PPC_ACOP:
2468 vcpu->arch.acop = set_reg_val(id, *val);
2469 break;
2470 case KVM_REG_PPC_WORT:
2471 vcpu->arch.wort = set_reg_val(id, *val);
2472 break;
Paul Mackerrase9cf1e02016-11-18 13:11:42 +11002473 case KVM_REG_PPC_TIDR:
2474 vcpu->arch.tid = set_reg_val(id, *val);
2475 break;
2476 case KVM_REG_PPC_PSSCR:
2477 vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS;
2478 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00002479 case KVM_REG_PPC_VPA_ADDR:
2480 addr = set_reg_val(id, *val);
2481 r = -EINVAL;
2482 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
2483 vcpu->arch.dtl.next_gpa))
2484 break;
2485 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
2486 break;
2487 case KVM_REG_PPC_VPA_SLB:
2488 addr = val->vpaval.addr;
2489 len = val->vpaval.length;
2490 r = -EINVAL;
2491 if (addr && !vcpu->arch.vpa.next_gpa)
2492 break;
2493 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
2494 break;
2495 case KVM_REG_PPC_VPA_DTL:
2496 addr = val->vpaval.addr;
2497 len = val->vpaval.length;
2498 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00002499 if (addr && (len < sizeof(struct dtl_entry) ||
2500 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00002501 break;
2502 len -= len % sizeof(struct dtl_entry);
2503 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
2504 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10002505 case KVM_REG_PPC_TB_OFFSET:
2506 /* round up to multiple of 2^24 */
2507 vcpu->arch.vcore->tb_offset =
2508 ALIGN(set_reg_val(id, *val), 1UL << 24);
2509 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002510 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10002511 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
2512 break;
2513 case KVM_REG_PPC_LPCR_64:
2514 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002515 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10002516 case KVM_REG_PPC_PPR:
2517 vcpu->arch.ppr = set_reg_val(id, *val);
2518 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002519#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
2520 case KVM_REG_PPC_TFHAR:
2521 vcpu->arch.tfhar = set_reg_val(id, *val);
2522 break;
2523 case KVM_REG_PPC_TFIAR:
2524 vcpu->arch.tfiar = set_reg_val(id, *val);
2525 break;
2526 case KVM_REG_PPC_TEXASR:
2527 vcpu->arch.texasr = set_reg_val(id, *val);
2528 break;
2529 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
2530 i = id - KVM_REG_PPC_TM_GPR0;
2531 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
2532 break;
2533 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
2534 {
2535 int j;
2536 i = id - KVM_REG_PPC_TM_VSR0;
2537 if (i < 32)
2538 for (j = 0; j < TS_FPRWIDTH; j++)
2539 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
2540 else
2541 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2542 vcpu->arch.vr_tm.vr[i-32] = val->vval;
2543 else
2544 r = -ENXIO;
2545 break;
2546 }
2547 case KVM_REG_PPC_TM_CR:
2548 vcpu->arch.cr_tm = set_reg_val(id, *val);
2549 break;
Paul Mackerras0d808df2016-11-07 15:09:58 +11002550 case KVM_REG_PPC_TM_XER:
2551 vcpu->arch.xer_tm = set_reg_val(id, *val);
2552 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11002553 case KVM_REG_PPC_TM_LR:
2554 vcpu->arch.lr_tm = set_reg_val(id, *val);
2555 break;
2556 case KVM_REG_PPC_TM_CTR:
2557 vcpu->arch.ctr_tm = set_reg_val(id, *val);
2558 break;
2559 case KVM_REG_PPC_TM_FPSCR:
2560 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
2561 break;
2562 case KVM_REG_PPC_TM_AMR:
2563 vcpu->arch.amr_tm = set_reg_val(id, *val);
2564 break;
2565 case KVM_REG_PPC_TM_PPR:
2566 vcpu->arch.ppr_tm = set_reg_val(id, *val);
2567 break;
2568 case KVM_REG_PPC_TM_VRSAVE:
2569 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
2570 break;
2571 case KVM_REG_PPC_TM_VSCR:
2572 if (cpu_has_feature(CPU_FTR_ALTIVEC))
2573 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
2574 else
2575 r = - ENXIO;
2576 break;
2577 case KVM_REG_PPC_TM_DSCR:
2578 vcpu->arch.dscr_tm = set_reg_val(id, *val);
2579 break;
2580 case KVM_REG_PPC_TM_TAR:
2581 vcpu->arch.tar_tm = set_reg_val(id, *val);
2582 break;
2583#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10002584 case KVM_REG_PPC_ARCH_COMPAT:
2585 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
2586 break;
Paul Mackerras58555642018-01-12 20:55:20 +11002587 case KVM_REG_PPC_DEC_EXPIRY:
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10002588 vcpu->arch.dec_expires = set_reg_val(id, *val);
Paul Mackerras58555642018-01-12 20:55:20 +11002589 break;
Paul Mackerrasa1f15822018-04-20 15:33:21 +10002590 case KVM_REG_PPC_ONLINE:
Paul Mackerras7aa15842018-04-20 19:53:22 +10002591 i = set_reg_val(id, *val);
2592 if (i && !vcpu->arch.online)
2593 atomic_inc(&vcpu->arch.vcore->online_count);
2594 else if (!i && vcpu->arch.online)
2595 atomic_dec(&vcpu->arch.vcore->online_count);
2596 vcpu->arch.online = i;
Paul Mackerrasa1f15822018-04-20 15:33:21 +10002597 break;
Paul Mackerras30323412018-10-08 16:31:13 +11002598 case KVM_REG_PPC_PTCR:
2599 vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val);
2600 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00002601 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00002602 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00002603 break;
2604 }
2605
2606 return r;
2607}
2608
Paul Mackerras45c940b2016-11-18 17:43:30 +11002609/*
2610 * On POWER9, threads are independent and can be in different partitions.
2611 * Therefore we consider each thread to be a subcore.
2612 * There is a restriction that all threads have to be in the same
2613 * MMU mode (radix or HPT), unfortunately, but since we only support
2614 * HPT guests on a HPT host so far, that isn't an impediment yet.
2615 */
Paul Mackerras516f7892017-10-16 16:11:57 +11002616static int threads_per_vcore(struct kvm *kvm)
Paul Mackerras45c940b2016-11-18 17:43:30 +11002617{
Nicholas Pigginaaae8c72021-05-28 19:07:42 +10002618 if (cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras45c940b2016-11-18 17:43:30 +11002619 return 1;
2620 return threads_per_subcore;
2621}
2622
Sam Bobroff1e175d22018-07-25 16:12:02 +10002623static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id)
Stewart Smithde9bdd12014-07-18 14:18:42 +10002624{
2625 struct kvmppc_vcore *vcore;
2626
2627 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
2628
2629 if (vcore == NULL)
2630 return NULL;
2631
Stewart Smithde9bdd12014-07-18 14:18:42 +10002632 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11002633 spin_lock_init(&vcore->stoltb_lock);
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07002634 rcuwait_init(&vcore->wait);
Stewart Smithde9bdd12014-07-18 14:18:42 +10002635 vcore->preempt_tb = TB_NIL;
2636 vcore->lpcr = kvm->arch.lpcr;
Sam Bobroff1e175d22018-07-25 16:12:02 +10002637 vcore->first_vcpuid = id;
Stewart Smithde9bdd12014-07-18 14:18:42 +10002638 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10002639 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10002640
2641 return vcore;
2642}
2643
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002644#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
2645static struct debugfs_timings_element {
2646 const char *name;
2647 size_t offset;
2648} timings[] = {
2649 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
2650 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
2651 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
2652 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
2653 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
2654};
2655
Thomas Meyer4bb817ed2017-09-03 14:19:31 +02002656#define N_TIMINGS (ARRAY_SIZE(timings))
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002657
2658struct debugfs_timings_state {
2659 struct kvm_vcpu *vcpu;
2660 unsigned int buflen;
2661 char buf[N_TIMINGS * 100];
2662};
2663
2664static int debugfs_timings_open(struct inode *inode, struct file *file)
2665{
2666 struct kvm_vcpu *vcpu = inode->i_private;
2667 struct debugfs_timings_state *p;
2668
2669 p = kzalloc(sizeof(*p), GFP_KERNEL);
2670 if (!p)
2671 return -ENOMEM;
2672
2673 kvm_get_kvm(vcpu->kvm);
2674 p->vcpu = vcpu;
2675 file->private_data = p;
2676
2677 return nonseekable_open(inode, file);
2678}
2679
2680static int debugfs_timings_release(struct inode *inode, struct file *file)
2681{
2682 struct debugfs_timings_state *p = file->private_data;
2683
2684 kvm_put_kvm(p->vcpu->kvm);
2685 kfree(p);
2686 return 0;
2687}
2688
2689static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
2690 size_t len, loff_t *ppos)
2691{
2692 struct debugfs_timings_state *p = file->private_data;
2693 struct kvm_vcpu *vcpu = p->vcpu;
2694 char *s, *buf_end;
2695 struct kvmhv_tb_accumulator tb;
2696 u64 count;
2697 loff_t pos;
2698 ssize_t n;
2699 int i, loops;
2700 bool ok;
2701
2702 if (!p->buflen) {
2703 s = p->buf;
2704 buf_end = s + sizeof(p->buf);
2705 for (i = 0; i < N_TIMINGS; ++i) {
2706 struct kvmhv_tb_accumulator *acc;
2707
2708 acc = (struct kvmhv_tb_accumulator *)
2709 ((unsigned long)vcpu + timings[i].offset);
2710 ok = false;
2711 for (loops = 0; loops < 1000; ++loops) {
2712 count = acc->seqcount;
2713 if (!(count & 1)) {
2714 smp_rmb();
2715 tb = *acc;
2716 smp_rmb();
2717 if (count == acc->seqcount) {
2718 ok = true;
2719 break;
2720 }
2721 }
2722 udelay(1);
2723 }
2724 if (!ok)
2725 snprintf(s, buf_end - s, "%s: stuck\n",
2726 timings[i].name);
2727 else
2728 snprintf(s, buf_end - s,
2729 "%s: %llu %llu %llu %llu\n",
2730 timings[i].name, count / 2,
2731 tb_to_ns(tb.tb_total),
2732 tb_to_ns(tb.tb_min),
2733 tb_to_ns(tb.tb_max));
2734 s += strlen(s);
2735 }
2736 p->buflen = s - p->buf;
2737 }
2738
2739 pos = *ppos;
2740 if (pos >= p->buflen)
2741 return 0;
2742 if (len > p->buflen - pos)
2743 len = p->buflen - pos;
2744 n = copy_to_user(buf, p->buf + pos, len);
2745 if (n) {
2746 if (n == len)
2747 return -EFAULT;
2748 len -= n;
2749 }
2750 *ppos = pos + len;
2751 return len;
2752}
2753
2754static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
2755 size_t len, loff_t *ppos)
2756{
2757 return -EACCES;
2758}
2759
2760static const struct file_operations debugfs_timings_ops = {
2761 .owner = THIS_MODULE,
2762 .open = debugfs_timings_open,
2763 .release = debugfs_timings_release,
2764 .read = debugfs_timings_read,
2765 .write = debugfs_timings_write,
2766 .llseek = generic_file_llseek,
2767};
2768
2769/* Create a debugfs directory for the vcpu */
2770static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
2771{
2772 char buf[16];
2773 struct kvm *kvm = vcpu->kvm;
2774
2775 snprintf(buf, sizeof(buf), "vcpu%u", id);
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002776 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
Greg Kroah-Hartmanc4fd5272020-02-09 11:58:57 +01002777 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir, vcpu,
2778 &debugfs_timings_ops);
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002779}
2780
2781#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2782static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
2783{
2784}
2785#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
2786
Sean Christophersonff030fd2019-12-18 13:55:00 -08002787static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002788{
Paul Mackerras3c313522017-02-06 13:24:41 +11002789 int err;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002790 int core;
2791 struct kvmppc_vcore *vcore;
Sean Christophersonff030fd2019-12-18 13:55:00 -08002792 struct kvm *kvm;
2793 unsigned int id;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002794
Sean Christophersonff030fd2019-12-18 13:55:00 -08002795 kvm = vcpu->kvm;
2796 id = vcpu->vcpu_id;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002797
2798 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02002799#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
2800 /*
2801 * The shared struct is never shared on HV,
2802 * so we can always use host endianness
2803 */
2804#ifdef __BIG_ENDIAN__
2805 vcpu->arch.shared_big_endian = true;
2806#else
2807 vcpu->arch.shared_big_endian = false;
2808#endif
2809#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00002810 vcpu->arch.mmcr[0] = MMCR0_FC;
Nicholas Piggin245ebf82021-11-23 19:51:49 +10002811 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
2812 vcpu->arch.mmcr[0] |= MMCR0_PMCCEXT;
2813 vcpu->arch.mmcra = MMCRA_BHRB_DISABLE;
2814 }
2815
Paul Mackerrasde56a942011-06-29 00:21:34 +00002816 vcpu->arch.ctrl = CTRL_RUNLATCH;
2817 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302818 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002819 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002820 spin_lock_init(&vcpu->arch.tbacct_lock);
2821 vcpu->arch.busy_preempt = TB_NIL;
Nicholas Pigginfd42b7b2021-08-12 02:00:35 +10002822 vcpu->arch.shregs.msr = MSR_ME;
Anton Blanchardd6829162014-01-08 21:25:30 +11002823 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002824
Paul Mackerras769377f2017-02-15 14:30:17 +11002825 /*
2826 * Set the default HFSCR for the guest from the host value.
2827 * This value is only used on POWER9.
Paul Mackerras57900692017-05-16 16:41:20 +10002828 * On POWER9, we want to virtualize the doorbell facility, so we
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11002829 * don't set the HFSCR_MSGP bit, and that causes those instructions
2830 * to trap and then we emulate them.
Paul Mackerras769377f2017-02-15 14:30:17 +11002831 */
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11002832 vcpu->arch.hfscr = HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB |
Alistair Popple4cb4ade2020-06-02 15:53:25 +10002833 HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP | HFSCR_PREFIX;
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11002834 if (cpu_has_feature(CPU_FTR_HVMODE)) {
2835 vcpu->arch.hfscr &= mfspr(SPRN_HFSCR);
Nicholas Pigginbd31ecf2021-07-16 12:43:09 +10002836#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11002837 if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST))
2838 vcpu->arch.hfscr |= HFSCR_TM;
Nicholas Pigginbd31ecf2021-07-16 12:43:09 +10002839#endif
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11002840 }
2841 if (cpu_has_feature(CPU_FTR_TM_COMP))
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11002842 vcpu->arch.hfscr |= HFSCR_TM;
Paul Mackerras769377f2017-02-15 14:30:17 +11002843
Nicholas Piggin8b210a82021-08-12 02:00:40 +10002844 vcpu->arch.hfscr_permitted = vcpu->arch.hfscr;
2845
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10002846 /*
Nicholas Piggin022ecb92021-11-23 19:52:15 +10002847 * PM, EBB, TM are demand-faulted so start with it clear.
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10002848 */
Nicholas Piggin022ecb92021-11-23 19:52:15 +10002849 vcpu->arch.hfscr &= ~(HFSCR_PM | HFSCR_EBB | HFSCR_TM);
Nicholas Piggin9d3ddb82021-11-23 19:51:53 +10002850
Paul Mackerrasde56a942011-06-29 00:21:34 +00002851 kvmppc_mmu_book3s_hv_init(vcpu);
2852
Paul Mackerras8455d792012-10-15 01:17:42 +00002853 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002854
2855 init_waitqueue_head(&vcpu->arch.cpu_run);
2856
2857 mutex_lock(&kvm->lock);
Paul Mackerras3c313522017-02-06 13:24:41 +11002858 vcore = NULL;
2859 err = -EINVAL;
Sam Bobroff1e175d22018-07-25 16:12:02 +10002860 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerrasb5c6f762018-07-26 15:38:41 +10002861 if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) {
2862 pr_devel("KVM: VCPU ID too high\n");
2863 core = KVM_MAX_VCORES;
2864 } else {
2865 BUG_ON(kvm->arch.smt_mode != 1);
2866 core = kvmppc_pack_vcpu_id(kvm, id);
2867 }
Sam Bobroff1e175d22018-07-25 16:12:02 +10002868 } else {
2869 core = id / kvm->arch.smt_mode;
2870 }
Paul Mackerras3c313522017-02-06 13:24:41 +11002871 if (core < KVM_MAX_VCORES) {
2872 vcore = kvm->arch.vcores[core];
Sam Bobroff1e175d22018-07-25 16:12:02 +10002873 if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) {
2874 pr_devel("KVM: collision on id %u", id);
2875 vcore = NULL;
2876 } else if (!vcore) {
Paul Mackerras0d4ee882019-05-23 16:35:34 +10002877 /*
2878 * Take mmu_setup_lock for mutual exclusion
2879 * with kvmppc_update_lpcr().
2880 */
Paul Mackerras3c313522017-02-06 13:24:41 +11002881 err = -ENOMEM;
Sam Bobroff1e175d22018-07-25 16:12:02 +10002882 vcore = kvmppc_vcore_create(kvm,
2883 id & ~(kvm->arch.smt_mode - 1));
Paul Mackerras0d4ee882019-05-23 16:35:34 +10002884 mutex_lock(&kvm->arch.mmu_setup_lock);
Paul Mackerras3c313522017-02-06 13:24:41 +11002885 kvm->arch.vcores[core] = vcore;
2886 kvm->arch.online_vcores++;
Paul Mackerras0d4ee882019-05-23 16:35:34 +10002887 mutex_unlock(&kvm->arch.mmu_setup_lock);
Paul Mackerras3c313522017-02-06 13:24:41 +11002888 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002889 }
2890 mutex_unlock(&kvm->lock);
2891
2892 if (!vcore)
Sean Christophersonff030fd2019-12-18 13:55:00 -08002893 return err;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002894
2895 spin_lock(&vcore->lock);
2896 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002897 spin_unlock(&vcore->lock);
2898 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002899 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10002900 vcpu->arch.thread_cpu = -1;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11002901 vcpu->arch.prev_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002902
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002903 vcpu->arch.cpu_type = KVM_CPU_3S_64;
2904 kvmppc_sanity_check(vcpu);
2905
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11002906 debugfs_vcpu_init(vcpu, id);
2907
Sean Christophersonc50bfbd2019-12-18 13:54:57 -08002908 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002909}
2910
Paul Mackerras3c313522017-02-06 13:24:41 +11002911static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode,
2912 unsigned long flags)
2913{
2914 int err;
Paul Mackerras57900692017-05-16 16:41:20 +10002915 int esmt = 0;
Paul Mackerras3c313522017-02-06 13:24:41 +11002916
2917 if (flags)
2918 return -EINVAL;
2919 if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode))
2920 return -EINVAL;
2921 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
2922 /*
2923 * On POWER8 (or POWER7), the threading mode is "strict",
2924 * so we pack smt_mode vcpus per vcore.
2925 */
2926 if (smt_mode > threads_per_subcore)
2927 return -EINVAL;
2928 } else {
2929 /*
2930 * On POWER9, the threading mode is "loose",
2931 * so each vcpu gets its own vcore.
2932 */
Paul Mackerras57900692017-05-16 16:41:20 +10002933 esmt = smt_mode;
Paul Mackerras3c313522017-02-06 13:24:41 +11002934 smt_mode = 1;
2935 }
2936 mutex_lock(&kvm->lock);
2937 err = -EBUSY;
2938 if (!kvm->arch.online_vcores) {
2939 kvm->arch.smt_mode = smt_mode;
Paul Mackerras57900692017-05-16 16:41:20 +10002940 kvm->arch.emul_smt_mode = esmt;
Paul Mackerras3c313522017-02-06 13:24:41 +11002941 err = 0;
2942 }
2943 mutex_unlock(&kvm->lock);
2944
2945 return err;
2946}
2947
Paul Mackerrasc35635e2013-04-18 19:51:04 +00002948static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
2949{
2950 if (vpa->pinned_addr)
2951 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
2952 vpa->dirty);
2953}
2954
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302955static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002956{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002957 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00002958 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
2959 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
2960 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002961 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002962}
2963
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302964static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
2965{
2966 /* Indicate we want to get back into the guest */
2967 return 1;
2968}
2969
Paul Mackerras19ccb762011-07-23 17:42:46 +10002970static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002971{
Paul Mackerras19ccb762011-07-23 17:42:46 +10002972 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002973
Paul Mackerras19ccb762011-07-23 17:42:46 +10002974 now = get_tb();
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10002975 if (now > kvmppc_dec_expires_host_tb(vcpu)) {
Paul Mackerras19ccb762011-07-23 17:42:46 +10002976 /* decrementer has already gone negative */
2977 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002978 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002979 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002980 }
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10002981 dec_nsec = tb_to_ns(kvmppc_dec_expires_host_tb(vcpu) - now);
Thomas Gleixner8b0e1952016-12-25 12:30:41 +01002982 hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002983 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002984}
2985
Paul Mackerras8b24e692017-06-26 15:45:51 +10002986extern int __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002987
2988static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
Nicholas Piggincb2553a2021-11-23 19:52:02 +10002989 struct kvm_vcpu *vcpu, u64 tb)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002990{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002991 u64 now;
2992
Paul Mackerras371fefd2011-06-29 00:23:08 +00002993 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2994 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11002995 spin_lock_irq(&vcpu->arch.tbacct_lock);
Nicholas Piggincb2553a2021-11-23 19:52:02 +10002996 now = tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002997 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
2998 vcpu->arch.stolen_logged;
2999 vcpu->arch.busy_preempt = now;
3000 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11003001 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003002 --vc->n_runnable;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003003 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003004}
3005
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003006static int kvmppc_grab_hwthread(int cpu)
3007{
3008 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10003009 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003010
Nicholas Piggind2e60072018-02-14 01:08:12 +10003011 tpaca = paca_ptrs[cpu];
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003012
3013 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00003014 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003015 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003016 tpaca->kvm_hstate.napping = 0;
3017 smp_wmb();
3018 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003019
3020 /*
3021 * If the thread is already executing in the kernel (e.g. handling
3022 * a stray interrupt), wait for it to get back to nap mode.
3023 * The smp_mb() is to ensure that our setting of hwthread_req
3024 * is visible before we look at hwthread_state, so if this
3025 * races with the code at system_reset_pSeries and the thread
3026 * misses our setting of hwthread_req, we are sure to see its
3027 * setting of hwthread_state, and vice versa.
3028 */
3029 smp_mb();
3030 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
3031 if (--timeout <= 0) {
3032 pr_err("KVM: couldn't grab cpu %d\n", cpu);
3033 return -EBUSY;
3034 }
3035 udelay(1);
3036 }
3037 return 0;
3038}
3039
3040static void kvmppc_release_hwthread(int cpu)
3041{
3042 struct paca_struct *tpaca;
3043
Nicholas Piggind2e60072018-02-14 01:08:12 +10003044 tpaca = paca_ptrs[cpu];
Paul Mackerras31a4d442017-10-19 15:14:20 +11003045 tpaca->kvm_hstate.hwthread_req = 0;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003046 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003047 tpaca->kvm_hstate.kvm_vcore = NULL;
3048 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00003049}
3050
Nicholas Piggin434398a2021-11-23 19:52:27 +10003051static DEFINE_PER_CPU(struct kvm *, cpu_in_guest);
3052
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003053static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu)
3054{
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003055 struct kvm_nested_guest *nested = vcpu->arch.nested;
Nicholas Piggin434398a2021-11-23 19:52:27 +10003056 cpumask_t *need_tlb_flush;
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003057 int i;
3058
Nicholas Piggin434398a2021-11-23 19:52:27 +10003059 if (nested)
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003060 need_tlb_flush = &nested->need_tlb_flush;
Nicholas Piggin434398a2021-11-23 19:52:27 +10003061 else
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003062 need_tlb_flush = &kvm->arch.need_tlb_flush;
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003063
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003064 cpu = cpu_first_tlb_thread_sibling(cpu);
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003065 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
3066 i += cpu_tlb_thread_sibling_step())
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003067 cpumask_set_cpu(i, need_tlb_flush);
3068
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003069 /*
Nicholas Piggin434398a2021-11-23 19:52:27 +10003070 * Make sure setting of bit in need_tlb_flush precedes testing of
3071 * cpu_in_guest. The matching barrier on the other side is hwsync
3072 * when switching to guest MMU mode, which happens between
3073 * cpu_in_guest being set to the guest kvm, and need_tlb_flush bit
3074 * being tested.
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003075 */
3076 smp_mb();
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003077
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003078 for (i = cpu; i <= cpu_last_tlb_thread_sibling(cpu);
Nicholas Piggin434398a2021-11-23 19:52:27 +10003079 i += cpu_tlb_thread_sibling_step()) {
3080 struct kvm *running = *per_cpu_ptr(&cpu_in_guest, i);
3081
3082 if (running == kvm)
Suraj Jitindar Singh77bbbc02021-06-02 14:04:41 +10003083 smp_call_function_single(i, do_nothing, NULL, 1);
Nicholas Piggin434398a2021-11-23 19:52:27 +10003084 }
Paul Mackerrasa29ebea2017-01-30 21:21:50 +11003085}
3086
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003087static void do_migrate_away_vcpu(void *arg)
3088{
3089 struct kvm_vcpu *vcpu = arg;
3090 struct kvm *kvm = vcpu->kvm;
3091
3092 /*
3093 * If the guest has GTSE, it may execute tlbie, so do a eieio; tlbsync;
3094 * ptesync sequence on the old CPU before migrating to a new one, in
3095 * case we interrupted the guest between a tlbie ; eieio ;
3096 * tlbsync; ptesync sequence.
3097 *
3098 * Otherwise, ptesync is sufficient for ordering tlbiel sequences.
3099 */
3100 if (kvm->arch.lpcr & LPCR_GTSE)
3101 asm volatile("eieio; tlbsync; ptesync");
3102 else
3103 asm volatile("ptesync");
Paul Mackerras371fefd2011-06-29 00:23:08 +00003104}
3105
Paul Mackerras8b24e692017-06-26 15:45:51 +10003106static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu)
3107{
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003108 struct kvm_nested_guest *nested = vcpu->arch.nested;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003109 struct kvm *kvm = vcpu->kvm;
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003110 int prev_cpu;
3111
3112 if (!cpu_has_feature(CPU_FTR_HVMODE))
3113 return;
3114
3115 if (nested)
3116 prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id];
3117 else
3118 prev_cpu = vcpu->arch.prev_cpu;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003119
3120 /*
3121 * With radix, the guest can do TLB invalidations itself,
3122 * and it could choose to use the local form (tlbiel) if
3123 * it is invalidating a translation that has only ever been
3124 * used on one vcpu. However, that doesn't mean it has
3125 * only ever been used on one physical cpu, since vcpus
3126 * can move around between pcpus. To cope with this, when
3127 * a vcpu moves from one pcpu to another, we need to tell
3128 * any vcpus running on the same core as this vcpu previously
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003129 * ran to flush the TLB.
Paul Mackerras8b24e692017-06-26 15:45:51 +10003130 */
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003131 if (prev_cpu != pcpu) {
Nicholas Piggind5c0e8332021-11-23 19:52:21 +10003132 if (prev_cpu >= 0) {
3133 if (cpu_first_tlb_thread_sibling(prev_cpu) !=
3134 cpu_first_tlb_thread_sibling(pcpu))
3135 radix_flush_cpu(kvm, prev_cpu, vcpu);
3136
3137 smp_call_function_single(prev_cpu,
3138 do_migrate_away_vcpu, vcpu, 1);
3139 }
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11003140 if (nested)
3141 nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu;
3142 else
3143 vcpu->arch.prev_cpu = pcpu;
3144 }
3145}
3146
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003147static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003148{
3149 int cpu;
3150 struct paca_struct *tpaca;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003151
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003152 cpu = vc->pcpu;
3153 if (vcpu) {
3154 if (vcpu->arch.timer_running) {
3155 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
3156 vcpu->arch.timer_running = 0;
3157 }
3158 cpu += vcpu->arch.ptid;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003159 vcpu->cpu = vc->pcpu;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003160 vcpu->arch.thread_cpu = cpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10003161 }
Nicholas Piggind2e60072018-02-14 01:08:12 +10003162 tpaca = paca_ptrs[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003163 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003164 tpaca->kvm_hstate.ptid = cpu - vc->pcpu;
Paul Mackerras4bb3c7a2018-03-21 21:32:01 +11003165 tpaca->kvm_hstate.fake_suspend = 0;
Paul Mackerrasec257162015-06-24 21:18:03 +10003166 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10003167 smp_wmb();
Paul Mackerras898b25b2017-06-22 15:08:42 +10003168 tpaca->kvm_hstate.kvm_vcore = vc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003169 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11003170 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003171}
3172
Paul Mackerras516f7892017-10-16 16:11:57 +11003173static void kvmppc_wait_for_nap(int n_threads)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003174{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003175 int cpu = smp_processor_id();
3176 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003177
Paul Mackerras45c940b2016-11-18 17:43:30 +11003178 if (n_threads <= 1)
3179 return;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003180 for (loops = 0; loops < 1000000; ++loops) {
3181 /*
3182 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003183 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003184 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003185 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003186 */
Paul Mackerras45c940b2016-11-18 17:43:30 +11003187 for (i = 1; i < n_threads; ++i)
Nicholas Piggind2e60072018-02-14 01:08:12 +10003188 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003189 break;
Paul Mackerras45c940b2016-11-18 17:43:30 +11003190 if (i == n_threads) {
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003191 HMT_medium();
3192 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003193 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003194 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00003195 }
3196 HMT_medium();
Paul Mackerras45c940b2016-11-18 17:43:30 +11003197 for (i = 1; i < n_threads; ++i)
Nicholas Piggind2e60072018-02-14 01:08:12 +10003198 if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11003199 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003200}
3201
3202/*
3203 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00003204 * this core are off-line. Then grab the threads so they can't
3205 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00003206 */
3207static int on_primary_thread(void)
3208{
3209 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10003210 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003211
Michael Ellerman3102f782014-05-23 18:15:29 +10003212 /* Are we on a primary subcore? */
3213 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00003214 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10003215
3216 thr = 0;
3217 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003218 if (cpu_online(cpu + thr))
3219 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00003220
3221 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10003222 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00003223 if (kvmppc_grab_hwthread(cpu + thr)) {
3224 /* Couldn't grab one; let the others go */
3225 do {
3226 kvmppc_release_hwthread(cpu + thr);
3227 } while (--thr > 0);
3228 return 0;
3229 }
3230 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00003231 return 1;
3232}
3233
Paul Mackerrasec257162015-06-24 21:18:03 +10003234/*
3235 * A list of virtual cores for each physical CPU.
3236 * These are vcores that could run but their runner VCPU tasks are
3237 * (or may be) preempted.
3238 */
3239struct preempted_vcore_list {
3240 struct list_head list;
3241 spinlock_t lock;
3242};
3243
3244static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
3245
3246static void init_vcore_lists(void)
3247{
3248 int cpu;
3249
3250 for_each_possible_cpu(cpu) {
3251 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
3252 spin_lock_init(&lp->lock);
3253 INIT_LIST_HEAD(&lp->list);
3254 }
3255}
3256
3257static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
3258{
3259 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3260
Nicholas Pigginecb6a722021-11-23 19:52:28 +10003261 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
3262
Paul Mackerrasec257162015-06-24 21:18:03 +10003263 vc->vcore_state = VCORE_PREEMPT;
3264 vc->pcpu = smp_processor_id();
Paul Mackerras516f7892017-10-16 16:11:57 +11003265 if (vc->num_threads < threads_per_vcore(vc->kvm)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003266 spin_lock(&lp->lock);
3267 list_add_tail(&vc->preempt_list, &lp->list);
3268 spin_unlock(&lp->lock);
3269 }
3270
3271 /* Start accumulating stolen time */
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003272 kvmppc_core_start_stolen(vc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003273}
3274
3275static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
3276{
Paul Mackerras402813f2015-07-16 17:11:13 +10003277 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10003278
Nicholas Pigginecb6a722021-11-23 19:52:28 +10003279 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
3280
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003281 kvmppc_core_end_stolen(vc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003282 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10003283 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10003284 spin_lock(&lp->lock);
3285 list_del_init(&vc->preempt_list);
3286 spin_unlock(&lp->lock);
3287 }
3288 vc->vcore_state = VCORE_INACTIVE;
3289}
3290
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003291/*
3292 * This stores information about the virtual cores currently
3293 * assigned to a physical core.
3294 */
Paul Mackerrasec257162015-06-24 21:18:03 +10003295struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003296 int n_subcores;
3297 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10003298 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003299 int subcore_threads[MAX_SUBCORES];
Paul Mackerras898b25b2017-06-22 15:08:42 +10003300 struct kvmppc_vcore *vc[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10003301};
3302
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003303/*
3304 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
Paul Mackerras516f7892017-10-16 16:11:57 +11003305 * respectively in 2-way micro-threading (split-core) mode on POWER8.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003306 */
3307static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
3308
Paul Mackerrasec257162015-06-24 21:18:03 +10003309static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
3310{
3311 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003312 cip->n_subcores = 1;
3313 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10003314 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003315 cip->subcore_threads[0] = vc->num_threads;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003316 cip->vc[0] = vc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003317}
3318
3319static bool subcore_config_ok(int n_subcores, int n_threads)
3320{
Paul Mackerras516f7892017-10-16 16:11:57 +11003321 /*
Paul Mackerras00608e12018-01-11 16:54:26 +11003322 * POWER9 "SMT4" cores are permanently in what is effectively a 4-way
3323 * split-core mode, with one thread per subcore.
Paul Mackerras516f7892017-10-16 16:11:57 +11003324 */
3325 if (cpu_has_feature(CPU_FTR_ARCH_300))
3326 return n_subcores <= 4 && n_threads == 1;
3327
3328 /* On POWER8, can only dynamically split if unsplit to begin with */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003329 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
3330 return false;
3331 if (n_subcores > MAX_SUBCORES)
3332 return false;
3333 if (n_subcores > 1) {
3334 if (!(dynamic_mt_modes & 2))
3335 n_subcores = 4;
3336 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
3337 return false;
3338 }
3339
3340 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10003341}
3342
Paul Mackerras898b25b2017-06-22 15:08:42 +10003343static void init_vcore_to_run(struct kvmppc_vcore *vc)
Paul Mackerrasec257162015-06-24 21:18:03 +10003344{
Paul Mackerrasec257162015-06-24 21:18:03 +10003345 vc->entry_exit_map = 0;
3346 vc->in_guest = 0;
3347 vc->napping_threads = 0;
3348 vc->conferring_threads = 0;
Paul Mackerras57b8daa2018-04-20 22:51:11 +10003349 vc->tb_offset_applied = 0;
Paul Mackerrasec257162015-06-24 21:18:03 +10003350}
3351
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003352static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
3353{
3354 int n_threads = vc->num_threads;
3355 int sub;
3356
3357 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
3358 return false;
3359
Paul Mackerrasaa227862018-09-12 10:42:12 +10003360 /* In one_vm_per_core mode, require all vcores to be from the same vm */
3361 if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm)
3362 return false;
3363
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003364 if (n_threads < cip->max_subcore_threads)
3365 n_threads = cip->max_subcore_threads;
Paul Mackerrasb0090312016-09-15 16:27:41 +10003366 if (!subcore_config_ok(cip->n_subcores + 1, n_threads))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003367 return false;
Paul Mackerrasb0090312016-09-15 16:27:41 +10003368 cip->max_subcore_threads = n_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003369
3370 sub = cip->n_subcores;
3371 ++cip->n_subcores;
3372 cip->total_threads += vc->num_threads;
3373 cip->subcore_threads[sub] = vc->num_threads;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003374 cip->vc[sub] = vc;
3375 init_vcore_to_run(vc);
3376 list_del_init(&vc->preempt_list);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003377
3378 return true;
3379}
3380
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003381/*
3382 * Work out whether it is possible to piggyback the execution of
3383 * vcore *pvc onto the execution of the other vcores described in *cip.
3384 */
3385static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
3386 int target_threads)
3387{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003388 if (cip->total_threads + pvc->num_threads > target_threads)
3389 return false;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003390
Paul Mackerrasb0090312016-09-15 16:27:41 +10003391 return can_dynamic_split(pvc, cip);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003392}
3393
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003394static void prepare_threads(struct kvmppc_vcore *vc)
3395{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003396 int i;
3397 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003398
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003399 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003400 if (signal_pending(vcpu->arch.run_task))
3401 vcpu->arch.ret = -EINTR;
3402 else if (vcpu->arch.vpa.update_pending ||
3403 vcpu->arch.slb_shadow.update_pending ||
3404 vcpu->arch.dtl.update_pending)
3405 vcpu->arch.ret = RESUME_GUEST;
3406 else
3407 continue;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003408 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003409 wake_up(&vcpu->arch.cpu_run);
3410 }
3411}
3412
Paul Mackerrasec257162015-06-24 21:18:03 +10003413static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003414{
Paul Mackerrasec257162015-06-24 21:18:03 +10003415 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
3416 struct kvmppc_vcore *pvc, *vcnext;
3417
3418 spin_lock(&lp->lock);
3419 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
3420 if (!spin_trylock(&pvc->lock))
3421 continue;
3422 prepare_threads(pvc);
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003423 if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003424 list_del_init(&pvc->preempt_list);
3425 if (pvc->runner == NULL) {
3426 pvc->vcore_state = VCORE_INACTIVE;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003427 kvmppc_core_end_stolen(pvc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003428 }
3429 spin_unlock(&pvc->lock);
3430 continue;
3431 }
3432 if (!can_piggyback(pvc, cip, target_threads)) {
3433 spin_unlock(&pvc->lock);
3434 continue;
3435 }
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003436 kvmppc_core_end_stolen(pvc, mftb());
Paul Mackerrasec257162015-06-24 21:18:03 +10003437 pvc->vcore_state = VCORE_PIGGYBACK;
3438 if (cip->total_threads >= target_threads)
3439 break;
3440 }
3441 spin_unlock(&lp->lock);
3442}
3443
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003444static bool recheck_signals_and_mmu(struct core_info *cip)
Paul Mackerras8b24e692017-06-26 15:45:51 +10003445{
3446 int sub, i;
3447 struct kvm_vcpu *vcpu;
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003448 struct kvmppc_vcore *vc;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003449
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003450 for (sub = 0; sub < cip->n_subcores; ++sub) {
3451 vc = cip->vc[sub];
3452 if (!vc->kvm->arch.mmu_ready)
3453 return true;
3454 for_each_runnable_thread(i, vcpu, vc)
Paul Mackerras8b24e692017-06-26 15:45:51 +10003455 if (signal_pending(vcpu->arch.run_task))
3456 return true;
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003457 }
Paul Mackerras8b24e692017-06-26 15:45:51 +10003458 return false;
3459}
3460
Paul Mackerrasec257162015-06-24 21:18:03 +10003461static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
3462{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003463 int still_running = 0, i;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003464 u64 now;
3465 long ret;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003466 struct kvm_vcpu *vcpu;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003467
Paul Mackerrasec257162015-06-24 21:18:03 +10003468 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003469 now = get_tb();
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003470 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras53655dd2018-10-08 16:30:54 +11003471 /*
3472 * It's safe to unlock the vcore in the loop here, because
3473 * for_each_runnable_thread() is safe against removal of
3474 * the vcpu, and the vcore state is VCORE_EXITING here,
3475 * so any vcpus becoming runnable will have their arch.trap
3476 * set to zero and can't actually run in the guest.
3477 */
3478 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003479 /* cancel pending dec exception if dec is positive */
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10003480 if (now < kvmppc_dec_expires_host_tb(vcpu) &&
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003481 kvmppc_core_pending_dec(vcpu))
3482 kvmppc_core_dequeue_dec(vcpu);
3483
3484 trace_kvm_guest_exit(vcpu);
3485
3486 ret = RESUME_GUEST;
3487 if (vcpu->arch.trap)
Tianjia Zhang8c99d342020-04-27 12:35:11 +08003488 ret = kvmppc_handle_exit_hv(vcpu,
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003489 vcpu->arch.run_task);
3490
3491 vcpu->arch.ret = ret;
3492 vcpu->arch.trap = 0;
3493
Paul Mackerras53655dd2018-10-08 16:30:54 +11003494 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10003495 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
3496 if (vcpu->arch.pending_exceptions)
3497 kvmppc_core_prepare_to_enter(vcpu);
3498 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003499 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10003500 else
3501 ++still_running;
3502 } else {
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003503 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003504 wake_up(&vcpu->arch.cpu_run);
3505 }
3506 }
Paul Mackerrasec257162015-06-24 21:18:03 +10003507 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10003508 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10003509 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10003510 } else if (vc->runner) {
3511 vc->vcore_state = VCORE_PREEMPT;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003512 kvmppc_core_start_stolen(vc, mftb());
Paul Mackerras563a1e92015-07-16 17:11:14 +10003513 } else {
3514 vc->vcore_state = VCORE_INACTIVE;
3515 }
Paul Mackerrasec257162015-06-24 21:18:03 +10003516 if (vc->n_runnable > 0 && vc->runner == NULL) {
3517 /* make sure there's a candidate runner awake */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003518 i = -1;
3519 vcpu = next_runnable_thread(vc, &i);
Paul Mackerrasec257162015-06-24 21:18:03 +10003520 wake_up(&vcpu->arch.cpu_run);
3521 }
3522 }
3523 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003524}
3525
Paul Mackerras371fefd2011-06-29 00:23:08 +00003526/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003527 * Clear core from the list of active host cores as we are about to
3528 * enter the guest. Only do this if it is the primary thread of the
3529 * core (not if a subcore) that is entering the guest.
3530 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003531static inline int kvmppc_clear_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003532{
3533 int core;
3534
3535 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003536 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003537 /*
3538 * Memory barrier can be omitted here as we will do a smp_wmb()
3539 * later in kvmppc_start_thread and we need ensure that state is
3540 * visible to other CPUs only after we enter guest.
3541 */
3542 core = cpu >> threads_shift;
3543 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003544 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003545}
3546
3547/*
3548 * Advertise this core as an active host core since we exited the guest
3549 * Only need to do this if it is the primary thread of the core that is
3550 * exiting.
3551 */
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003552static inline int kvmppc_set_host_core(unsigned int cpu)
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003553{
3554 int core;
3555
3556 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003557 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003558
3559 /*
3560 * Memory barrier can be omitted here because we do a spin_unlock
3561 * immediately after this which provides the memory barrier.
3562 */
3563 core = cpu >> threads_shift;
3564 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01003565 return 0;
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003566}
3567
Paul Mackerras8b24e692017-06-26 15:45:51 +10003568static void set_irq_happened(int trap)
3569{
3570 switch (trap) {
3571 case BOOK3S_INTERRUPT_EXTERNAL:
3572 local_paca->irq_happened |= PACA_IRQ_EE;
3573 break;
3574 case BOOK3S_INTERRUPT_H_DOORBELL:
3575 local_paca->irq_happened |= PACA_IRQ_DBELL;
3576 break;
3577 case BOOK3S_INTERRUPT_HMI:
3578 local_paca->irq_happened |= PACA_IRQ_HMI;
3579 break;
Nicholas Piggin6de66382017-11-05 23:33:55 +11003580 case BOOK3S_INTERRUPT_SYSTEM_RESET:
3581 replay_system_reset();
3582 break;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003583 }
3584}
3585
Suresh Warrierb8e6a872015-12-17 14:59:07 -06003586/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00003587 * Run a set of guest threads on a physical core.
3588 * Called with vc->lock held.
3589 */
Paul Mackerras66feed62015-03-28 14:21:12 +11003590static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00003591{
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003592 struct kvm_vcpu *vcpu;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003593 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003594 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10003595 struct core_info core_info;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003596 struct kvmppc_vcore *pvc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003597 struct kvm_split_mode split_info, *sip;
3598 int split, subcore_size, active;
3599 int sub;
3600 bool thr0_done;
3601 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10003602 int pcpu, thr;
3603 int target_threads;
Paul Mackerras45c940b2016-11-18 17:43:30 +11003604 int controlled_threads;
Paul Mackerras8b24e692017-06-26 15:45:51 +10003605 int trap;
Paul Mackerras516f7892017-10-16 16:11:57 +11003606 bool is_power8;
Paul Mackerras081f3232012-06-01 20:20:24 +10003607
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10003608 if (WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300)))
3609 return;
3610
Paul Mackerras081f3232012-06-01 20:20:24 +10003611 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11003612 * Remove from the list any threads that have a signal pending
3613 * or need a VPA update done
3614 */
3615 prepare_threads(vc);
3616
3617 /* if the runner is no longer runnable, let the caller pick a new one */
3618 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
3619 return;
3620
3621 /*
3622 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10003623 */
Paul Mackerras898b25b2017-06-22 15:08:42 +10003624 init_vcore_to_run(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11003625 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10003626
3627 /*
Paul Mackerras45c940b2016-11-18 17:43:30 +11003628 * Number of threads that we will be controlling: the same as
3629 * the number of threads per subcore, except on POWER9,
3630 * where it's 1 because the threads are (mostly) independent.
3631 */
Paul Mackerras516f7892017-10-16 16:11:57 +11003632 controlled_threads = threads_per_vcore(vc->kvm);
Paul Mackerras45c940b2016-11-18 17:43:30 +11003633
3634 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10003635 * Make sure we are running on primary threads, and that secondary
3636 * threads are offline. Also check if the number of threads in this
3637 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00003638 */
Nicholas Pigginb1b16972021-01-18 16:28:06 +10003639 if ((controlled_threads > 1) &&
3640 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10003641 for_each_runnable_thread(i, vcpu, vc) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00003642 vcpu->arch.ret = -EBUSY;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10003643 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerras25fedfc2015-03-28 14:21:05 +11003644 wake_up(&vcpu->arch.cpu_run);
3645 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00003646 goto out;
3647 }
3648
Paul Mackerrasec257162015-06-24 21:18:03 +10003649 /*
3650 * See if we could run any other vcores on the physical core
3651 * along with this one.
3652 */
3653 init_core_info(&core_info, vc);
3654 pcpu = smp_processor_id();
Paul Mackerras45c940b2016-11-18 17:43:30 +11003655 target_threads = controlled_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10003656 if (target_smt_mode && target_smt_mode < target_threads)
3657 target_threads = target_smt_mode;
3658 if (vc->num_threads < target_threads)
3659 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10003660
Paul Mackerras8b24e692017-06-26 15:45:51 +10003661 /*
Paul Mackerras8b24e692017-06-26 15:45:51 +10003662 * Hard-disable interrupts, and check resched flag and signals.
3663 * If we need to reschedule or deliver a signal, clean up
3664 * and return without going into the guest(s).
Paul Mackerras072df812017-11-09 14:30:24 +11003665 * If the mmu_ready flag has been cleared, don't go into the
Paul Mackerras38c53af2017-11-08 14:44:04 +11003666 * guest because that means a HPT resize operation is in progress.
Paul Mackerras8b24e692017-06-26 15:45:51 +10003667 */
3668 local_irq_disable();
3669 hard_irq_disable();
3670 if (lazy_irq_pending() || need_resched() ||
Paul Mackerrasd28eafc2019-08-27 11:31:37 +10003671 recheck_signals_and_mmu(&core_info)) {
Paul Mackerras8b24e692017-06-26 15:45:51 +10003672 local_irq_enable();
3673 vc->vcore_state = VCORE_INACTIVE;
3674 /* Unlock all except the primary vcore */
3675 for (sub = 1; sub < core_info.n_subcores; ++sub) {
3676 pvc = core_info.vc[sub];
3677 /* Put back on to the preempted vcores list */
3678 kvmppc_vcore_preempt(pvc);
3679 spin_unlock(&pvc->lock);
3680 }
3681 for (i = 0; i < controlled_threads; ++i)
3682 kvmppc_release_hwthread(pcpu + i);
3683 return;
3684 }
3685
3686 kvmppc_clear_host_core(pcpu);
3687
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003688 /* Decide on micro-threading (split-core) mode */
3689 subcore_size = threads_per_subcore;
3690 cmd_bit = stat_bit = 0;
3691 split = core_info.n_subcores;
3692 sip = NULL;
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10003693 is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S);
Paul Mackerras516f7892017-10-16 16:11:57 +11003694
Nicholas Pigginb1b16972021-01-18 16:28:06 +10003695 if (split > 1) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003696 sip = &split_info;
3697 memset(&split_info, 0, sizeof(split_info));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003698 for (sub = 0; sub < core_info.n_subcores; ++sub)
Paul Mackerras898b25b2017-06-22 15:08:42 +10003699 split_info.vc[sub] = core_info.vc[sub];
Paul Mackerras516f7892017-10-16 16:11:57 +11003700
3701 if (is_power8) {
3702 if (split == 2 && (dynamic_mt_modes & 2)) {
3703 cmd_bit = HID0_POWER8_1TO2LPAR;
3704 stat_bit = HID0_POWER8_2LPARMODE;
3705 } else {
3706 split = 4;
3707 cmd_bit = HID0_POWER8_1TO4LPAR;
3708 stat_bit = HID0_POWER8_4LPARMODE;
3709 }
3710 subcore_size = MAX_SMT_THREADS / split;
3711 split_info.rpr = mfspr(SPRN_RPR);
3712 split_info.pmmar = mfspr(SPRN_PMMAR);
3713 split_info.ldbar = mfspr(SPRN_LDBAR);
3714 split_info.subcore_size = subcore_size;
3715 } else {
3716 split_info.subcore_size = 1;
3717 }
3718
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003719 /* order writes to split_info before kvm_split_mode pointer */
3720 smp_wmb();
3721 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003722
Paul Mackerrasc0101502017-10-19 14:11:23 +11003723 for (thr = 0; thr < controlled_threads; ++thr) {
Nicholas Piggind2e60072018-02-14 01:08:12 +10003724 struct paca_struct *paca = paca_ptrs[pcpu + thr];
3725
Nicholas Piggind2e60072018-02-14 01:08:12 +10003726 paca->kvm_hstate.napping = 0;
3727 paca->kvm_hstate.kvm_split_mode = sip;
Paul Mackerrasc0101502017-10-19 14:11:23 +11003728 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003729
Paul Mackerras516f7892017-10-16 16:11:57 +11003730 /* Initiate micro-threading (split-core) on POWER8 if required */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003731 if (cmd_bit) {
3732 unsigned long hid0 = mfspr(SPRN_HID0);
3733
3734 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
3735 mb();
3736 mtspr(SPRN_HID0, hid0);
3737 isync();
3738 for (;;) {
3739 hid0 = mfspr(SPRN_HID0);
3740 if (hid0 & stat_bit)
3741 break;
3742 cpu_relax();
3743 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00003744 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00003745
Paul Mackerras7aa15842018-04-20 19:53:22 +10003746 /*
3747 * On POWER8, set RWMR register.
3748 * Since it only affects PURR and SPURR, it doesn't affect
3749 * the host, so we don't save/restore the host value.
3750 */
3751 if (is_power8) {
3752 unsigned long rwmr_val = RWMR_RPA_P8_8THREAD;
3753 int n_online = atomic_read(&vc->online_count);
3754
3755 /*
3756 * Use the 8-thread value if we're doing split-core
3757 * or if the vcore's online count looks bogus.
3758 */
3759 if (split == 1 && threads_per_subcore == MAX_SMT_THREADS &&
3760 n_online >= 1 && n_online <= MAX_SMT_THREADS)
3761 rwmr_val = p8_rwmr_values[n_online];
3762 mtspr(SPRN_RWMR, rwmr_val);
3763 }
3764
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003765 /* Start all the threads */
3766 active = 0;
3767 for (sub = 0; sub < core_info.n_subcores; ++sub) {
Paul Mackerras516f7892017-10-16 16:11:57 +11003768 thr = is_power8 ? subcore_thread_map[sub] : sub;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003769 thr0_done = false;
3770 active |= 1 << thr;
Paul Mackerras898b25b2017-06-22 15:08:42 +10003771 pvc = core_info.vc[sub];
3772 pvc->pcpu = pcpu + thr;
3773 for_each_runnable_thread(i, vcpu, pvc) {
3774 kvmppc_start_thread(vcpu, pvc);
3775 kvmppc_create_dtl_entry(vcpu, pvc);
3776 trace_kvm_guest_enter(vcpu);
3777 if (!vcpu->arch.ptid)
3778 thr0_done = true;
3779 active |= 1 << (thr + vcpu->arch.ptid);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003780 }
Paul Mackerras898b25b2017-06-22 15:08:42 +10003781 /*
3782 * We need to start the first thread of each subcore
3783 * even if it doesn't have a vcpu.
3784 */
3785 if (!thr0_done)
3786 kvmppc_start_thread(NULL, pvc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003787 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05303788
3789 /*
3790 * Ensure that split_info.do_nap is set after setting
3791 * the vcore pointer in the PACA of the secondaries.
3792 */
3793 smp_mb();
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05303794
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003795 /*
3796 * When doing micro-threading, poke the inactive threads as well.
3797 * This gets them to the nap instruction after kvm_do_nap,
3798 * which reduces the time taken to unsplit later.
3799 */
Nicholas Pigginb1b16972021-01-18 16:28:06 +10003800 if (cmd_bit) {
Paul Mackerras516f7892017-10-16 16:11:57 +11003801 split_info.do_nap = 1; /* ask secondaries to nap when done */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003802 for (thr = 1; thr < threads_per_subcore; ++thr)
3803 if (!(active & (1 << thr)))
3804 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerras516f7892017-10-16 16:11:57 +11003805 }
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11003806
Paul Mackerras2f12f032012-10-15 01:17:17 +00003807 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003808 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003809
3810 trace_kvmppc_run_core(vc, 0);
3811
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003812 for (sub = 0; sub < core_info.n_subcores; ++sub)
Paul Mackerras898b25b2017-06-22 15:08:42 +10003813 spin_unlock(&core_info.vc[sub]->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003814
Laurent Vivier61bd0f662018-03-02 11:51:56 +01003815 guest_enter_irqoff();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003816
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11003817 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003818
Naveen N. Raoa4bc64d2018-04-19 12:34:05 +05303819 this_cpu_disable_ftrace();
3820
Alexey Kardashevskiy3309bec2019-03-29 16:40:13 +11003821 /*
3822 * Interrupts will be enabled once we get into the guest,
3823 * so tell lockdep that we're about to enable interrupts.
3824 */
3825 trace_hardirqs_on();
3826
Paul Mackerras8b24e692017-06-26 15:45:51 +10003827 trap = __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10003828
Alexey Kardashevskiy3309bec2019-03-29 16:40:13 +11003829 trace_hardirqs_off();
3830
Naveen N. Raoa4bc64d2018-04-19 12:34:05 +05303831 this_cpu_enable_ftrace();
3832
Paul Mackerrasec257162015-06-24 21:18:03 +10003833 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
3834
Paul Mackerras8b24e692017-06-26 15:45:51 +10003835 set_irq_happened(trap);
3836
Paul Mackerrasec257162015-06-24 21:18:03 +10003837 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003838 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10003839 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00003840
Paul Mackerras371fefd2011-06-29 00:23:08 +00003841 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras516f7892017-10-16 16:11:57 +11003842 kvmppc_wait_for_nap(controlled_threads);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003843
3844 /* Return to whole-core mode if we split the core earlier */
Paul Mackerras516f7892017-10-16 16:11:57 +11003845 if (cmd_bit) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003846 unsigned long hid0 = mfspr(SPRN_HID0);
3847 unsigned long loops = 0;
3848
3849 hid0 &= ~HID0_POWER8_DYNLPARDIS;
3850 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
3851 mb();
3852 mtspr(SPRN_HID0, hid0);
3853 isync();
3854 for (;;) {
3855 hid0 = mfspr(SPRN_HID0);
3856 if (!(hid0 & stat_bit))
3857 break;
3858 cpu_relax();
3859 ++loops;
3860 }
Nicholas Pigginb1b16972021-01-18 16:28:06 +10003861 split_info.do_nap = 0;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003862 }
3863
Paul Mackerras8b24e692017-06-26 15:45:51 +10003864 kvmppc_set_host_core(pcpu);
3865
Laurent Vivier235cee12021-10-28 00:21:50 +10003866 context_tracking_guest_exit();
3867 if (!vtime_accounting_enabled_this_cpu()) {
3868 local_irq_enable();
3869 /*
3870 * Service IRQs here before vtime_account_guest_exit() so any
3871 * ticks that occurred while running the guest are accounted to
3872 * the guest. If vtime accounting is enabled, accounting uses
3873 * TB rather than ticks, so it can be done without enabling
3874 * interrupts here, which has the problem that it accounts
3875 * interrupt processing overhead to the host.
3876 */
3877 local_irq_disable();
3878 }
3879 vtime_account_guest_exit();
Nicholas Piggin11266522021-01-30 23:08:12 +10003880
Paul Mackerras8b24e692017-06-26 15:45:51 +10003881 local_irq_enable();
3882
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003883 /* Let secondaries go back to the offline loop */
Paul Mackerras45c940b2016-11-18 17:43:30 +11003884 for (i = 0; i < controlled_threads; ++i) {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10003885 kvmppc_release_hwthread(pcpu + i);
3886 if (sip && sip->napped[i])
3887 kvmppc_ipi_thread(pcpu + i);
3888 }
3889
Paul Mackerras371fefd2011-06-29 00:23:08 +00003890 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003891
Paul Mackerras371fefd2011-06-29 00:23:08 +00003892 /* make sure updates to secondary vcpu structs are visible now */
3893 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00003894
Paul Mackerras36ee41d2018-01-30 10:51:32 +11003895 preempt_enable();
3896
Paul Mackerras898b25b2017-06-22 15:08:42 +10003897 for (sub = 0; sub < core_info.n_subcores; ++sub) {
3898 pvc = core_info.vc[sub];
3899 post_guest_process(pvc, pvc == vc);
3900 }
Paul Mackerrasde56a942011-06-29 00:21:34 +00003901
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00003902 spin_lock(&vc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003903
Paul Mackerrasde56a942011-06-29 00:21:34 +00003904 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10003905 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06003906 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00003907}
3908
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10003909static inline bool hcall_is_xics(unsigned long req)
3910{
3911 return req == H_EOI || req == H_CPPR || req == H_IPI ||
3912 req == H_IPOLL || req == H_XIRR || req == H_XIRR_X;
3913}
3914
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10003915static void vcpu_vpa_increment_dispatch(struct kvm_vcpu *vcpu)
Paul Mackerras95a64322018-10-08 16:30:55 +11003916{
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10003917 struct lppaca *lp = vcpu->arch.vpa.pinned_addr;
3918 if (lp) {
Paul Mackerras95a64322018-10-08 16:30:55 +11003919 u32 yield_count = be32_to_cpu(lp->yield_count) + 1;
3920 lp->yield_count = cpu_to_be32(yield_count);
3921 vcpu->arch.vpa.dirty = 1;
3922 }
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10003923}
Paul Mackerras95a64322018-10-08 16:30:55 +11003924
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003925/* call our hypervisor to load up HV regs and go */
3926static int kvmhv_vcpu_entry_p9_nested(struct kvm_vcpu *vcpu, u64 time_limit, unsigned long lpcr, u64 *tb)
3927{
3928 struct kvmppc_vcore *vc = vcpu->arch.vcore;
3929 unsigned long host_psscr;
Nicholas Piggind5f48012021-11-23 19:52:09 +10003930 unsigned long msr;
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003931 struct hv_guest_state hvregs;
Nicholas Piggind5f48012021-11-23 19:52:09 +10003932 struct p9_host_os_sprs host_os_sprs;
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003933 s64 dec;
Nicholas Piggind5f48012021-11-23 19:52:09 +10003934 int trap;
Paul Mackerras95a64322018-10-08 16:30:55 +11003935
Nicholas Pigginb49c65c2021-11-23 19:52:23 +10003936 msr = mfmsr();
Paul Mackerras95a64322018-10-08 16:30:55 +11003937
Nicholas Piggind5f48012021-11-23 19:52:09 +10003938 save_p9_host_os_sprs(&host_os_sprs);
Paul Mackerras95a64322018-10-08 16:30:55 +11003939
Nicholas Piggin6ffe2c62021-05-28 19:07:30 +10003940 /*
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003941 * We need to save and restore the guest visible part of the
3942 * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor
3943 * doesn't do this for us. Note only required if pseries since
3944 * this is done in kvmhv_vcpu_entry_p9() below otherwise.
3945 */
3946 host_psscr = mfspr(SPRN_PSSCR_PR);
Nicholas Piggind5f48012021-11-23 19:52:09 +10003947
Nicholas Pigginb49c65c2021-11-23 19:52:23 +10003948 kvmppc_msr_hard_disable_set_facilities(vcpu, msr);
Nicholas Piggind5f48012021-11-23 19:52:09 +10003949 if (lazy_irq_pending())
3950 return 0;
3951
Nicholas Piggind5f48012021-11-23 19:52:09 +10003952 if (unlikely(load_vcpu_state(vcpu, &host_os_sprs)))
3953 msr = mfmsr(); /* TM restore can update msr */
3954
Nicholas Piggina089a682021-11-23 19:52:19 +10003955 if (vcpu->arch.psscr != host_psscr)
3956 mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr);
3957
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003958 kvmhv_save_hv_regs(vcpu, &hvregs);
3959 hvregs.lpcr = lpcr;
3960 vcpu->arch.regs.msr = vcpu->arch.shregs.msr;
3961 hvregs.version = HV_GUEST_STATE_VERSION;
3962 if (vcpu->arch.nested) {
3963 hvregs.lpid = vcpu->arch.nested->shadow_lpid;
3964 hvregs.vcpu_token = vcpu->arch.nested_vcpu_id;
3965 } else {
3966 hvregs.lpid = vcpu->kvm->arch.lpid;
3967 hvregs.vcpu_token = vcpu->vcpu_id;
3968 }
3969 hvregs.hdec_expiry = time_limit;
3970
3971 /*
3972 * When setting DEC, we must always deal with irq_work_raise
3973 * via NMI vs setting DEC. The problem occurs right as we
3974 * switch into guest mode if a NMI hits and sets pending work
3975 * and sets DEC, then that will apply to the guest and not
3976 * bring us back to the host.
Nicholas Piggin6ffe2c62021-05-28 19:07:30 +10003977 *
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003978 * irq_work_raise could check a flag (or possibly LPCR[HDICE]
3979 * for example) and set HDEC to 1? That wouldn't solve the
3980 * nested hv case which needs to abort the hcall or zero the
3981 * time limit.
Nicholas Piggin6ffe2c62021-05-28 19:07:30 +10003982 *
3983 * XXX: Another day's problem.
3984 */
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003985 mtspr(SPRN_DEC, kvmppc_dec_expires_host_tb(vcpu) - *tb);
3986
3987 mtspr(SPRN_DAR, vcpu->arch.shregs.dar);
3988 mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr);
Nicholas Piggin3e7b3372021-11-23 19:52:11 +10003989 switch_pmu_to_guest(vcpu, &host_os_sprs);
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003990 trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs),
3991 __pa(&vcpu->arch.regs));
3992 kvmhv_restore_hv_return_state(vcpu, &hvregs);
Nicholas Piggin3e7b3372021-11-23 19:52:11 +10003993 switch_pmu_to_host(vcpu, &host_os_sprs);
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003994 vcpu->arch.shregs.msr = vcpu->arch.regs.msr;
3995 vcpu->arch.shregs.dar = mfspr(SPRN_DAR);
3996 vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR);
3997 vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR);
Nicholas Piggin08b3f082021-11-23 19:52:08 +10003998
Nicholas Piggind5f48012021-11-23 19:52:09 +10003999 store_vcpu_state(vcpu);
4000
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004001 dec = mfspr(SPRN_DEC);
4002 if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */
4003 dec = (s32) dec;
4004 *tb = mftb();
4005 vcpu->arch.dec_expires = dec + (*tb + vc->tb_offset);
4006
Nicholas Piggind5f48012021-11-23 19:52:09 +10004007 timer_rearm_host_dec(*tb);
4008
4009 restore_p9_host_os_sprs(vcpu, &host_os_sprs);
Nicholas Piggina089a682021-11-23 19:52:19 +10004010 if (vcpu->arch.psscr != host_psscr)
4011 mtspr(SPRN_PSSCR_PR, host_psscr);
Nicholas Piggind5f48012021-11-23 19:52:09 +10004012
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004013 return trap;
4014}
4015
Paul Mackerras95a64322018-10-08 16:30:55 +11004016/*
4017 * Guest entry for POWER9 and later CPUs.
4018 */
4019static int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit,
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004020 unsigned long lpcr, u64 *tb)
Paul Mackerras95a64322018-10-08 16:30:55 +11004021{
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004022 u64 next_timer;
Nicholas Piggin57dc0ee2021-11-23 19:51:51 +10004023 int trap;
Paul Mackerras95a64322018-10-08 16:30:55 +11004024
Nicholas Piggin4ebbd072021-11-23 19:51:42 +10004025 next_timer = timer_get_next_tb();
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004026 if (*tb >= next_timer)
Paul Mackerras95a64322018-10-08 16:30:55 +11004027 return BOOK3S_INTERRUPT_HV_DECREMENTER;
Nicholas Piggin4ebbd072021-11-23 19:51:42 +10004028 if (next_timer < time_limit)
4029 time_limit = next_timer;
Nicholas Piggin46dea772021-11-23 19:52:22 +10004030 else if (*tb >= time_limit) /* nested time limit */
4031 return BOOK3S_INTERRUPT_NESTED_HV_DECREMENTER;
Paul Mackerras95a64322018-10-08 16:30:55 +11004032
Nicholas Piggin34e119c92021-11-23 19:51:57 +10004033 vcpu->arch.ceded = 0;
4034
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10004035 vcpu_vpa_increment_dispatch(vcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11004036
Paul Mackerras360cae32018-10-08 16:31:04 +11004037 if (kvmhv_on_pseries()) {
Nicholas Piggin08b3f082021-11-23 19:52:08 +10004038 trap = kvmhv_vcpu_entry_p9_nested(vcpu, time_limit, lpcr, tb);
Paul Mackerras4bad7772018-10-08 16:31:06 +11004039
4040 /* H_CEDE has to be handled now, not later */
4041 if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested &&
4042 kvmppc_get_gpr(vcpu, 3) == H_CEDE) {
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004043 kvmppc_cede(vcpu);
Michael Roth1f50cc12020-03-10 16:11:28 -05004044 kvmppc_set_gpr(vcpu, 3, 0);
Paul Mackerras4bad7772018-10-08 16:31:06 +11004045 trap = 0;
4046 }
Nicholas Piggin3c1a43222021-11-23 19:52:00 +10004047
Paul Mackerras360cae32018-10-08 16:31:04 +11004048 } else {
Nicholas Piggin434398a2021-11-23 19:52:27 +10004049 struct kvm *kvm = vcpu->kvm;
4050
Nicholas Piggin09512c22021-05-28 19:07:31 +10004051 kvmppc_xive_push_vcpu(vcpu);
Nicholas Piggin434398a2021-11-23 19:52:27 +10004052
4053 __this_cpu_write(cpu_in_guest, kvm);
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004054 trap = kvmhv_vcpu_entry_p9(vcpu, time_limit, lpcr, tb);
Nicholas Piggin434398a2021-11-23 19:52:27 +10004055 __this_cpu_write(cpu_in_guest, NULL);
4056
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004057 if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested &&
4058 !(vcpu->arch.shregs.msr & MSR_PR)) {
4059 unsigned long req = kvmppc_get_gpr(vcpu, 3);
Nicholas Piggin09512c22021-05-28 19:07:31 +10004060
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004061 /* H_CEDE has to be handled now, not later */
4062 if (req == H_CEDE) {
4063 kvmppc_cede(vcpu);
4064 kvmppc_xive_rearm_escalation(vcpu); /* may un-cede */
4065 kvmppc_set_gpr(vcpu, 3, 0);
4066 trap = 0;
4067
4068 /* XICS hcalls must be handled before xive is pulled */
4069 } else if (hcall_is_xics(req)) {
4070 int ret;
4071
4072 ret = kvmppc_xive_xics_hcall(vcpu, req);
4073 if (ret != H_TOO_HARD) {
4074 kvmppc_set_gpr(vcpu, 3, ret);
4075 trap = 0;
4076 }
4077 }
4078 }
4079 kvmppc_xive_pull_vcpu(vcpu);
Nicholas Piggin89d35b22021-05-28 19:07:34 +10004080
Nicholas Piggin434398a2021-11-23 19:52:27 +10004081 if (kvm_is_radix(kvm))
Nicholas Piggin079a09a2021-05-28 19:07:50 +10004082 vcpu->arch.slb_max = 0;
Paul Mackerras360cae32018-10-08 16:31:04 +11004083 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004084
Nicholas Pigginb1adcf52021-11-23 19:51:54 +10004085 vcpu_vpa_increment_dispatch(vcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11004086
4087 return trap;
4088}
4089
4090/*
Paul Mackerras19ccb762011-07-23 17:42:46 +10004091 * Wait for some other vcpu thread to execute us, and
4092 * wake us up when we need to handle something in the host.
4093 */
Paul Mackerrasec257162015-06-24 21:18:03 +10004094static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
4095 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00004096{
Paul Mackerras371fefd2011-06-29 00:23:08 +00004097 DEFINE_WAIT(wait);
4098
Paul Mackerras19ccb762011-07-23 17:42:46 +10004099 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10004100 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
4101 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004102 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10004103 spin_lock(&vc->lock);
4104 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10004105 finish_wait(&vcpu->arch.cpu_run, &wait);
4106}
Paul Mackerras371fefd2011-06-29 00:23:08 +00004107
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004108static void grow_halt_poll_ns(struct kvmppc_vcore *vc)
4109{
Nir Weiner7fa08e72019-01-27 12:17:14 +02004110 if (!halt_poll_ns_grow)
4111 return;
4112
Nir Weinerdee339b2019-01-27 12:17:16 +02004113 vc->halt_poll_ns *= halt_poll_ns_grow;
4114 if (vc->halt_poll_ns < halt_poll_ns_grow_start)
Nir Weiner49113d32019-01-27 12:17:15 +02004115 vc->halt_poll_ns = halt_poll_ns_grow_start;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004116}
4117
4118static void shrink_halt_poll_ns(struct kvmppc_vcore *vc)
4119{
4120 if (halt_poll_ns_shrink == 0)
4121 vc->halt_poll_ns = 0;
4122 else
4123 vc->halt_poll_ns /= halt_poll_ns_shrink;
4124}
4125
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004126#ifdef CONFIG_KVM_XICS
4127static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4128{
Paul Mackerras03f95332019-02-04 22:07:20 +11004129 if (!xics_on_xive())
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004130 return false;
Benjamin Herrenschmidt2267ea72018-01-12 13:37:13 +11004131 return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr <
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004132 vcpu->arch.xive_saved_state.cppr;
4133}
4134#else
4135static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu)
4136{
4137 return false;
4138}
4139#endif /* CONFIG_KVM_XICS */
4140
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10004141static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu)
4142{
4143 if (vcpu->arch.pending_exceptions || vcpu->arch.prodded ||
Paul Mackerrasee3308a2017-06-20 15:46:12 +10004144 kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu))
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10004145 return true;
4146
4147 return false;
4148}
4149
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004150static bool kvmppc_vcpu_check_block(struct kvm_vcpu *vcpu)
4151{
4152 if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu))
4153 return true;
4154 return false;
4155}
4156
Suraj Jitindar Singh908a0932016-10-14 11:53:23 +11004157/*
4158 * Check to see if any of the runnable vcpus on the vcore have pending
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004159 * exceptions or are no longer ceded
4160 */
4161static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc)
4162{
4163 struct kvm_vcpu *vcpu;
4164 int i;
4165
4166 for_each_runnable_thread(i, vcpu, vc) {
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004167 if (kvmppc_vcpu_check_block(vcpu))
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004168 return 1;
4169 }
4170
4171 return 0;
4172}
4173
Paul Mackerras19ccb762011-07-23 17:42:46 +10004174/*
4175 * All the vcpus in this vcore are idle, so wait for a decrementer
4176 * or external interrupt to one of the vcpus. vc->lock is held.
4177 */
4178static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
4179{
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004180 ktime_t cur, start_poll, start_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004181 int do_sleep = 1;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004182 u64 block_ns;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11004183
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004184 WARN_ON_ONCE(cpu_has_feature(CPU_FTR_ARCH_300));
4185
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004186 /* Poll for pending exceptions and ceded state */
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004187 cur = start_poll = ktime_get();
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004188 if (vc->halt_poll_ns) {
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004189 ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns);
Jing Zhang0193cc92021-06-18 22:27:03 +00004190 ++vc->runner->stat.generic.halt_attempted_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004191
4192 vc->vcore_state = VCORE_POLLING;
4193 spin_unlock(&vc->lock);
4194
4195 do {
4196 if (kvmppc_vcore_check_block(vc)) {
4197 do_sleep = 0;
4198 break;
4199 }
4200 cur = ktime_get();
Wanpeng Li6bd5b742021-05-18 05:00:31 -07004201 } while (kvm_vcpu_can_poll(cur, stop));
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004202
4203 spin_lock(&vc->lock);
4204 vc->vcore_state = VCORE_INACTIVE;
4205
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004206 if (!do_sleep) {
Jing Zhang0193cc92021-06-18 22:27:03 +00004207 ++vc->runner->stat.generic.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004208 goto out;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004209 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004210 }
4211
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004212 prepare_to_rcuwait(&vc->wait);
4213 set_current_state(TASK_INTERRUPTIBLE);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004214 if (kvmppc_vcore_check_block(vc)) {
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004215 finish_rcuwait(&vc->wait);
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004216 do_sleep = 0;
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004217 /* If we polled, count this as a successful poll */
4218 if (vc->halt_poll_ns)
Jing Zhang0193cc92021-06-18 22:27:03 +00004219 ++vc->runner->stat.generic.halt_successful_poll;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004220 goto out;
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11004221 }
4222
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004223 start_wait = ktime_get();
4224
Paul Mackerras19ccb762011-07-23 17:42:46 +10004225 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004226 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004227 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00004228 schedule();
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004229 finish_rcuwait(&vc->wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004230 spin_lock(&vc->lock);
4231 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004232 trace_kvmppc_vcore_blocked(vc, 1);
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004233 ++vc->runner->stat.halt_successful_wait;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004234
4235 cur = ktime_get();
4236
4237out:
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004238 block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll);
4239
4240 /* Attribute wait time */
4241 if (do_sleep) {
Jing Zhang87bcc5f2021-08-02 16:56:32 +00004242 vc->runner->stat.generic.halt_wait_ns +=
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004243 ktime_to_ns(cur) - ktime_to_ns(start_wait);
Jing Zhang8ccba532021-08-02 16:56:33 +00004244 KVM_STATS_LOG_HIST_UPDATE(
4245 vc->runner->stat.generic.halt_wait_hist,
4246 ktime_to_ns(cur) - ktime_to_ns(start_wait));
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004247 /* Attribute failed poll time */
Jing Zhang8ccba532021-08-02 16:56:33 +00004248 if (vc->halt_poll_ns) {
Jing Zhang0193cc92021-06-18 22:27:03 +00004249 vc->runner->stat.generic.halt_poll_fail_ns +=
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004250 ktime_to_ns(start_wait) -
4251 ktime_to_ns(start_poll);
Jing Zhang8ccba532021-08-02 16:56:33 +00004252 KVM_STATS_LOG_HIST_UPDATE(
4253 vc->runner->stat.generic.halt_poll_fail_hist,
4254 ktime_to_ns(start_wait) -
4255 ktime_to_ns(start_poll));
4256 }
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004257 } else {
4258 /* Attribute successful poll time */
Jing Zhang8ccba532021-08-02 16:56:33 +00004259 if (vc->halt_poll_ns) {
Jing Zhang0193cc92021-06-18 22:27:03 +00004260 vc->runner->stat.generic.halt_poll_success_ns +=
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004261 ktime_to_ns(cur) -
4262 ktime_to_ns(start_poll);
Jing Zhang8ccba532021-08-02 16:56:33 +00004263 KVM_STATS_LOG_HIST_UPDATE(
4264 vc->runner->stat.generic.halt_poll_success_hist,
4265 ktime_to_ns(cur) - ktime_to_ns(start_poll));
4266 }
Suraj Jitindar Singh2a27f512016-08-02 14:03:23 +10004267 }
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004268
4269 /* Adjust poll time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11004270 if (halt_poll_ns) {
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004271 if (block_ns <= vc->halt_poll_ns)
4272 ;
4273 /* We slept and blocked for longer than the max halt time */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11004274 else if (vc->halt_poll_ns && block_ns > halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004275 shrink_halt_poll_ns(vc);
4276 /* We slept and our poll time is too small */
Suraj Jitindar Singh307d93e42016-10-14 11:53:20 +11004277 else if (vc->halt_poll_ns < halt_poll_ns &&
4278 block_ns < halt_poll_ns)
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004279 grow_halt_poll_ns(vc);
Suraj Jitindar Singhe03f3922016-10-14 11:53:21 +11004280 if (vc->halt_poll_ns > halt_poll_ns)
4281 vc->halt_poll_ns = halt_poll_ns;
Suraj Jitindar Singh0cda69d2016-08-02 14:03:21 +10004282 } else
4283 vc->halt_poll_ns = 0;
4284
4285 trace_kvmppc_vcore_wakeup(do_sleep, block_ns);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004286}
4287
Paul Mackerras360cae32018-10-08 16:31:04 +11004288/*
4289 * This never fails for a radix guest, as none of the operations it does
4290 * for a radix guest can fail or have a way to report failure.
Paul Mackerras360cae32018-10-08 16:31:04 +11004291 */
Paul Mackerras432953b2017-11-09 15:37:10 +11004292static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu)
4293{
4294 int r = 0;
4295 struct kvm *kvm = vcpu->kvm;
4296
Paul Mackerras0d4ee882019-05-23 16:35:34 +10004297 mutex_lock(&kvm->arch.mmu_setup_lock);
Paul Mackerras432953b2017-11-09 15:37:10 +11004298 if (!kvm->arch.mmu_ready) {
4299 if (!kvm_is_radix(kvm))
4300 r = kvmppc_hv_setup_htab_rma(vcpu);
4301 if (!r) {
4302 if (cpu_has_feature(CPU_FTR_ARCH_300))
4303 kvmppc_setup_partition_table(kvm);
4304 kvm->arch.mmu_ready = 1;
4305 }
4306 }
Paul Mackerras0d4ee882019-05-23 16:35:34 +10004307 mutex_unlock(&kvm->arch.mmu_setup_lock);
Paul Mackerras432953b2017-11-09 15:37:10 +11004308 return r;
4309}
4310
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004311static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu)
Paul Mackerras19ccb762011-07-23 17:42:46 +10004312{
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004313 struct kvm_run *run = vcpu->run;
Paul Mackerras38c53af2017-11-08 14:44:04 +11004314 int n_ceded, i, r;
Paul Mackerras19ccb762011-07-23 17:42:46 +10004315 struct kvmppc_vcore *vc;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004316 struct kvm_vcpu *v;
Paul Mackerras9e368f22011-06-29 00:40:08 +00004317
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004318 trace_kvmppc_run_vcpu_enter(vcpu);
4319
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004320 run->exit_reason = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004321 vcpu->arch.ret = RESUME_GUEST;
4322 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00004323 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004324
Paul Mackerras371fefd2011-06-29 00:23:08 +00004325 /*
4326 * Synchronize with other threads in this virtual core
4327 */
4328 vc = vcpu->arch.vcore;
4329 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004330 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004331 vcpu->arch.run_task = current;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00004332 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10004333 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00004334 vcpu->arch.busy_preempt = TB_NIL;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004335 WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004336 ++vc->n_runnable;
4337
Paul Mackerras19ccb762011-07-23 17:42:46 +10004338 /*
4339 * This happens the first time this is called for a vcpu.
4340 * If the vcore is already running, we may be able to start
4341 * this thread straight away and have it join in.
4342 */
Paul Mackerras8455d792012-10-15 01:17:42 +00004343 if (!signal_pending(current)) {
Paul Mackerrasc0093f12017-11-20 16:12:25 +11004344 if ((vc->vcore_state == VCORE_PIGGYBACK ||
4345 vc->vcore_state == VCORE_RUNNING) &&
Paul Mackerrasec257162015-06-24 21:18:03 +10004346 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00004347 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10004348 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004349 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00004350 } else if (vc->vcore_state == VCORE_SLEEPING) {
Davidlohr Buesoda4ad882020-04-23 22:48:37 -07004351 rcuwait_wake_up(&vc->wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004352 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00004353
Paul Mackerras8455d792012-10-15 01:17:42 +00004354 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10004355
4356 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4357 !signal_pending(current)) {
Paul Mackerras072df812017-11-09 14:30:24 +11004358 /* See if the MMU is ready to go */
4359 if (!vcpu->kvm->arch.mmu_ready) {
Paul Mackerras38c53af2017-11-08 14:44:04 +11004360 spin_unlock(&vc->lock);
Paul Mackerras432953b2017-11-09 15:37:10 +11004361 r = kvmhv_setup_mmu(vcpu);
Paul Mackerras38c53af2017-11-08 14:44:04 +11004362 spin_lock(&vc->lock);
4363 if (r) {
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004364 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4365 run->fail_entry.
Paul Mackerras432953b2017-11-09 15:37:10 +11004366 hardware_entry_failure_reason = 0;
Paul Mackerras38c53af2017-11-08 14:44:04 +11004367 vcpu->arch.ret = r;
4368 break;
4369 }
4370 }
4371
Paul Mackerrasec257162015-06-24 21:18:03 +10004372 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4373 kvmppc_vcore_end_preempt(vc);
4374
Paul Mackerras8455d792012-10-15 01:17:42 +00004375 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10004376 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004377 continue;
4378 }
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004379 for_each_runnable_thread(i, v, vc) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06004380 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004381 if (signal_pending(v->arch.run_task)) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004382 kvmppc_remove_runnable(vc, v, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10004383 v->stat.signal_exits++;
Tianjia Zhang2610a572020-04-27 12:35:10 +08004384 v->run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras19ccb762011-07-23 17:42:46 +10004385 v->arch.ret = -EINTR;
4386 wake_up(&v->arch.cpu_run);
4387 }
4388 }
Paul Mackerras8455d792012-10-15 01:17:42 +00004389 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
4390 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00004391 n_ceded = 0;
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004392 for_each_runnable_thread(i, v, vc) {
Paul Mackerras1da4e2f2017-05-19 16:26:16 +10004393 if (!kvmppc_vcpu_woken(v))
Paul Mackerras8455d792012-10-15 01:17:42 +00004394 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00004395 else
4396 v->arch.ceded = 0;
4397 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004398 vc->runner = vcpu;
4399 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00004400 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03004401 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10004402 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004403 /* Let something else run */
4404 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10004405 if (vc->vcore_state == VCORE_PREEMPT)
4406 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004407 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00004408 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11004409 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00004410 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00004411 }
4412
Paul Mackerras8455d792012-10-15 01:17:42 +00004413 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
4414 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10004415 vc->vcore_state == VCORE_EXITING ||
4416 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10004417 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00004418
Paul Mackerras5fc3e642015-09-18 13:13:44 +10004419 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
4420 kvmppc_vcore_end_preempt(vc);
4421
Paul Mackerras8455d792012-10-15 01:17:42 +00004422 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004423 kvmppc_remove_runnable(vc, vcpu, mftb());
Paul Mackerras8455d792012-10-15 01:17:42 +00004424 vcpu->stat.signal_exits++;
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004425 run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras8455d792012-10-15 01:17:42 +00004426 vcpu->arch.ret = -EINTR;
4427 }
4428
4429 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
4430 /* Wake up some vcpu to run the core */
Suraj Jitindar Singh7b5f8272016-08-02 14:03:20 +10004431 i = -1;
4432 v = next_runnable_thread(vc, &i);
Paul Mackerras8455d792012-10-15 01:17:42 +00004433 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004434 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00004435
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004436 trace_kvmppc_run_vcpu_exit(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10004437 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00004438 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00004439}
4440
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004441int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit,
Paul Mackerras360cae32018-10-08 16:31:04 +11004442 unsigned long lpcr)
Paul Mackerras95a64322018-10-08 16:30:55 +11004443{
Sean Christopherson63fa47b2021-12-13 17:45:56 +00004444 struct rcuwait *wait = kvm_arch_vcpu_get_wait(vcpu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004445 struct kvm_run *run = vcpu->run;
Suraj Jitindar Singh9d0b0482018-10-08 16:31:11 +11004446 int trap, r, pcpu;
Nicholas Piggin48013cb2021-05-28 19:07:32 +10004447 int srcu_idx;
Paul Mackerras95a64322018-10-08 16:30:55 +11004448 struct kvmppc_vcore *vc;
4449 struct kvm *kvm = vcpu->kvm;
Paul Mackerras360cae32018-10-08 16:31:04 +11004450 struct kvm_nested_guest *nested = vcpu->arch.nested;
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004451 unsigned long flags;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004452 u64 tb;
Paul Mackerras95a64322018-10-08 16:30:55 +11004453
4454 trace_kvmppc_run_vcpu_enter(vcpu);
4455
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004456 run->exit_reason = 0;
Paul Mackerras95a64322018-10-08 16:30:55 +11004457 vcpu->arch.ret = RESUME_GUEST;
4458 vcpu->arch.trap = 0;
4459
4460 vc = vcpu->arch.vcore;
4461 vcpu->arch.ceded = 0;
4462 vcpu->arch.run_task = current;
Paul Mackerras95a64322018-10-08 16:30:55 +11004463 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerras95a64322018-10-08 16:30:55 +11004464 vcpu->arch.last_inst = KVM_INST_FETCH_FAILED;
Paul Mackerras95a64322018-10-08 16:30:55 +11004465
4466 /* See if the MMU is ready to go */
Nicholas Piggin4c9a6892021-11-23 19:52:26 +10004467 if (unlikely(!kvm->arch.mmu_ready)) {
Nicholas Piggin079a09a2021-05-28 19:07:50 +10004468 r = kvmhv_setup_mmu(vcpu);
4469 if (r) {
4470 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4471 run->fail_entry.hardware_entry_failure_reason = 0;
4472 vcpu->arch.ret = r;
4473 return r;
4474 }
4475 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004476
4477 if (need_resched())
4478 cond_resched();
4479
4480 kvmppc_update_vpas(vcpu);
4481
Paul Mackerras95a64322018-10-08 16:30:55 +11004482 preempt_disable();
4483 pcpu = smp_processor_id();
Nicholas Piggin079a09a2021-05-28 19:07:50 +10004484 if (kvm_is_radix(kvm))
4485 kvmppc_prepare_radix_vcpu(vcpu, pcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11004486
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004487 /* flags save not required, but irq_pmu has no disable/enable API */
4488 powerpc_local_irq_pmu_save(flags);
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004489
Paul Mackerras95a64322018-10-08 16:30:55 +11004490 if (signal_pending(current))
4491 goto sigpend;
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004492 if (need_resched() || !kvm->arch.mmu_ready)
Paul Mackerras95a64322018-10-08 16:30:55 +11004493 goto out;
4494
Paul Mackerras360cae32018-10-08 16:31:04 +11004495 if (!nested) {
4496 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11004497 if (test_bit(BOOK3S_IRQPRIO_EXTERNAL,
4498 &vcpu->arch.pending_exceptions))
4499 lpcr |= LPCR_MER;
4500 } else if (vcpu->arch.pending_exceptions ||
4501 vcpu->arch.doorbell_request ||
4502 xive_interrupt_pending(vcpu)) {
4503 vcpu->arch.ret = RESUME_HOST;
4504 goto out;
4505 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004506
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004507 if (vcpu->arch.timer_running) {
4508 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
4509 vcpu->arch.timer_running = 0;
4510 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004511
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004512 tb = mftb();
4513
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004514 vcpu->cpu = pcpu;
4515 vcpu->arch.thread_cpu = pcpu;
4516 vc->pcpu = pcpu;
4517 local_paca->kvm_hstate.kvm_vcpu = vcpu;
4518 local_paca->kvm_hstate.ptid = 0;
4519 local_paca->kvm_hstate.fake_suspend = 0;
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004520
Nicholas Piggin617326f2021-11-23 19:52:29 +10004521 __kvmppc_create_dtl_entry(vcpu, pcpu, tb + vc->tb_offset, 0);
Paul Mackerras95a64322018-10-08 16:30:55 +11004522
Paul Mackerras95a64322018-10-08 16:30:55 +11004523 trace_kvm_guest_enter(vcpu);
4524
Paul Mackerras95a64322018-10-08 16:30:55 +11004525 guest_enter_irqoff();
4526
4527 srcu_idx = srcu_read_lock(&kvm->srcu);
4528
4529 this_cpu_disable_ftrace();
4530
Paul Mackerras1b28d552019-05-28 15:01:59 +10004531 /* Tell lockdep that we're about to enable interrupts */
4532 trace_hardirqs_on();
4533
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004534 trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr, &tb);
Paul Mackerras95a64322018-10-08 16:30:55 +11004535 vcpu->arch.trap = trap;
4536
Paul Mackerras1b28d552019-05-28 15:01:59 +10004537 trace_hardirqs_off();
4538
Paul Mackerras95a64322018-10-08 16:30:55 +11004539 this_cpu_enable_ftrace();
4540
4541 srcu_read_unlock(&kvm->srcu, srcu_idx);
4542
Paul Mackerras95a64322018-10-08 16:30:55 +11004543 set_irq_happened(trap);
4544
Laurent Vivier235cee12021-10-28 00:21:50 +10004545 context_tracking_guest_exit();
4546 if (!vtime_accounting_enabled_this_cpu()) {
4547 local_irq_enable();
4548 /*
4549 * Service IRQs here before vtime_account_guest_exit() so any
4550 * ticks that occurred while running the guest are accounted to
4551 * the guest. If vtime accounting is enabled, accounting uses
4552 * TB rather than ticks, so it can be done without enabling
4553 * interrupts here, which has the problem that it accounts
4554 * interrupt processing overhead to the host.
4555 */
4556 local_irq_disable();
4557 }
4558 vtime_account_guest_exit();
Nicholas Piggin11266522021-01-30 23:08:12 +10004559
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004560 vcpu->cpu = -1;
4561 vcpu->arch.thread_cpu = -1;
Paul Mackerras95a64322018-10-08 16:30:55 +11004562
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004563 powerpc_local_irq_pmu_restore(flags);
Paul Mackerras95a64322018-10-08 16:30:55 +11004564
4565 preempt_enable();
4566
Suraj Jitindar Singh3c25ab32019-06-20 11:46:51 +10004567 /*
4568 * cancel pending decrementer exception if DEC is now positive, or if
4569 * entering a nested guest in which case the decrementer is now owned
4570 * by L2 and the L1 decrementer is provided in hdec_expires
4571 */
4572 if (kvmppc_core_pending_dec(vcpu) &&
Nicholas Piggincb2553a2021-11-23 19:52:02 +10004573 ((tb < kvmppc_dec_expires_host_tb(vcpu)) ||
Suraj Jitindar Singh3c25ab32019-06-20 11:46:51 +10004574 (trap == BOOK3S_INTERRUPT_SYSCALL &&
4575 kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED)))
Paul Mackerras95a64322018-10-08 16:30:55 +11004576 kvmppc_core_dequeue_dec(vcpu);
4577
4578 trace_kvm_guest_exit(vcpu);
4579 r = RESUME_GUEST;
Paul Mackerras360cae32018-10-08 16:31:04 +11004580 if (trap) {
4581 if (!nested)
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004582 r = kvmppc_handle_exit_hv(vcpu, current);
Paul Mackerras360cae32018-10-08 16:31:04 +11004583 else
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004584 r = kvmppc_handle_nested_exit(vcpu);
Paul Mackerras360cae32018-10-08 16:31:04 +11004585 }
Paul Mackerras95a64322018-10-08 16:30:55 +11004586 vcpu->arch.ret = r;
4587
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004588 if (is_kvmppc_resume_guest(r) && !kvmppc_vcpu_check_block(vcpu)) {
Paul Mackerras95a64322018-10-08 16:30:55 +11004589 kvmppc_set_timer(vcpu);
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004590
Sean Christopherson63fa47b2021-12-13 17:45:56 +00004591 prepare_to_rcuwait(wait);
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004592 for (;;) {
4593 set_current_state(TASK_INTERRUPTIBLE);
Paul Mackerras95a64322018-10-08 16:30:55 +11004594 if (signal_pending(current)) {
4595 vcpu->stat.signal_exits++;
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004596 run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras95a64322018-10-08 16:30:55 +11004597 vcpu->arch.ret = -EINTR;
4598 break;
4599 }
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004600
4601 if (kvmppc_vcpu_check_block(vcpu))
4602 break;
4603
4604 trace_kvmppc_vcore_blocked(vc, 0);
4605 schedule();
4606 trace_kvmppc_vcore_blocked(vc, 1);
Paul Mackerras95a64322018-10-08 16:30:55 +11004607 }
Sean Christopherson63fa47b2021-12-13 17:45:56 +00004608 finish_rcuwait(wait);
Paul Mackerras95a64322018-10-08 16:30:55 +11004609 }
4610 vcpu->arch.ceded = 0;
4611
Paul Mackerras95a64322018-10-08 16:30:55 +11004612 done:
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004613 trace_kvmppc_run_vcpu_exit(vcpu);
Paul Mackerras95a64322018-10-08 16:30:55 +11004614
4615 return vcpu->arch.ret;
4616
4617 sigpend:
4618 vcpu->stat.signal_exits++;
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004619 run->exit_reason = KVM_EXIT_INTR;
Paul Mackerras95a64322018-10-08 16:30:55 +11004620 vcpu->arch.ret = -EINTR;
4621 out:
Nicholas Piggin2251fbe2021-11-23 19:51:58 +10004622 powerpc_local_irq_pmu_restore(flags);
Paul Mackerras95a64322018-10-08 16:30:55 +11004623 preempt_enable();
4624 goto done;
4625}
4626
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004627static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00004628{
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004629 struct kvm_run *run = vcpu->run;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00004630 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00004631 int srcu_idx;
Paul Mackerras1b151ce2017-09-13 15:53:48 +10004632 struct kvm *kvm;
Nicholas Piggin34e119c92021-11-23 19:51:57 +10004633 unsigned long msr;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00004634
Alexander Grafaf8f38b2011-08-10 13:57:08 +02004635 if (!vcpu->arch.sane) {
4636 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
4637 return -EINVAL;
4638 }
4639
Nicholas Piggin34e119c92021-11-23 19:51:57 +10004640 /* No need to go into the guest when all we'll do is come back out */
4641 if (signal_pending(current)) {
4642 run->exit_reason = KVM_EXIT_INTR;
4643 return -EINTR;
4644 }
4645
4646#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
Paul Mackerras46a704f2017-06-15 16:10:27 +10004647 /*
4648 * Don't allow entry with a suspended transaction, because
4649 * the guest entry/exit code will lose it.
Paul Mackerras46a704f2017-06-15 16:10:27 +10004650 */
Paul Mackerras46a704f2017-06-15 16:10:27 +10004651 if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs &&
4652 (current->thread.regs->msr & MSR_TM)) {
4653 if (MSR_TM_ACTIVE(current->thread.regs->msr)) {
4654 run->exit_reason = KVM_EXIT_FAIL_ENTRY;
4655 run->fail_entry.hardware_entry_failure_reason = 0;
4656 return -EINVAL;
4657 }
Paul Mackerras46a704f2017-06-15 16:10:27 +10004658 }
4659#endif
4660
Paul Mackerras7aa15842018-04-20 19:53:22 +10004661 /*
4662 * Force online to 1 for the sake of old userspace which doesn't
4663 * set it.
4664 */
4665 if (!vcpu->arch.online) {
4666 atomic_inc(&vcpu->arch.vcore->online_count);
4667 vcpu->arch.online = 1;
4668 }
4669
Scott Wood25051b5a2011-11-08 18:23:23 -06004670 kvmppc_core_prepare_to_enter(vcpu);
4671
Paul Mackerras1b151ce2017-09-13 15:53:48 +10004672 kvm = vcpu->kvm;
4673 atomic_inc(&kvm->arch.vcpus_running);
4674 /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00004675 smp_mb();
4676
Nicholas Piggin34e119c92021-11-23 19:51:57 +10004677 msr = 0;
4678 if (IS_ENABLED(CONFIG_PPC_FPU))
4679 msr |= MSR_FP;
4680 if (cpu_has_feature(CPU_FTR_ALTIVEC))
4681 msr |= MSR_VEC;
4682 if (cpu_has_feature(CPU_FTR_VSX))
4683 msr |= MSR_VSX;
Nicholas Piggin022ecb92021-11-23 19:52:15 +10004684 if ((cpu_has_feature(CPU_FTR_TM) ||
4685 cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) &&
4686 (vcpu->arch.hfscr & HFSCR_TM))
Nicholas Piggin34e119c92021-11-23 19:51:57 +10004687 msr |= MSR_TM;
4688 msr = msr_check_and_set(msr);
Anton Blanchard579e6332015-10-29 11:44:09 +11004689
Nicholas Piggin34e119c92021-11-23 19:51:57 +10004690 kvmppc_save_user_regs();
Paul Mackerrasca8efa12017-06-06 16:47:22 +10004691
Nicholas Piggin52367562021-11-23 19:52:16 +10004692 kvmppc_save_current_sprs();
Paul Mackerras19ccb762011-07-23 17:42:46 +10004693
Nicholas Pigginecb6a722021-11-23 19:52:28 +10004694 if (!cpu_has_feature(CPU_FTR_ARCH_300))
4695 vcpu->arch.waitp = &vcpu->arch.vcore->wait;
Leonardo Bras8a9c8922019-11-26 19:36:30 -03004696 vcpu->arch.pgdir = kvm->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00004697 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10004698
Paul Mackerrasa8606e22011-06-29 00:22:05 +00004699 do {
Nicholas Piggin0bf7e1b2021-05-28 19:07:51 +10004700 if (cpu_has_feature(CPU_FTR_ARCH_300))
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004701 r = kvmhv_run_single_vcpu(vcpu, ~(u64)0,
Paul Mackerras360cae32018-10-08 16:31:04 +11004702 vcpu->arch.vcore->lpcr);
Paul Mackerras95a64322018-10-08 16:30:55 +11004703 else
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004704 r = kvmppc_run_vcpu(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00004705
Nicholas Piggin9dc2bab2021-05-28 19:07:33 +10004706 if (run->exit_reason == KVM_EXIT_PAPR_HCALL) {
4707 if (WARN_ON_ONCE(vcpu->arch.shregs.msr & MSR_PR)) {
4708 /*
4709 * These should have been caught reflected
4710 * into the guest by now. Final sanity check:
4711 * don't allow userspace to execute hcalls in
4712 * the hypervisor.
4713 */
4714 r = RESUME_GUEST;
4715 continue;
4716 }
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004717 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00004718 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06004719 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06004720 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00004721 } else if (r == RESUME_PAGE_FAULT) {
Paul Mackerras432953b2017-11-09 15:37:10 +11004722 srcu_idx = srcu_read_lock(&kvm->srcu);
Tianjia Zhang8c99d342020-04-27 12:35:11 +08004723 r = kvmppc_book3s_hv_page_fault(vcpu,
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00004724 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
Paul Mackerras432953b2017-11-09 15:37:10 +11004725 srcu_read_unlock(&kvm->srcu, srcu_idx);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004726 } else if (r == RESUME_PASSTHROUGH) {
Paul Mackerras03f95332019-02-04 22:07:20 +11004727 if (WARN_ON(xics_on_xive()))
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10004728 r = H_SUCCESS;
4729 else
4730 r = kvmppc_xics_rm_complete(vcpu, 0);
4731 }
Greg Kurze59d24e2014-02-06 17:36:56 +01004732 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00004733
Paul Mackerrasc7b67672012-10-15 01:18:07 +00004734 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras432953b2017-11-09 15:37:10 +11004735 atomic_dec(&kvm->arch.vcpus_running);
Nicholas Piggin59dc5bf2021-06-18 01:51:03 +10004736
4737 srr_regs_clobbered();
4738
Paul Mackerrasa8606e22011-06-29 00:22:05 +00004739 return r;
4740}
4741
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00004742static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10004743 int shift, int sllp)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00004744{
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10004745 (*sps)->page_shift = shift;
4746 (*sps)->slb_enc = sllp;
4747 (*sps)->enc[0].page_shift = shift;
4748 (*sps)->enc[0].pte_enc = kvmppc_pgsize_lp_encoding(shift, shift);
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05304749 /*
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10004750 * Add 16MB MPSS support (may get filtered out by userspace)
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05304751 */
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10004752 if (shift != 24) {
4753 int penc = kvmppc_pgsize_lp_encoding(shift, 24);
4754 if (penc != -1) {
4755 (*sps)->enc[1].page_shift = 24;
4756 (*sps)->enc[1].pte_enc = penc;
4757 }
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05304758 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00004759 (*sps)++;
4760}
4761
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304762static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
4763 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00004764{
4765 struct kvm_ppc_one_seg_page_size *sps;
4766
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11004767 /*
Paul Mackerrase3bfed12017-08-25 19:53:39 +10004768 * POWER7, POWER8 and POWER9 all support 32 storage keys for data.
4769 * POWER7 doesn't support keys for instruction accesses,
4770 * POWER8 and POWER9 do.
4771 */
4772 info->data_keys = 32;
4773 info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0;
4774
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10004775 /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */
4776 info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS;
4777 info->slb_size = 32;
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00004778
4779 /* We only support these sizes for now, and no muti-size segments */
4780 sps = &info->sps[0];
Paul Mackerras8dc6cca2017-09-11 15:29:45 +10004781 kvmppc_add_seg_page_size(&sps, 12, 0);
4782 kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01);
4783 kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L);
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00004784
Paul Mackerras901f8c32018-10-08 14:24:30 +11004785 /* If running as a nested hypervisor, we don't support HPT guests */
4786 if (kvmhv_on_pseries())
4787 info->flags |= KVM_PPC_NO_HASH;
4788
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00004789 return 0;
4790}
4791
Paul Mackerras82ed3612011-12-15 02:03:22 +00004792/*
4793 * Get (and clear) the dirty memory log for a memory slot.
4794 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304795static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
4796 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00004797{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02004798 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00004799 struct kvm_memory_slot *memslot;
Marc Zyngier46808a42021-11-16 16:04:02 +00004800 int r;
4801 unsigned long n, i;
Paul Mackerrase641a312017-10-26 16:39:19 +11004802 unsigned long *buf, *p;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11004803 struct kvm_vcpu *vcpu;
Paul Mackerras82ed3612011-12-15 02:03:22 +00004804
4805 mutex_lock(&kvm->slots_lock);
4806
4807 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07004808 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00004809 goto out;
4810
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02004811 slots = kvm_memslots(kvm);
4812 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00004813 r = -ENOENT;
Sean Christopherson0577d1a2020-02-18 13:07:31 -08004814 if (!memslot || !memslot->dirty_bitmap)
Paul Mackerras82ed3612011-12-15 02:03:22 +00004815 goto out;
4816
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11004817 /*
Paul Mackerrase641a312017-10-26 16:39:19 +11004818 * Use second half of bitmap area because both HPT and radix
4819 * accumulate bits in the first half.
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11004820 */
Paul Mackerras82ed3612011-12-15 02:03:22 +00004821 n = kvm_dirty_bitmap_bytes(memslot);
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11004822 buf = memslot->dirty_bitmap + n / sizeof(long);
4823 memset(buf, 0, n);
Paul Mackerras82ed3612011-12-15 02:03:22 +00004824
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11004825 if (kvm_is_radix(kvm))
4826 r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf);
4827 else
4828 r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf);
Paul Mackerras82ed3612011-12-15 02:03:22 +00004829 if (r)
4830 goto out;
4831
Paul Mackerrase641a312017-10-26 16:39:19 +11004832 /*
4833 * We accumulate dirty bits in the first half of the
4834 * memslot's dirty_bitmap area, for when pages are paged
4835 * out or modified by the host directly. Pick up these
4836 * bits and add them to the map.
4837 */
4838 p = memslot->dirty_bitmap;
4839 for (i = 0; i < n / sizeof(long); ++i)
4840 buf[i] |= xchg(&p[i], 0);
4841
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11004842 /* Harvest dirty bits from VPA and DTL updates */
4843 /* Note: we never modify the SLB shadow buffer areas */
4844 kvm_for_each_vcpu(i, vcpu, kvm) {
4845 spin_lock(&vcpu->arch.vpa_update_lock);
4846 kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf);
4847 kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf);
4848 spin_unlock(&vcpu->arch.vpa_update_lock);
4849 }
4850
Paul Mackerras82ed3612011-12-15 02:03:22 +00004851 r = -EFAULT;
Paul Mackerras8f7b79b2017-01-30 21:21:48 +11004852 if (copy_to_user(log->dirty_bitmap, buf, n))
Paul Mackerras82ed3612011-12-15 02:03:22 +00004853 goto out;
4854
4855 r = 0;
4856out:
4857 mutex_unlock(&kvm->slots_lock);
4858 return r;
4859}
4860
Sean Christophersone96c81e2020-02-18 13:07:27 -08004861static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00004862{
Sean Christophersone96c81e2020-02-18 13:07:27 -08004863 vfree(slot->arch.rmap);
4864 slot->arch.rmap = NULL;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00004865}
4866
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304867static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
Sean Christopherson537a17b2021-12-06 20:54:11 +01004868 const struct kvm_memory_slot *old,
4869 struct kvm_memory_slot *new,
4870 enum kvm_mr_change change)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00004871{
Sean Christopherson82307e62020-02-18 13:07:18 -08004872 if (change == KVM_MR_CREATE) {
Paolo Bonzini5a213b92021-12-19 15:27:21 +01004873 unsigned long size = array_size(new->npages, sizeof(*new->arch.rmap));
Alexey Kardashevskiy511d25d2021-09-01 18:45:12 +10004874
4875 if ((size >> PAGE_SHIFT) > totalram_pages())
4876 return -ENOMEM;
4877
Paolo Bonzini5a213b92021-12-19 15:27:21 +01004878 new->arch.rmap = vzalloc(size);
Sean Christopherson537a17b2021-12-06 20:54:11 +01004879 if (!new->arch.rmap)
Sean Christopherson82307e62020-02-18 13:07:18 -08004880 return -ENOMEM;
Sean Christopherson537a17b2021-12-06 20:54:11 +01004881 } else if (change != KVM_MR_DELETE) {
4882 new->arch.rmap = old->arch.rmap;
Sean Christopherson82307e62020-02-18 13:07:18 -08004883 }
4884
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00004885 return 0;
4886}
4887
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05304888static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Sean Christopherson537a17b2021-12-06 20:54:11 +01004889 struct kvm_memory_slot *old,
Bharata B Raof032b732018-12-12 15:15:30 +11004890 const struct kvm_memory_slot *new,
4891 enum kvm_mr_change change)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00004892{
Yongji Xiea56ee9f2016-11-04 13:55:12 +08004893 /*
Sean Christophersoneaaaed12021-12-06 20:54:14 +01004894 * If we are creating or modifying a memslot, it might make
Yongji Xiea56ee9f2016-11-04 13:55:12 +08004895 * some address that was previously cached as emulated
4896 * MMIO be no longer emulated MMIO, so invalidate
4897 * all the caches of emulated MMIO translations.
4898 */
Sean Christophersoneaaaed12021-12-06 20:54:14 +01004899 if (change != KVM_MR_DELETE)
Yongji Xiea56ee9f2016-11-04 13:55:12 +08004900 atomic64_inc(&kvm->arch.mmio_update);
Paul Mackerras5af3e9d2018-12-12 15:17:17 +11004901
4902 /*
4903 * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels
4904 * have already called kvm_arch_flush_shadow_memslot() to
4905 * flush shadow mappings. For KVM_MR_CREATE we have no
4906 * previous mappings. So the only case to handle is
4907 * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit
4908 * has been changed.
4909 * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES
4910 * to get rid of any THP PTEs in the partition-scoped page tables
4911 * so we can track dirtiness at the page level; we flush when
4912 * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to
4913 * using THP PTEs.
4914 */
4915 if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) &&
4916 ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES))
4917 kvmppc_radix_flush_memslot(kvm, old);
Bharata B Raoc3262252019-11-25 08:36:29 +05304918 /*
4919 * If UV hasn't yet called H_SVM_INIT_START, don't register memslots.
4920 */
4921 if (!kvm->arch.secure_guest)
4922 return;
4923
4924 switch (change) {
4925 case KVM_MR_CREATE:
Laurent Dufoura2ce7202020-07-27 11:07:18 -07004926 /*
4927 * @TODO kvmppc_uvmem_memslot_create() can fail and
4928 * return error. Fix this.
4929 */
4930 kvmppc_uvmem_memslot_create(kvm, new);
Bharata B Raoc3262252019-11-25 08:36:29 +05304931 break;
4932 case KVM_MR_DELETE:
Laurent Dufoura2ce7202020-07-27 11:07:18 -07004933 kvmppc_uvmem_memslot_delete(kvm, old);
Bharata B Raoc3262252019-11-25 08:36:29 +05304934 break;
4935 default:
4936 /* TODO: Handle KVM_MR_MOVE */
4937 break;
4938 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00004939}
4940
Paul Mackerrasa0144e22013-09-20 14:52:38 +10004941/*
4942 * Update LPCR values in kvm->arch and in vcores.
Paul Mackerras0d4ee882019-05-23 16:35:34 +10004943 * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion
4944 * of kvm->arch.lpcr update).
Paul Mackerrasa0144e22013-09-20 14:52:38 +10004945 */
4946void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
4947{
4948 long int i;
4949 u32 cores_done = 0;
4950
4951 if ((kvm->arch.lpcr & mask) == lpcr)
4952 return;
4953
4954 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
4955
4956 for (i = 0; i < KVM_MAX_VCORES; ++i) {
4957 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
4958 if (!vc)
4959 continue;
Nicholas Piggin67145ef2021-04-12 11:48:36 +10004960
Paul Mackerrasa0144e22013-09-20 14:52:38 +10004961 spin_lock(&vc->lock);
4962 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
Nicholas Piggin67145ef2021-04-12 11:48:36 +10004963 verify_lpcr(kvm, vc->lpcr);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10004964 spin_unlock(&vc->lock);
4965 if (++cores_done >= kvm->arch.online_vcores)
4966 break;
4967 }
4968}
4969
Paul Mackerrasded13fc2017-11-22 14:38:53 +11004970void kvmppc_setup_partition_table(struct kvm *kvm)
Paul Mackerras7a840842016-11-16 22:25:20 +11004971{
4972 unsigned long dw0, dw1;
4973
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11004974 if (!kvm_is_radix(kvm)) {
4975 /* PS field - page size for VRMA */
4976 dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) |
4977 ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1);
4978 /* HTABSIZE and HTABORG fields */
4979 dw0 |= kvm->arch.sdr1;
Paul Mackerras7a840842016-11-16 22:25:20 +11004980
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11004981 /* Second dword as set by userspace */
4982 dw1 = kvm->arch.process_table;
4983 } else {
4984 dw0 = PATB_HR | radix__get_tree_size() |
4985 __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE;
4986 dw1 = PATB_GR | kvm->arch.process_table;
4987 }
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11004988 kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1);
Paul Mackerras7a840842016-11-16 22:25:20 +11004989}
4990
Paul Mackerras1b151ce2017-09-13 15:53:48 +10004991/*
4992 * Set up HPT (hashed page table) and RMA (real-mode area).
Paul Mackerras0d4ee882019-05-23 16:35:34 +10004993 * Must be called with kvm->arch.mmu_setup_lock held.
Paul Mackerras1b151ce2017-09-13 15:53:48 +10004994 */
Paul Mackerras32fad282012-05-04 02:32:53 +00004995static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00004996{
4997 int err = 0;
4998 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00004999 unsigned long hva;
5000 struct kvm_memory_slot *memslot;
5001 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005002 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005003 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00005004 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005005
Paul Mackerras32fad282012-05-04 02:32:53 +00005006 /* Allocate hashed page table (if not done already) and reset it */
David Gibson3f9d4f52016-12-20 16:49:00 +11005007 if (!kvm->arch.hpt.virt) {
David Gibsonaae07772016-12-20 16:49:02 +11005008 int order = KVM_DEFAULT_HPT_ORDER;
5009 struct kvm_hpt_info info;
5010
5011 err = kvmppc_allocate_hpt(&info, order);
5012 /* If we get here, it means userspace didn't specify a
5013 * size explicitly. So, try successively smaller
5014 * sizes if the default failed. */
5015 while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER)
5016 err = kvmppc_allocate_hpt(&info, order);
5017
5018 if (err < 0) {
Paul Mackerras32fad282012-05-04 02:32:53 +00005019 pr_err("KVM: Couldn't alloc HPT\n");
5020 goto out;
5021 }
David Gibsonaae07772016-12-20 16:49:02 +11005022
5023 kvmppc_set_hpt(kvm, &info);
Paul Mackerras32fad282012-05-04 02:32:53 +00005024 }
5025
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005026 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00005027 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005028 memslot = gfn_to_memslot(kvm, 0);
5029
5030 /* We must have some memory at 0 by now */
5031 err = -EINVAL;
5032 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00005033 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005034
5035 /* Look up the VMA for the start of this memory slot */
5036 hva = memslot->userspace_addr;
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005037 mmap_read_lock(kvm->mm);
Liam Howlett900c83f2021-06-28 19:39:05 -07005038 vma = vma_lookup(kvm->mm, hva);
5039 if (!vma || (vma->vm_flags & VM_IO))
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005040 goto up_out;
5041
5042 psize = vma_kernel_pagesize(vma);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005043
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005044 mmap_read_unlock(kvm->mm);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005045
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005046 /* We can handle 4k, 64k or 16M pages in the VRMA */
Paul Mackerrasdebd5742018-03-02 15:38:04 +11005047 if (psize >= 0x1000000)
5048 psize = 0x1000000;
5049 else if (psize >= 0x10000)
5050 psize = 0x10000;
5051 else
5052 psize = 0x1000;
5053 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005054
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005055 senc = slb_pgsize_encoding(psize);
5056 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
5057 (VRMA_VSID << SLB_VSID_SHIFT_1T);
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005058 /* Create HPTEs in the hash page table for the VRMA */
5059 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005060
Paul Mackerras7a840842016-11-16 22:25:20 +11005061 /* Update VRMASD field in the LPCR */
5062 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
5063 /* the -4 is to account for senc values starting at 0x10 */
5064 lpcr = senc << (LPCR_VRMASD_SH - 4);
5065 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerras7a840842016-11-16 22:25:20 +11005066 }
Paul Mackerrasa0144e22013-09-20 14:52:38 +10005067
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005068 /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005069 smp_wmb();
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005070 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00005071 out_srcu:
5072 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005073 out:
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005074 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005075
Paul Mackerrasc77162d2011-12-12 12:31:00 +00005076 up_out:
Michel Lespinassed8ed45c2020-06-08 21:33:25 -07005077 mmap_read_unlock(kvm->mm);
Lai Jiangshan505d6422013-03-16 00:50:49 +08005078 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00005079}
5080
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005081/*
5082 * Must be called with kvm->arch.mmu_setup_lock held and
5083 * mmu_ready = 0 and no vcpus running.
5084 */
Paul Mackerras18c36402017-09-13 16:00:10 +10005085int kvmppc_switch_mmu_to_hpt(struct kvm *kvm)
5086{
Nicholas Piggineacc8182021-11-23 19:51:46 +10005087 unsigned long lpcr, lpcr_mask;
5088
Paul Mackerrasaa069a92018-09-21 20:02:01 +10005089 if (nesting_enabled(kvm))
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005090 kvmhv_release_all_nested(kvm);
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005091 kvmppc_rmap_reset(kvm);
5092 kvm->arch.process_table = 0;
Sean Christophersonb1c53562021-04-01 17:56:53 -07005093 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005094 spin_lock(&kvm->mmu_lock);
5095 kvm->arch.radix = 0;
5096 spin_unlock(&kvm->mmu_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10005097 kvmppc_free_radix(kvm);
Nicholas Piggineacc8182021-11-23 19:51:46 +10005098
5099 lpcr = LPCR_VPM1;
5100 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5101 if (cpu_has_feature(CPU_FTR_ARCH_31))
5102 lpcr_mask |= LPCR_HAIL;
5103 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
5104
Paul Mackerras18c36402017-09-13 16:00:10 +10005105 return 0;
5106}
5107
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005108/*
5109 * Must be called with kvm->arch.mmu_setup_lock held and
5110 * mmu_ready = 0 and no vcpus running.
5111 */
Paul Mackerras18c36402017-09-13 16:00:10 +10005112int kvmppc_switch_mmu_to_radix(struct kvm *kvm)
5113{
Nicholas Piggineacc8182021-11-23 19:51:46 +10005114 unsigned long lpcr, lpcr_mask;
Paul Mackerras18c36402017-09-13 16:00:10 +10005115 int err;
5116
5117 err = kvmppc_init_vm_radix(kvm);
5118 if (err)
5119 return err;
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005120 kvmppc_rmap_reset(kvm);
Sean Christophersonb1c53562021-04-01 17:56:53 -07005121 /* Mutual exclusion with kvm_unmap_gfn_range etc. */
Paul Mackerras234ff0b2018-11-16 21:28:18 +11005122 spin_lock(&kvm->mmu_lock);
5123 kvm->arch.radix = 1;
5124 spin_unlock(&kvm->mmu_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10005125 kvmppc_free_hpt(&kvm->arch.hpt);
Nicholas Piggineacc8182021-11-23 19:51:46 +10005126
5127 lpcr = LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5128 lpcr_mask = LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR;
5129 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5130 lpcr_mask |= LPCR_HAIL;
5131 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5132 (kvm->arch.host_lpcr & LPCR_HAIL))
5133 lpcr |= LPCR_HAIL;
5134 }
5135 kvmppc_update_lpcr(kvm, lpcr, lpcr_mask);
5136
Paul Mackerras18c36402017-09-13 16:00:10 +10005137 return 0;
5138}
5139
Suresh Warrier79b6c242015-12-17 14:59:06 -06005140#ifdef CONFIG_KVM_XICS
5141/*
5142 * Allocate a per-core structure for managing state about which cores are
5143 * running in the host versus the guest and for exchanging data between
5144 * real mode KVM and CPU running in the host.
5145 * This is only done for the first VM.
5146 * The allocated structure stays even if all VMs have stopped.
5147 * It is only freed when the kvm-hv module is unloaded.
5148 * It's OK for this routine to fail, we just don't support host
5149 * core operations like redirecting H_IPI wakeups.
5150 */
5151void kvmppc_alloc_host_rm_ops(void)
5152{
5153 struct kvmppc_host_rm_ops *ops;
5154 unsigned long l_ops;
5155 int cpu, core;
5156 int size;
5157
Nicholas Pigginecb6a722021-11-23 19:52:28 +10005158 if (cpu_has_feature(CPU_FTR_ARCH_300))
5159 return;
5160
Suresh Warrier79b6c242015-12-17 14:59:06 -06005161 /* Not the first time here ? */
5162 if (kvmppc_host_rm_ops_hv != NULL)
5163 return;
5164
5165 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
5166 if (!ops)
5167 return;
5168
5169 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
5170 ops->rm_core = kzalloc(size, GFP_KERNEL);
5171
5172 if (!ops->rm_core) {
5173 kfree(ops);
5174 return;
5175 }
5176
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02005177 cpus_read_lock();
Suresh Warrier6f3bb802015-12-17 14:59:08 -06005178
Suresh Warrier79b6c242015-12-17 14:59:06 -06005179 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
5180 if (!cpu_online(cpu))
5181 continue;
5182
5183 core = cpu >> threads_shift;
5184 ops->rm_core[core].rm_state.in_host = 1;
5185 }
5186
Suresh Warrier0c2a6602015-12-17 14:59:09 -06005187 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
5188
Suresh Warrier79b6c242015-12-17 14:59:06 -06005189 /*
5190 * Make the contents of the kvmppc_host_rm_ops structure visible
5191 * to other CPUs before we assign it to the global variable.
5192 * Do an atomic assignment (no locks used here), but if someone
5193 * beats us to it, just free our copy and return.
5194 */
5195 smp_wmb();
5196 l_ops = (unsigned long) ops;
5197
5198 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02005199 cpus_read_unlock();
Suresh Warrier79b6c242015-12-17 14:59:06 -06005200 kfree(ops->rm_core);
5201 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06005202 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06005203 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06005204
Sebastian Andrzej Siewior419af25f2017-05-24 10:15:21 +02005205 cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE,
5206 "ppc/kvm_book3s:prepare",
5207 kvmppc_set_host_core,
5208 kvmppc_clear_host_core);
5209 cpus_read_unlock();
Suresh Warrier79b6c242015-12-17 14:59:06 -06005210}
5211
5212void kvmppc_free_host_rm_ops(void)
5213{
5214 if (kvmppc_host_rm_ops_hv) {
Anna-Maria Gleixner3f7cd912016-11-27 00:13:45 +01005215 cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE);
Suresh Warrier79b6c242015-12-17 14:59:06 -06005216 kfree(kvmppc_host_rm_ops_hv->rm_core);
5217 kfree(kvmppc_host_rm_ops_hv);
5218 kvmppc_host_rm_ops_hv = NULL;
5219 }
5220}
5221#endif
5222
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305223static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005224{
Paul Mackerras32fad282012-05-04 02:32:53 +00005225 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11005226 char buf[32];
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005227 int ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00005228
Bharata B Raoca9f4942019-11-25 08:36:26 +05305229 mutex_init(&kvm->arch.uvmem_lock);
5230 INIT_LIST_HEAD(&kvm->arch.uvmem_pfns);
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005231 mutex_init(&kvm->arch.mmu_setup_lock);
5232
Paul Mackerras32fad282012-05-04 02:32:53 +00005233 /* Allocate the guest's logical partition ID */
5234
5235 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08005236 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00005237 return -ENOMEM;
5238 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00005239
Suresh Warrier79b6c242015-12-17 14:59:06 -06005240 kvmppc_alloc_host_rm_ops();
5241
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005242 kvmhv_vm_nested_init(kvm);
5243
Paul Mackerras1b400ba2012-11-21 23:28:08 +00005244 /*
5245 * Since we don't flush the TLB when tearing down a VM,
5246 * and this lpid might have previously been used,
5247 * make sure we flush on each core before running the new VM.
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005248 * On POWER9, the tlbie in mmu_partition_table_set_entry()
5249 * does this flush for us.
Paul Mackerras1b400ba2012-11-21 23:28:08 +00005250 */
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005251 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5252 cpumask_setall(&kvm->arch.need_tlb_flush);
Paul Mackerras1b400ba2012-11-21 23:28:08 +00005253
Paul Mackerras699a0ea2014-06-02 11:02:59 +10005254 /* Start out with the default set of hcalls enabled */
5255 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
5256 sizeof(kvm->arch.enabled_hcalls));
5257
Paul Mackerras7a840842016-11-16 22:25:20 +11005258 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5259 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005260
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005261 /* Init LPCR for virtual RMA mode */
Paul Mackerrasf3c99f92018-10-08 16:31:12 +11005262 if (cpu_has_feature(CPU_FTR_HVMODE)) {
5263 kvm->arch.host_lpid = mfspr(SPRN_LPID);
5264 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
5265 lpcr &= LPCR_PECE | LPCR_LPES;
5266 } else {
5267 lpcr = 0;
5268 }
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11005269 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
5270 LPCR_VPM0 | LPCR_VPM1;
5271 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
5272 (VRMA_VSID << SLB_VSID_SHIFT_1T);
5273 /* On POWER8 turn on online bit to enable PURR/SPURR */
5274 if (cpu_has_feature(CPU_FTR_ARCH_207S))
5275 lpcr |= LPCR_ONL;
Paul Mackerras84f71392016-11-22 14:30:14 +11005276 /*
5277 * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed)
5278 * Set HVICE bit to enable hypervisor virtualization interrupts.
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005279 * Set HEIC to prevent OS interrupts to go to hypervisor (should
5280 * be unnecessary but better safe than sorry in case we re-enable
5281 * EE in HV mode with this LPCR still set)
Paul Mackerras84f71392016-11-22 14:30:14 +11005282 */
5283 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerras7a840842016-11-16 22:25:20 +11005284 lpcr &= ~LPCR_VPM0;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005285 lpcr |= LPCR_HVICE | LPCR_HEIC;
5286
5287 /*
5288 * If xive is enabled, we route 0x500 interrupts directly
5289 * to the guest.
5290 */
Paul Mackerras03f95332019-02-04 22:07:20 +11005291 if (xics_on_xive())
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005292 lpcr |= LPCR_LPES;
Paul Mackerras84f71392016-11-22 14:30:14 +11005293 }
5294
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005295 /*
Paul Mackerras18c36402017-09-13 16:00:10 +10005296 * If the host uses radix, the guest starts out as radix.
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005297 */
5298 if (radix_enabled()) {
5299 kvm->arch.radix = 1;
Paul Mackerras1b151ce2017-09-13 15:53:48 +10005300 kvm->arch.mmu_ready = 1;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005301 lpcr &= ~LPCR_VPM1;
5302 lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR;
Nicholas Piggineacc8182021-11-23 19:51:46 +10005303 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5304 cpu_has_feature(CPU_FTR_ARCH_31) &&
5305 (kvm->arch.host_lpcr & LPCR_HAIL))
5306 lpcr |= LPCR_HAIL;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005307 ret = kvmppc_init_vm_radix(kvm);
5308 if (ret) {
5309 kvmppc_free_lpid(kvm->arch.lpid);
5310 return ret;
5311 }
5312 kvmppc_setup_partition_table(kvm);
5313 }
5314
Nicholas Piggin67145ef2021-04-12 11:48:36 +10005315 verify_lpcr(kvm, lpcr);
Paul Mackerras9e368f22011-06-29 00:40:08 +00005316 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005317
David Gibson5e985962016-12-20 16:49:05 +11005318 /* Initialization for future HPT resizes */
5319 kvm->arch.resize_hpt = NULL;
5320
Paul Mackerras512691d2012-10-15 01:15:41 +00005321 /*
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005322 * Work out how many sets the TLB has, for the use of
5323 * the TLB invalidation loop in book3s_hv_rmhandlers.S.
5324 */
Aneesh Kumar K.Ve8063942020-10-07 11:03:05 +05305325 if (cpu_has_feature(CPU_FTR_ARCH_31)) {
5326 /*
5327 * P10 will flush all the congruence class with a single tlbiel
5328 */
5329 kvm->arch.tlb_sets = 1;
5330 } else if (radix_enabled())
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005331 kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */
5332 else if (cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras7c5b06c2016-11-18 08:28:51 +11005333 kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */
5334 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
5335 kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */
5336 else
5337 kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */
5338
5339 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10005340 * Track that we now have a HV mode VM active. This blocks secondary
5341 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00005342 */
Nicholas Pigginaaae8c72021-05-28 19:07:42 +10005343 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005344 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00005345
Paul Mackerrase23a8082015-03-28 14:21:01 +11005346 /*
Paul Mackerras3c313522017-02-06 13:24:41 +11005347 * Initialize smt_mode depending on processor.
5348 * POWER8 and earlier have to use "strict" threading, where
5349 * all vCPUs in a vcore have to run on the same (sub)core,
5350 * whereas on POWER9 the threads can each run a different
5351 * guest.
5352 */
5353 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5354 kvm->arch.smt_mode = threads_per_subcore;
5355 else
5356 kvm->arch.smt_mode = 1;
Paul Mackerras57900692017-05-16 16:41:20 +10005357 kvm->arch.emul_smt_mode = 1;
Paul Mackerras3c313522017-02-06 13:24:41 +11005358
5359 /*
Paul Mackerrase23a8082015-03-28 14:21:01 +11005360 * Create a debugfs directory for the VM
5361 */
5362 snprintf(buf, sizeof(buf), "vm%d", current->pid);
5363 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
Greg Kroah-Hartman929f45e2018-05-29 18:22:04 +02005364 kvmppc_mmu_debugfs_init(kvm);
Paul Mackerras9a94d3e2018-10-08 16:30:57 +11005365 if (radix_enabled())
5366 kvmhv_radix_debugfs_init(kvm);
Paul Mackerrase23a8082015-03-28 14:21:01 +11005367
David Gibson54738c02011-06-29 00:22:41 +00005368 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00005369}
5370
Paul Mackerrasf1378b12013-09-27 15:33:43 +05305371static void kvmppc_free_vcores(struct kvm *kvm)
5372{
5373 long int i;
5374
Paul Mackerras23316312015-10-21 16:03:14 +11005375 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05305376 kfree(kvm->arch.vcores[i]);
5377 kvm->arch.online_vcores = 0;
5378}
5379
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305380static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005381{
Paul Mackerrase23a8082015-03-28 14:21:01 +11005382 debugfs_remove_recursive(kvm->arch.debugfs_dir);
5383
Nicholas Pigginaaae8c72021-05-28 19:07:42 +10005384 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005385 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00005386
Paul Mackerrasf1378b12013-09-27 15:33:43 +05305387 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00005388
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005389
Paul Mackerras5a319352017-01-30 21:21:46 +11005390 if (kvm_is_radix(kvm))
5391 kvmppc_free_radix(kvm);
5392 else
David Gibsonaae07772016-12-20 16:49:02 +11005393 kvmppc_free_hpt(&kvm->arch.hpt);
Suresh Warrierc57875f2016-08-19 15:35:50 +10005394
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005395 /* Perform global invalidation and return lpid to the pool */
5396 if (cpu_has_feature(CPU_FTR_ARCH_300)) {
Paul Mackerrasaa069a92018-09-21 20:02:01 +10005397 if (nesting_enabled(kvm))
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005398 kvmhv_release_all_nested(kvm);
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005399 kvm->arch.process_table = 0;
Paul Mackerrasd89c69f2019-12-18 11:43:06 +11005400 if (kvm->arch.secure_guest)
5401 uv_svm_terminate(kvm->arch.lpid);
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11005402 kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0);
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005403 }
Bharata B Raoca9f4942019-11-25 08:36:26 +05305404
Suraj Jitindar Singh89329c02018-10-08 16:30:59 +11005405 kvmppc_free_lpid(kvm->arch.lpid);
5406
Suresh Warrierc57875f2016-08-19 15:35:50 +10005407 kvmppc_free_pimap(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00005408}
5409
5410/* We don't need to emulate any privileged instructions or dcbz */
Tianjia Zhang8c99d342020-04-27 12:35:11 +08005411static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305412 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005413{
5414 return EMULATE_FAIL;
5415}
5416
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305417static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
5418 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005419{
5420 return EMULATE_FAIL;
5421}
5422
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305423static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
5424 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00005425{
5426 return EMULATE_FAIL;
5427}
5428
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305429static int kvmppc_core_check_processor_compat_hv(void)
5430{
Paul Mackerrasde760db2018-10-08 16:31:16 +11005431 if (cpu_has_feature(CPU_FTR_HVMODE) &&
5432 cpu_has_feature(CPU_FTR_ARCH_206))
5433 return 0;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10005434
Paul Mackerrasde760db2018-10-08 16:31:16 +11005435 /* POWER9 in radix mode is capable of being a nested hypervisor. */
5436 if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled())
5437 return 0;
5438
5439 return -EIO;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305440}
5441
Suresh Warrier8daaafc2016-08-19 15:35:48 +10005442#ifdef CONFIG_KVM_XICS
5443
5444void kvmppc_free_pimap(struct kvm *kvm)
5445{
5446 kfree(kvm->arch.pimap);
5447}
5448
Suresh Warrierc57875f2016-08-19 15:35:50 +10005449static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void)
Suresh Warrier8daaafc2016-08-19 15:35:48 +10005450{
5451 return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL);
5452}
Suresh Warrierc57875f2016-08-19 15:35:50 +10005453
5454static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5455{
5456 struct irq_desc *desc;
5457 struct kvmppc_irq_map *irq_map;
5458 struct kvmppc_passthru_irqmap *pimap;
5459 struct irq_chip *chip;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005460 int i, rc = 0;
Cédric Le Goater17530812021-07-01 15:27:48 +02005461 struct irq_data *host_data;
Suresh Warrierc57875f2016-08-19 15:35:50 +10005462
Suresh Warrier644abbb2016-08-19 15:35:54 +10005463 if (!kvm_irq_bypass)
5464 return 1;
5465
Suresh Warrierc57875f2016-08-19 15:35:50 +10005466 desc = irq_to_desc(host_irq);
5467 if (!desc)
5468 return -EIO;
5469
5470 mutex_lock(&kvm->lock);
5471
5472 pimap = kvm->arch.pimap;
5473 if (pimap == NULL) {
5474 /* First call, allocate structure to hold IRQ map */
5475 pimap = kvmppc_alloc_pimap();
5476 if (pimap == NULL) {
5477 mutex_unlock(&kvm->lock);
5478 return -ENOMEM;
5479 }
5480 kvm->arch.pimap = pimap;
5481 }
5482
5483 /*
5484 * For now, we only support interrupts for which the EOI operation
5485 * is an OPAL call followed by a write to XIRR, since that's
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005486 * what our real-mode EOI code does, or a XIVE interrupt
Suresh Warrierc57875f2016-08-19 15:35:50 +10005487 */
5488 chip = irq_data_get_irq_chip(&desc->irq_data);
Cédric Le Goaterba418a02021-07-01 15:27:31 +02005489 if (!chip || !is_pnv_opal_msi(chip)) {
Suresh Warrierc57875f2016-08-19 15:35:50 +10005490 pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n",
5491 host_irq, guest_gsi);
5492 mutex_unlock(&kvm->lock);
5493 return -ENOENT;
5494 }
5495
5496 /*
5497 * See if we already have an entry for this guest IRQ number.
5498 * If it's mapped to a hardware IRQ number, that's an error,
5499 * otherwise re-use this entry.
5500 */
5501 for (i = 0; i < pimap->n_mapped; i++) {
5502 if (guest_gsi == pimap->mapped[i].v_hwirq) {
5503 if (pimap->mapped[i].r_hwirq) {
5504 mutex_unlock(&kvm->lock);
5505 return -EINVAL;
5506 }
5507 break;
5508 }
5509 }
5510
5511 if (i == KVMPPC_PIRQ_MAPPED) {
5512 mutex_unlock(&kvm->lock);
5513 return -EAGAIN; /* table is full */
5514 }
5515
5516 irq_map = &pimap->mapped[i];
5517
5518 irq_map->v_hwirq = guest_gsi;
Suresh Warrierc57875f2016-08-19 15:35:50 +10005519 irq_map->desc = desc;
5520
Suresh Warriere3c13e52016-08-19 15:35:51 +10005521 /*
5522 * Order the above two stores before the next to serialize with
5523 * the KVM real mode handler.
5524 */
5525 smp_wmb();
Cédric Le Goater17530812021-07-01 15:27:48 +02005526
5527 /*
5528 * The 'host_irq' number is mapped in the PCI-MSI domain but
5529 * the underlying calls, which will EOI the interrupt in real
5530 * mode, need an HW IRQ number mapped in the XICS IRQ domain.
5531 */
5532 host_data = irq_domain_get_irq_data(irq_get_default_host(), host_irq);
5533 irq_map->r_hwirq = (unsigned int)irqd_to_hwirq(host_data);
Suresh Warriere3c13e52016-08-19 15:35:51 +10005534
Suresh Warrierc57875f2016-08-19 15:35:50 +10005535 if (i == pimap->n_mapped)
5536 pimap->n_mapped++;
5537
Paul Mackerras03f95332019-02-04 22:07:20 +11005538 if (xics_on_xive())
Cédric Le Goatere5e78b12021-07-01 15:27:32 +02005539 rc = kvmppc_xive_set_mapped(kvm, guest_gsi, host_irq);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005540 else
Cédric Le Goater17530812021-07-01 15:27:48 +02005541 kvmppc_xics_set_mapped(kvm, guest_gsi, irq_map->r_hwirq);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005542 if (rc)
5543 irq_map->r_hwirq = 0;
Paul Mackerras5d375192016-08-19 15:35:56 +10005544
Suresh Warrierc57875f2016-08-19 15:35:50 +10005545 mutex_unlock(&kvm->lock);
5546
5547 return 0;
5548}
5549
5550static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi)
5551{
5552 struct irq_desc *desc;
5553 struct kvmppc_passthru_irqmap *pimap;
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005554 int i, rc = 0;
Suresh Warrierc57875f2016-08-19 15:35:50 +10005555
Suresh Warrier644abbb2016-08-19 15:35:54 +10005556 if (!kvm_irq_bypass)
5557 return 0;
5558
Suresh Warrierc57875f2016-08-19 15:35:50 +10005559 desc = irq_to_desc(host_irq);
5560 if (!desc)
5561 return -EIO;
5562
5563 mutex_lock(&kvm->lock);
Markus Elfringa1c52e12017-01-20 11:00:08 +01005564 if (!kvm->arch.pimap)
5565 goto unlock;
Suresh Warrierc57875f2016-08-19 15:35:50 +10005566
Suresh Warrierc57875f2016-08-19 15:35:50 +10005567 pimap = kvm->arch.pimap;
5568
5569 for (i = 0; i < pimap->n_mapped; i++) {
5570 if (guest_gsi == pimap->mapped[i].v_hwirq)
5571 break;
5572 }
5573
5574 if (i == pimap->n_mapped) {
5575 mutex_unlock(&kvm->lock);
5576 return -ENODEV;
5577 }
5578
Paul Mackerras03f95332019-02-04 22:07:20 +11005579 if (xics_on_xive())
Cédric Le Goatere5e78b12021-07-01 15:27:32 +02005580 rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, host_irq);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005581 else
5582 kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq);
Paul Mackerras5d375192016-08-19 15:35:56 +10005583
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005584 /* invalidate the entry (what do do on error from the above ?) */
Suresh Warrierc57875f2016-08-19 15:35:50 +10005585 pimap->mapped[i].r_hwirq = 0;
5586
5587 /*
5588 * We don't free this structure even when the count goes to
5589 * zero. The structure is freed when we destroy the VM.
5590 */
Markus Elfringa1c52e12017-01-20 11:00:08 +01005591 unlock:
Suresh Warrierc57875f2016-08-19 15:35:50 +10005592 mutex_unlock(&kvm->lock);
Benjamin Herrenschmidt5af50992017-04-05 17:54:56 +10005593 return rc;
Suresh Warrierc57875f2016-08-19 15:35:50 +10005594}
5595
5596static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons,
5597 struct irq_bypass_producer *prod)
5598{
5599 int ret = 0;
5600 struct kvm_kernel_irqfd *irqfd =
5601 container_of(cons, struct kvm_kernel_irqfd, consumer);
5602
5603 irqfd->producer = prod;
5604
5605 ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
5606 if (ret)
5607 pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n",
5608 prod->irq, irqfd->gsi, ret);
5609
5610 return ret;
5611}
5612
5613static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons,
5614 struct irq_bypass_producer *prod)
5615{
5616 int ret;
5617 struct kvm_kernel_irqfd *irqfd =
5618 container_of(cons, struct kvm_kernel_irqfd, consumer);
5619
5620 irqfd->producer = NULL;
5621
5622 /*
5623 * When producer of consumer is unregistered, we change back to
5624 * default external interrupt handling mode - KVM real mode
5625 * will switch back to host.
5626 */
5627 ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi);
5628 if (ret)
5629 pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n",
5630 prod->irq, irqfd->gsi, ret);
5631}
Suresh Warrier8daaafc2016-08-19 15:35:48 +10005632#endif
5633
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305634static long kvm_arch_vm_ioctl_hv(struct file *filp,
5635 unsigned int ioctl, unsigned long arg)
5636{
5637 struct kvm *kvm __maybe_unused = filp->private_data;
5638 void __user *argp = (void __user *)arg;
5639 long r;
5640
5641 switch (ioctl) {
5642
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305643 case KVM_PPC_ALLOCATE_HTAB: {
5644 u32 htab_order;
5645
Fabiano Rosas05e62952020-09-11 01:16:07 -03005646 /* If we're a nested hypervisor, we currently only support radix */
5647 if (kvmhv_on_pseries()) {
5648 r = -EOPNOTSUPP;
5649 break;
5650 }
5651
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305652 r = -EFAULT;
5653 if (get_user(htab_order, (u32 __user *)argp))
5654 break;
David Gibsonf98a8bf2016-12-20 16:49:03 +11005655 r = kvmppc_alloc_reset_hpt(kvm, htab_order);
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305656 if (r)
5657 break;
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305658 r = 0;
5659 break;
5660 }
5661
5662 case KVM_PPC_GET_HTAB_FD: {
5663 struct kvm_get_htab_fd ghf;
5664
5665 r = -EFAULT;
5666 if (copy_from_user(&ghf, argp, sizeof(ghf)))
5667 break;
5668 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
5669 break;
5670 }
5671
David Gibson5e985962016-12-20 16:49:05 +11005672 case KVM_PPC_RESIZE_HPT_PREPARE: {
5673 struct kvm_ppc_resize_hpt rhpt;
5674
5675 r = -EFAULT;
5676 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
5677 break;
5678
5679 r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt);
5680 break;
5681 }
5682
5683 case KVM_PPC_RESIZE_HPT_COMMIT: {
5684 struct kvm_ppc_resize_hpt rhpt;
5685
5686 r = -EFAULT;
5687 if (copy_from_user(&rhpt, argp, sizeof(rhpt)))
5688 break;
5689
5690 r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt);
5691 break;
5692 }
5693
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05305694 default:
5695 r = -ENOTTY;
5696 }
5697
5698 return r;
5699}
5700
Paul Mackerras699a0ea2014-06-02 11:02:59 +10005701/*
5702 * List of hcall numbers to enable by default.
5703 * For compatibility with old userspace, we enable by default
5704 * all hcalls that were implemented before the hcall-enabling
5705 * facility was added. Note this list should not include H_RTAS.
5706 */
5707static unsigned int default_hcall_list[] = {
5708 H_REMOVE,
5709 H_ENTER,
5710 H_READ,
5711 H_PROTECT,
5712 H_BULK_REMOVE,
Nicholas Piggin0fd85cb82021-04-12 11:48:41 +10005713#ifdef CONFIG_SPAPR_TCE_IOMMU
Paul Mackerras699a0ea2014-06-02 11:02:59 +10005714 H_GET_TCE,
5715 H_PUT_TCE,
Nicholas Piggin0fd85cb82021-04-12 11:48:41 +10005716#endif
Paul Mackerras699a0ea2014-06-02 11:02:59 +10005717 H_SET_DABR,
5718 H_SET_XDABR,
5719 H_CEDE,
5720 H_PROD,
5721 H_CONFER,
5722 H_REGISTER_VPA,
5723#ifdef CONFIG_KVM_XICS
5724 H_EOI,
5725 H_CPPR,
5726 H_IPI,
5727 H_IPOLL,
5728 H_XIRR,
5729 H_XIRR_X,
5730#endif
5731 0
5732};
5733
5734static void init_default_hcalls(void)
5735{
5736 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10005737 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10005738
Paul Mackerrasae2113a2014-06-02 11:03:00 +10005739 for (i = 0; default_hcall_list[i]; ++i) {
5740 hcall = default_hcall_list[i];
5741 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
5742 __set_bit(hcall / 4, default_enabled_hcalls);
5743 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10005744}
5745
Paul Mackerrasc9270132017-01-30 21:21:41 +11005746static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg)
5747{
Paul Mackerras468808b2017-01-30 21:21:42 +11005748 unsigned long lpcr;
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005749 int radix;
Paul Mackerras18c36402017-09-13 16:00:10 +10005750 int err;
Paul Mackerras468808b2017-01-30 21:21:42 +11005751
5752 /* If not on a POWER9, reject it */
5753 if (!cpu_has_feature(CPU_FTR_ARCH_300))
5754 return -ENODEV;
5755
5756 /* If any unknown flags set, reject it */
5757 if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE))
5758 return -EINVAL;
5759
Paul Mackerras468808b2017-01-30 21:21:42 +11005760 /* GR (guest radix) bit in process_table field must match */
Paul Mackerras18c36402017-09-13 16:00:10 +10005761 radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX);
Paul Mackerras8cf4ecc2017-01-30 21:21:53 +11005762 if (!!(cfg->process_table & PATB_GR) != radix)
Paul Mackerras468808b2017-01-30 21:21:42 +11005763 return -EINVAL;
5764
5765 /* Process table size field must be reasonable, i.e. <= 24 */
5766 if ((cfg->process_table & PRTS_MASK) > 24)
5767 return -EINVAL;
5768
Paul Mackerras18c36402017-09-13 16:00:10 +10005769 /* We can change a guest to/from radix now, if the host is radix */
5770 if (radix && !radix_enabled())
5771 return -EINVAL;
5772
Paul Mackerrasde760db2018-10-08 16:31:16 +11005773 /* If we're a nested hypervisor, we currently only support radix */
5774 if (kvmhv_on_pseries() && !radix)
5775 return -EINVAL;
5776
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005777 mutex_lock(&kvm->arch.mmu_setup_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10005778 if (radix != kvm_is_radix(kvm)) {
5779 if (kvm->arch.mmu_ready) {
5780 kvm->arch.mmu_ready = 0;
5781 /* order mmu_ready vs. vcpus_running */
5782 smp_mb();
5783 if (atomic_read(&kvm->arch.vcpus_running)) {
5784 kvm->arch.mmu_ready = 1;
5785 err = -EBUSY;
5786 goto out_unlock;
5787 }
5788 }
5789 if (radix)
5790 err = kvmppc_switch_mmu_to_radix(kvm);
5791 else
5792 err = kvmppc_switch_mmu_to_hpt(kvm);
5793 if (err)
5794 goto out_unlock;
5795 }
5796
Paul Mackerras468808b2017-01-30 21:21:42 +11005797 kvm->arch.process_table = cfg->process_table;
5798 kvmppc_setup_partition_table(kvm);
5799
5800 lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0;
5801 kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE);
Paul Mackerras18c36402017-09-13 16:00:10 +10005802 err = 0;
Paul Mackerras468808b2017-01-30 21:21:42 +11005803
Paul Mackerras18c36402017-09-13 16:00:10 +10005804 out_unlock:
Paul Mackerras0d4ee882019-05-23 16:35:34 +10005805 mutex_unlock(&kvm->arch.mmu_setup_lock);
Paul Mackerras18c36402017-09-13 16:00:10 +10005806 return err;
Paul Mackerrasc9270132017-01-30 21:21:41 +11005807}
5808
Paul Mackerrasaa069a92018-09-21 20:02:01 +10005809static int kvmhv_enable_nested(struct kvm *kvm)
5810{
5811 if (!nested)
5812 return -EPERM;
Nicholas Piggincbcff8b2021-05-28 19:07:46 +10005813 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Paul Mackerrasaa069a92018-09-21 20:02:01 +10005814 return -ENODEV;
Nicholas Piggin0bf7e1b2021-05-28 19:07:51 +10005815 if (!radix_enabled())
Paul Mackerrasaa069a92018-09-21 20:02:01 +10005816 return -ENODEV;
5817
5818 /* kvm == NULL means the caller is testing if the capability exists */
5819 if (kvm)
5820 kvm->arch.nested_enable = true;
5821 return 0;
5822}
5823
Suraj Jitindar Singhdceadcf2018-12-14 16:29:06 +11005824static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
5825 int size)
5826{
5827 int rc = -EINVAL;
5828
5829 if (kvmhv_vcpu_is_radix(vcpu)) {
5830 rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size);
5831
5832 if (rc > 0)
5833 rc = -EINVAL;
5834 }
5835
5836 /* For now quadrants are the only way to access nested guest memory */
5837 if (rc && vcpu->arch.nested)
5838 rc = -EAGAIN;
5839
5840 return rc;
5841}
5842
5843static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr,
5844 int size)
5845{
5846 int rc = -EINVAL;
5847
5848 if (kvmhv_vcpu_is_radix(vcpu)) {
5849 rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size);
5850
5851 if (rc > 0)
5852 rc = -EINVAL;
5853 }
5854
5855 /* For now quadrants are the only way to access nested guest memory */
5856 if (rc && vcpu->arch.nested)
5857 rc = -EAGAIN;
5858
5859 return rc;
5860}
5861
Bharata B Rao22945682019-11-25 08:36:30 +05305862static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa)
5863{
5864 unpin_vpa(kvm, vpa);
5865 vpa->gpa = 0;
5866 vpa->pinned_addr = NULL;
5867 vpa->dirty = false;
5868 vpa->update_pending = 0;
5869}
5870
5871/*
Paul Mackerras9a5788c2020-03-19 15:29:55 +11005872 * Enable a guest to become a secure VM, or test whether
5873 * that could be enabled.
5874 * Called when the KVM_CAP_PPC_SECURE_GUEST capability is
5875 * tested (kvm == NULL) or enabled (kvm != NULL).
5876 */
5877static int kvmhv_enable_svm(struct kvm *kvm)
5878{
5879 if (!kvmppc_uvmem_available())
5880 return -EINVAL;
5881 if (kvm)
5882 kvm->arch.svm_enabled = 1;
5883 return 0;
5884}
5885
5886/*
Bharata B Rao22945682019-11-25 08:36:30 +05305887 * IOCTL handler to turn off secure mode of guest
5888 *
5889 * - Release all device pages
5890 * - Issue ucall to terminate the guest on the UV side
5891 * - Unpin the VPA pages.
5892 * - Reinit the partition scoped page tables
5893 */
5894static int kvmhv_svm_off(struct kvm *kvm)
5895{
5896 struct kvm_vcpu *vcpu;
5897 int mmu_was_ready;
5898 int srcu_idx;
5899 int ret = 0;
Marc Zyngier46808a42021-11-16 16:04:02 +00005900 unsigned long i;
Bharata B Rao22945682019-11-25 08:36:30 +05305901
5902 if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START))
5903 return ret;
5904
5905 mutex_lock(&kvm->arch.mmu_setup_lock);
5906 mmu_was_ready = kvm->arch.mmu_ready;
5907 if (kvm->arch.mmu_ready) {
5908 kvm->arch.mmu_ready = 0;
5909 /* order mmu_ready vs. vcpus_running */
5910 smp_mb();
5911 if (atomic_read(&kvm->arch.vcpus_running)) {
5912 kvm->arch.mmu_ready = 1;
5913 ret = -EBUSY;
5914 goto out;
5915 }
5916 }
5917
5918 srcu_idx = srcu_read_lock(&kvm->srcu);
5919 for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) {
5920 struct kvm_memory_slot *memslot;
5921 struct kvm_memslots *slots = __kvm_memslots(kvm, i);
Maciej S. Szmigieroa54d8062021-12-06 20:54:30 +01005922 int bkt;
Bharata B Rao22945682019-11-25 08:36:30 +05305923
5924 if (!slots)
5925 continue;
5926
Maciej S. Szmigieroa54d8062021-12-06 20:54:30 +01005927 kvm_for_each_memslot(memslot, bkt, slots) {
Sukadev Bhattiproluce477a72019-12-19 13:51:45 -08005928 kvmppc_uvmem_drop_pages(memslot, kvm, true);
Bharata B Rao22945682019-11-25 08:36:30 +05305929 uv_unregister_mem_slot(kvm->arch.lpid, memslot->id);
5930 }
5931 }
5932 srcu_read_unlock(&kvm->srcu, srcu_idx);
5933
5934 ret = uv_svm_terminate(kvm->arch.lpid);
5935 if (ret != U_SUCCESS) {
5936 ret = -EINVAL;
5937 goto out;
5938 }
5939
5940 /*
5941 * When secure guest is reset, all the guest pages are sent
5942 * to UV via UV_PAGE_IN before the non-boot vcpus get a
5943 * chance to run and unpin their VPA pages. Unpinning of all
5944 * VPA pages is done here explicitly so that VPA pages
5945 * can be migrated to the secure side.
5946 *
5947 * This is required to for the secure SMP guest to reboot
5948 * correctly.
5949 */
5950 kvm_for_each_vcpu(i, vcpu, kvm) {
5951 spin_lock(&vcpu->arch.vpa_update_lock);
5952 unpin_vpa_reset(kvm, &vcpu->arch.dtl);
5953 unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow);
5954 unpin_vpa_reset(kvm, &vcpu->arch.vpa);
5955 spin_unlock(&vcpu->arch.vpa_update_lock);
5956 }
5957
5958 kvmppc_setup_partition_table(kvm);
5959 kvm->arch.secure_guest = 0;
5960 kvm->arch.mmu_ready = mmu_was_ready;
5961out:
5962 mutex_unlock(&kvm->arch.mmu_setup_lock);
5963 return ret;
5964}
5965
Ravi Bangoriad9a47ed2020-12-16 16:12:19 +05305966static int kvmhv_enable_dawr1(struct kvm *kvm)
5967{
5968 if (!cpu_has_feature(CPU_FTR_DAWR1))
5969 return -ENODEV;
5970
5971 /* kvm == NULL means the caller is testing if the capability exists */
5972 if (kvm)
5973 kvm->arch.dawr1_enabled = true;
5974 return 0;
5975}
5976
Fabiano Rosasa7220762021-02-05 13:41:54 -03005977static bool kvmppc_hash_v3_possible(void)
5978{
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10005979 if (!cpu_has_feature(CPU_FTR_ARCH_300))
Fabiano Rosasa7220762021-02-05 13:41:54 -03005980 return false;
5981
Nicholas Pigginfae5c9f2021-05-28 19:07:52 +10005982 if (!cpu_has_feature(CPU_FTR_HVMODE))
5983 return false;
5984
5985 /*
5986 * POWER9 chips before version 2.02 can't have some threads in
5987 * HPT mode and some in radix mode on the same core.
5988 */
5989 if (radix_enabled()) {
5990 unsigned int pvr = mfspr(SPRN_PVR);
5991 if ((pvr >> 16) == PVR_POWER9 &&
5992 (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) ||
5993 ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101)))
5994 return false;
5995 }
5996
5997 return true;
Fabiano Rosasa7220762021-02-05 13:41:54 -03005998}
5999
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306000static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306001 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
6002 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
6003 .get_one_reg = kvmppc_get_one_reg_hv,
6004 .set_one_reg = kvmppc_set_one_reg_hv,
6005 .vcpu_load = kvmppc_core_vcpu_load_hv,
6006 .vcpu_put = kvmppc_core_vcpu_put_hv,
Nicholas Piggin87a45e02019-10-02 16:00:22 +10006007 .inject_interrupt = kvmppc_inject_interrupt_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306008 .set_msr = kvmppc_set_msr_hv,
6009 .vcpu_run = kvmppc_vcpu_run_hv,
6010 .vcpu_create = kvmppc_core_vcpu_create_hv,
6011 .vcpu_free = kvmppc_core_vcpu_free_hv,
6012 .check_requests = kvmppc_core_check_requests_hv,
6013 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
6014 .flush_memslot = kvmppc_core_flush_memslot_hv,
6015 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
6016 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
Sean Christophersonb1c53562021-04-01 17:56:53 -07006017 .unmap_gfn_range = kvm_unmap_gfn_range_hv,
6018 .age_gfn = kvm_age_gfn_hv,
6019 .test_age_gfn = kvm_test_age_gfn_hv,
6020 .set_spte_gfn = kvm_set_spte_gfn_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306021 .free_memslot = kvmppc_core_free_memslot_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306022 .init_vm = kvmppc_core_init_vm_hv,
6023 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306024 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
6025 .emulate_op = kvmppc_core_emulate_op_hv,
6026 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
6027 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
6028 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
6029 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10006030 .hcall_implemented = kvmppc_hcall_impl_hv,
Suresh Warrierc57875f2016-08-19 15:35:50 +10006031#ifdef CONFIG_KVM_XICS
6032 .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv,
6033 .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv,
6034#endif
Paul Mackerrasc9270132017-01-30 21:21:41 +11006035 .configure_mmu = kvmhv_configure_mmu,
6036 .get_rmmu_info = kvmhv_get_rmmu_info,
Paul Mackerras3c313522017-02-06 13:24:41 +11006037 .set_smt_mode = kvmhv_set_smt_mode,
Paul Mackerrasaa069a92018-09-21 20:02:01 +10006038 .enable_nested = kvmhv_enable_nested,
Suraj Jitindar Singhdceadcf2018-12-14 16:29:06 +11006039 .load_from_eaddr = kvmhv_load_from_eaddr,
6040 .store_to_eaddr = kvmhv_store_to_eaddr,
Paul Mackerras9a5788c2020-03-19 15:29:55 +11006041 .enable_svm = kvmhv_enable_svm,
Bharata B Rao22945682019-11-25 08:36:30 +05306042 .svm_off = kvmhv_svm_off,
Ravi Bangoriad9a47ed2020-12-16 16:12:19 +05306043 .enable_dawr1 = kvmhv_enable_dawr1,
Fabiano Rosasa7220762021-02-05 13:41:54 -03006044 .hash_v3_possible = kvmppc_hash_v3_possible,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306045};
6046
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306047static int kvm_init_subcore_bitmap(void)
6048{
6049 int i, j;
6050 int nr_cores = cpu_nr_cores();
6051 struct sibling_subcore_state *sibling_subcore_state;
6052
6053 for (i = 0; i < nr_cores; i++) {
6054 int first_cpu = i * threads_per_core;
6055 int node = cpu_to_node(first_cpu);
6056
6057 /* Ignore if it is already allocated. */
Nicholas Piggind2e60072018-02-14 01:08:12 +10006058 if (paca_ptrs[first_cpu]->sibling_subcore_state)
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306059 continue;
6060
6061 sibling_subcore_state =
wangbo08434ab2019-01-07 20:15:52 +08006062 kzalloc_node(sizeof(struct sibling_subcore_state),
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306063 GFP_KERNEL, node);
6064 if (!sibling_subcore_state)
6065 return -ENOMEM;
6066
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306067
6068 for (j = 0; j < threads_per_core; j++) {
6069 int cpu = first_cpu + j;
6070
Nicholas Piggind2e60072018-02-14 01:08:12 +10006071 paca_ptrs[cpu]->sibling_subcore_state =
6072 sibling_subcore_state;
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306073 }
6074 }
6075 return 0;
6076}
6077
Paul Mackerras5a319352017-01-30 21:21:46 +11006078static int kvmppc_radix_possible(void)
6079{
6080 return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled();
6081}
6082
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306083static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00006084{
6085 int r;
Nicholas Piggin2275d7b2019-09-03 01:29:31 +10006086
6087 if (!tlbie_capable) {
6088 pr_err("KVM-HV: Host does not support TLBIE\n");
6089 return -ENODEV;
6090 }
6091
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306092 /*
6093 * FIXME!! Do we need to check on all cpus ?
6094 */
6095 r = kvmppc_core_check_processor_compat_hv();
6096 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11006097 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00006098
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11006099 r = kvmhv_nested_init();
6100 if (r)
6101 return r;
6102
Nicholas Piggin9c5a4322021-11-23 19:52:31 +10006103 if (!cpu_has_feature(CPU_FTR_ARCH_300)) {
6104 r = kvm_init_subcore_bitmap();
6105 if (r)
6106 return r;
6107 }
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05306108
Paul Mackerrasf7257582016-11-18 09:02:08 +11006109 /*
6110 * We need a way of accessing the XICS interrupt controller,
Nicholas Piggind2e60072018-02-14 01:08:12 +10006111 * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or
Paul Mackerrasf7257582016-11-18 09:02:08 +11006112 * indirectly, via OPAL.
6113 */
6114#ifdef CONFIG_SMP
Paul Mackerras03f95332019-02-04 22:07:20 +11006115 if (!xics_on_xive() && !kvmhv_on_pseries() &&
Paul Mackerrasf3c18e92018-10-08 16:31:05 +11006116 !local_paca->kvm_hstate.xics_phys) {
Paul Mackerrasf7257582016-11-18 09:02:08 +11006117 struct device_node *np;
6118
6119 np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc");
6120 if (!np) {
6121 pr_err("KVM-HV: Cannot determine method for accessing XICS\n");
6122 return -ENODEV;
6123 }
Nicholas Mc Guire51eaa082018-07-07 08:53:07 +02006124 /* presence of intc confirmed - node can be dropped again */
6125 of_node_put(np);
Paul Mackerrasf7257582016-11-18 09:02:08 +11006126 }
6127#endif
6128
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306129 kvm_ops_hv.owner = THIS_MODULE;
6130 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00006131
Paul Mackerras699a0ea2014-06-02 11:02:59 +10006132 init_default_hcalls();
6133
Paul Mackerrasec257162015-06-24 21:18:03 +10006134 init_vcore_lists();
6135
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306136 r = kvmppc_mmu_hv_init();
Paul Mackerras5a319352017-01-30 21:21:46 +11006137 if (r)
6138 return r;
6139
6140 if (kvmppc_radix_possible())
6141 r = kvmppc_radix_init();
Paul Mackerras00608e12018-01-11 16:54:26 +11006142
Bharata B Raoca9f4942019-11-25 08:36:26 +05306143 r = kvmppc_uvmem_init();
6144 if (r < 0)
6145 pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r);
6146
Paul Mackerrasde56a942011-06-29 00:21:34 +00006147 return r;
6148}
6149
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306150static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00006151{
Bharata B Raoca9f4942019-11-25 08:36:26 +05306152 kvmppc_uvmem_free();
Suresh Warrier79b6c242015-12-17 14:59:06 -06006153 kvmppc_free_host_rm_ops();
Paul Mackerras5a319352017-01-30 21:21:46 +11006154 if (kvmppc_radix_possible())
6155 kvmppc_radix_exit();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05306156 kvmppc_hv_ops = NULL;
Paul Mackerras8e3f5fc2018-10-08 16:31:03 +11006157 kvmhv_nested_exit();
Paul Mackerrasde56a942011-06-29 00:21:34 +00006158}
6159
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05306160module_init(kvmppc_book3s_init_hv);
6161module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05306162MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01006163MODULE_ALIAS_MISCDEV(KVM_MINOR);
6164MODULE_ALIAS("devname:kvm");