blob: 542ec97129b4c3d1b3b47b29d6ea7c771908d784 [file] [log] [blame]
Paul Mackerrasde56a942011-06-29 00:21:34 +00001/*
2 * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com>
3 * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved.
4 *
5 * Authors:
6 * Paul Mackerras <paulus@au1.ibm.com>
7 * Alexander Graf <agraf@suse.de>
8 * Kevin Wolf <mail@kevin-wolf.de>
9 *
10 * Description: KVM functions specific to running on Book 3S
11 * processors in hypervisor mode (specifically POWER7 and later).
12 *
13 * This file is derived from arch/powerpc/kvm/book3s.c,
14 * by Alexander Graf <agraf@suse.de>.
15 *
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License, version 2, as
18 * published by the Free Software Foundation.
19 */
20
21#include <linux/kvm_host.h>
22#include <linux/err.h>
23#include <linux/slab.h>
24#include <linux/preempt.h>
25#include <linux/sched.h>
26#include <linux/delay.h>
Paul Gortmaker66b15db2011-05-27 10:46:24 -040027#include <linux/export.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000028#include <linux/fs.h>
29#include <linux/anon_inodes.h>
30#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>
Paul Mackerrasde56a942011-06-29 00:21:34 +000036
37#include <asm/reg.h>
38#include <asm/cputable.h>
39#include <asm/cacheflush.h>
40#include <asm/tlbflush.h>
41#include <asm/uaccess.h>
42#include <asm/io.h>
43#include <asm/kvm_ppc.h>
44#include <asm/kvm_book3s.h>
45#include <asm/mmu_context.h>
46#include <asm/lppaca.h>
47#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000048#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000049#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000050#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010051#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000052#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110053#include <asm/dbell.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000054#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000055#include <linux/vmalloc.h>
56#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000057#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053058#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000059
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053060#include "book3s.h"
61
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060062#define CREATE_TRACE_POINTS
63#include "trace_hv.h"
64
Paul Mackerrasde56a942011-06-29 00:21:34 +000065/* #define EXIT_DEBUG */
66/* #define EXIT_DEBUG_SIMPLE */
67/* #define EXIT_DEBUG_INT */
68
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000069/* Used to indicate that a guest page fault needs to be handled */
70#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
71
Paul Mackerrasc7b67672012-10-15 01:18:07 +000072/* Used as a "null" value for timebase values */
73#define TB_NIL (~(u64)0)
74
Paul Mackerras699a0ea2014-06-02 11:02:59 +100075static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
76
Paul Mackerrasb4deba52015-07-02 20:38:16 +100077static int dynamic_mt_modes = 6;
78module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
79MODULE_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 +100080static int target_smt_mode;
81module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
82MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +100083
Paul Mackerras19ccb762011-07-23 17:42:46 +100084static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000085static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100086
Paul Mackerras66feed62015-03-28 14:21:12 +110087static bool kvmppc_ipi_thread(int cpu)
88{
89 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
90 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
91 preempt_disable();
92 if (cpu_first_thread_sibling(cpu) ==
93 cpu_first_thread_sibling(smp_processor_id())) {
94 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
95 msg |= cpu_thread_in_core(cpu);
96 smp_mb();
97 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
98 preempt_enable();
99 return true;
100 }
101 preempt_enable();
102 }
103
104#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
105 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
106 xics_wake_cpu(cpu);
107 return true;
108 }
109#endif
110
111 return false;
112}
113
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530114static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000115{
Paul Mackerrasec257162015-06-24 21:18:03 +1000116 int cpu;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000117 wait_queue_head_t *wqp;
118
119 wqp = kvm_arch_vcpu_wq(vcpu);
120 if (waitqueue_active(wqp)) {
121 wake_up_interruptible(wqp);
122 ++vcpu->stat.halt_wakeup;
123 }
124
Paul Mackerrasec257162015-06-24 21:18:03 +1000125 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100126 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000127
128 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000129 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100130 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
131 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000132}
133
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000134/*
135 * We use the vcpu_load/put functions to measure stolen time.
136 * Stolen time is counted as time when either the vcpu is able to
137 * run as part of a virtual core, but the task running the vcore
138 * is preempted or sleeping, or when the vcpu needs something done
139 * in the kernel by the task running the vcpu, but that task is
140 * preempted or sleeping. Those two things have to be counted
141 * separately, since one of the vcpu tasks will take on the job
142 * of running the core, and the other vcpu tasks in the vcore will
143 * sleep waiting for it to do that, but that sleep shouldn't count
144 * as stolen time.
145 *
146 * Hence we accumulate stolen time when the vcpu can run as part of
147 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
148 * needs its task to do other things in the kernel (for example,
149 * service a page fault) in busy_stolen. We don't accumulate
150 * stolen time for a vcore when it is inactive, or for a vcpu
151 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
152 * a misnomer; it means that the vcpu task is not executing in
153 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
154 * the kernel. We don't have any way of dividing up that time
155 * between time that the vcpu is genuinely stopped, time that
156 * the task is actively working on behalf of the vcpu, and time
157 * that the task is preempted, so we don't count any of it as
158 * stolen.
159 *
160 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100161 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
162 * lock. The stolen times are measured in units of timebase ticks.
163 * (Note that the != TB_NIL checks below are purely defensive;
164 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000165 */
166
Paul Mackerrasec257162015-06-24 21:18:03 +1000167static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
168{
169 unsigned long flags;
170
171 spin_lock_irqsave(&vc->stoltb_lock, flags);
172 vc->preempt_tb = mftb();
173 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
174}
175
176static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
177{
178 unsigned long flags;
179
180 spin_lock_irqsave(&vc->stoltb_lock, flags);
181 if (vc->preempt_tb != TB_NIL) {
182 vc->stolen_tb += mftb() - vc->preempt_tb;
183 vc->preempt_tb = TB_NIL;
184 }
185 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
186}
187
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530188static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000189{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000190 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100191 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000192
Paul Mackerras2711e242014-12-04 16:43:28 +1100193 /*
194 * We can test vc->runner without taking the vcore lock,
195 * because only this task ever sets vc->runner to this
196 * vcpu, and once it is set to this vcpu, only this task
197 * ever sets it to NULL.
198 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000199 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
200 kvmppc_core_end_stolen(vc);
201
Paul Mackerras2711e242014-12-04 16:43:28 +1100202 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000203 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
204 vcpu->arch.busy_preempt != TB_NIL) {
205 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
206 vcpu->arch.busy_preempt = TB_NIL;
207 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100208 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000209}
210
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530211static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000212{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000213 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100214 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000215
Paul Mackerrasec257162015-06-24 21:18:03 +1000216 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
217 kvmppc_core_start_stolen(vc);
218
Paul Mackerras2711e242014-12-04 16:43:28 +1100219 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000220 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
221 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100222 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000223}
224
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530225static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000226{
Paul Mackerrasc20875a2015-11-12 16:43:02 +1100227 /*
228 * Check for illegal transactional state bit combination
229 * and if we find it, force the TS field to a safe state.
230 */
231 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
232 msr &= ~MSR_TS_MASK;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000233 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000234 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000235}
236
Thomas Huth5358a962015-05-22 09:25:02 +0200237static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000238{
239 vcpu->arch.pvr = pvr;
240}
241
Thomas Huth5358a962015-05-22 09:25:02 +0200242static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000243{
244 unsigned long pcr = 0;
245 struct kvmppc_vcore *vc = vcpu->arch.vcore;
246
247 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000248 switch (arch_compat) {
249 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100250 /*
251 * If an arch bit is set in PCR, all the defined
252 * higher-order arch bits also have to be set.
253 */
254 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000255 break;
256 case PVR_ARCH_206:
257 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100258 pcr = PCR_ARCH_206;
259 break;
260 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000261 break;
262 default:
263 return -EINVAL;
264 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100265
266 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
267 /* POWER7 can't emulate POWER8 */
268 if (!(pcr & PCR_ARCH_206))
269 return -EINVAL;
270 pcr &= ~PCR_ARCH_206;
271 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000272 }
273
274 spin_lock(&vc->lock);
275 vc->arch_compat = arch_compat;
276 vc->pcr = pcr;
277 spin_unlock(&vc->lock);
278
279 return 0;
280}
281
Thomas Huth5358a962015-05-22 09:25:02 +0200282static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000283{
284 int r;
285
286 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
287 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
288 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
289 for (r = 0; r < 16; ++r)
290 pr_err("r%2d = %.16lx r%d = %.16lx\n",
291 r, kvmppc_get_gpr(vcpu, r),
292 r+16, kvmppc_get_gpr(vcpu, r+16));
293 pr_err("ctr = %.16lx lr = %.16lx\n",
294 vcpu->arch.ctr, vcpu->arch.lr);
295 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
296 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
297 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
298 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
299 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
300 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
301 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
302 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
303 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
304 pr_err("fault dar = %.16lx dsisr = %.8x\n",
305 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
306 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
307 for (r = 0; r < vcpu->arch.slb_max; ++r)
308 pr_err(" ESID = %.16llx VSID = %.16llx\n",
309 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
310 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000311 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000312 vcpu->arch.last_inst);
313}
314
Thomas Huth5358a962015-05-22 09:25:02 +0200315static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000316{
David Hildenbrande09fefd2015-11-05 09:03:50 +0100317 struct kvm_vcpu *ret;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000318
319 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +0100320 ret = kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000321 mutex_unlock(&kvm->lock);
322 return ret;
323}
324
325static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
326{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000327 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200328 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000329}
330
Paul Mackerras55b665b2012-09-25 20:33:06 +0000331static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
332 unsigned long addr, unsigned long len)
333{
334 /* check address is cacheline aligned */
335 if (addr & (L1_CACHE_BYTES - 1))
336 return -EINVAL;
337 spin_lock(&vcpu->arch.vpa_update_lock);
338 if (v->next_gpa != addr || v->len != len) {
339 v->next_gpa = addr;
340 v->len = addr ? len : 0;
341 v->update_pending = 1;
342 }
343 spin_unlock(&vcpu->arch.vpa_update_lock);
344 return 0;
345}
346
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000347/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
348struct reg_vpa {
349 u32 dummy;
350 union {
Alexander Graf02407552014-06-11 10:34:19 +0200351 __be16 hword;
352 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000353 } length;
354};
355
356static int vpa_is_registered(struct kvmppc_vpa *vpap)
357{
358 if (vpap->update_pending)
359 return vpap->next_gpa != 0;
360 return vpap->pinned_addr != NULL;
361}
362
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000363static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
364 unsigned long flags,
365 unsigned long vcpuid, unsigned long vpa)
366{
367 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000368 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000369 void *va;
370 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000371 int err;
372 int subfunc;
373 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000374
375 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
376 if (!tvcpu)
377 return H_PARAMETER;
378
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000379 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
380 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
381 subfunc == H_VPA_REG_SLB) {
382 /* Registering new area - address must be cache-line aligned */
383 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000384 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000385
386 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000387 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
388 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000389 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000390 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200391 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000392 else
Alexander Graf02407552014-06-11 10:34:19 +0200393 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000394 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000395
396 /* Check length */
397 if (len > nb || len < sizeof(struct reg_vpa))
398 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000399 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000400 vpa = 0;
401 len = 0;
402 }
403
404 err = H_PARAMETER;
405 vpap = NULL;
406 spin_lock(&tvcpu->arch.vpa_update_lock);
407
408 switch (subfunc) {
409 case H_VPA_REG_VPA: /* register VPA */
410 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000411 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000412 vpap = &tvcpu->arch.vpa;
413 err = 0;
414 break;
415
416 case H_VPA_REG_DTL: /* register DTL */
417 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000418 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000419 len -= len % sizeof(struct dtl_entry);
420
421 /* Check that they have previously registered a VPA */
422 err = H_RESOURCE;
423 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000424 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000425
426 vpap = &tvcpu->arch.dtl;
427 err = 0;
428 break;
429
430 case H_VPA_REG_SLB: /* register SLB shadow buffer */
431 /* Check that they have previously registered a VPA */
432 err = H_RESOURCE;
433 if (!vpa_is_registered(&tvcpu->arch.vpa))
434 break;
435
436 vpap = &tvcpu->arch.slb_shadow;
437 err = 0;
438 break;
439
440 case H_VPA_DEREG_VPA: /* deregister VPA */
441 /* Check they don't still have a DTL or SLB buf registered */
442 err = H_RESOURCE;
443 if (vpa_is_registered(&tvcpu->arch.dtl) ||
444 vpa_is_registered(&tvcpu->arch.slb_shadow))
445 break;
446
447 vpap = &tvcpu->arch.vpa;
448 err = 0;
449 break;
450
451 case H_VPA_DEREG_DTL: /* deregister DTL */
452 vpap = &tvcpu->arch.dtl;
453 err = 0;
454 break;
455
456 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
457 vpap = &tvcpu->arch.slb_shadow;
458 err = 0;
459 break;
460 }
461
462 if (vpap) {
463 vpap->next_gpa = vpa;
464 vpap->len = len;
465 vpap->update_pending = 1;
466 }
467
468 spin_unlock(&tvcpu->arch.vpa_update_lock);
469
470 return err;
471}
472
Paul Mackerras081f3232012-06-01 20:20:24 +1000473static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000474{
Paul Mackerras081f3232012-06-01 20:20:24 +1000475 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000476 void *va;
477 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000478 unsigned long gpa;
479
480 /*
481 * We need to pin the page pointed to by vpap->next_gpa,
482 * but we can't call kvmppc_pin_guest_page under the lock
483 * as it does get_user_pages() and down_read(). So we
484 * have to drop the lock, pin the page, then get the lock
485 * again and check that a new area didn't get registered
486 * in the meantime.
487 */
488 for (;;) {
489 gpa = vpap->next_gpa;
490 spin_unlock(&vcpu->arch.vpa_update_lock);
491 va = NULL;
492 nb = 0;
493 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000494 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000495 spin_lock(&vcpu->arch.vpa_update_lock);
496 if (gpa == vpap->next_gpa)
497 break;
498 /* sigh... unpin that one and try again */
499 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000500 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000501 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000502
503 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000504 if (va && nb < vpap->len) {
505 /*
506 * If it's now too short, it must be that userspace
507 * has changed the mappings underlying guest memory,
508 * so unregister the region.
509 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000510 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000511 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000512 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000513 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000514 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
515 vpap->dirty);
516 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000517 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000518 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000519 if (va)
520 vpap->pinned_end = va + vpap->len;
521}
Paul Mackerras93e60242011-12-12 12:28:55 +0000522
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000523static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
524{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000525 if (!(vcpu->arch.vpa.update_pending ||
526 vcpu->arch.slb_shadow.update_pending ||
527 vcpu->arch.dtl.update_pending))
528 return;
529
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000530 spin_lock(&vcpu->arch.vpa_update_lock);
531 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000532 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000533 if (vcpu->arch.vpa.pinned_addr)
534 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000535 }
536 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000537 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000538 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
539 vcpu->arch.dtl_index = 0;
540 }
541 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000542 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000543 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000544}
545
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000546/*
547 * Return the accumulated stolen time for the vcore up until `now'.
548 * The caller should hold the vcore lock.
549 */
550static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
551{
552 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100553 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000554
Paul Mackerras2711e242014-12-04 16:43:28 +1100555 spin_lock_irqsave(&vc->stoltb_lock, flags);
556 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000557 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100558 vc->preempt_tb != TB_NIL)
559 p += now - vc->preempt_tb;
560 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000561 return p;
562}
563
Paul Mackerras0456ec42012-02-03 00:56:21 +0000564static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
565 struct kvmppc_vcore *vc)
566{
567 struct dtl_entry *dt;
568 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000569 unsigned long stolen;
570 unsigned long core_stolen;
571 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000572
573 dt = vcpu->arch.dtl_ptr;
574 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000575 now = mftb();
576 core_stolen = vcore_stolen_time(vc, now);
577 stolen = core_stolen - vcpu->arch.stolen_logged;
578 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100579 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000580 stolen += vcpu->arch.busy_stolen;
581 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100582 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000583 if (!dt || !vpa)
584 return;
585 memset(dt, 0, sizeof(struct dtl_entry));
586 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200587 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
588 dt->timebase = cpu_to_be64(now + vc->tb_offset);
589 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
590 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
591 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000592 ++dt;
593 if (dt == vcpu->arch.dtl.pinned_end)
594 dt = vcpu->arch.dtl.pinned_addr;
595 vcpu->arch.dtl_ptr = dt;
596 /* order writing *dt vs. writing vpa->dtl_idx */
597 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200598 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000599 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000600}
601
Michael Neuling96423822014-06-02 11:03:01 +1000602static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
603{
604 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
605 return true;
606 if ((!vcpu->arch.vcore->arch_compat) &&
607 cpu_has_feature(CPU_FTR_ARCH_207S))
608 return true;
609 return false;
610}
611
612static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
613 unsigned long resource, unsigned long value1,
614 unsigned long value2)
615{
616 switch (resource) {
617 case H_SET_MODE_RESOURCE_SET_CIABR:
618 if (!kvmppc_power8_compatible(vcpu))
619 return H_P2;
620 if (value2)
621 return H_P4;
622 if (mflags)
623 return H_UNSUPPORTED_FLAG_START;
624 /* Guests can't breakpoint the hypervisor */
625 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
626 return H_P3;
627 vcpu->arch.ciabr = value1;
628 return H_SUCCESS;
629 case H_SET_MODE_RESOURCE_SET_DAWR:
630 if (!kvmppc_power8_compatible(vcpu))
631 return H_P2;
632 if (mflags)
633 return H_UNSUPPORTED_FLAG_START;
634 if (value2 & DABRX_HYP)
635 return H_P4;
636 vcpu->arch.dawr = value1;
637 vcpu->arch.dawrx = value2;
638 return H_SUCCESS;
639 default:
640 return H_TOO_HARD;
641 }
642}
643
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100644static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
645{
646 struct kvmppc_vcore *vcore = target->arch.vcore;
647
648 /*
649 * We expect to have been called by the real mode handler
650 * (kvmppc_rm_h_confer()) which would have directly returned
651 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
652 * have useful work to do and should not confer) so we don't
653 * recheck that here.
654 */
655
656 spin_lock(&vcore->lock);
657 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000658 vcore->vcore_state != VCORE_INACTIVE &&
659 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100660 target = vcore->runner;
661 spin_unlock(&vcore->lock);
662
663 return kvm_vcpu_yield_to(target);
664}
665
666static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
667{
668 int yield_count = 0;
669 struct lppaca *lppaca;
670
671 spin_lock(&vcpu->arch.vpa_update_lock);
672 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
673 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100674 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100675 spin_unlock(&vcpu->arch.vpa_update_lock);
676 return yield_count;
677}
678
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000679int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
680{
681 unsigned long req = kvmppc_get_gpr(vcpu, 3);
682 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100683 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000684 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000685 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000686
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000687 if (req <= MAX_HCALL_OPCODE &&
688 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
689 return RESUME_HOST;
690
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000691 switch (req) {
692 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000693 break;
694 case H_PROD:
695 target = kvmppc_get_gpr(vcpu, 4);
696 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
697 if (!tvcpu) {
698 ret = H_PARAMETER;
699 break;
700 }
701 tvcpu->arch.prodded = 1;
702 smp_mb();
703 if (vcpu->arch.ceded) {
704 if (waitqueue_active(&vcpu->wq)) {
705 wake_up_interruptible(&vcpu->wq);
706 vcpu->stat.halt_wakeup++;
707 }
708 }
709 break;
710 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000711 target = kvmppc_get_gpr(vcpu, 4);
712 if (target == -1)
713 break;
714 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
715 if (!tvcpu) {
716 ret = H_PARAMETER;
717 break;
718 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100719 yield_count = kvmppc_get_gpr(vcpu, 5);
720 if (kvmppc_get_yield_count(tvcpu) != yield_count)
721 break;
722 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000723 break;
724 case H_REGISTER_VPA:
725 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
726 kvmppc_get_gpr(vcpu, 5),
727 kvmppc_get_gpr(vcpu, 6));
728 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000729 case H_RTAS:
730 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
731 return RESUME_HOST;
732
Paul Mackerrasc9438092013-11-16 17:46:05 +1100733 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000734 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100735 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000736
737 if (rc == -ENOENT)
738 return RESUME_HOST;
739 else if (rc == 0)
740 break;
741
742 /* Send the error out to userspace via KVM_RUN */
743 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100744 case H_LOGICAL_CI_LOAD:
745 ret = kvmppc_h_logical_ci_load(vcpu);
746 if (ret == H_TOO_HARD)
747 return RESUME_HOST;
748 break;
749 case H_LOGICAL_CI_STORE:
750 ret = kvmppc_h_logical_ci_store(vcpu);
751 if (ret == H_TOO_HARD)
752 return RESUME_HOST;
753 break;
Michael Neuling96423822014-06-02 11:03:01 +1000754 case H_SET_MODE:
755 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
756 kvmppc_get_gpr(vcpu, 5),
757 kvmppc_get_gpr(vcpu, 6),
758 kvmppc_get_gpr(vcpu, 7));
759 if (ret == H_TOO_HARD)
760 return RESUME_HOST;
761 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000762 case H_XIRR:
763 case H_CPPR:
764 case H_EOI:
765 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000766 case H_IPOLL:
767 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000768 if (kvmppc_xics_enabled(vcpu)) {
769 ret = kvmppc_xics_hcall(vcpu, req);
770 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100771 }
772 return RESUME_HOST;
773 case H_PUT_TCE:
774 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
775 kvmppc_get_gpr(vcpu, 5),
776 kvmppc_get_gpr(vcpu, 6));
777 if (ret == H_TOO_HARD)
778 return RESUME_HOST;
779 break;
780 case H_PUT_TCE_INDIRECT:
781 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
782 kvmppc_get_gpr(vcpu, 5),
783 kvmppc_get_gpr(vcpu, 6),
784 kvmppc_get_gpr(vcpu, 7));
785 if (ret == H_TOO_HARD)
786 return RESUME_HOST;
787 break;
788 case H_STUFF_TCE:
789 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
790 kvmppc_get_gpr(vcpu, 5),
791 kvmppc_get_gpr(vcpu, 6),
792 kvmppc_get_gpr(vcpu, 7));
793 if (ret == H_TOO_HARD)
794 return RESUME_HOST;
795 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000796 default:
797 return RESUME_HOST;
798 }
799 kvmppc_set_gpr(vcpu, 3, ret);
800 vcpu->arch.hcall_needed = 0;
801 return RESUME_GUEST;
802}
803
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000804static int kvmppc_hcall_impl_hv(unsigned long cmd)
805{
806 switch (cmd) {
807 case H_CEDE:
808 case H_PROD:
809 case H_CONFER:
810 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000811 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100812 case H_LOGICAL_CI_LOAD:
813 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000814#ifdef CONFIG_KVM_XICS
815 case H_XIRR:
816 case H_CPPR:
817 case H_EOI:
818 case H_IPI:
819 case H_IPOLL:
820 case H_XIRR_X:
821#endif
822 return 1;
823 }
824
825 /* See if it's in the real-mode table */
826 return kvmppc_hcall_impl_hv_realmode(cmd);
827}
828
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530829static int kvmppc_emulate_debug_inst(struct kvm_run *run,
830 struct kvm_vcpu *vcpu)
831{
832 u32 last_inst;
833
834 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
835 EMULATE_DONE) {
836 /*
837 * Fetch failed, so return to guest and
838 * try executing it again.
839 */
840 return RESUME_GUEST;
841 }
842
843 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
844 run->exit_reason = KVM_EXIT_DEBUG;
845 run->debug.arch.address = kvmppc_get_pc(vcpu);
846 return RESUME_HOST;
847 } else {
848 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
849 return RESUME_GUEST;
850 }
851}
852
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530853static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
854 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000855{
856 int r = RESUME_HOST;
857
858 vcpu->stat.sum_exits++;
859
Paul Mackerras1c9e3d52015-11-12 16:43:48 +1100860 /*
861 * This can happen if an interrupt occurs in the last stages
862 * of guest entry or the first stages of guest exit (i.e. after
863 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
864 * and before setting it to KVM_GUEST_MODE_HOST_HV).
865 * That can happen due to a bug, or due to a machine check
866 * occurring at just the wrong time.
867 */
868 if (vcpu->arch.shregs.msr & MSR_HV) {
869 printk(KERN_EMERG "KVM trap in HV mode!\n");
870 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
871 vcpu->arch.trap, kvmppc_get_pc(vcpu),
872 vcpu->arch.shregs.msr);
873 kvmppc_dump_regs(vcpu);
874 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
875 run->hw.hardware_exit_reason = vcpu->arch.trap;
876 return RESUME_HOST;
877 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000878 run->exit_reason = KVM_EXIT_UNKNOWN;
879 run->ready_for_interrupt_injection = 1;
880 switch (vcpu->arch.trap) {
881 /* We're good on these - the host merely wanted to get our attention */
882 case BOOK3S_INTERRUPT_HV_DECREMENTER:
883 vcpu->stat.dec_exits++;
884 r = RESUME_GUEST;
885 break;
886 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100887 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000888 vcpu->stat.ext_intr_exits++;
889 r = RESUME_GUEST;
890 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100891 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
892 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000893 case BOOK3S_INTERRUPT_PERFMON:
894 r = RESUME_GUEST;
895 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000896 case BOOK3S_INTERRUPT_MACHINE_CHECK:
897 /*
898 * Deliver a machine check interrupt to the guest.
899 * We have to do this, even if the host has handled the
900 * machine check, because machine checks use SRR0/1 and
901 * the interrupt might have trashed guest state in them.
902 */
903 kvmppc_book3s_queue_irqprio(vcpu,
904 BOOK3S_INTERRUPT_MACHINE_CHECK);
905 r = RESUME_GUEST;
906 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000907 case BOOK3S_INTERRUPT_PROGRAM:
908 {
909 ulong flags;
910 /*
911 * Normally program interrupts are delivered directly
912 * to the guest by the hardware, but we can get here
913 * as a result of a hypervisor emulation interrupt
914 * (e40) getting turned into a 700 by BML RTAS.
915 */
916 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
917 kvmppc_core_queue_program(vcpu, flags);
918 r = RESUME_GUEST;
919 break;
920 }
921 case BOOK3S_INTERRUPT_SYSCALL:
922 {
923 /* hcall - punt to userspace */
924 int i;
925
Liu Ping Fan27025a62013-11-19 14:12:48 +0800926 /* hypercall with MSR_PR has already been handled in rmode,
927 * and never reaches here.
928 */
929
Paul Mackerrasde56a942011-06-29 00:21:34 +0000930 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
931 for (i = 0; i < 9; ++i)
932 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
933 run->exit_reason = KVM_EXIT_PAPR_HCALL;
934 vcpu->arch.hcall_needed = 1;
935 r = RESUME_HOST;
936 break;
937 }
938 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000939 * We get these next two if the guest accesses a page which it thinks
940 * it has mapped but which is not actually present, either because
941 * it is for an emulated I/O device or because the corresonding
942 * host page has been paged out. Any other HDSI/HISI interrupts
943 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000944 */
945 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000946 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000947 break;
948 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000949 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
950 vcpu->arch.fault_dsisr = 0;
951 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000952 break;
953 /*
954 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530955 * If the guest debug is disabled, generate a program interrupt
956 * to the guest. If guest debug is enabled, we need to check
957 * whether the instruction is a software breakpoint instruction.
958 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000959 */
960 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +1100961 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
962 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
963 swab32(vcpu->arch.emul_inst) :
964 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530965 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
966 r = kvmppc_emulate_debug_inst(run, vcpu);
967 } else {
968 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
969 r = RESUME_GUEST;
970 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100971 break;
972 /*
973 * This occurs if the guest (kernel or userspace), does something that
974 * is prohibited by HFSCR. We just generate a program interrupt to
975 * the guest.
976 */
977 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
978 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000979 r = RESUME_GUEST;
980 break;
981 default:
982 kvmppc_dump_regs(vcpu);
983 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
984 vcpu->arch.trap, kvmppc_get_pc(vcpu),
985 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000986 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000987 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000988 break;
989 }
990
Paul Mackerrasde56a942011-06-29 00:21:34 +0000991 return r;
992}
993
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530994static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
995 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000996{
997 int i;
998
Paul Mackerrasde56a942011-06-29 00:21:34 +0000999 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301000 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001001 for (i = 0; i < vcpu->arch.slb_max; i++) {
1002 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1003 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1004 }
1005
1006 return 0;
1007}
1008
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301009static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1010 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001011{
1012 int i, j;
1013
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001014 /* Only accept the same PVR as the host's, since we can't spoof it */
1015 if (sregs->pvr != vcpu->arch.pvr)
1016 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001017
1018 j = 0;
1019 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1020 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1021 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1022 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1023 ++j;
1024 }
1025 }
1026 vcpu->arch.slb_max = j;
1027
1028 return 0;
1029}
1030
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001031static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1032 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001033{
Paul Mackerras8f902b02015-03-20 20:39:38 +11001034 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001035 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1036 u64 mask;
1037
Paul Mackerras8f902b02015-03-20 20:39:38 +11001038 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001039 spin_lock(&vc->lock);
1040 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001041 * If ILE (interrupt little-endian) has changed, update the
1042 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1043 */
1044 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001045 struct kvm_vcpu *vcpu;
1046 int i;
1047
Anton Blanchardd6829162014-01-08 21:25:30 +11001048 kvm_for_each_vcpu(i, vcpu, kvm) {
1049 if (vcpu->arch.vcore != vc)
1050 continue;
1051 if (new_lpcr & LPCR_ILE)
1052 vcpu->arch.intr_msr |= MSR_LE;
1053 else
1054 vcpu->arch.intr_msr &= ~MSR_LE;
1055 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001056 }
1057
1058 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001059 * Userspace can only modify DPFD (default prefetch depth),
1060 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001061 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001062 */
1063 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001064 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1065 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001066
1067 /* Broken 32-bit version of LPCR must not clear top bits */
1068 if (preserve_top32)
1069 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001070 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1071 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001072 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001073}
1074
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301075static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1076 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001077{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001078 int r = 0;
1079 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001080
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001081 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301082 case KVM_REG_PPC_DEBUG_INST:
1083 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1084 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001085 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001086 *val = get_reg_val(id, 0);
1087 break;
1088 case KVM_REG_PPC_DABR:
1089 *val = get_reg_val(id, vcpu->arch.dabr);
1090 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001091 case KVM_REG_PPC_DABRX:
1092 *val = get_reg_val(id, vcpu->arch.dabrx);
1093 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001094 case KVM_REG_PPC_DSCR:
1095 *val = get_reg_val(id, vcpu->arch.dscr);
1096 break;
1097 case KVM_REG_PPC_PURR:
1098 *val = get_reg_val(id, vcpu->arch.purr);
1099 break;
1100 case KVM_REG_PPC_SPURR:
1101 *val = get_reg_val(id, vcpu->arch.spurr);
1102 break;
1103 case KVM_REG_PPC_AMR:
1104 *val = get_reg_val(id, vcpu->arch.amr);
1105 break;
1106 case KVM_REG_PPC_UAMOR:
1107 *val = get_reg_val(id, vcpu->arch.uamor);
1108 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001109 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001110 i = id - KVM_REG_PPC_MMCR0;
1111 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1112 break;
1113 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1114 i = id - KVM_REG_PPC_PMC1;
1115 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001116 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001117 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1118 i = id - KVM_REG_PPC_SPMC1;
1119 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1120 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001121 case KVM_REG_PPC_SIAR:
1122 *val = get_reg_val(id, vcpu->arch.siar);
1123 break;
1124 case KVM_REG_PPC_SDAR:
1125 *val = get_reg_val(id, vcpu->arch.sdar);
1126 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001127 case KVM_REG_PPC_SIER:
1128 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001129 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001130 case KVM_REG_PPC_IAMR:
1131 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001132 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001133 case KVM_REG_PPC_PSPB:
1134 *val = get_reg_val(id, vcpu->arch.pspb);
1135 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001136 case KVM_REG_PPC_DPDES:
1137 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1138 break;
1139 case KVM_REG_PPC_DAWR:
1140 *val = get_reg_val(id, vcpu->arch.dawr);
1141 break;
1142 case KVM_REG_PPC_DAWRX:
1143 *val = get_reg_val(id, vcpu->arch.dawrx);
1144 break;
1145 case KVM_REG_PPC_CIABR:
1146 *val = get_reg_val(id, vcpu->arch.ciabr);
1147 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001148 case KVM_REG_PPC_CSIGR:
1149 *val = get_reg_val(id, vcpu->arch.csigr);
1150 break;
1151 case KVM_REG_PPC_TACR:
1152 *val = get_reg_val(id, vcpu->arch.tacr);
1153 break;
1154 case KVM_REG_PPC_TCSCR:
1155 *val = get_reg_val(id, vcpu->arch.tcscr);
1156 break;
1157 case KVM_REG_PPC_PID:
1158 *val = get_reg_val(id, vcpu->arch.pid);
1159 break;
1160 case KVM_REG_PPC_ACOP:
1161 *val = get_reg_val(id, vcpu->arch.acop);
1162 break;
1163 case KVM_REG_PPC_WORT:
1164 *val = get_reg_val(id, vcpu->arch.wort);
1165 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001166 case KVM_REG_PPC_VPA_ADDR:
1167 spin_lock(&vcpu->arch.vpa_update_lock);
1168 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1169 spin_unlock(&vcpu->arch.vpa_update_lock);
1170 break;
1171 case KVM_REG_PPC_VPA_SLB:
1172 spin_lock(&vcpu->arch.vpa_update_lock);
1173 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1174 val->vpaval.length = vcpu->arch.slb_shadow.len;
1175 spin_unlock(&vcpu->arch.vpa_update_lock);
1176 break;
1177 case KVM_REG_PPC_VPA_DTL:
1178 spin_lock(&vcpu->arch.vpa_update_lock);
1179 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1180 val->vpaval.length = vcpu->arch.dtl.len;
1181 spin_unlock(&vcpu->arch.vpa_update_lock);
1182 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001183 case KVM_REG_PPC_TB_OFFSET:
1184 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1185 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001186 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001187 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001188 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1189 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001190 case KVM_REG_PPC_PPR:
1191 *val = get_reg_val(id, vcpu->arch.ppr);
1192 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001193#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1194 case KVM_REG_PPC_TFHAR:
1195 *val = get_reg_val(id, vcpu->arch.tfhar);
1196 break;
1197 case KVM_REG_PPC_TFIAR:
1198 *val = get_reg_val(id, vcpu->arch.tfiar);
1199 break;
1200 case KVM_REG_PPC_TEXASR:
1201 *val = get_reg_val(id, vcpu->arch.texasr);
1202 break;
1203 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1204 i = id - KVM_REG_PPC_TM_GPR0;
1205 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1206 break;
1207 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1208 {
1209 int j;
1210 i = id - KVM_REG_PPC_TM_VSR0;
1211 if (i < 32)
1212 for (j = 0; j < TS_FPRWIDTH; j++)
1213 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1214 else {
1215 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1216 val->vval = vcpu->arch.vr_tm.vr[i-32];
1217 else
1218 r = -ENXIO;
1219 }
1220 break;
1221 }
1222 case KVM_REG_PPC_TM_CR:
1223 *val = get_reg_val(id, vcpu->arch.cr_tm);
1224 break;
1225 case KVM_REG_PPC_TM_LR:
1226 *val = get_reg_val(id, vcpu->arch.lr_tm);
1227 break;
1228 case KVM_REG_PPC_TM_CTR:
1229 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1230 break;
1231 case KVM_REG_PPC_TM_FPSCR:
1232 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1233 break;
1234 case KVM_REG_PPC_TM_AMR:
1235 *val = get_reg_val(id, vcpu->arch.amr_tm);
1236 break;
1237 case KVM_REG_PPC_TM_PPR:
1238 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1239 break;
1240 case KVM_REG_PPC_TM_VRSAVE:
1241 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1242 break;
1243 case KVM_REG_PPC_TM_VSCR:
1244 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1245 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1246 else
1247 r = -ENXIO;
1248 break;
1249 case KVM_REG_PPC_TM_DSCR:
1250 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1251 break;
1252 case KVM_REG_PPC_TM_TAR:
1253 *val = get_reg_val(id, vcpu->arch.tar_tm);
1254 break;
1255#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001256 case KVM_REG_PPC_ARCH_COMPAT:
1257 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1258 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001259 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001260 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001261 break;
1262 }
1263
1264 return r;
1265}
1266
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301267static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1268 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001269{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001270 int r = 0;
1271 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001272 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001273
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001274 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001275 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001276 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001277 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001278 r = -EINVAL;
1279 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001280 case KVM_REG_PPC_DABR:
1281 vcpu->arch.dabr = set_reg_val(id, *val);
1282 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001283 case KVM_REG_PPC_DABRX:
1284 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1285 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001286 case KVM_REG_PPC_DSCR:
1287 vcpu->arch.dscr = set_reg_val(id, *val);
1288 break;
1289 case KVM_REG_PPC_PURR:
1290 vcpu->arch.purr = set_reg_val(id, *val);
1291 break;
1292 case KVM_REG_PPC_SPURR:
1293 vcpu->arch.spurr = set_reg_val(id, *val);
1294 break;
1295 case KVM_REG_PPC_AMR:
1296 vcpu->arch.amr = set_reg_val(id, *val);
1297 break;
1298 case KVM_REG_PPC_UAMOR:
1299 vcpu->arch.uamor = set_reg_val(id, *val);
1300 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001301 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001302 i = id - KVM_REG_PPC_MMCR0;
1303 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1304 break;
1305 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1306 i = id - KVM_REG_PPC_PMC1;
1307 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1308 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001309 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1310 i = id - KVM_REG_PPC_SPMC1;
1311 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1312 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001313 case KVM_REG_PPC_SIAR:
1314 vcpu->arch.siar = set_reg_val(id, *val);
1315 break;
1316 case KVM_REG_PPC_SDAR:
1317 vcpu->arch.sdar = set_reg_val(id, *val);
1318 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001319 case KVM_REG_PPC_SIER:
1320 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001321 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001322 case KVM_REG_PPC_IAMR:
1323 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001324 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001325 case KVM_REG_PPC_PSPB:
1326 vcpu->arch.pspb = set_reg_val(id, *val);
1327 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001328 case KVM_REG_PPC_DPDES:
1329 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1330 break;
1331 case KVM_REG_PPC_DAWR:
1332 vcpu->arch.dawr = set_reg_val(id, *val);
1333 break;
1334 case KVM_REG_PPC_DAWRX:
1335 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1336 break;
1337 case KVM_REG_PPC_CIABR:
1338 vcpu->arch.ciabr = set_reg_val(id, *val);
1339 /* Don't allow setting breakpoints in hypervisor code */
1340 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1341 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1342 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001343 case KVM_REG_PPC_CSIGR:
1344 vcpu->arch.csigr = set_reg_val(id, *val);
1345 break;
1346 case KVM_REG_PPC_TACR:
1347 vcpu->arch.tacr = set_reg_val(id, *val);
1348 break;
1349 case KVM_REG_PPC_TCSCR:
1350 vcpu->arch.tcscr = set_reg_val(id, *val);
1351 break;
1352 case KVM_REG_PPC_PID:
1353 vcpu->arch.pid = set_reg_val(id, *val);
1354 break;
1355 case KVM_REG_PPC_ACOP:
1356 vcpu->arch.acop = set_reg_val(id, *val);
1357 break;
1358 case KVM_REG_PPC_WORT:
1359 vcpu->arch.wort = set_reg_val(id, *val);
1360 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001361 case KVM_REG_PPC_VPA_ADDR:
1362 addr = set_reg_val(id, *val);
1363 r = -EINVAL;
1364 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1365 vcpu->arch.dtl.next_gpa))
1366 break;
1367 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1368 break;
1369 case KVM_REG_PPC_VPA_SLB:
1370 addr = val->vpaval.addr;
1371 len = val->vpaval.length;
1372 r = -EINVAL;
1373 if (addr && !vcpu->arch.vpa.next_gpa)
1374 break;
1375 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1376 break;
1377 case KVM_REG_PPC_VPA_DTL:
1378 addr = val->vpaval.addr;
1379 len = val->vpaval.length;
1380 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001381 if (addr && (len < sizeof(struct dtl_entry) ||
1382 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001383 break;
1384 len -= len % sizeof(struct dtl_entry);
1385 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1386 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001387 case KVM_REG_PPC_TB_OFFSET:
1388 /* round up to multiple of 2^24 */
1389 vcpu->arch.vcore->tb_offset =
1390 ALIGN(set_reg_val(id, *val), 1UL << 24);
1391 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001392 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001393 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1394 break;
1395 case KVM_REG_PPC_LPCR_64:
1396 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001397 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001398 case KVM_REG_PPC_PPR:
1399 vcpu->arch.ppr = set_reg_val(id, *val);
1400 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001401#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1402 case KVM_REG_PPC_TFHAR:
1403 vcpu->arch.tfhar = set_reg_val(id, *val);
1404 break;
1405 case KVM_REG_PPC_TFIAR:
1406 vcpu->arch.tfiar = set_reg_val(id, *val);
1407 break;
1408 case KVM_REG_PPC_TEXASR:
1409 vcpu->arch.texasr = set_reg_val(id, *val);
1410 break;
1411 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1412 i = id - KVM_REG_PPC_TM_GPR0;
1413 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1414 break;
1415 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1416 {
1417 int j;
1418 i = id - KVM_REG_PPC_TM_VSR0;
1419 if (i < 32)
1420 for (j = 0; j < TS_FPRWIDTH; j++)
1421 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1422 else
1423 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1424 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1425 else
1426 r = -ENXIO;
1427 break;
1428 }
1429 case KVM_REG_PPC_TM_CR:
1430 vcpu->arch.cr_tm = set_reg_val(id, *val);
1431 break;
1432 case KVM_REG_PPC_TM_LR:
1433 vcpu->arch.lr_tm = set_reg_val(id, *val);
1434 break;
1435 case KVM_REG_PPC_TM_CTR:
1436 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1437 break;
1438 case KVM_REG_PPC_TM_FPSCR:
1439 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1440 break;
1441 case KVM_REG_PPC_TM_AMR:
1442 vcpu->arch.amr_tm = set_reg_val(id, *val);
1443 break;
1444 case KVM_REG_PPC_TM_PPR:
1445 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1446 break;
1447 case KVM_REG_PPC_TM_VRSAVE:
1448 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1449 break;
1450 case KVM_REG_PPC_TM_VSCR:
1451 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1452 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1453 else
1454 r = - ENXIO;
1455 break;
1456 case KVM_REG_PPC_TM_DSCR:
1457 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1458 break;
1459 case KVM_REG_PPC_TM_TAR:
1460 vcpu->arch.tar_tm = set_reg_val(id, *val);
1461 break;
1462#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001463 case KVM_REG_PPC_ARCH_COMPAT:
1464 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1465 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001466 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001467 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001468 break;
1469 }
1470
1471 return r;
1472}
1473
Stewart Smithde9bdd12014-07-18 14:18:42 +10001474static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1475{
1476 struct kvmppc_vcore *vcore;
1477
1478 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1479
1480 if (vcore == NULL)
1481 return NULL;
1482
1483 INIT_LIST_HEAD(&vcore->runnable_threads);
1484 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001485 spin_lock_init(&vcore->stoltb_lock);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001486 init_waitqueue_head(&vcore->wq);
1487 vcore->preempt_tb = TB_NIL;
1488 vcore->lpcr = kvm->arch.lpcr;
1489 vcore->first_vcpuid = core * threads_per_subcore;
1490 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001491 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001492
1493 return vcore;
1494}
1495
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001496#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1497static struct debugfs_timings_element {
1498 const char *name;
1499 size_t offset;
1500} timings[] = {
1501 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1502 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1503 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1504 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1505 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1506};
1507
1508#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1509
1510struct debugfs_timings_state {
1511 struct kvm_vcpu *vcpu;
1512 unsigned int buflen;
1513 char buf[N_TIMINGS * 100];
1514};
1515
1516static int debugfs_timings_open(struct inode *inode, struct file *file)
1517{
1518 struct kvm_vcpu *vcpu = inode->i_private;
1519 struct debugfs_timings_state *p;
1520
1521 p = kzalloc(sizeof(*p), GFP_KERNEL);
1522 if (!p)
1523 return -ENOMEM;
1524
1525 kvm_get_kvm(vcpu->kvm);
1526 p->vcpu = vcpu;
1527 file->private_data = p;
1528
1529 return nonseekable_open(inode, file);
1530}
1531
1532static int debugfs_timings_release(struct inode *inode, struct file *file)
1533{
1534 struct debugfs_timings_state *p = file->private_data;
1535
1536 kvm_put_kvm(p->vcpu->kvm);
1537 kfree(p);
1538 return 0;
1539}
1540
1541static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1542 size_t len, loff_t *ppos)
1543{
1544 struct debugfs_timings_state *p = file->private_data;
1545 struct kvm_vcpu *vcpu = p->vcpu;
1546 char *s, *buf_end;
1547 struct kvmhv_tb_accumulator tb;
1548 u64 count;
1549 loff_t pos;
1550 ssize_t n;
1551 int i, loops;
1552 bool ok;
1553
1554 if (!p->buflen) {
1555 s = p->buf;
1556 buf_end = s + sizeof(p->buf);
1557 for (i = 0; i < N_TIMINGS; ++i) {
1558 struct kvmhv_tb_accumulator *acc;
1559
1560 acc = (struct kvmhv_tb_accumulator *)
1561 ((unsigned long)vcpu + timings[i].offset);
1562 ok = false;
1563 for (loops = 0; loops < 1000; ++loops) {
1564 count = acc->seqcount;
1565 if (!(count & 1)) {
1566 smp_rmb();
1567 tb = *acc;
1568 smp_rmb();
1569 if (count == acc->seqcount) {
1570 ok = true;
1571 break;
1572 }
1573 }
1574 udelay(1);
1575 }
1576 if (!ok)
1577 snprintf(s, buf_end - s, "%s: stuck\n",
1578 timings[i].name);
1579 else
1580 snprintf(s, buf_end - s,
1581 "%s: %llu %llu %llu %llu\n",
1582 timings[i].name, count / 2,
1583 tb_to_ns(tb.tb_total),
1584 tb_to_ns(tb.tb_min),
1585 tb_to_ns(tb.tb_max));
1586 s += strlen(s);
1587 }
1588 p->buflen = s - p->buf;
1589 }
1590
1591 pos = *ppos;
1592 if (pos >= p->buflen)
1593 return 0;
1594 if (len > p->buflen - pos)
1595 len = p->buflen - pos;
1596 n = copy_to_user(buf, p->buf + pos, len);
1597 if (n) {
1598 if (n == len)
1599 return -EFAULT;
1600 len -= n;
1601 }
1602 *ppos = pos + len;
1603 return len;
1604}
1605
1606static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1607 size_t len, loff_t *ppos)
1608{
1609 return -EACCES;
1610}
1611
1612static const struct file_operations debugfs_timings_ops = {
1613 .owner = THIS_MODULE,
1614 .open = debugfs_timings_open,
1615 .release = debugfs_timings_release,
1616 .read = debugfs_timings_read,
1617 .write = debugfs_timings_write,
1618 .llseek = generic_file_llseek,
1619};
1620
1621/* Create a debugfs directory for the vcpu */
1622static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1623{
1624 char buf[16];
1625 struct kvm *kvm = vcpu->kvm;
1626
1627 snprintf(buf, sizeof(buf), "vcpu%u", id);
1628 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1629 return;
1630 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1631 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1632 return;
1633 vcpu->arch.debugfs_timings =
1634 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1635 vcpu, &debugfs_timings_ops);
1636}
1637
1638#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1639static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1640{
1641}
1642#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1643
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301644static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1645 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001646{
1647 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001648 int err = -EINVAL;
1649 int core;
1650 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001651
Michael Ellerman3102f782014-05-23 18:15:29 +10001652 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001653 if (core >= KVM_MAX_VCORES)
1654 goto out;
1655
1656 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001657 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001658 if (!vcpu)
1659 goto out;
1660
1661 err = kvm_vcpu_init(vcpu, kvm, id);
1662 if (err)
1663 goto free_vcpu;
1664
1665 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001666#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1667 /*
1668 * The shared struct is never shared on HV,
1669 * so we can always use host endianness
1670 */
1671#ifdef __BIG_ENDIAN__
1672 vcpu->arch.shared_big_endian = true;
1673#else
1674 vcpu->arch.shared_big_endian = false;
1675#endif
1676#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001677 vcpu->arch.mmcr[0] = MMCR0_FC;
1678 vcpu->arch.ctrl = CTRL_RUNLATCH;
1679 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301680 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001681 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001682 spin_lock_init(&vcpu->arch.tbacct_lock);
1683 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001684 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001685
Paul Mackerrasde56a942011-06-29 00:21:34 +00001686 kvmppc_mmu_book3s_hv_init(vcpu);
1687
Paul Mackerras8455d792012-10-15 01:17:42 +00001688 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001689
1690 init_waitqueue_head(&vcpu->arch.cpu_run);
1691
1692 mutex_lock(&kvm->lock);
1693 vcore = kvm->arch.vcores[core];
1694 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001695 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001696 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001697 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001698 }
1699 mutex_unlock(&kvm->lock);
1700
1701 if (!vcore)
1702 goto free_vcpu;
1703
1704 spin_lock(&vcore->lock);
1705 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001706 spin_unlock(&vcore->lock);
1707 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001708 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001709 vcpu->arch.thread_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001710
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001711 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1712 kvmppc_sanity_check(vcpu);
1713
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001714 debugfs_vcpu_init(vcpu, id);
1715
Paul Mackerrasde56a942011-06-29 00:21:34 +00001716 return vcpu;
1717
1718free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001719 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001720out:
1721 return ERR_PTR(err);
1722}
1723
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001724static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1725{
1726 if (vpa->pinned_addr)
1727 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1728 vpa->dirty);
1729}
1730
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301731static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001732{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001733 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001734 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1735 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1736 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001737 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001738 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001739 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001740}
1741
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301742static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1743{
1744 /* Indicate we want to get back into the guest */
1745 return 1;
1746}
1747
Paul Mackerras19ccb762011-07-23 17:42:46 +10001748static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001749{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001750 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001751
Paul Mackerras19ccb762011-07-23 17:42:46 +10001752 now = get_tb();
1753 if (now > vcpu->arch.dec_expires) {
1754 /* decrementer has already gone negative */
1755 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001756 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001757 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001758 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001759 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1760 / tb_ticks_per_sec;
1761 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1762 HRTIMER_MODE_REL);
1763 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001764}
1765
Paul Mackerras19ccb762011-07-23 17:42:46 +10001766static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001767{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001768 vcpu->arch.ceded = 0;
1769 if (vcpu->arch.timer_running) {
1770 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1771 vcpu->arch.timer_running = 0;
1772 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001773}
1774
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001775extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001776
1777static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1778 struct kvm_vcpu *vcpu)
1779{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001780 u64 now;
1781
Paul Mackerras371fefd2011-06-29 00:23:08 +00001782 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1783 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001784 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001785 now = mftb();
1786 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1787 vcpu->arch.stolen_logged;
1788 vcpu->arch.busy_preempt = now;
1789 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001790 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001791 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001792 list_del(&vcpu->arch.run_list);
1793}
1794
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001795static int kvmppc_grab_hwthread(int cpu)
1796{
1797 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001798 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001799
1800 tpaca = &paca[cpu];
1801
1802 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00001803 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001804 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001805 tpaca->kvm_hstate.napping = 0;
1806 smp_wmb();
1807 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001808
1809 /*
1810 * If the thread is already executing in the kernel (e.g. handling
1811 * a stray interrupt), wait for it to get back to nap mode.
1812 * The smp_mb() is to ensure that our setting of hwthread_req
1813 * is visible before we look at hwthread_state, so if this
1814 * races with the code at system_reset_pSeries and the thread
1815 * misses our setting of hwthread_req, we are sure to see its
1816 * setting of hwthread_state, and vice versa.
1817 */
1818 smp_mb();
1819 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1820 if (--timeout <= 0) {
1821 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1822 return -EBUSY;
1823 }
1824 udelay(1);
1825 }
1826 return 0;
1827}
1828
1829static void kvmppc_release_hwthread(int cpu)
1830{
1831 struct paca_struct *tpaca;
1832
1833 tpaca = &paca[cpu];
1834 tpaca->kvm_hstate.hwthread_req = 0;
1835 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001836 tpaca->kvm_hstate.kvm_vcore = NULL;
1837 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001838}
1839
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001840static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001841{
1842 int cpu;
1843 struct paca_struct *tpaca;
Paul Mackerrasec257162015-06-24 21:18:03 +10001844 struct kvmppc_vcore *mvc = vc->master_vcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001845
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001846 cpu = vc->pcpu;
1847 if (vcpu) {
1848 if (vcpu->arch.timer_running) {
1849 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1850 vcpu->arch.timer_running = 0;
1851 }
1852 cpu += vcpu->arch.ptid;
1853 vcpu->cpu = mvc->pcpu;
1854 vcpu->arch.thread_cpu = cpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001855 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001856 tpaca = &paca[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001857 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001858 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001859 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001860 smp_wmb();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001861 tpaca->kvm_hstate.kvm_vcore = mvc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001862 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11001863 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001864}
1865
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001866static void kvmppc_wait_for_nap(void)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001867{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001868 int cpu = smp_processor_id();
1869 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001870
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001871 for (loops = 0; loops < 1000000; ++loops) {
1872 /*
1873 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001874 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001875 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001876 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001877 */
1878 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001879 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001880 break;
1881 if (i == threads_per_subcore) {
1882 HMT_medium();
1883 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001884 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001885 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001886 }
1887 HMT_medium();
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001888 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001889 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001890 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001891}
1892
1893/*
1894 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001895 * this core are off-line. Then grab the threads so they can't
1896 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001897 */
1898static int on_primary_thread(void)
1899{
1900 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001901 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001902
Michael Ellerman3102f782014-05-23 18:15:29 +10001903 /* Are we on a primary subcore? */
1904 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001905 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001906
1907 thr = 0;
1908 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001909 if (cpu_online(cpu + thr))
1910 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001911
1912 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001913 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001914 if (kvmppc_grab_hwthread(cpu + thr)) {
1915 /* Couldn't grab one; let the others go */
1916 do {
1917 kvmppc_release_hwthread(cpu + thr);
1918 } while (--thr > 0);
1919 return 0;
1920 }
1921 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001922 return 1;
1923}
1924
Paul Mackerrasec257162015-06-24 21:18:03 +10001925/*
1926 * A list of virtual cores for each physical CPU.
1927 * These are vcores that could run but their runner VCPU tasks are
1928 * (or may be) preempted.
1929 */
1930struct preempted_vcore_list {
1931 struct list_head list;
1932 spinlock_t lock;
1933};
1934
1935static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
1936
1937static void init_vcore_lists(void)
1938{
1939 int cpu;
1940
1941 for_each_possible_cpu(cpu) {
1942 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
1943 spin_lock_init(&lp->lock);
1944 INIT_LIST_HEAD(&lp->list);
1945 }
1946}
1947
1948static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
1949{
1950 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
1951
1952 vc->vcore_state = VCORE_PREEMPT;
1953 vc->pcpu = smp_processor_id();
1954 if (vc->num_threads < threads_per_subcore) {
1955 spin_lock(&lp->lock);
1956 list_add_tail(&vc->preempt_list, &lp->list);
1957 spin_unlock(&lp->lock);
1958 }
1959
1960 /* Start accumulating stolen time */
1961 kvmppc_core_start_stolen(vc);
1962}
1963
1964static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
1965{
Paul Mackerras402813f2015-07-16 17:11:13 +10001966 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10001967
1968 kvmppc_core_end_stolen(vc);
1969 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10001970 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10001971 spin_lock(&lp->lock);
1972 list_del_init(&vc->preempt_list);
1973 spin_unlock(&lp->lock);
1974 }
1975 vc->vcore_state = VCORE_INACTIVE;
1976}
1977
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001978/*
1979 * This stores information about the virtual cores currently
1980 * assigned to a physical core.
1981 */
Paul Mackerrasec257162015-06-24 21:18:03 +10001982struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001983 int n_subcores;
1984 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10001985 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001986 int subcore_threads[MAX_SUBCORES];
1987 struct kvm *subcore_vm[MAX_SUBCORES];
1988 struct list_head vcs[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10001989};
1990
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001991/*
1992 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
1993 * respectively in 2-way micro-threading (split-core) mode.
1994 */
1995static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
1996
Paul Mackerrasec257162015-06-24 21:18:03 +10001997static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
1998{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001999 int sub;
2000
Paul Mackerrasec257162015-06-24 21:18:03 +10002001 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002002 cip->n_subcores = 1;
2003 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002004 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002005 cip->subcore_threads[0] = vc->num_threads;
2006 cip->subcore_vm[0] = vc->kvm;
2007 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2008 INIT_LIST_HEAD(&cip->vcs[sub]);
2009 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2010}
2011
2012static bool subcore_config_ok(int n_subcores, int n_threads)
2013{
2014 /* Can only dynamically split if unsplit to begin with */
2015 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2016 return false;
2017 if (n_subcores > MAX_SUBCORES)
2018 return false;
2019 if (n_subcores > 1) {
2020 if (!(dynamic_mt_modes & 2))
2021 n_subcores = 4;
2022 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2023 return false;
2024 }
2025
2026 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10002027}
2028
2029static void init_master_vcore(struct kvmppc_vcore *vc)
2030{
2031 vc->master_vcore = vc;
2032 vc->entry_exit_map = 0;
2033 vc->in_guest = 0;
2034 vc->napping_threads = 0;
2035 vc->conferring_threads = 0;
2036}
2037
2038/*
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002039 * See if the existing subcores can be split into 3 (or fewer) subcores
2040 * of at most two threads each, so we can fit in another vcore. This
2041 * assumes there are at most two subcores and at most 6 threads in total.
Paul Mackerrasec257162015-06-24 21:18:03 +10002042 */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002043static bool can_split_piggybacked_subcores(struct core_info *cip)
2044{
2045 int sub, new_sub;
2046 int large_sub = -1;
2047 int thr;
2048 int n_subcores = cip->n_subcores;
2049 struct kvmppc_vcore *vc, *vcnext;
2050 struct kvmppc_vcore *master_vc = NULL;
2051
2052 for (sub = 0; sub < cip->n_subcores; ++sub) {
2053 if (cip->subcore_threads[sub] <= 2)
2054 continue;
2055 if (large_sub >= 0)
2056 return false;
2057 large_sub = sub;
2058 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2059 preempt_list);
2060 if (vc->num_threads > 2)
2061 return false;
2062 n_subcores += (cip->subcore_threads[sub] - 1) >> 1;
2063 }
Paul Mackerrasf74f2e22015-11-03 16:03:30 +11002064 if (large_sub < 0 || !subcore_config_ok(n_subcores + 1, 2))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002065 return false;
2066
2067 /*
2068 * Seems feasible, so go through and move vcores to new subcores.
2069 * Note that when we have two or more vcores in one subcore,
2070 * all those vcores must have only one thread each.
2071 */
2072 new_sub = cip->n_subcores;
2073 thr = 0;
2074 sub = large_sub;
2075 list_for_each_entry_safe(vc, vcnext, &cip->vcs[sub], preempt_list) {
2076 if (thr >= 2) {
2077 list_del(&vc->preempt_list);
2078 list_add_tail(&vc->preempt_list, &cip->vcs[new_sub]);
2079 /* vc->num_threads must be 1 */
2080 if (++cip->subcore_threads[new_sub] == 1) {
2081 cip->subcore_vm[new_sub] = vc->kvm;
2082 init_master_vcore(vc);
2083 master_vc = vc;
2084 ++cip->n_subcores;
2085 } else {
2086 vc->master_vcore = master_vc;
2087 ++new_sub;
2088 }
2089 }
2090 thr += vc->num_threads;
2091 }
2092 cip->subcore_threads[large_sub] = 2;
2093 cip->max_subcore_threads = 2;
2094
2095 return true;
2096}
2097
2098static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2099{
2100 int n_threads = vc->num_threads;
2101 int sub;
2102
2103 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2104 return false;
2105
2106 if (n_threads < cip->max_subcore_threads)
2107 n_threads = cip->max_subcore_threads;
2108 if (subcore_config_ok(cip->n_subcores + 1, n_threads)) {
2109 cip->max_subcore_threads = n_threads;
2110 } else if (cip->n_subcores <= 2 && cip->total_threads <= 6 &&
2111 vc->num_threads <= 2) {
2112 /*
2113 * We may be able to fit another subcore in by
2114 * splitting an existing subcore with 3 or 4
2115 * threads into two 2-thread subcores, or one
2116 * with 5 or 6 threads into three subcores.
2117 * We can only do this if those subcores have
2118 * piggybacked virtual cores.
2119 */
2120 if (!can_split_piggybacked_subcores(cip))
2121 return false;
2122 } else {
2123 return false;
2124 }
2125
2126 sub = cip->n_subcores;
2127 ++cip->n_subcores;
2128 cip->total_threads += vc->num_threads;
2129 cip->subcore_threads[sub] = vc->num_threads;
2130 cip->subcore_vm[sub] = vc->kvm;
2131 init_master_vcore(vc);
2132 list_del(&vc->preempt_list);
2133 list_add_tail(&vc->preempt_list, &cip->vcs[sub]);
2134
2135 return true;
2136}
2137
2138static bool can_piggyback_subcore(struct kvmppc_vcore *pvc,
2139 struct core_info *cip, int sub)
Paul Mackerrasec257162015-06-24 21:18:03 +10002140{
2141 struct kvmppc_vcore *vc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002142 int n_thr;
Paul Mackerrasec257162015-06-24 21:18:03 +10002143
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002144 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2145 preempt_list);
Paul Mackerrasec257162015-06-24 21:18:03 +10002146
2147 /* require same VM and same per-core reg values */
2148 if (pvc->kvm != vc->kvm ||
2149 pvc->tb_offset != vc->tb_offset ||
2150 pvc->pcr != vc->pcr ||
2151 pvc->lpcr != vc->lpcr)
2152 return false;
2153
2154 /* P8 guest with > 1 thread per core would see wrong TIR value */
2155 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
2156 (vc->num_threads > 1 || pvc->num_threads > 1))
2157 return false;
2158
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002159 n_thr = cip->subcore_threads[sub] + pvc->num_threads;
2160 if (n_thr > cip->max_subcore_threads) {
2161 if (!subcore_config_ok(cip->n_subcores, n_thr))
2162 return false;
2163 cip->max_subcore_threads = n_thr;
2164 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002165
2166 cip->total_threads += pvc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002167 cip->subcore_threads[sub] = n_thr;
Paul Mackerrasec257162015-06-24 21:18:03 +10002168 pvc->master_vcore = vc;
2169 list_del(&pvc->preempt_list);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002170 list_add_tail(&pvc->preempt_list, &cip->vcs[sub]);
Paul Mackerrasec257162015-06-24 21:18:03 +10002171
2172 return true;
2173}
2174
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002175/*
2176 * Work out whether it is possible to piggyback the execution of
2177 * vcore *pvc onto the execution of the other vcores described in *cip.
2178 */
2179static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2180 int target_threads)
2181{
2182 int sub;
2183
2184 if (cip->total_threads + pvc->num_threads > target_threads)
2185 return false;
2186 for (sub = 0; sub < cip->n_subcores; ++sub)
2187 if (cip->subcore_threads[sub] &&
2188 can_piggyback_subcore(pvc, cip, sub))
2189 return true;
2190
2191 if (can_dynamic_split(pvc, cip))
2192 return true;
2193
2194 return false;
2195}
2196
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002197static void prepare_threads(struct kvmppc_vcore *vc)
2198{
2199 struct kvm_vcpu *vcpu, *vnext;
2200
2201 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2202 arch.run_list) {
2203 if (signal_pending(vcpu->arch.run_task))
2204 vcpu->arch.ret = -EINTR;
2205 else if (vcpu->arch.vpa.update_pending ||
2206 vcpu->arch.slb_shadow.update_pending ||
2207 vcpu->arch.dtl.update_pending)
2208 vcpu->arch.ret = RESUME_GUEST;
2209 else
2210 continue;
2211 kvmppc_remove_runnable(vc, vcpu);
2212 wake_up(&vcpu->arch.cpu_run);
2213 }
2214}
2215
Paul Mackerrasec257162015-06-24 21:18:03 +10002216static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002217{
Paul Mackerrasec257162015-06-24 21:18:03 +10002218 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2219 struct kvmppc_vcore *pvc, *vcnext;
2220
2221 spin_lock(&lp->lock);
2222 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2223 if (!spin_trylock(&pvc->lock))
2224 continue;
2225 prepare_threads(pvc);
2226 if (!pvc->n_runnable) {
2227 list_del_init(&pvc->preempt_list);
2228 if (pvc->runner == NULL) {
2229 pvc->vcore_state = VCORE_INACTIVE;
2230 kvmppc_core_end_stolen(pvc);
2231 }
2232 spin_unlock(&pvc->lock);
2233 continue;
2234 }
2235 if (!can_piggyback(pvc, cip, target_threads)) {
2236 spin_unlock(&pvc->lock);
2237 continue;
2238 }
2239 kvmppc_core_end_stolen(pvc);
2240 pvc->vcore_state = VCORE_PIGGYBACK;
2241 if (cip->total_threads >= target_threads)
2242 break;
2243 }
2244 spin_unlock(&lp->lock);
2245}
2246
2247static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2248{
2249 int still_running = 0;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002250 u64 now;
2251 long ret;
2252 struct kvm_vcpu *vcpu, *vnext;
2253
Paul Mackerrasec257162015-06-24 21:18:03 +10002254 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002255 now = get_tb();
2256 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2257 arch.run_list) {
2258 /* cancel pending dec exception if dec is positive */
2259 if (now < vcpu->arch.dec_expires &&
2260 kvmppc_core_pending_dec(vcpu))
2261 kvmppc_core_dequeue_dec(vcpu);
2262
2263 trace_kvm_guest_exit(vcpu);
2264
2265 ret = RESUME_GUEST;
2266 if (vcpu->arch.trap)
2267 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2268 vcpu->arch.run_task);
2269
2270 vcpu->arch.ret = ret;
2271 vcpu->arch.trap = 0;
2272
Paul Mackerrasec257162015-06-24 21:18:03 +10002273 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2274 if (vcpu->arch.pending_exceptions)
2275 kvmppc_core_prepare_to_enter(vcpu);
2276 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002277 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002278 else
2279 ++still_running;
2280 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002281 kvmppc_remove_runnable(vc, vcpu);
2282 wake_up(&vcpu->arch.cpu_run);
2283 }
2284 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002285 list_del_init(&vc->preempt_list);
2286 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10002287 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002288 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10002289 } else if (vc->runner) {
2290 vc->vcore_state = VCORE_PREEMPT;
2291 kvmppc_core_start_stolen(vc);
2292 } else {
2293 vc->vcore_state = VCORE_INACTIVE;
2294 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002295 if (vc->n_runnable > 0 && vc->runner == NULL) {
2296 /* make sure there's a candidate runner awake */
2297 vcpu = list_first_entry(&vc->runnable_threads,
2298 struct kvm_vcpu, arch.run_list);
2299 wake_up(&vcpu->arch.cpu_run);
2300 }
2301 }
2302 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002303}
2304
Paul Mackerras371fefd2011-06-29 00:23:08 +00002305/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002306 * Clear core from the list of active host cores as we are about to
2307 * enter the guest. Only do this if it is the primary thread of the
2308 * core (not if a subcore) that is entering the guest.
2309 */
2310static inline void kvmppc_clear_host_core(int cpu)
2311{
2312 int core;
2313
2314 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2315 return;
2316 /*
2317 * Memory barrier can be omitted here as we will do a smp_wmb()
2318 * later in kvmppc_start_thread and we need ensure that state is
2319 * visible to other CPUs only after we enter guest.
2320 */
2321 core = cpu >> threads_shift;
2322 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
2323}
2324
2325/*
2326 * Advertise this core as an active host core since we exited the guest
2327 * Only need to do this if it is the primary thread of the core that is
2328 * exiting.
2329 */
2330static inline void kvmppc_set_host_core(int cpu)
2331{
2332 int core;
2333
2334 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2335 return;
2336
2337 /*
2338 * Memory barrier can be omitted here because we do a spin_unlock
2339 * immediately after this which provides the memory barrier.
2340 */
2341 core = cpu >> threads_shift;
2342 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
2343}
2344
2345/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00002346 * Run a set of guest threads on a physical core.
2347 * Called with vc->lock held.
2348 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002349static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002350{
Paul Mackerras17d48902015-04-29 14:49:07 +10002351 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002352 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002353 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002354 struct core_info core_info;
2355 struct kvmppc_vcore *pvc, *vcnext;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002356 struct kvm_split_mode split_info, *sip;
2357 int split, subcore_size, active;
2358 int sub;
2359 bool thr0_done;
2360 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10002361 int pcpu, thr;
2362 int target_threads;
Paul Mackerras081f3232012-06-01 20:20:24 +10002363
2364 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002365 * Remove from the list any threads that have a signal pending
2366 * or need a VPA update done
2367 */
2368 prepare_threads(vc);
2369
2370 /* if the runner is no longer runnable, let the caller pick a new one */
2371 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2372 return;
2373
2374 /*
2375 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002376 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002377 init_master_vcore(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002378 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002379
2380 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002381 * Make sure we are running on primary threads, and that secondary
2382 * threads are offline. Also check if the number of threads in this
2383 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002384 */
Michael Ellerman3102f782014-05-23 18:15:29 +10002385 if ((threads_per_core > 1) &&
2386 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras17d48902015-04-29 14:49:07 +10002387 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2388 arch.run_list) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002389 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002390 kvmppc_remove_runnable(vc, vcpu);
2391 wake_up(&vcpu->arch.cpu_run);
2392 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002393 goto out;
2394 }
2395
Paul Mackerrasec257162015-06-24 21:18:03 +10002396 /*
2397 * See if we could run any other vcores on the physical core
2398 * along with this one.
2399 */
2400 init_core_info(&core_info, vc);
2401 pcpu = smp_processor_id();
2402 target_threads = threads_per_subcore;
2403 if (target_smt_mode && target_smt_mode < target_threads)
2404 target_threads = target_smt_mode;
2405 if (vc->num_threads < target_threads)
2406 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002407
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002408 /* Decide on micro-threading (split-core) mode */
2409 subcore_size = threads_per_subcore;
2410 cmd_bit = stat_bit = 0;
2411 split = core_info.n_subcores;
2412 sip = NULL;
2413 if (split > 1) {
2414 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2415 if (split == 2 && (dynamic_mt_modes & 2)) {
2416 cmd_bit = HID0_POWER8_1TO2LPAR;
2417 stat_bit = HID0_POWER8_2LPARMODE;
2418 } else {
2419 split = 4;
2420 cmd_bit = HID0_POWER8_1TO4LPAR;
2421 stat_bit = HID0_POWER8_4LPARMODE;
Paul Mackerrasec257162015-06-24 21:18:03 +10002422 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002423 subcore_size = MAX_SMT_THREADS / split;
2424 sip = &split_info;
2425 memset(&split_info, 0, sizeof(split_info));
2426 split_info.rpr = mfspr(SPRN_RPR);
2427 split_info.pmmar = mfspr(SPRN_PMMAR);
2428 split_info.ldbar = mfspr(SPRN_LDBAR);
2429 split_info.subcore_size = subcore_size;
2430 for (sub = 0; sub < core_info.n_subcores; ++sub)
2431 split_info.master_vcs[sub] =
2432 list_first_entry(&core_info.vcs[sub],
2433 struct kvmppc_vcore, preempt_list);
2434 /* order writes to split_info before kvm_split_mode pointer */
2435 smp_wmb();
2436 }
2437 pcpu = smp_processor_id();
2438 for (thr = 0; thr < threads_per_subcore; ++thr)
2439 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2440
2441 /* Initiate micro-threading (split-core) if required */
2442 if (cmd_bit) {
2443 unsigned long hid0 = mfspr(SPRN_HID0);
2444
2445 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2446 mb();
2447 mtspr(SPRN_HID0, hid0);
2448 isync();
2449 for (;;) {
2450 hid0 = mfspr(SPRN_HID0);
2451 if (hid0 & stat_bit)
2452 break;
2453 cpu_relax();
2454 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002455 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002456
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002457 kvmppc_clear_host_core(pcpu);
2458
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002459 /* Start all the threads */
2460 active = 0;
2461 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2462 thr = subcore_thread_map[sub];
2463 thr0_done = false;
2464 active |= 1 << thr;
2465 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2466 pvc->pcpu = pcpu + thr;
2467 list_for_each_entry(vcpu, &pvc->runnable_threads,
2468 arch.run_list) {
2469 kvmppc_start_thread(vcpu, pvc);
2470 kvmppc_create_dtl_entry(vcpu, pvc);
2471 trace_kvm_guest_enter(vcpu);
2472 if (!vcpu->arch.ptid)
2473 thr0_done = true;
2474 active |= 1 << (thr + vcpu->arch.ptid);
2475 }
2476 /*
2477 * We need to start the first thread of each subcore
2478 * even if it doesn't have a vcpu.
2479 */
2480 if (pvc->master_vcore == pvc && !thr0_done)
2481 kvmppc_start_thread(NULL, pvc);
2482 thr += pvc->num_threads;
2483 }
2484 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302485
2486 /*
2487 * Ensure that split_info.do_nap is set after setting
2488 * the vcore pointer in the PACA of the secondaries.
2489 */
2490 smp_mb();
2491 if (cmd_bit)
2492 split_info.do_nap = 1; /* ask secondaries to nap when done */
2493
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002494 /*
2495 * When doing micro-threading, poke the inactive threads as well.
2496 * This gets them to the nap instruction after kvm_do_nap,
2497 * which reduces the time taken to unsplit later.
2498 */
2499 if (split > 1)
2500 for (thr = 1; thr < threads_per_subcore; ++thr)
2501 if (!(active & (1 << thr)))
2502 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002503
Paul Mackerras2f12f032012-10-15 01:17:17 +00002504 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002505 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002506
2507 trace_kvmppc_run_core(vc, 0);
2508
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002509 for (sub = 0; sub < core_info.n_subcores; ++sub)
2510 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2511 spin_unlock(&pvc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002512
Paul Mackerras19ccb762011-07-23 17:42:46 +10002513 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002514
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002515 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002516
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002517 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002518
Paul Mackerrasec257162015-06-24 21:18:03 +10002519 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2520
2521 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002522 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002523 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002524
Paul Mackerras371fefd2011-06-29 00:23:08 +00002525 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002526 kvmppc_wait_for_nap();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002527
2528 /* Return to whole-core mode if we split the core earlier */
2529 if (split > 1) {
2530 unsigned long hid0 = mfspr(SPRN_HID0);
2531 unsigned long loops = 0;
2532
2533 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2534 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2535 mb();
2536 mtspr(SPRN_HID0, hid0);
2537 isync();
2538 for (;;) {
2539 hid0 = mfspr(SPRN_HID0);
2540 if (!(hid0 & stat_bit))
2541 break;
2542 cpu_relax();
2543 ++loops;
2544 }
2545 split_info.do_nap = 0;
2546 }
2547
2548 /* Let secondaries go back to the offline loop */
2549 for (i = 0; i < threads_per_subcore; ++i) {
2550 kvmppc_release_hwthread(pcpu + i);
2551 if (sip && sip->napped[i])
2552 kvmppc_ipi_thread(pcpu + i);
2553 }
2554
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002555 kvmppc_set_host_core(pcpu);
2556
Paul Mackerras371fefd2011-06-29 00:23:08 +00002557 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002558
Paul Mackerras371fefd2011-06-29 00:23:08 +00002559 /* make sure updates to secondary vcpu structs are visible now */
2560 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002561 kvm_guest_exit();
2562
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002563 for (sub = 0; sub < core_info.n_subcores; ++sub)
2564 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2565 preempt_list)
2566 post_guest_process(pvc, pvc == vc);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002567
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002568 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002569 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002570
Paul Mackerrasde56a942011-06-29 00:21:34 +00002571 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002572 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002573 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002574}
2575
Paul Mackerras19ccb762011-07-23 17:42:46 +10002576/*
2577 * Wait for some other vcpu thread to execute us, and
2578 * wake us up when we need to handle something in the host.
2579 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002580static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2581 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002582{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002583 DEFINE_WAIT(wait);
2584
Paul Mackerras19ccb762011-07-23 17:42:46 +10002585 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002586 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2587 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002588 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002589 spin_lock(&vc->lock);
2590 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002591 finish_wait(&vcpu->arch.cpu_run, &wait);
2592}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002593
Paul Mackerras19ccb762011-07-23 17:42:46 +10002594/*
2595 * All the vcpus in this vcore are idle, so wait for a decrementer
2596 * or external interrupt to one of the vcpus. vc->lock is held.
2597 */
2598static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2599{
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002600 struct kvm_vcpu *vcpu;
2601 int do_sleep = 1;
2602
Paul Mackerras19ccb762011-07-23 17:42:46 +10002603 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002604
2605 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002606
2607 /*
2608 * Check one last time for pending exceptions and ceded state after
2609 * we put ourselves on the wait queue
2610 */
2611 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
2612 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
2613 do_sleep = 0;
2614 break;
2615 }
2616 }
2617
2618 if (!do_sleep) {
2619 finish_wait(&vc->wq, &wait);
2620 return;
2621 }
2622
Paul Mackerras19ccb762011-07-23 17:42:46 +10002623 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002624 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002625 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002626 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002627 finish_wait(&vc->wq, &wait);
2628 spin_lock(&vc->lock);
2629 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002630 trace_kvmppc_vcore_blocked(vc, 1);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002631}
2632
2633static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2634{
2635 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002636 struct kvmppc_vcore *vc;
2637 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002638
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002639 trace_kvmppc_run_vcpu_enter(vcpu);
2640
Paul Mackerras371fefd2011-06-29 00:23:08 +00002641 kvm_run->exit_reason = 0;
2642 vcpu->arch.ret = RESUME_GUEST;
2643 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00002644 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002645
Paul Mackerras371fefd2011-06-29 00:23:08 +00002646 /*
2647 * Synchronize with other threads in this virtual core
2648 */
2649 vc = vcpu->arch.vcore;
2650 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002651 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002652 vcpu->arch.run_task = current;
2653 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002654 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10002655 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002656 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002657 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
2658 ++vc->n_runnable;
2659
Paul Mackerras19ccb762011-07-23 17:42:46 +10002660 /*
2661 * This happens the first time this is called for a vcpu.
2662 * If the vcore is already running, we may be able to start
2663 * this thread straight away and have it join in.
2664 */
Paul Mackerras8455d792012-10-15 01:17:42 +00002665 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002666 if (vc->vcore_state == VCORE_PIGGYBACK) {
2667 struct kvmppc_vcore *mvc = vc->master_vcore;
2668 if (spin_trylock(&mvc->lock)) {
2669 if (mvc->vcore_state == VCORE_RUNNING &&
2670 !VCORE_IS_EXITING(mvc)) {
2671 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002672 kvmppc_start_thread(vcpu, vc);
Paul Mackerrasec257162015-06-24 21:18:03 +10002673 trace_kvm_guest_enter(vcpu);
2674 }
2675 spin_unlock(&mvc->lock);
2676 }
2677 } else if (vc->vcore_state == VCORE_RUNNING &&
2678 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00002679 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002680 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002681 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00002682 } else if (vc->vcore_state == VCORE_SLEEPING) {
2683 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002684 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002685
Paul Mackerras8455d792012-10-15 01:17:42 +00002686 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002687
2688 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2689 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002690 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2691 kvmppc_vcore_end_preempt(vc);
2692
Paul Mackerras8455d792012-10-15 01:17:42 +00002693 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002694 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002695 continue;
2696 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002697 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
2698 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06002699 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002700 if (signal_pending(v->arch.run_task)) {
2701 kvmppc_remove_runnable(vc, v);
2702 v->stat.signal_exits++;
2703 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2704 v->arch.ret = -EINTR;
2705 wake_up(&v->arch.cpu_run);
2706 }
2707 }
Paul Mackerras8455d792012-10-15 01:17:42 +00002708 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2709 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00002710 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002711 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002712 if (!v->arch.pending_exceptions)
2713 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002714 else
2715 v->arch.ceded = 0;
2716 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002717 vc->runner = vcpu;
2718 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002719 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03002720 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002721 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002722 /* Let something else run */
2723 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002724 if (vc->vcore_state == VCORE_PREEMPT)
2725 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002726 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00002727 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002728 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00002729 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002730 }
2731
Paul Mackerras8455d792012-10-15 01:17:42 +00002732 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2733 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002734 vc->vcore_state == VCORE_EXITING ||
2735 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10002736 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00002737
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002738 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2739 kvmppc_vcore_end_preempt(vc);
2740
Paul Mackerras8455d792012-10-15 01:17:42 +00002741 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2742 kvmppc_remove_runnable(vc, vcpu);
2743 vcpu->stat.signal_exits++;
2744 kvm_run->exit_reason = KVM_EXIT_INTR;
2745 vcpu->arch.ret = -EINTR;
2746 }
2747
2748 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2749 /* Wake up some vcpu to run the core */
2750 v = list_first_entry(&vc->runnable_threads,
2751 struct kvm_vcpu, arch.run_list);
2752 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002753 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002754
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002755 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002756 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002757 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002758}
2759
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302760static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002761{
2762 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002763 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002764
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002765 if (!vcpu->arch.sane) {
2766 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2767 return -EINVAL;
2768 }
2769
Scott Wood25051b52011-11-08 18:23:23 -06002770 kvmppc_core_prepare_to_enter(vcpu);
2771
Paul Mackerras19ccb762011-07-23 17:42:46 +10002772 /* No need to go into the guest when all we'll do is come back out */
2773 if (signal_pending(current)) {
2774 run->exit_reason = KVM_EXIT_INTR;
2775 return -EINTR;
2776 }
2777
Paul Mackerras32fad282012-05-04 02:32:53 +00002778 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002779 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002780 smp_mb();
2781
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002782 /* On the first time here, set up HTAB and VRMA */
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002783 if (!vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002784 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002785 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002786 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002787 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002788
Anton Blanchard579e6332015-10-29 11:44:09 +11002789 flush_all_to_thread(current);
2790
Paul Mackerras19ccb762011-07-23 17:42:46 +10002791 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002792 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002793 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002794
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002795 do {
2796 r = kvmppc_run_vcpu(run, vcpu);
2797
2798 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2799 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002800 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002801 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002802 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002803 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002804 } else if (r == RESUME_PAGE_FAULT) {
2805 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2806 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2807 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2808 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002809 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002810 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002811
2812 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002813 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002814 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002815 return r;
2816}
2817
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002818static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2819 int linux_psize)
2820{
2821 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2822
2823 if (!def->shift)
2824 return;
2825 (*sps)->page_shift = def->shift;
2826 (*sps)->slb_enc = def->sllp;
2827 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002828 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302829 /*
2830 * Add 16MB MPSS support if host supports it
2831 */
2832 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2833 (*sps)->enc[1].page_shift = 24;
2834 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2835 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002836 (*sps)++;
2837}
2838
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302839static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2840 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002841{
2842 struct kvm_ppc_one_seg_page_size *sps;
2843
2844 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2845 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2846 info->flags |= KVM_PPC_1T_SEGMENTS;
2847 info->slb_size = mmu_slb_size;
2848
2849 /* We only support these sizes for now, and no muti-size segments */
2850 sps = &info->sps[0];
2851 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2852 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2853 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2854
2855 return 0;
2856}
2857
Paul Mackerras82ed3612011-12-15 02:03:22 +00002858/*
2859 * Get (and clear) the dirty memory log for a memory slot.
2860 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302861static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2862 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002863{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002864 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00002865 struct kvm_memory_slot *memslot;
2866 int r;
2867 unsigned long n;
2868
2869 mutex_lock(&kvm->slots_lock);
2870
2871 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002872 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002873 goto out;
2874
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002875 slots = kvm_memslots(kvm);
2876 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002877 r = -ENOENT;
2878 if (!memslot->dirty_bitmap)
2879 goto out;
2880
2881 n = kvm_dirty_bitmap_bytes(memslot);
2882 memset(memslot->dirty_bitmap, 0, n);
2883
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002884 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002885 if (r)
2886 goto out;
2887
2888 r = -EFAULT;
2889 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2890 goto out;
2891
2892 r = 0;
2893out:
2894 mutex_unlock(&kvm->slots_lock);
2895 return r;
2896}
2897
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302898static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2899 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002900{
2901 if (!dont || free->arch.rmap != dont->arch.rmap) {
2902 vfree(free->arch.rmap);
2903 free->arch.rmap = NULL;
2904 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002905}
2906
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302907static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2908 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002909{
2910 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2911 if (!slot->arch.rmap)
2912 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002913
2914 return 0;
2915}
2916
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302917static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2918 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002919 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002920{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002921 return 0;
2922}
2923
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302924static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002925 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002926 const struct kvm_memory_slot *old,
2927 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002928{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002929 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002930 struct kvm_memslots *slots;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002931 struct kvm_memory_slot *memslot;
2932
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002933 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002934 /*
2935 * If modifying a memslot, reset all the rmap dirty bits.
2936 * If this is a new memslot, we don't need to do anything
2937 * since the rmap array starts out as all zeroes,
2938 * i.e. no pages are dirty.
2939 */
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002940 slots = kvm_memslots(kvm);
2941 memslot = id_to_memslot(slots, mem->slot);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002942 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2943 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002944}
2945
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002946/*
2947 * Update LPCR values in kvm->arch and in vcores.
2948 * Caller must hold kvm->lock.
2949 */
2950void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2951{
2952 long int i;
2953 u32 cores_done = 0;
2954
2955 if ((kvm->arch.lpcr & mask) == lpcr)
2956 return;
2957
2958 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2959
2960 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2961 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2962 if (!vc)
2963 continue;
2964 spin_lock(&vc->lock);
2965 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2966 spin_unlock(&vc->lock);
2967 if (++cores_done >= kvm->arch.online_vcores)
2968 break;
2969 }
2970}
2971
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302972static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2973{
2974 return;
2975}
2976
Paul Mackerras32fad282012-05-04 02:32:53 +00002977static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002978{
2979 int err = 0;
2980 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002981 unsigned long hva;
2982 struct kvm_memory_slot *memslot;
2983 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002984 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002985 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002986 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002987
2988 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002989 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002990 goto out; /* another vcpu beat us to it */
2991
Paul Mackerras32fad282012-05-04 02:32:53 +00002992 /* Allocate hashed page table (if not done already) and reset it */
2993 if (!kvm->arch.hpt_virt) {
2994 err = kvmppc_alloc_hpt(kvm, NULL);
2995 if (err) {
2996 pr_err("KVM: Couldn't alloc HPT\n");
2997 goto out;
2998 }
2999 }
3000
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003001 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003002 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003003 memslot = gfn_to_memslot(kvm, 0);
3004
3005 /* We must have some memory at 0 by now */
3006 err = -EINVAL;
3007 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003008 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003009
3010 /* Look up the VMA for the start of this memory slot */
3011 hva = memslot->userspace_addr;
3012 down_read(&current->mm->mmap_sem);
3013 vma = find_vma(current->mm, hva);
3014 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
3015 goto up_out;
3016
3017 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003018 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003019
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003020 up_read(&current->mm->mmap_sem);
3021
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003022 /* We can handle 4k, 64k or 16M pages in the VRMA */
3023 err = -EINVAL;
3024 if (!(psize == 0x1000 || psize == 0x10000 ||
3025 psize == 0x1000000))
3026 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003027
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003028 /* Update VRMASD field in the LPCR */
3029 senc = slb_pgsize_encoding(psize);
3030 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
3031 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3032 /* the -4 is to account for senc values starting at 0x10 */
3033 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003034
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003035 /* Create HPTEs in the hash page table for the VRMA */
3036 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003037
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003038 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003039
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003040 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003041 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003042 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003043 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003044 out_srcu:
3045 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003046 out:
3047 mutex_unlock(&kvm->lock);
3048 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003049
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003050 up_out:
3051 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08003052 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003053}
3054
Suresh Warrier79b6c242015-12-17 14:59:06 -06003055#ifdef CONFIG_KVM_XICS
3056/*
3057 * Allocate a per-core structure for managing state about which cores are
3058 * running in the host versus the guest and for exchanging data between
3059 * real mode KVM and CPU running in the host.
3060 * This is only done for the first VM.
3061 * The allocated structure stays even if all VMs have stopped.
3062 * It is only freed when the kvm-hv module is unloaded.
3063 * It's OK for this routine to fail, we just don't support host
3064 * core operations like redirecting H_IPI wakeups.
3065 */
3066void kvmppc_alloc_host_rm_ops(void)
3067{
3068 struct kvmppc_host_rm_ops *ops;
3069 unsigned long l_ops;
3070 int cpu, core;
3071 int size;
3072
3073 /* Not the first time here ? */
3074 if (kvmppc_host_rm_ops_hv != NULL)
3075 return;
3076
3077 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
3078 if (!ops)
3079 return;
3080
3081 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
3082 ops->rm_core = kzalloc(size, GFP_KERNEL);
3083
3084 if (!ops->rm_core) {
3085 kfree(ops);
3086 return;
3087 }
3088
3089 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
3090 if (!cpu_online(cpu))
3091 continue;
3092
3093 core = cpu >> threads_shift;
3094 ops->rm_core[core].rm_state.in_host = 1;
3095 }
3096
3097 /*
3098 * Make the contents of the kvmppc_host_rm_ops structure visible
3099 * to other CPUs before we assign it to the global variable.
3100 * Do an atomic assignment (no locks used here), but if someone
3101 * beats us to it, just free our copy and return.
3102 */
3103 smp_wmb();
3104 l_ops = (unsigned long) ops;
3105
3106 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
3107 kfree(ops->rm_core);
3108 kfree(ops);
3109 }
3110}
3111
3112void kvmppc_free_host_rm_ops(void)
3113{
3114 if (kvmppc_host_rm_ops_hv) {
3115 kfree(kvmppc_host_rm_ops_hv->rm_core);
3116 kfree(kvmppc_host_rm_ops_hv);
3117 kvmppc_host_rm_ops_hv = NULL;
3118 }
3119}
3120#endif
3121
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303122static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003123{
Paul Mackerras32fad282012-05-04 02:32:53 +00003124 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11003125 char buf[32];
Paul Mackerrasde56a942011-06-29 00:21:34 +00003126
Paul Mackerras32fad282012-05-04 02:32:53 +00003127 /* Allocate the guest's logical partition ID */
3128
3129 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08003130 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00003131 return -ENOMEM;
3132 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003133
Suresh Warrier79b6c242015-12-17 14:59:06 -06003134 kvmppc_alloc_host_rm_ops();
3135
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003136 /*
3137 * Since we don't flush the TLB when tearing down a VM,
3138 * and this lpid might have previously been used,
3139 * make sure we flush on each core before running the new VM.
3140 */
3141 cpumask_setall(&kvm->arch.need_tlb_flush);
3142
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003143 /* Start out with the default set of hcalls enabled */
3144 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3145 sizeof(kvm->arch.enabled_hcalls));
3146
Paul Mackerras9e368f22011-06-29 00:40:08 +00003147 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003148
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003149 /* Init LPCR for virtual RMA mode */
3150 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3151 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3152 lpcr &= LPCR_PECE | LPCR_LPES;
3153 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3154 LPCR_VPM0 | LPCR_VPM1;
3155 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3156 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3157 /* On POWER8 turn on online bit to enable PURR/SPURR */
3158 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3159 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00003160 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003161
Paul Mackerras512691d2012-10-15 01:15:41 +00003162 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10003163 * Track that we now have a HV mode VM active. This blocks secondary
3164 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00003165 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10003166 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003167
Paul Mackerrase23a8082015-03-28 14:21:01 +11003168 /*
3169 * Create a debugfs directory for the VM
3170 */
3171 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3172 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3173 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3174 kvmppc_mmu_debugfs_init(kvm);
3175
David Gibson54738c02011-06-29 00:22:41 +00003176 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003177}
3178
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303179static void kvmppc_free_vcores(struct kvm *kvm)
3180{
3181 long int i;
3182
Paul Mackerras23316312015-10-21 16:03:14 +11003183 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303184 kfree(kvm->arch.vcores[i]);
3185 kvm->arch.online_vcores = 0;
3186}
3187
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303188static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003189{
Paul Mackerrase23a8082015-03-28 14:21:01 +11003190 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3191
Michael Ellerman441c19c2014-05-23 18:15:25 +10003192 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003193
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303194 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003195
Paul Mackerrasde56a942011-06-29 00:21:34 +00003196 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003197}
3198
3199/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303200static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3201 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003202{
3203 return EMULATE_FAIL;
3204}
3205
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303206static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3207 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003208{
3209 return EMULATE_FAIL;
3210}
3211
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303212static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3213 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003214{
3215 return EMULATE_FAIL;
3216}
3217
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303218static int kvmppc_core_check_processor_compat_hv(void)
3219{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003220 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3221 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303222 return -EIO;
3223 return 0;
3224}
3225
3226static long kvm_arch_vm_ioctl_hv(struct file *filp,
3227 unsigned int ioctl, unsigned long arg)
3228{
3229 struct kvm *kvm __maybe_unused = filp->private_data;
3230 void __user *argp = (void __user *)arg;
3231 long r;
3232
3233 switch (ioctl) {
3234
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303235 case KVM_PPC_ALLOCATE_HTAB: {
3236 u32 htab_order;
3237
3238 r = -EFAULT;
3239 if (get_user(htab_order, (u32 __user *)argp))
3240 break;
3241 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
3242 if (r)
3243 break;
3244 r = -EFAULT;
3245 if (put_user(htab_order, (u32 __user *)argp))
3246 break;
3247 r = 0;
3248 break;
3249 }
3250
3251 case KVM_PPC_GET_HTAB_FD: {
3252 struct kvm_get_htab_fd ghf;
3253
3254 r = -EFAULT;
3255 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3256 break;
3257 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3258 break;
3259 }
3260
3261 default:
3262 r = -ENOTTY;
3263 }
3264
3265 return r;
3266}
3267
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003268/*
3269 * List of hcall numbers to enable by default.
3270 * For compatibility with old userspace, we enable by default
3271 * all hcalls that were implemented before the hcall-enabling
3272 * facility was added. Note this list should not include H_RTAS.
3273 */
3274static unsigned int default_hcall_list[] = {
3275 H_REMOVE,
3276 H_ENTER,
3277 H_READ,
3278 H_PROTECT,
3279 H_BULK_REMOVE,
3280 H_GET_TCE,
3281 H_PUT_TCE,
3282 H_SET_DABR,
3283 H_SET_XDABR,
3284 H_CEDE,
3285 H_PROD,
3286 H_CONFER,
3287 H_REGISTER_VPA,
3288#ifdef CONFIG_KVM_XICS
3289 H_EOI,
3290 H_CPPR,
3291 H_IPI,
3292 H_IPOLL,
3293 H_XIRR,
3294 H_XIRR_X,
3295#endif
3296 0
3297};
3298
3299static void init_default_hcalls(void)
3300{
3301 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003302 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003303
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003304 for (i = 0; default_hcall_list[i]; ++i) {
3305 hcall = default_hcall_list[i];
3306 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3307 __set_bit(hcall / 4, default_enabled_hcalls);
3308 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003309}
3310
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303311static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303312 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3313 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3314 .get_one_reg = kvmppc_get_one_reg_hv,
3315 .set_one_reg = kvmppc_set_one_reg_hv,
3316 .vcpu_load = kvmppc_core_vcpu_load_hv,
3317 .vcpu_put = kvmppc_core_vcpu_put_hv,
3318 .set_msr = kvmppc_set_msr_hv,
3319 .vcpu_run = kvmppc_vcpu_run_hv,
3320 .vcpu_create = kvmppc_core_vcpu_create_hv,
3321 .vcpu_free = kvmppc_core_vcpu_free_hv,
3322 .check_requests = kvmppc_core_check_requests_hv,
3323 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3324 .flush_memslot = kvmppc_core_flush_memslot_hv,
3325 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3326 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3327 .unmap_hva = kvm_unmap_hva_hv,
3328 .unmap_hva_range = kvm_unmap_hva_range_hv,
3329 .age_hva = kvm_age_hva_hv,
3330 .test_age_hva = kvm_test_age_hva_hv,
3331 .set_spte_hva = kvm_set_spte_hva_hv,
3332 .mmu_destroy = kvmppc_mmu_destroy_hv,
3333 .free_memslot = kvmppc_core_free_memslot_hv,
3334 .create_memslot = kvmppc_core_create_memslot_hv,
3335 .init_vm = kvmppc_core_init_vm_hv,
3336 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303337 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3338 .emulate_op = kvmppc_core_emulate_op_hv,
3339 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3340 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3341 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3342 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003343 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303344};
3345
3346static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003347{
3348 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303349 /*
3350 * FIXME!! Do we need to check on all cpus ?
3351 */
3352 r = kvmppc_core_check_processor_compat_hv();
3353 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11003354 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003355
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303356 kvm_ops_hv.owner = THIS_MODULE;
3357 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003358
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003359 init_default_hcalls();
3360
Paul Mackerrasec257162015-06-24 21:18:03 +10003361 init_vcore_lists();
3362
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303363 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00003364 return r;
3365}
3366
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303367static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003368{
Suresh Warrier79b6c242015-12-17 14:59:06 -06003369 kvmppc_free_host_rm_ops();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303370 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003371}
3372
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303373module_init(kvmppc_book3s_init_hv);
3374module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05303375MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01003376MODULE_ALIAS_MISCDEV(KVM_MINOR);
3377MODULE_ALIAS("devname:kvm");