blob: 0173ce22111147aa8ff29d4eb34374c5be922872 [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>
Stewart Smith9678cda2014-07-18 14:18:43 +100039#include <asm/cache.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000040#include <asm/cacheflush.h>
41#include <asm/tlbflush.h>
42#include <asm/uaccess.h>
43#include <asm/io.h>
44#include <asm/kvm_ppc.h>
45#include <asm/kvm_book3s.h>
46#include <asm/mmu_context.h>
47#include <asm/lppaca.h>
48#include <asm/processor.h>
Paul Mackerras371fefd2011-06-29 00:23:08 +000049#include <asm/cputhreads.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000050#include <asm/page.h>
Michael Neulingde1d9242011-11-09 20:39:49 +000051#include <asm/hvcall.h>
David Howellsae3a1972012-03-28 18:30:02 +010052#include <asm/switch_to.h>
Paul Mackerras512691d2012-10-15 01:15:41 +000053#include <asm/smp.h>
Paul Mackerras66feed62015-03-28 14:21:12 +110054#include <asm/dbell.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000055#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000056#include <linux/vmalloc.h>
57#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000058#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053059#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000060
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053061#include "book3s.h"
62
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060063#define CREATE_TRACE_POINTS
64#include "trace_hv.h"
65
Paul Mackerrasde56a942011-06-29 00:21:34 +000066/* #define EXIT_DEBUG */
67/* #define EXIT_DEBUG_SIMPLE */
68/* #define EXIT_DEBUG_INT */
69
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000070/* Used to indicate that a guest page fault needs to be handled */
71#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
72
Paul Mackerrasc7b67672012-10-15 01:18:07 +000073/* Used as a "null" value for timebase values */
74#define TB_NIL (~(u64)0)
75
Paul Mackerras699a0ea2014-06-02 11:02:59 +100076static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
77
Stewart Smith9678cda2014-07-18 14:18:43 +100078#if defined(CONFIG_PPC_64K_PAGES)
79#define MPP_BUFFER_ORDER 0
80#elif defined(CONFIG_PPC_4K_PAGES)
81#define MPP_BUFFER_ORDER 3
82#endif
83
Paul Mackerrasec257162015-06-24 21:18:03 +100084static int target_smt_mode;
85module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
86MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +100087
Paul Mackerras19ccb762011-07-23 17:42:46 +100088static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000089static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100090
Paul Mackerras66feed62015-03-28 14:21:12 +110091static bool kvmppc_ipi_thread(int cpu)
92{
93 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
94 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
95 preempt_disable();
96 if (cpu_first_thread_sibling(cpu) ==
97 cpu_first_thread_sibling(smp_processor_id())) {
98 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
99 msg |= cpu_thread_in_core(cpu);
100 smp_mb();
101 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
102 preempt_enable();
103 return true;
104 }
105 preempt_enable();
106 }
107
108#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
109 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
110 xics_wake_cpu(cpu);
111 return true;
112 }
113#endif
114
115 return false;
116}
117
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530118static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000119{
Paul Mackerrasec257162015-06-24 21:18:03 +1000120 int cpu;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000121 wait_queue_head_t *wqp;
122
123 wqp = kvm_arch_vcpu_wq(vcpu);
124 if (waitqueue_active(wqp)) {
125 wake_up_interruptible(wqp);
126 ++vcpu->stat.halt_wakeup;
127 }
128
Paul Mackerrasec257162015-06-24 21:18:03 +1000129 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100130 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000131
132 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000133 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100134 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
135 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000136}
137
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000138/*
139 * We use the vcpu_load/put functions to measure stolen time.
140 * Stolen time is counted as time when either the vcpu is able to
141 * run as part of a virtual core, but the task running the vcore
142 * is preempted or sleeping, or when the vcpu needs something done
143 * in the kernel by the task running the vcpu, but that task is
144 * preempted or sleeping. Those two things have to be counted
145 * separately, since one of the vcpu tasks will take on the job
146 * of running the core, and the other vcpu tasks in the vcore will
147 * sleep waiting for it to do that, but that sleep shouldn't count
148 * as stolen time.
149 *
150 * Hence we accumulate stolen time when the vcpu can run as part of
151 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
152 * needs its task to do other things in the kernel (for example,
153 * service a page fault) in busy_stolen. We don't accumulate
154 * stolen time for a vcore when it is inactive, or for a vcpu
155 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
156 * a misnomer; it means that the vcpu task is not executing in
157 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
158 * the kernel. We don't have any way of dividing up that time
159 * between time that the vcpu is genuinely stopped, time that
160 * the task is actively working on behalf of the vcpu, and time
161 * that the task is preempted, so we don't count any of it as
162 * stolen.
163 *
164 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100165 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
166 * lock. The stolen times are measured in units of timebase ticks.
167 * (Note that the != TB_NIL checks below are purely defensive;
168 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000169 */
170
Paul Mackerrasec257162015-06-24 21:18:03 +1000171static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
172{
173 unsigned long flags;
174
175 spin_lock_irqsave(&vc->stoltb_lock, flags);
176 vc->preempt_tb = mftb();
177 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
178}
179
180static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
181{
182 unsigned long flags;
183
184 spin_lock_irqsave(&vc->stoltb_lock, flags);
185 if (vc->preempt_tb != TB_NIL) {
186 vc->stolen_tb += mftb() - vc->preempt_tb;
187 vc->preempt_tb = TB_NIL;
188 }
189 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
190}
191
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530192static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000193{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000194 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100195 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000196
Paul Mackerras2711e242014-12-04 16:43:28 +1100197 /*
198 * We can test vc->runner without taking the vcore lock,
199 * because only this task ever sets vc->runner to this
200 * vcpu, and once it is set to this vcpu, only this task
201 * ever sets it to NULL.
202 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000203 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
204 kvmppc_core_end_stolen(vc);
205
Paul Mackerras2711e242014-12-04 16:43:28 +1100206 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000207 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
208 vcpu->arch.busy_preempt != TB_NIL) {
209 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
210 vcpu->arch.busy_preempt = TB_NIL;
211 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100212 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000213}
214
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530215static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000216{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000217 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100218 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000219
Paul Mackerrasec257162015-06-24 21:18:03 +1000220 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
221 kvmppc_core_start_stolen(vc);
222
Paul Mackerras2711e242014-12-04 16:43:28 +1100223 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000224 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
225 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100226 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000227}
228
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530229static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000230{
231 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000232 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000233}
234
Thomas Huth5358a962015-05-22 09:25:02 +0200235static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000236{
237 vcpu->arch.pvr = pvr;
238}
239
Thomas Huth5358a962015-05-22 09:25:02 +0200240static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000241{
242 unsigned long pcr = 0;
243 struct kvmppc_vcore *vc = vcpu->arch.vcore;
244
245 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000246 switch (arch_compat) {
247 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100248 /*
249 * If an arch bit is set in PCR, all the defined
250 * higher-order arch bits also have to be set.
251 */
252 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000253 break;
254 case PVR_ARCH_206:
255 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100256 pcr = PCR_ARCH_206;
257 break;
258 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000259 break;
260 default:
261 return -EINVAL;
262 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100263
264 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
265 /* POWER7 can't emulate POWER8 */
266 if (!(pcr & PCR_ARCH_206))
267 return -EINVAL;
268 pcr &= ~PCR_ARCH_206;
269 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000270 }
271
272 spin_lock(&vc->lock);
273 vc->arch_compat = arch_compat;
274 vc->pcr = pcr;
275 spin_unlock(&vc->lock);
276
277 return 0;
278}
279
Thomas Huth5358a962015-05-22 09:25:02 +0200280static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000281{
282 int r;
283
284 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
285 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
286 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
287 for (r = 0; r < 16; ++r)
288 pr_err("r%2d = %.16lx r%d = %.16lx\n",
289 r, kvmppc_get_gpr(vcpu, r),
290 r+16, kvmppc_get_gpr(vcpu, r+16));
291 pr_err("ctr = %.16lx lr = %.16lx\n",
292 vcpu->arch.ctr, vcpu->arch.lr);
293 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
294 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
295 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
296 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
297 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
298 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
299 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
300 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
301 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
302 pr_err("fault dar = %.16lx dsisr = %.8x\n",
303 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
304 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
305 for (r = 0; r < vcpu->arch.slb_max; ++r)
306 pr_err(" ESID = %.16llx VSID = %.16llx\n",
307 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
308 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000309 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000310 vcpu->arch.last_inst);
311}
312
Thomas Huth5358a962015-05-22 09:25:02 +0200313static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000314{
315 int r;
316 struct kvm_vcpu *v, *ret = NULL;
317
318 mutex_lock(&kvm->lock);
319 kvm_for_each_vcpu(r, v, kvm) {
320 if (v->vcpu_id == id) {
321 ret = v;
322 break;
323 }
324 }
325 mutex_unlock(&kvm->lock);
326 return ret;
327}
328
329static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
330{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000331 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200332 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000333}
334
Paul Mackerras55b665b2012-09-25 20:33:06 +0000335static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
336 unsigned long addr, unsigned long len)
337{
338 /* check address is cacheline aligned */
339 if (addr & (L1_CACHE_BYTES - 1))
340 return -EINVAL;
341 spin_lock(&vcpu->arch.vpa_update_lock);
342 if (v->next_gpa != addr || v->len != len) {
343 v->next_gpa = addr;
344 v->len = addr ? len : 0;
345 v->update_pending = 1;
346 }
347 spin_unlock(&vcpu->arch.vpa_update_lock);
348 return 0;
349}
350
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000351/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
352struct reg_vpa {
353 u32 dummy;
354 union {
Alexander Graf02407552014-06-11 10:34:19 +0200355 __be16 hword;
356 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000357 } length;
358};
359
360static int vpa_is_registered(struct kvmppc_vpa *vpap)
361{
362 if (vpap->update_pending)
363 return vpap->next_gpa != 0;
364 return vpap->pinned_addr != NULL;
365}
366
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000367static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
368 unsigned long flags,
369 unsigned long vcpuid, unsigned long vpa)
370{
371 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000372 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000373 void *va;
374 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000375 int err;
376 int subfunc;
377 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000378
379 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
380 if (!tvcpu)
381 return H_PARAMETER;
382
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000383 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
384 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
385 subfunc == H_VPA_REG_SLB) {
386 /* Registering new area - address must be cache-line aligned */
387 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000388 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000389
390 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000391 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
392 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000393 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000394 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200395 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000396 else
Alexander Graf02407552014-06-11 10:34:19 +0200397 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000398 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000399
400 /* Check length */
401 if (len > nb || len < sizeof(struct reg_vpa))
402 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000403 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000404 vpa = 0;
405 len = 0;
406 }
407
408 err = H_PARAMETER;
409 vpap = NULL;
410 spin_lock(&tvcpu->arch.vpa_update_lock);
411
412 switch (subfunc) {
413 case H_VPA_REG_VPA: /* register VPA */
414 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000415 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000416 vpap = &tvcpu->arch.vpa;
417 err = 0;
418 break;
419
420 case H_VPA_REG_DTL: /* register DTL */
421 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000422 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000423 len -= len % sizeof(struct dtl_entry);
424
425 /* Check that they have previously registered a VPA */
426 err = H_RESOURCE;
427 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000428 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000429
430 vpap = &tvcpu->arch.dtl;
431 err = 0;
432 break;
433
434 case H_VPA_REG_SLB: /* register SLB shadow buffer */
435 /* Check that they have previously registered a VPA */
436 err = H_RESOURCE;
437 if (!vpa_is_registered(&tvcpu->arch.vpa))
438 break;
439
440 vpap = &tvcpu->arch.slb_shadow;
441 err = 0;
442 break;
443
444 case H_VPA_DEREG_VPA: /* deregister VPA */
445 /* Check they don't still have a DTL or SLB buf registered */
446 err = H_RESOURCE;
447 if (vpa_is_registered(&tvcpu->arch.dtl) ||
448 vpa_is_registered(&tvcpu->arch.slb_shadow))
449 break;
450
451 vpap = &tvcpu->arch.vpa;
452 err = 0;
453 break;
454
455 case H_VPA_DEREG_DTL: /* deregister DTL */
456 vpap = &tvcpu->arch.dtl;
457 err = 0;
458 break;
459
460 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
461 vpap = &tvcpu->arch.slb_shadow;
462 err = 0;
463 break;
464 }
465
466 if (vpap) {
467 vpap->next_gpa = vpa;
468 vpap->len = len;
469 vpap->update_pending = 1;
470 }
471
472 spin_unlock(&tvcpu->arch.vpa_update_lock);
473
474 return err;
475}
476
Paul Mackerras081f3232012-06-01 20:20:24 +1000477static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000478{
Paul Mackerras081f3232012-06-01 20:20:24 +1000479 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000480 void *va;
481 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000482 unsigned long gpa;
483
484 /*
485 * We need to pin the page pointed to by vpap->next_gpa,
486 * but we can't call kvmppc_pin_guest_page under the lock
487 * as it does get_user_pages() and down_read(). So we
488 * have to drop the lock, pin the page, then get the lock
489 * again and check that a new area didn't get registered
490 * in the meantime.
491 */
492 for (;;) {
493 gpa = vpap->next_gpa;
494 spin_unlock(&vcpu->arch.vpa_update_lock);
495 va = NULL;
496 nb = 0;
497 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000498 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000499 spin_lock(&vcpu->arch.vpa_update_lock);
500 if (gpa == vpap->next_gpa)
501 break;
502 /* sigh... unpin that one and try again */
503 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000504 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000505 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000506
507 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000508 if (va && nb < vpap->len) {
509 /*
510 * If it's now too short, it must be that userspace
511 * has changed the mappings underlying guest memory,
512 * so unregister the region.
513 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000514 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000515 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000516 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000517 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000518 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
519 vpap->dirty);
520 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000521 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000522 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000523 if (va)
524 vpap->pinned_end = va + vpap->len;
525}
Paul Mackerras93e60242011-12-12 12:28:55 +0000526
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000527static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
528{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000529 if (!(vcpu->arch.vpa.update_pending ||
530 vcpu->arch.slb_shadow.update_pending ||
531 vcpu->arch.dtl.update_pending))
532 return;
533
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000534 spin_lock(&vcpu->arch.vpa_update_lock);
535 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000536 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000537 if (vcpu->arch.vpa.pinned_addr)
538 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000539 }
540 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000541 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000542 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
543 vcpu->arch.dtl_index = 0;
544 }
545 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000546 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000547 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000548}
549
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000550/*
551 * Return the accumulated stolen time for the vcore up until `now'.
552 * The caller should hold the vcore lock.
553 */
554static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
555{
556 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100557 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000558
Paul Mackerras2711e242014-12-04 16:43:28 +1100559 spin_lock_irqsave(&vc->stoltb_lock, flags);
560 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000561 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100562 vc->preempt_tb != TB_NIL)
563 p += now - vc->preempt_tb;
564 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000565 return p;
566}
567
Paul Mackerras0456ec42012-02-03 00:56:21 +0000568static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
569 struct kvmppc_vcore *vc)
570{
571 struct dtl_entry *dt;
572 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000573 unsigned long stolen;
574 unsigned long core_stolen;
575 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000576
577 dt = vcpu->arch.dtl_ptr;
578 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000579 now = mftb();
580 core_stolen = vcore_stolen_time(vc, now);
581 stolen = core_stolen - vcpu->arch.stolen_logged;
582 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100583 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000584 stolen += vcpu->arch.busy_stolen;
585 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100586 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000587 if (!dt || !vpa)
588 return;
589 memset(dt, 0, sizeof(struct dtl_entry));
590 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200591 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
592 dt->timebase = cpu_to_be64(now + vc->tb_offset);
593 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
594 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
595 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000596 ++dt;
597 if (dt == vcpu->arch.dtl.pinned_end)
598 dt = vcpu->arch.dtl.pinned_addr;
599 vcpu->arch.dtl_ptr = dt;
600 /* order writing *dt vs. writing vpa->dtl_idx */
601 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200602 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000603 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000604}
605
Michael Neuling96423822014-06-02 11:03:01 +1000606static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
607{
608 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
609 return true;
610 if ((!vcpu->arch.vcore->arch_compat) &&
611 cpu_has_feature(CPU_FTR_ARCH_207S))
612 return true;
613 return false;
614}
615
616static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
617 unsigned long resource, unsigned long value1,
618 unsigned long value2)
619{
620 switch (resource) {
621 case H_SET_MODE_RESOURCE_SET_CIABR:
622 if (!kvmppc_power8_compatible(vcpu))
623 return H_P2;
624 if (value2)
625 return H_P4;
626 if (mflags)
627 return H_UNSUPPORTED_FLAG_START;
628 /* Guests can't breakpoint the hypervisor */
629 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
630 return H_P3;
631 vcpu->arch.ciabr = value1;
632 return H_SUCCESS;
633 case H_SET_MODE_RESOURCE_SET_DAWR:
634 if (!kvmppc_power8_compatible(vcpu))
635 return H_P2;
636 if (mflags)
637 return H_UNSUPPORTED_FLAG_START;
638 if (value2 & DABRX_HYP)
639 return H_P4;
640 vcpu->arch.dawr = value1;
641 vcpu->arch.dawrx = value2;
642 return H_SUCCESS;
643 default:
644 return H_TOO_HARD;
645 }
646}
647
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100648static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
649{
650 struct kvmppc_vcore *vcore = target->arch.vcore;
651
652 /*
653 * We expect to have been called by the real mode handler
654 * (kvmppc_rm_h_confer()) which would have directly returned
655 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
656 * have useful work to do and should not confer) so we don't
657 * recheck that here.
658 */
659
660 spin_lock(&vcore->lock);
661 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000662 vcore->vcore_state != VCORE_INACTIVE &&
663 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100664 target = vcore->runner;
665 spin_unlock(&vcore->lock);
666
667 return kvm_vcpu_yield_to(target);
668}
669
670static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
671{
672 int yield_count = 0;
673 struct lppaca *lppaca;
674
675 spin_lock(&vcpu->arch.vpa_update_lock);
676 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
677 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100678 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100679 spin_unlock(&vcpu->arch.vpa_update_lock);
680 return yield_count;
681}
682
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000683int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
684{
685 unsigned long req = kvmppc_get_gpr(vcpu, 3);
686 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100687 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000688 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000689 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000690
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000691 if (req <= MAX_HCALL_OPCODE &&
692 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
693 return RESUME_HOST;
694
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000695 switch (req) {
696 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000697 break;
698 case H_PROD:
699 target = kvmppc_get_gpr(vcpu, 4);
700 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
701 if (!tvcpu) {
702 ret = H_PARAMETER;
703 break;
704 }
705 tvcpu->arch.prodded = 1;
706 smp_mb();
707 if (vcpu->arch.ceded) {
708 if (waitqueue_active(&vcpu->wq)) {
709 wake_up_interruptible(&vcpu->wq);
710 vcpu->stat.halt_wakeup++;
711 }
712 }
713 break;
714 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000715 target = kvmppc_get_gpr(vcpu, 4);
716 if (target == -1)
717 break;
718 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
719 if (!tvcpu) {
720 ret = H_PARAMETER;
721 break;
722 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100723 yield_count = kvmppc_get_gpr(vcpu, 5);
724 if (kvmppc_get_yield_count(tvcpu) != yield_count)
725 break;
726 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000727 break;
728 case H_REGISTER_VPA:
729 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
730 kvmppc_get_gpr(vcpu, 5),
731 kvmppc_get_gpr(vcpu, 6));
732 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000733 case H_RTAS:
734 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
735 return RESUME_HOST;
736
Paul Mackerrasc9438092013-11-16 17:46:05 +1100737 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000738 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100739 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000740
741 if (rc == -ENOENT)
742 return RESUME_HOST;
743 else if (rc == 0)
744 break;
745
746 /* Send the error out to userspace via KVM_RUN */
747 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100748 case H_LOGICAL_CI_LOAD:
749 ret = kvmppc_h_logical_ci_load(vcpu);
750 if (ret == H_TOO_HARD)
751 return RESUME_HOST;
752 break;
753 case H_LOGICAL_CI_STORE:
754 ret = kvmppc_h_logical_ci_store(vcpu);
755 if (ret == H_TOO_HARD)
756 return RESUME_HOST;
757 break;
Michael Neuling96423822014-06-02 11:03:01 +1000758 case H_SET_MODE:
759 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
760 kvmppc_get_gpr(vcpu, 5),
761 kvmppc_get_gpr(vcpu, 6),
762 kvmppc_get_gpr(vcpu, 7));
763 if (ret == H_TOO_HARD)
764 return RESUME_HOST;
765 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000766 case H_XIRR:
767 case H_CPPR:
768 case H_EOI:
769 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000770 case H_IPOLL:
771 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000772 if (kvmppc_xics_enabled(vcpu)) {
773 ret = kvmppc_xics_hcall(vcpu, req);
774 break;
775 } /* fallthrough */
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000776 default:
777 return RESUME_HOST;
778 }
779 kvmppc_set_gpr(vcpu, 3, ret);
780 vcpu->arch.hcall_needed = 0;
781 return RESUME_GUEST;
782}
783
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000784static int kvmppc_hcall_impl_hv(unsigned long cmd)
785{
786 switch (cmd) {
787 case H_CEDE:
788 case H_PROD:
789 case H_CONFER:
790 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000791 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100792 case H_LOGICAL_CI_LOAD:
793 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000794#ifdef CONFIG_KVM_XICS
795 case H_XIRR:
796 case H_CPPR:
797 case H_EOI:
798 case H_IPI:
799 case H_IPOLL:
800 case H_XIRR_X:
801#endif
802 return 1;
803 }
804
805 /* See if it's in the real-mode table */
806 return kvmppc_hcall_impl_hv_realmode(cmd);
807}
808
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530809static int kvmppc_emulate_debug_inst(struct kvm_run *run,
810 struct kvm_vcpu *vcpu)
811{
812 u32 last_inst;
813
814 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
815 EMULATE_DONE) {
816 /*
817 * Fetch failed, so return to guest and
818 * try executing it again.
819 */
820 return RESUME_GUEST;
821 }
822
823 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
824 run->exit_reason = KVM_EXIT_DEBUG;
825 run->debug.arch.address = kvmppc_get_pc(vcpu);
826 return RESUME_HOST;
827 } else {
828 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
829 return RESUME_GUEST;
830 }
831}
832
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530833static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
834 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000835{
836 int r = RESUME_HOST;
837
838 vcpu->stat.sum_exits++;
839
840 run->exit_reason = KVM_EXIT_UNKNOWN;
841 run->ready_for_interrupt_injection = 1;
842 switch (vcpu->arch.trap) {
843 /* We're good on these - the host merely wanted to get our attention */
844 case BOOK3S_INTERRUPT_HV_DECREMENTER:
845 vcpu->stat.dec_exits++;
846 r = RESUME_GUEST;
847 break;
848 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100849 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000850 vcpu->stat.ext_intr_exits++;
851 r = RESUME_GUEST;
852 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100853 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
854 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000855 case BOOK3S_INTERRUPT_PERFMON:
856 r = RESUME_GUEST;
857 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000858 case BOOK3S_INTERRUPT_MACHINE_CHECK:
859 /*
860 * Deliver a machine check interrupt to the guest.
861 * We have to do this, even if the host has handled the
862 * machine check, because machine checks use SRR0/1 and
863 * the interrupt might have trashed guest state in them.
864 */
865 kvmppc_book3s_queue_irqprio(vcpu,
866 BOOK3S_INTERRUPT_MACHINE_CHECK);
867 r = RESUME_GUEST;
868 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000869 case BOOK3S_INTERRUPT_PROGRAM:
870 {
871 ulong flags;
872 /*
873 * Normally program interrupts are delivered directly
874 * to the guest by the hardware, but we can get here
875 * as a result of a hypervisor emulation interrupt
876 * (e40) getting turned into a 700 by BML RTAS.
877 */
878 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
879 kvmppc_core_queue_program(vcpu, flags);
880 r = RESUME_GUEST;
881 break;
882 }
883 case BOOK3S_INTERRUPT_SYSCALL:
884 {
885 /* hcall - punt to userspace */
886 int i;
887
Liu Ping Fan27025a62013-11-19 14:12:48 +0800888 /* hypercall with MSR_PR has already been handled in rmode,
889 * and never reaches here.
890 */
891
Paul Mackerrasde56a942011-06-29 00:21:34 +0000892 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
893 for (i = 0; i < 9; ++i)
894 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
895 run->exit_reason = KVM_EXIT_PAPR_HCALL;
896 vcpu->arch.hcall_needed = 1;
897 r = RESUME_HOST;
898 break;
899 }
900 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000901 * We get these next two if the guest accesses a page which it thinks
902 * it has mapped but which is not actually present, either because
903 * it is for an emulated I/O device or because the corresonding
904 * host page has been paged out. Any other HDSI/HISI interrupts
905 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000906 */
907 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000908 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000909 break;
910 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000911 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
912 vcpu->arch.fault_dsisr = 0;
913 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000914 break;
915 /*
916 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530917 * If the guest debug is disabled, generate a program interrupt
918 * to the guest. If guest debug is enabled, we need to check
919 * whether the instruction is a software breakpoint instruction.
920 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000921 */
922 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +1100923 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
924 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
925 swab32(vcpu->arch.emul_inst) :
926 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530927 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
928 r = kvmppc_emulate_debug_inst(run, vcpu);
929 } else {
930 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
931 r = RESUME_GUEST;
932 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100933 break;
934 /*
935 * This occurs if the guest (kernel or userspace), does something that
936 * is prohibited by HFSCR. We just generate a program interrupt to
937 * the guest.
938 */
939 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
940 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000941 r = RESUME_GUEST;
942 break;
943 default:
944 kvmppc_dump_regs(vcpu);
945 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
946 vcpu->arch.trap, kvmppc_get_pc(vcpu),
947 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000948 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000949 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000950 break;
951 }
952
Paul Mackerrasde56a942011-06-29 00:21:34 +0000953 return r;
954}
955
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530956static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
957 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000958{
959 int i;
960
Paul Mackerrasde56a942011-06-29 00:21:34 +0000961 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +0530962 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000963 for (i = 0; i < vcpu->arch.slb_max; i++) {
964 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
965 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
966 }
967
968 return 0;
969}
970
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530971static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
972 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000973{
974 int i, j;
975
Paul Mackerras9333e6c2014-09-02 16:14:43 +1000976 /* Only accept the same PVR as the host's, since we can't spoof it */
977 if (sregs->pvr != vcpu->arch.pvr)
978 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000979
980 j = 0;
981 for (i = 0; i < vcpu->arch.slb_nr; i++) {
982 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
983 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
984 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
985 ++j;
986 }
987 }
988 vcpu->arch.slb_max = j;
989
990 return 0;
991}
992
Alexey Kardashevskiya0840242014-07-19 17:59:34 +1000993static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
994 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000995{
Paul Mackerras8f902b02015-03-20 20:39:38 +1100996 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000997 struct kvmppc_vcore *vc = vcpu->arch.vcore;
998 u64 mask;
999
Paul Mackerras8f902b02015-03-20 20:39:38 +11001000 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001001 spin_lock(&vc->lock);
1002 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001003 * If ILE (interrupt little-endian) has changed, update the
1004 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1005 */
1006 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001007 struct kvm_vcpu *vcpu;
1008 int i;
1009
Anton Blanchardd6829162014-01-08 21:25:30 +11001010 kvm_for_each_vcpu(i, vcpu, kvm) {
1011 if (vcpu->arch.vcore != vc)
1012 continue;
1013 if (new_lpcr & LPCR_ILE)
1014 vcpu->arch.intr_msr |= MSR_LE;
1015 else
1016 vcpu->arch.intr_msr &= ~MSR_LE;
1017 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001018 }
1019
1020 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001021 * Userspace can only modify DPFD (default prefetch depth),
1022 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001023 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001024 */
1025 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001026 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1027 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001028
1029 /* Broken 32-bit version of LPCR must not clear top bits */
1030 if (preserve_top32)
1031 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001032 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1033 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001034 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001035}
1036
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301037static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1038 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001039{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001040 int r = 0;
1041 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001042
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001043 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301044 case KVM_REG_PPC_DEBUG_INST:
1045 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1046 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001047 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001048 *val = get_reg_val(id, 0);
1049 break;
1050 case KVM_REG_PPC_DABR:
1051 *val = get_reg_val(id, vcpu->arch.dabr);
1052 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001053 case KVM_REG_PPC_DABRX:
1054 *val = get_reg_val(id, vcpu->arch.dabrx);
1055 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001056 case KVM_REG_PPC_DSCR:
1057 *val = get_reg_val(id, vcpu->arch.dscr);
1058 break;
1059 case KVM_REG_PPC_PURR:
1060 *val = get_reg_val(id, vcpu->arch.purr);
1061 break;
1062 case KVM_REG_PPC_SPURR:
1063 *val = get_reg_val(id, vcpu->arch.spurr);
1064 break;
1065 case KVM_REG_PPC_AMR:
1066 *val = get_reg_val(id, vcpu->arch.amr);
1067 break;
1068 case KVM_REG_PPC_UAMOR:
1069 *val = get_reg_val(id, vcpu->arch.uamor);
1070 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001071 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001072 i = id - KVM_REG_PPC_MMCR0;
1073 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1074 break;
1075 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1076 i = id - KVM_REG_PPC_PMC1;
1077 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001078 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001079 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1080 i = id - KVM_REG_PPC_SPMC1;
1081 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1082 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001083 case KVM_REG_PPC_SIAR:
1084 *val = get_reg_val(id, vcpu->arch.siar);
1085 break;
1086 case KVM_REG_PPC_SDAR:
1087 *val = get_reg_val(id, vcpu->arch.sdar);
1088 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001089 case KVM_REG_PPC_SIER:
1090 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001091 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001092 case KVM_REG_PPC_IAMR:
1093 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001094 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001095 case KVM_REG_PPC_PSPB:
1096 *val = get_reg_val(id, vcpu->arch.pspb);
1097 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001098 case KVM_REG_PPC_DPDES:
1099 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1100 break;
1101 case KVM_REG_PPC_DAWR:
1102 *val = get_reg_val(id, vcpu->arch.dawr);
1103 break;
1104 case KVM_REG_PPC_DAWRX:
1105 *val = get_reg_val(id, vcpu->arch.dawrx);
1106 break;
1107 case KVM_REG_PPC_CIABR:
1108 *val = get_reg_val(id, vcpu->arch.ciabr);
1109 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001110 case KVM_REG_PPC_CSIGR:
1111 *val = get_reg_val(id, vcpu->arch.csigr);
1112 break;
1113 case KVM_REG_PPC_TACR:
1114 *val = get_reg_val(id, vcpu->arch.tacr);
1115 break;
1116 case KVM_REG_PPC_TCSCR:
1117 *val = get_reg_val(id, vcpu->arch.tcscr);
1118 break;
1119 case KVM_REG_PPC_PID:
1120 *val = get_reg_val(id, vcpu->arch.pid);
1121 break;
1122 case KVM_REG_PPC_ACOP:
1123 *val = get_reg_val(id, vcpu->arch.acop);
1124 break;
1125 case KVM_REG_PPC_WORT:
1126 *val = get_reg_val(id, vcpu->arch.wort);
1127 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001128 case KVM_REG_PPC_VPA_ADDR:
1129 spin_lock(&vcpu->arch.vpa_update_lock);
1130 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1131 spin_unlock(&vcpu->arch.vpa_update_lock);
1132 break;
1133 case KVM_REG_PPC_VPA_SLB:
1134 spin_lock(&vcpu->arch.vpa_update_lock);
1135 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1136 val->vpaval.length = vcpu->arch.slb_shadow.len;
1137 spin_unlock(&vcpu->arch.vpa_update_lock);
1138 break;
1139 case KVM_REG_PPC_VPA_DTL:
1140 spin_lock(&vcpu->arch.vpa_update_lock);
1141 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1142 val->vpaval.length = vcpu->arch.dtl.len;
1143 spin_unlock(&vcpu->arch.vpa_update_lock);
1144 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001145 case KVM_REG_PPC_TB_OFFSET:
1146 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1147 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001148 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001149 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001150 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1151 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001152 case KVM_REG_PPC_PPR:
1153 *val = get_reg_val(id, vcpu->arch.ppr);
1154 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001155#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1156 case KVM_REG_PPC_TFHAR:
1157 *val = get_reg_val(id, vcpu->arch.tfhar);
1158 break;
1159 case KVM_REG_PPC_TFIAR:
1160 *val = get_reg_val(id, vcpu->arch.tfiar);
1161 break;
1162 case KVM_REG_PPC_TEXASR:
1163 *val = get_reg_val(id, vcpu->arch.texasr);
1164 break;
1165 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1166 i = id - KVM_REG_PPC_TM_GPR0;
1167 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1168 break;
1169 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1170 {
1171 int j;
1172 i = id - KVM_REG_PPC_TM_VSR0;
1173 if (i < 32)
1174 for (j = 0; j < TS_FPRWIDTH; j++)
1175 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1176 else {
1177 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1178 val->vval = vcpu->arch.vr_tm.vr[i-32];
1179 else
1180 r = -ENXIO;
1181 }
1182 break;
1183 }
1184 case KVM_REG_PPC_TM_CR:
1185 *val = get_reg_val(id, vcpu->arch.cr_tm);
1186 break;
1187 case KVM_REG_PPC_TM_LR:
1188 *val = get_reg_val(id, vcpu->arch.lr_tm);
1189 break;
1190 case KVM_REG_PPC_TM_CTR:
1191 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1192 break;
1193 case KVM_REG_PPC_TM_FPSCR:
1194 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1195 break;
1196 case KVM_REG_PPC_TM_AMR:
1197 *val = get_reg_val(id, vcpu->arch.amr_tm);
1198 break;
1199 case KVM_REG_PPC_TM_PPR:
1200 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1201 break;
1202 case KVM_REG_PPC_TM_VRSAVE:
1203 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1204 break;
1205 case KVM_REG_PPC_TM_VSCR:
1206 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1207 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1208 else
1209 r = -ENXIO;
1210 break;
1211 case KVM_REG_PPC_TM_DSCR:
1212 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1213 break;
1214 case KVM_REG_PPC_TM_TAR:
1215 *val = get_reg_val(id, vcpu->arch.tar_tm);
1216 break;
1217#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001218 case KVM_REG_PPC_ARCH_COMPAT:
1219 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1220 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001221 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001222 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001223 break;
1224 }
1225
1226 return r;
1227}
1228
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301229static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1230 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001231{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001232 int r = 0;
1233 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001234 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001235
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001236 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001237 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001238 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001239 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001240 r = -EINVAL;
1241 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001242 case KVM_REG_PPC_DABR:
1243 vcpu->arch.dabr = set_reg_val(id, *val);
1244 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001245 case KVM_REG_PPC_DABRX:
1246 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1247 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001248 case KVM_REG_PPC_DSCR:
1249 vcpu->arch.dscr = set_reg_val(id, *val);
1250 break;
1251 case KVM_REG_PPC_PURR:
1252 vcpu->arch.purr = set_reg_val(id, *val);
1253 break;
1254 case KVM_REG_PPC_SPURR:
1255 vcpu->arch.spurr = set_reg_val(id, *val);
1256 break;
1257 case KVM_REG_PPC_AMR:
1258 vcpu->arch.amr = set_reg_val(id, *val);
1259 break;
1260 case KVM_REG_PPC_UAMOR:
1261 vcpu->arch.uamor = set_reg_val(id, *val);
1262 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001263 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001264 i = id - KVM_REG_PPC_MMCR0;
1265 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1266 break;
1267 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1268 i = id - KVM_REG_PPC_PMC1;
1269 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1270 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001271 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1272 i = id - KVM_REG_PPC_SPMC1;
1273 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1274 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001275 case KVM_REG_PPC_SIAR:
1276 vcpu->arch.siar = set_reg_val(id, *val);
1277 break;
1278 case KVM_REG_PPC_SDAR:
1279 vcpu->arch.sdar = set_reg_val(id, *val);
1280 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001281 case KVM_REG_PPC_SIER:
1282 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001283 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001284 case KVM_REG_PPC_IAMR:
1285 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001286 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001287 case KVM_REG_PPC_PSPB:
1288 vcpu->arch.pspb = set_reg_val(id, *val);
1289 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001290 case KVM_REG_PPC_DPDES:
1291 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1292 break;
1293 case KVM_REG_PPC_DAWR:
1294 vcpu->arch.dawr = set_reg_val(id, *val);
1295 break;
1296 case KVM_REG_PPC_DAWRX:
1297 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1298 break;
1299 case KVM_REG_PPC_CIABR:
1300 vcpu->arch.ciabr = set_reg_val(id, *val);
1301 /* Don't allow setting breakpoints in hypervisor code */
1302 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1303 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1304 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001305 case KVM_REG_PPC_CSIGR:
1306 vcpu->arch.csigr = set_reg_val(id, *val);
1307 break;
1308 case KVM_REG_PPC_TACR:
1309 vcpu->arch.tacr = set_reg_val(id, *val);
1310 break;
1311 case KVM_REG_PPC_TCSCR:
1312 vcpu->arch.tcscr = set_reg_val(id, *val);
1313 break;
1314 case KVM_REG_PPC_PID:
1315 vcpu->arch.pid = set_reg_val(id, *val);
1316 break;
1317 case KVM_REG_PPC_ACOP:
1318 vcpu->arch.acop = set_reg_val(id, *val);
1319 break;
1320 case KVM_REG_PPC_WORT:
1321 vcpu->arch.wort = set_reg_val(id, *val);
1322 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001323 case KVM_REG_PPC_VPA_ADDR:
1324 addr = set_reg_val(id, *val);
1325 r = -EINVAL;
1326 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1327 vcpu->arch.dtl.next_gpa))
1328 break;
1329 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1330 break;
1331 case KVM_REG_PPC_VPA_SLB:
1332 addr = val->vpaval.addr;
1333 len = val->vpaval.length;
1334 r = -EINVAL;
1335 if (addr && !vcpu->arch.vpa.next_gpa)
1336 break;
1337 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1338 break;
1339 case KVM_REG_PPC_VPA_DTL:
1340 addr = val->vpaval.addr;
1341 len = val->vpaval.length;
1342 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001343 if (addr && (len < sizeof(struct dtl_entry) ||
1344 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001345 break;
1346 len -= len % sizeof(struct dtl_entry);
1347 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1348 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001349 case KVM_REG_PPC_TB_OFFSET:
1350 /* round up to multiple of 2^24 */
1351 vcpu->arch.vcore->tb_offset =
1352 ALIGN(set_reg_val(id, *val), 1UL << 24);
1353 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001354 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001355 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1356 break;
1357 case KVM_REG_PPC_LPCR_64:
1358 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001359 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001360 case KVM_REG_PPC_PPR:
1361 vcpu->arch.ppr = set_reg_val(id, *val);
1362 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001363#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1364 case KVM_REG_PPC_TFHAR:
1365 vcpu->arch.tfhar = set_reg_val(id, *val);
1366 break;
1367 case KVM_REG_PPC_TFIAR:
1368 vcpu->arch.tfiar = set_reg_val(id, *val);
1369 break;
1370 case KVM_REG_PPC_TEXASR:
1371 vcpu->arch.texasr = set_reg_val(id, *val);
1372 break;
1373 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1374 i = id - KVM_REG_PPC_TM_GPR0;
1375 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1376 break;
1377 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1378 {
1379 int j;
1380 i = id - KVM_REG_PPC_TM_VSR0;
1381 if (i < 32)
1382 for (j = 0; j < TS_FPRWIDTH; j++)
1383 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1384 else
1385 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1386 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1387 else
1388 r = -ENXIO;
1389 break;
1390 }
1391 case KVM_REG_PPC_TM_CR:
1392 vcpu->arch.cr_tm = set_reg_val(id, *val);
1393 break;
1394 case KVM_REG_PPC_TM_LR:
1395 vcpu->arch.lr_tm = set_reg_val(id, *val);
1396 break;
1397 case KVM_REG_PPC_TM_CTR:
1398 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1399 break;
1400 case KVM_REG_PPC_TM_FPSCR:
1401 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1402 break;
1403 case KVM_REG_PPC_TM_AMR:
1404 vcpu->arch.amr_tm = set_reg_val(id, *val);
1405 break;
1406 case KVM_REG_PPC_TM_PPR:
1407 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1408 break;
1409 case KVM_REG_PPC_TM_VRSAVE:
1410 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1411 break;
1412 case KVM_REG_PPC_TM_VSCR:
1413 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1414 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1415 else
1416 r = - ENXIO;
1417 break;
1418 case KVM_REG_PPC_TM_DSCR:
1419 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1420 break;
1421 case KVM_REG_PPC_TM_TAR:
1422 vcpu->arch.tar_tm = set_reg_val(id, *val);
1423 break;
1424#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001425 case KVM_REG_PPC_ARCH_COMPAT:
1426 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1427 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001428 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001429 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001430 break;
1431 }
1432
1433 return r;
1434}
1435
Stewart Smithde9bdd12014-07-18 14:18:42 +10001436static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1437{
1438 struct kvmppc_vcore *vcore;
1439
1440 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1441
1442 if (vcore == NULL)
1443 return NULL;
1444
1445 INIT_LIST_HEAD(&vcore->runnable_threads);
1446 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001447 spin_lock_init(&vcore->stoltb_lock);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001448 init_waitqueue_head(&vcore->wq);
1449 vcore->preempt_tb = TB_NIL;
1450 vcore->lpcr = kvm->arch.lpcr;
1451 vcore->first_vcpuid = core * threads_per_subcore;
1452 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001453 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001454
Stewart Smith9678cda2014-07-18 14:18:43 +10001455 vcore->mpp_buffer_is_valid = false;
1456
1457 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1458 vcore->mpp_buffer = (void *)__get_free_pages(
1459 GFP_KERNEL|__GFP_ZERO,
1460 MPP_BUFFER_ORDER);
1461
Stewart Smithde9bdd12014-07-18 14:18:42 +10001462 return vcore;
1463}
1464
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001465#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1466static struct debugfs_timings_element {
1467 const char *name;
1468 size_t offset;
1469} timings[] = {
1470 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1471 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1472 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1473 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1474 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1475};
1476
1477#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1478
1479struct debugfs_timings_state {
1480 struct kvm_vcpu *vcpu;
1481 unsigned int buflen;
1482 char buf[N_TIMINGS * 100];
1483};
1484
1485static int debugfs_timings_open(struct inode *inode, struct file *file)
1486{
1487 struct kvm_vcpu *vcpu = inode->i_private;
1488 struct debugfs_timings_state *p;
1489
1490 p = kzalloc(sizeof(*p), GFP_KERNEL);
1491 if (!p)
1492 return -ENOMEM;
1493
1494 kvm_get_kvm(vcpu->kvm);
1495 p->vcpu = vcpu;
1496 file->private_data = p;
1497
1498 return nonseekable_open(inode, file);
1499}
1500
1501static int debugfs_timings_release(struct inode *inode, struct file *file)
1502{
1503 struct debugfs_timings_state *p = file->private_data;
1504
1505 kvm_put_kvm(p->vcpu->kvm);
1506 kfree(p);
1507 return 0;
1508}
1509
1510static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1511 size_t len, loff_t *ppos)
1512{
1513 struct debugfs_timings_state *p = file->private_data;
1514 struct kvm_vcpu *vcpu = p->vcpu;
1515 char *s, *buf_end;
1516 struct kvmhv_tb_accumulator tb;
1517 u64 count;
1518 loff_t pos;
1519 ssize_t n;
1520 int i, loops;
1521 bool ok;
1522
1523 if (!p->buflen) {
1524 s = p->buf;
1525 buf_end = s + sizeof(p->buf);
1526 for (i = 0; i < N_TIMINGS; ++i) {
1527 struct kvmhv_tb_accumulator *acc;
1528
1529 acc = (struct kvmhv_tb_accumulator *)
1530 ((unsigned long)vcpu + timings[i].offset);
1531 ok = false;
1532 for (loops = 0; loops < 1000; ++loops) {
1533 count = acc->seqcount;
1534 if (!(count & 1)) {
1535 smp_rmb();
1536 tb = *acc;
1537 smp_rmb();
1538 if (count == acc->seqcount) {
1539 ok = true;
1540 break;
1541 }
1542 }
1543 udelay(1);
1544 }
1545 if (!ok)
1546 snprintf(s, buf_end - s, "%s: stuck\n",
1547 timings[i].name);
1548 else
1549 snprintf(s, buf_end - s,
1550 "%s: %llu %llu %llu %llu\n",
1551 timings[i].name, count / 2,
1552 tb_to_ns(tb.tb_total),
1553 tb_to_ns(tb.tb_min),
1554 tb_to_ns(tb.tb_max));
1555 s += strlen(s);
1556 }
1557 p->buflen = s - p->buf;
1558 }
1559
1560 pos = *ppos;
1561 if (pos >= p->buflen)
1562 return 0;
1563 if (len > p->buflen - pos)
1564 len = p->buflen - pos;
1565 n = copy_to_user(buf, p->buf + pos, len);
1566 if (n) {
1567 if (n == len)
1568 return -EFAULT;
1569 len -= n;
1570 }
1571 *ppos = pos + len;
1572 return len;
1573}
1574
1575static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1576 size_t len, loff_t *ppos)
1577{
1578 return -EACCES;
1579}
1580
1581static const struct file_operations debugfs_timings_ops = {
1582 .owner = THIS_MODULE,
1583 .open = debugfs_timings_open,
1584 .release = debugfs_timings_release,
1585 .read = debugfs_timings_read,
1586 .write = debugfs_timings_write,
1587 .llseek = generic_file_llseek,
1588};
1589
1590/* Create a debugfs directory for the vcpu */
1591static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1592{
1593 char buf[16];
1594 struct kvm *kvm = vcpu->kvm;
1595
1596 snprintf(buf, sizeof(buf), "vcpu%u", id);
1597 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1598 return;
1599 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1600 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1601 return;
1602 vcpu->arch.debugfs_timings =
1603 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1604 vcpu, &debugfs_timings_ops);
1605}
1606
1607#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1608static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1609{
1610}
1611#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1612
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301613static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1614 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001615{
1616 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001617 int err = -EINVAL;
1618 int core;
1619 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001620
Michael Ellerman3102f782014-05-23 18:15:29 +10001621 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001622 if (core >= KVM_MAX_VCORES)
1623 goto out;
1624
1625 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001626 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001627 if (!vcpu)
1628 goto out;
1629
1630 err = kvm_vcpu_init(vcpu, kvm, id);
1631 if (err)
1632 goto free_vcpu;
1633
1634 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001635#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1636 /*
1637 * The shared struct is never shared on HV,
1638 * so we can always use host endianness
1639 */
1640#ifdef __BIG_ENDIAN__
1641 vcpu->arch.shared_big_endian = true;
1642#else
1643 vcpu->arch.shared_big_endian = false;
1644#endif
1645#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001646 vcpu->arch.mmcr[0] = MMCR0_FC;
1647 vcpu->arch.ctrl = CTRL_RUNLATCH;
1648 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301649 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001650 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001651 spin_lock_init(&vcpu->arch.tbacct_lock);
1652 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001653 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001654
Paul Mackerrasde56a942011-06-29 00:21:34 +00001655 kvmppc_mmu_book3s_hv_init(vcpu);
1656
Paul Mackerras8455d792012-10-15 01:17:42 +00001657 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001658
1659 init_waitqueue_head(&vcpu->arch.cpu_run);
1660
1661 mutex_lock(&kvm->lock);
1662 vcore = kvm->arch.vcores[core];
1663 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001664 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001665 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001666 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001667 }
1668 mutex_unlock(&kvm->lock);
1669
1670 if (!vcore)
1671 goto free_vcpu;
1672
1673 spin_lock(&vcore->lock);
1674 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001675 spin_unlock(&vcore->lock);
1676 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001677 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001678 vcpu->arch.thread_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001679
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001680 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1681 kvmppc_sanity_check(vcpu);
1682
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001683 debugfs_vcpu_init(vcpu, id);
1684
Paul Mackerrasde56a942011-06-29 00:21:34 +00001685 return vcpu;
1686
1687free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001688 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001689out:
1690 return ERR_PTR(err);
1691}
1692
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001693static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1694{
1695 if (vpa->pinned_addr)
1696 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1697 vpa->dirty);
1698}
1699
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301700static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001701{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001702 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001703 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1704 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1705 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001706 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001707 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001708 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001709}
1710
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301711static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1712{
1713 /* Indicate we want to get back into the guest */
1714 return 1;
1715}
1716
Paul Mackerras19ccb762011-07-23 17:42:46 +10001717static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001718{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001719 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001720
Paul Mackerras19ccb762011-07-23 17:42:46 +10001721 now = get_tb();
1722 if (now > vcpu->arch.dec_expires) {
1723 /* decrementer has already gone negative */
1724 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001725 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001726 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001727 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001728 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1729 / tb_ticks_per_sec;
1730 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1731 HRTIMER_MODE_REL);
1732 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001733}
1734
Paul Mackerras19ccb762011-07-23 17:42:46 +10001735static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001736{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001737 vcpu->arch.ceded = 0;
1738 if (vcpu->arch.timer_running) {
1739 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1740 vcpu->arch.timer_running = 0;
1741 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001742}
1743
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001744extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001745
1746static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1747 struct kvm_vcpu *vcpu)
1748{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001749 u64 now;
1750
Paul Mackerras371fefd2011-06-29 00:23:08 +00001751 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1752 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001753 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001754 now = mftb();
1755 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1756 vcpu->arch.stolen_logged;
1757 vcpu->arch.busy_preempt = now;
1758 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001759 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001760 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001761 list_del(&vcpu->arch.run_list);
1762}
1763
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001764static int kvmppc_grab_hwthread(int cpu)
1765{
1766 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001767 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001768
1769 tpaca = &paca[cpu];
1770
1771 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00001772 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001773 tpaca->kvm_hstate.napping = 0;
1774 smp_wmb();
1775 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001776
1777 /*
1778 * If the thread is already executing in the kernel (e.g. handling
1779 * a stray interrupt), wait for it to get back to nap mode.
1780 * The smp_mb() is to ensure that our setting of hwthread_req
1781 * is visible before we look at hwthread_state, so if this
1782 * races with the code at system_reset_pSeries and the thread
1783 * misses our setting of hwthread_req, we are sure to see its
1784 * setting of hwthread_state, and vice versa.
1785 */
1786 smp_mb();
1787 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1788 if (--timeout <= 0) {
1789 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1790 return -EBUSY;
1791 }
1792 udelay(1);
1793 }
1794 return 0;
1795}
1796
1797static void kvmppc_release_hwthread(int cpu)
1798{
1799 struct paca_struct *tpaca;
1800
1801 tpaca = &paca[cpu];
1802 tpaca->kvm_hstate.hwthread_req = 0;
1803 tpaca->kvm_hstate.kvm_vcpu = NULL;
1804}
1805
Paul Mackerras371fefd2011-06-29 00:23:08 +00001806static void kvmppc_start_thread(struct kvm_vcpu *vcpu)
1807{
1808 int cpu;
1809 struct paca_struct *tpaca;
1810 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasec257162015-06-24 21:18:03 +10001811 struct kvmppc_vcore *mvc = vc->master_vcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001812
Paul Mackerras19ccb762011-07-23 17:42:46 +10001813 if (vcpu->arch.timer_running) {
1814 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1815 vcpu->arch.timer_running = 0;
1816 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001817 cpu = vc->pcpu + vcpu->arch.ptid;
1818 tpaca = &paca[cpu];
Paul Mackerrasec257162015-06-24 21:18:03 +10001819 tpaca->kvm_hstate.kvm_vcore = mvc;
1820 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
1821 vcpu->cpu = mvc->pcpu;
1822 vcpu->arch.thread_cpu = cpu;
1823 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras371fefd2011-06-29 00:23:08 +00001824 smp_wmb();
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001825 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001826 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11001827 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001828}
1829
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001830static void kvmppc_wait_for_nap(void)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001831{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001832 int cpu = smp_processor_id();
1833 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001834
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001835 for (loops = 0; loops < 1000000; ++loops) {
1836 /*
1837 * Check if all threads are finished.
1838 * We set the vcpu pointer when starting a thread
1839 * and the thread clears it when finished, so we look
1840 * for any threads that still have a non-NULL vcpu ptr.
1841 */
1842 for (i = 1; i < threads_per_subcore; ++i)
1843 if (paca[cpu + i].kvm_hstate.kvm_vcpu)
1844 break;
1845 if (i == threads_per_subcore) {
1846 HMT_medium();
1847 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001848 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001849 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001850 }
1851 HMT_medium();
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001852 for (i = 1; i < threads_per_subcore; ++i)
1853 if (paca[cpu + i].kvm_hstate.kvm_vcpu)
1854 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001855}
1856
1857/*
1858 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001859 * this core are off-line. Then grab the threads so they can't
1860 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001861 */
1862static int on_primary_thread(void)
1863{
1864 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001865 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001866
Michael Ellerman3102f782014-05-23 18:15:29 +10001867 /* Are we on a primary subcore? */
1868 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001869 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001870
1871 thr = 0;
1872 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001873 if (cpu_online(cpu + thr))
1874 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001875
1876 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001877 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001878 if (kvmppc_grab_hwthread(cpu + thr)) {
1879 /* Couldn't grab one; let the others go */
1880 do {
1881 kvmppc_release_hwthread(cpu + thr);
1882 } while (--thr > 0);
1883 return 0;
1884 }
1885 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001886 return 1;
1887}
1888
Stewart Smith9678cda2014-07-18 14:18:43 +10001889static void kvmppc_start_saving_l2_cache(struct kvmppc_vcore *vc)
1890{
1891 phys_addr_t phy_addr, mpp_addr;
1892
1893 phy_addr = (phys_addr_t)virt_to_phys(vc->mpp_buffer);
1894 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1895
1896 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_ABORT);
1897 logmpp(mpp_addr | PPC_LOGMPP_LOG_L2);
1898
1899 vc->mpp_buffer_is_valid = true;
1900}
1901
1902static void kvmppc_start_restoring_l2_cache(const struct kvmppc_vcore *vc)
1903{
1904 phys_addr_t phy_addr, mpp_addr;
1905
1906 phy_addr = virt_to_phys(vc->mpp_buffer);
1907 mpp_addr = phy_addr & PPC_MPPE_ADDRESS_MASK;
1908
1909 /* We must abort any in-progress save operations to ensure
1910 * the table is valid so that prefetch engine knows when to
1911 * stop prefetching. */
1912 logmpp(mpp_addr | PPC_LOGMPP_LOG_ABORT);
1913 mtspr(SPRN_MPPR, mpp_addr | PPC_MPPR_FETCH_WHOLE_TABLE);
1914}
1915
Paul Mackerrasec257162015-06-24 21:18:03 +10001916/*
1917 * A list of virtual cores for each physical CPU.
1918 * These are vcores that could run but their runner VCPU tasks are
1919 * (or may be) preempted.
1920 */
1921struct preempted_vcore_list {
1922 struct list_head list;
1923 spinlock_t lock;
1924};
1925
1926static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
1927
1928static void init_vcore_lists(void)
1929{
1930 int cpu;
1931
1932 for_each_possible_cpu(cpu) {
1933 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
1934 spin_lock_init(&lp->lock);
1935 INIT_LIST_HEAD(&lp->list);
1936 }
1937}
1938
1939static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
1940{
1941 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
1942
1943 vc->vcore_state = VCORE_PREEMPT;
1944 vc->pcpu = smp_processor_id();
1945 if (vc->num_threads < threads_per_subcore) {
1946 spin_lock(&lp->lock);
1947 list_add_tail(&vc->preempt_list, &lp->list);
1948 spin_unlock(&lp->lock);
1949 }
1950
1951 /* Start accumulating stolen time */
1952 kvmppc_core_start_stolen(vc);
1953}
1954
1955static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
1956{
1957 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
1958
1959 kvmppc_core_end_stolen(vc);
1960 if (!list_empty(&vc->preempt_list)) {
1961 spin_lock(&lp->lock);
1962 list_del_init(&vc->preempt_list);
1963 spin_unlock(&lp->lock);
1964 }
1965 vc->vcore_state = VCORE_INACTIVE;
1966}
1967
1968struct core_info {
1969 int total_threads;
1970 struct list_head vcs;
1971};
1972
1973static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
1974{
1975 memset(cip, 0, sizeof(*cip));
1976 cip->total_threads = vc->num_threads;
1977 INIT_LIST_HEAD(&cip->vcs);
1978 list_add_tail(&vc->preempt_list, &cip->vcs);
1979}
1980
1981static void init_master_vcore(struct kvmppc_vcore *vc)
1982{
1983 vc->master_vcore = vc;
1984 vc->entry_exit_map = 0;
1985 vc->in_guest = 0;
1986 vc->napping_threads = 0;
1987 vc->conferring_threads = 0;
1988}
1989
1990/*
1991 * Work out whether it is possible to piggyback the execute of
1992 * vcore *pvc onto the execution of the other vcores described in *cip.
1993 */
1994static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
1995 int target_threads)
1996{
1997 struct kvmppc_vcore *vc;
1998
1999 vc = list_first_entry(&cip->vcs, struct kvmppc_vcore, preempt_list);
2000
2001 /* require same VM and same per-core reg values */
2002 if (pvc->kvm != vc->kvm ||
2003 pvc->tb_offset != vc->tb_offset ||
2004 pvc->pcr != vc->pcr ||
2005 pvc->lpcr != vc->lpcr)
2006 return false;
2007
2008 /* P8 guest with > 1 thread per core would see wrong TIR value */
2009 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
2010 (vc->num_threads > 1 || pvc->num_threads > 1))
2011 return false;
2012
2013 if (cip->total_threads + pvc->num_threads > target_threads)
2014 return false;
2015
2016 cip->total_threads += pvc->num_threads;
2017 pvc->master_vcore = vc;
2018 list_del(&pvc->preempt_list);
2019 list_add_tail(&pvc->preempt_list, &cip->vcs);
2020
2021 return true;
2022}
2023
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002024static void prepare_threads(struct kvmppc_vcore *vc)
2025{
2026 struct kvm_vcpu *vcpu, *vnext;
2027
2028 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2029 arch.run_list) {
2030 if (signal_pending(vcpu->arch.run_task))
2031 vcpu->arch.ret = -EINTR;
2032 else if (vcpu->arch.vpa.update_pending ||
2033 vcpu->arch.slb_shadow.update_pending ||
2034 vcpu->arch.dtl.update_pending)
2035 vcpu->arch.ret = RESUME_GUEST;
2036 else
2037 continue;
2038 kvmppc_remove_runnable(vc, vcpu);
2039 wake_up(&vcpu->arch.cpu_run);
2040 }
2041}
2042
Paul Mackerrasec257162015-06-24 21:18:03 +10002043static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002044{
Paul Mackerrasec257162015-06-24 21:18:03 +10002045 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2046 struct kvmppc_vcore *pvc, *vcnext;
2047
2048 spin_lock(&lp->lock);
2049 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2050 if (!spin_trylock(&pvc->lock))
2051 continue;
2052 prepare_threads(pvc);
2053 if (!pvc->n_runnable) {
2054 list_del_init(&pvc->preempt_list);
2055 if (pvc->runner == NULL) {
2056 pvc->vcore_state = VCORE_INACTIVE;
2057 kvmppc_core_end_stolen(pvc);
2058 }
2059 spin_unlock(&pvc->lock);
2060 continue;
2061 }
2062 if (!can_piggyback(pvc, cip, target_threads)) {
2063 spin_unlock(&pvc->lock);
2064 continue;
2065 }
2066 kvmppc_core_end_stolen(pvc);
2067 pvc->vcore_state = VCORE_PIGGYBACK;
2068 if (cip->total_threads >= target_threads)
2069 break;
2070 }
2071 spin_unlock(&lp->lock);
2072}
2073
2074static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2075{
2076 int still_running = 0;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002077 u64 now;
2078 long ret;
2079 struct kvm_vcpu *vcpu, *vnext;
2080
Paul Mackerrasec257162015-06-24 21:18:03 +10002081 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002082 now = get_tb();
2083 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2084 arch.run_list) {
2085 /* cancel pending dec exception if dec is positive */
2086 if (now < vcpu->arch.dec_expires &&
2087 kvmppc_core_pending_dec(vcpu))
2088 kvmppc_core_dequeue_dec(vcpu);
2089
2090 trace_kvm_guest_exit(vcpu);
2091
2092 ret = RESUME_GUEST;
2093 if (vcpu->arch.trap)
2094 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2095 vcpu->arch.run_task);
2096
2097 vcpu->arch.ret = ret;
2098 vcpu->arch.trap = 0;
2099
Paul Mackerrasec257162015-06-24 21:18:03 +10002100 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2101 if (vcpu->arch.pending_exceptions)
2102 kvmppc_core_prepare_to_enter(vcpu);
2103 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002104 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002105 else
2106 ++still_running;
2107 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002108 kvmppc_remove_runnable(vc, vcpu);
2109 wake_up(&vcpu->arch.cpu_run);
2110 }
2111 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002112 list_del_init(&vc->preempt_list);
2113 if (!is_master) {
2114 vc->vcore_state = vc->runner ? VCORE_PREEMPT : VCORE_INACTIVE;
2115 if (still_running > 0)
2116 kvmppc_vcore_preempt(vc);
2117 if (vc->n_runnable > 0 && vc->runner == NULL) {
2118 /* make sure there's a candidate runner awake */
2119 vcpu = list_first_entry(&vc->runnable_threads,
2120 struct kvm_vcpu, arch.run_list);
2121 wake_up(&vcpu->arch.cpu_run);
2122 }
2123 }
2124 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002125}
2126
Paul Mackerras371fefd2011-06-29 00:23:08 +00002127/*
2128 * Run a set of guest threads on a physical core.
2129 * Called with vc->lock held.
2130 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002131static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002132{
Paul Mackerras17d48902015-04-29 14:49:07 +10002133 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002134 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002135 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002136 struct core_info core_info;
2137 struct kvmppc_vcore *pvc, *vcnext;
2138 int pcpu, thr;
2139 int target_threads;
Paul Mackerras081f3232012-06-01 20:20:24 +10002140
2141 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002142 * Remove from the list any threads that have a signal pending
2143 * or need a VPA update done
2144 */
2145 prepare_threads(vc);
2146
2147 /* if the runner is no longer runnable, let the caller pick a new one */
2148 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2149 return;
2150
2151 /*
2152 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002153 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002154 init_master_vcore(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002155 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002156
2157 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002158 * Make sure we are running on primary threads, and that secondary
2159 * threads are offline. Also check if the number of threads in this
2160 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002161 */
Michael Ellerman3102f782014-05-23 18:15:29 +10002162 if ((threads_per_core > 1) &&
2163 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras17d48902015-04-29 14:49:07 +10002164 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2165 arch.run_list) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002166 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002167 kvmppc_remove_runnable(vc, vcpu);
2168 wake_up(&vcpu->arch.cpu_run);
2169 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002170 goto out;
2171 }
2172
Paul Mackerrasec257162015-06-24 21:18:03 +10002173 /*
2174 * See if we could run any other vcores on the physical core
2175 * along with this one.
2176 */
2177 init_core_info(&core_info, vc);
2178 pcpu = smp_processor_id();
2179 target_threads = threads_per_subcore;
2180 if (target_smt_mode && target_smt_mode < target_threads)
2181 target_threads = target_smt_mode;
2182 if (vc->num_threads < target_threads)
2183 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002184
Paul Mackerrasec257162015-06-24 21:18:03 +10002185 thr = 0;
2186 list_for_each_entry(pvc, &core_info.vcs, preempt_list) {
2187 pvc->pcpu = pcpu + thr;
2188 list_for_each_entry(vcpu, &pvc->runnable_threads,
2189 arch.run_list) {
2190 kvmppc_start_thread(vcpu);
2191 kvmppc_create_dtl_entry(vcpu, pvc);
2192 trace_kvm_guest_enter(vcpu);
2193 }
2194 thr += pvc->num_threads;
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002195 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002196
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002197 /* Set this explicitly in case thread 0 doesn't have a vcpu */
2198 get_paca()->kvm_hstate.kvm_vcore = vc;
2199 get_paca()->kvm_hstate.ptid = 0;
2200
Paul Mackerras2f12f032012-10-15 01:17:17 +00002201 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002202 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002203
2204 trace_kvmppc_run_core(vc, 0);
2205
Paul Mackerrasec257162015-06-24 21:18:03 +10002206 list_for_each_entry(pvc, &core_info.vcs, preempt_list)
2207 spin_unlock(&pvc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002208
Paul Mackerras19ccb762011-07-23 17:42:46 +10002209 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002210
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002211 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002212
Stewart Smith9678cda2014-07-18 14:18:43 +10002213 if (vc->mpp_buffer_is_valid)
2214 kvmppc_start_restoring_l2_cache(vc);
2215
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002216 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002217
Stewart Smith9678cda2014-07-18 14:18:43 +10002218 if (vc->mpp_buffer)
2219 kvmppc_start_saving_l2_cache(vc);
2220
Paul Mackerrasec257162015-06-24 21:18:03 +10002221 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2222
2223 spin_lock(&vc->lock);
2224 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
2225 vc->vcore_state = VCORE_EXITING;
2226
Paul Mackerras19ccb762011-07-23 17:42:46 +10002227 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11002228 kvmppc_wait_for_nap();
Michael Ellerman3102f782014-05-23 18:15:29 +10002229 for (i = 0; i < threads_per_subcore; ++i)
Paul Mackerras2f12f032012-10-15 01:17:17 +00002230 kvmppc_release_hwthread(vc->pcpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002231 spin_unlock(&vc->lock);
2232
2233 /* make sure updates to secondary vcpu structs are visible now */
2234 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002235 kvm_guest_exit();
2236
Paul Mackerrasec257162015-06-24 21:18:03 +10002237 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs, preempt_list)
2238 post_guest_process(pvc, pvc == vc);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002239
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002240 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002241 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002242
Paul Mackerrasde56a942011-06-29 00:21:34 +00002243 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002244 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002245 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002246}
2247
Paul Mackerras19ccb762011-07-23 17:42:46 +10002248/*
2249 * Wait for some other vcpu thread to execute us, and
2250 * wake us up when we need to handle something in the host.
2251 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002252static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2253 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002254{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002255 DEFINE_WAIT(wait);
2256
Paul Mackerras19ccb762011-07-23 17:42:46 +10002257 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002258 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2259 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002260 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002261 spin_lock(&vc->lock);
2262 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002263 finish_wait(&vcpu->arch.cpu_run, &wait);
2264}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002265
Paul Mackerras19ccb762011-07-23 17:42:46 +10002266/*
2267 * All the vcpus in this vcore are idle, so wait for a decrementer
2268 * or external interrupt to one of the vcpus. vc->lock is held.
2269 */
2270static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2271{
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002272 struct kvm_vcpu *vcpu;
2273 int do_sleep = 1;
2274
Paul Mackerras19ccb762011-07-23 17:42:46 +10002275 DEFINE_WAIT(wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002276
2277 prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002278
2279 /*
2280 * Check one last time for pending exceptions and ceded state after
2281 * we put ourselves on the wait queue
2282 */
2283 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
2284 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
2285 do_sleep = 0;
2286 break;
2287 }
2288 }
2289
2290 if (!do_sleep) {
2291 finish_wait(&vc->wq, &wait);
2292 return;
2293 }
2294
Paul Mackerras19ccb762011-07-23 17:42:46 +10002295 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002296 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002297 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002298 schedule();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002299 finish_wait(&vc->wq, &wait);
2300 spin_lock(&vc->lock);
2301 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002302 trace_kvmppc_vcore_blocked(vc, 1);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002303}
2304
2305static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2306{
2307 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002308 struct kvmppc_vcore *vc;
2309 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002310
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002311 trace_kvmppc_run_vcpu_enter(vcpu);
2312
Paul Mackerras371fefd2011-06-29 00:23:08 +00002313 kvm_run->exit_reason = 0;
2314 vcpu->arch.ret = RESUME_GUEST;
2315 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00002316 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002317
Paul Mackerras371fefd2011-06-29 00:23:08 +00002318 /*
2319 * Synchronize with other threads in this virtual core
2320 */
2321 vc = vcpu->arch.vcore;
2322 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002323 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002324 vcpu->arch.run_task = current;
2325 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002326 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10002327 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002328 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002329 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
2330 ++vc->n_runnable;
2331
Paul Mackerras19ccb762011-07-23 17:42:46 +10002332 /*
2333 * This happens the first time this is called for a vcpu.
2334 * If the vcore is already running, we may be able to start
2335 * this thread straight away and have it join in.
2336 */
Paul Mackerras8455d792012-10-15 01:17:42 +00002337 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002338 if (vc->vcore_state == VCORE_PIGGYBACK) {
2339 struct kvmppc_vcore *mvc = vc->master_vcore;
2340 if (spin_trylock(&mvc->lock)) {
2341 if (mvc->vcore_state == VCORE_RUNNING &&
2342 !VCORE_IS_EXITING(mvc)) {
2343 kvmppc_create_dtl_entry(vcpu, vc);
2344 kvmppc_start_thread(vcpu);
2345 trace_kvm_guest_enter(vcpu);
2346 }
2347 spin_unlock(&mvc->lock);
2348 }
2349 } else if (vc->vcore_state == VCORE_RUNNING &&
2350 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00002351 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002352 kvmppc_start_thread(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002353 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00002354 } else if (vc->vcore_state == VCORE_SLEEPING) {
2355 wake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002356 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002357
Paul Mackerras8455d792012-10-15 01:17:42 +00002358 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002359
2360 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2361 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002362 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2363 kvmppc_vcore_end_preempt(vc);
2364
Paul Mackerras8455d792012-10-15 01:17:42 +00002365 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002366 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002367 continue;
2368 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002369 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
2370 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06002371 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002372 if (signal_pending(v->arch.run_task)) {
2373 kvmppc_remove_runnable(vc, v);
2374 v->stat.signal_exits++;
2375 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2376 v->arch.ret = -EINTR;
2377 wake_up(&v->arch.cpu_run);
2378 }
2379 }
Paul Mackerras8455d792012-10-15 01:17:42 +00002380 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2381 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00002382 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002383 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002384 if (!v->arch.pending_exceptions)
2385 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002386 else
2387 v->arch.ceded = 0;
2388 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002389 vc->runner = vcpu;
2390 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002391 kvmppc_vcore_blocked(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002392 } else if (should_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002393 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002394 /* Let something else run */
2395 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002396 if (vc->vcore_state == VCORE_PREEMPT)
2397 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002398 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00002399 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002400 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00002401 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002402 }
2403
Paul Mackerras8455d792012-10-15 01:17:42 +00002404 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2405 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerrasec257162015-06-24 21:18:03 +10002406 vc->vcore_state == VCORE_EXITING))
2407 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00002408
2409 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2410 kvmppc_remove_runnable(vc, vcpu);
2411 vcpu->stat.signal_exits++;
2412 kvm_run->exit_reason = KVM_EXIT_INTR;
2413 vcpu->arch.ret = -EINTR;
2414 }
2415
2416 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2417 /* Wake up some vcpu to run the core */
2418 v = list_first_entry(&vc->runnable_threads,
2419 struct kvm_vcpu, arch.run_list);
2420 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002421 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002422
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002423 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002424 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002425 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002426}
2427
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302428static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002429{
2430 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002431 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002432
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002433 if (!vcpu->arch.sane) {
2434 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2435 return -EINVAL;
2436 }
2437
Scott Wood25051b52011-11-08 18:23:23 -06002438 kvmppc_core_prepare_to_enter(vcpu);
2439
Paul Mackerras19ccb762011-07-23 17:42:46 +10002440 /* No need to go into the guest when all we'll do is come back out */
2441 if (signal_pending(current)) {
2442 run->exit_reason = KVM_EXIT_INTR;
2443 return -EINTR;
2444 }
2445
Paul Mackerras32fad282012-05-04 02:32:53 +00002446 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002447 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002448 smp_mb();
2449
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002450 /* On the first time here, set up HTAB and VRMA */
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002451 if (!vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002452 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002453 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002454 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002455 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002456
2457 flush_fp_to_thread(current);
2458 flush_altivec_to_thread(current);
2459 flush_vsx_to_thread(current);
2460 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002461 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002462 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002463
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002464 do {
2465 r = kvmppc_run_vcpu(run, vcpu);
2466
2467 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2468 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002469 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002470 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002471 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002472 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002473 } else if (r == RESUME_PAGE_FAULT) {
2474 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2475 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2476 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2477 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002478 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002479 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002480
2481 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002482 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002483 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002484 return r;
2485}
2486
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002487static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2488 int linux_psize)
2489{
2490 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2491
2492 if (!def->shift)
2493 return;
2494 (*sps)->page_shift = def->shift;
2495 (*sps)->slb_enc = def->sllp;
2496 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002497 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302498 /*
2499 * Add 16MB MPSS support if host supports it
2500 */
2501 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2502 (*sps)->enc[1].page_shift = 24;
2503 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2504 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002505 (*sps)++;
2506}
2507
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302508static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2509 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002510{
2511 struct kvm_ppc_one_seg_page_size *sps;
2512
2513 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2514 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2515 info->flags |= KVM_PPC_1T_SEGMENTS;
2516 info->slb_size = mmu_slb_size;
2517
2518 /* We only support these sizes for now, and no muti-size segments */
2519 sps = &info->sps[0];
2520 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2521 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2522 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2523
2524 return 0;
2525}
2526
Paul Mackerras82ed3612011-12-15 02:03:22 +00002527/*
2528 * Get (and clear) the dirty memory log for a memory slot.
2529 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302530static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2531 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002532{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002533 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00002534 struct kvm_memory_slot *memslot;
2535 int r;
2536 unsigned long n;
2537
2538 mutex_lock(&kvm->slots_lock);
2539
2540 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002541 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002542 goto out;
2543
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002544 slots = kvm_memslots(kvm);
2545 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002546 r = -ENOENT;
2547 if (!memslot->dirty_bitmap)
2548 goto out;
2549
2550 n = kvm_dirty_bitmap_bytes(memslot);
2551 memset(memslot->dirty_bitmap, 0, n);
2552
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002553 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002554 if (r)
2555 goto out;
2556
2557 r = -EFAULT;
2558 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2559 goto out;
2560
2561 r = 0;
2562out:
2563 mutex_unlock(&kvm->slots_lock);
2564 return r;
2565}
2566
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302567static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2568 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002569{
2570 if (!dont || free->arch.rmap != dont->arch.rmap) {
2571 vfree(free->arch.rmap);
2572 free->arch.rmap = NULL;
2573 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002574}
2575
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302576static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2577 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002578{
2579 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2580 if (!slot->arch.rmap)
2581 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002582
2583 return 0;
2584}
2585
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302586static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2587 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002588 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002589{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002590 return 0;
2591}
2592
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302593static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002594 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002595 const struct kvm_memory_slot *old,
2596 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002597{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002598 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002599 struct kvm_memslots *slots;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002600 struct kvm_memory_slot *memslot;
2601
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002602 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002603 /*
2604 * If modifying a memslot, reset all the rmap dirty bits.
2605 * If this is a new memslot, we don't need to do anything
2606 * since the rmap array starts out as all zeroes,
2607 * i.e. no pages are dirty.
2608 */
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002609 slots = kvm_memslots(kvm);
2610 memslot = id_to_memslot(slots, mem->slot);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002611 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2612 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002613}
2614
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002615/*
2616 * Update LPCR values in kvm->arch and in vcores.
2617 * Caller must hold kvm->lock.
2618 */
2619void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2620{
2621 long int i;
2622 u32 cores_done = 0;
2623
2624 if ((kvm->arch.lpcr & mask) == lpcr)
2625 return;
2626
2627 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2628
2629 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2630 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2631 if (!vc)
2632 continue;
2633 spin_lock(&vc->lock);
2634 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2635 spin_unlock(&vc->lock);
2636 if (++cores_done >= kvm->arch.online_vcores)
2637 break;
2638 }
2639}
2640
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302641static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2642{
2643 return;
2644}
2645
Paul Mackerras32fad282012-05-04 02:32:53 +00002646static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002647{
2648 int err = 0;
2649 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002650 unsigned long hva;
2651 struct kvm_memory_slot *memslot;
2652 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002653 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002654 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002655 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002656
2657 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002658 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002659 goto out; /* another vcpu beat us to it */
2660
Paul Mackerras32fad282012-05-04 02:32:53 +00002661 /* Allocate hashed page table (if not done already) and reset it */
2662 if (!kvm->arch.hpt_virt) {
2663 err = kvmppc_alloc_hpt(kvm, NULL);
2664 if (err) {
2665 pr_err("KVM: Couldn't alloc HPT\n");
2666 goto out;
2667 }
2668 }
2669
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002670 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002671 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002672 memslot = gfn_to_memslot(kvm, 0);
2673
2674 /* We must have some memory at 0 by now */
2675 err = -EINVAL;
2676 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002677 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002678
2679 /* Look up the VMA for the start of this memory slot */
2680 hva = memslot->userspace_addr;
2681 down_read(&current->mm->mmap_sem);
2682 vma = find_vma(current->mm, hva);
2683 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
2684 goto up_out;
2685
2686 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002687 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002688
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002689 up_read(&current->mm->mmap_sem);
2690
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002691 /* We can handle 4k, 64k or 16M pages in the VRMA */
2692 err = -EINVAL;
2693 if (!(psize == 0x1000 || psize == 0x10000 ||
2694 psize == 0x1000000))
2695 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002696
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002697 /* Update VRMASD field in the LPCR */
2698 senc = slb_pgsize_encoding(psize);
2699 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
2700 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2701 /* the -4 is to account for senc values starting at 0x10 */
2702 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00002703
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002704 /* Create HPTEs in the hash page table for the VRMA */
2705 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002706
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002707 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002708
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002709 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002710 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002711 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002712 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002713 out_srcu:
2714 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002715 out:
2716 mutex_unlock(&kvm->lock);
2717 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002718
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002719 up_out:
2720 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08002721 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002722}
2723
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302724static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002725{
Paul Mackerras32fad282012-05-04 02:32:53 +00002726 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11002727 char buf[32];
Paul Mackerrasde56a942011-06-29 00:21:34 +00002728
Paul Mackerras32fad282012-05-04 02:32:53 +00002729 /* Allocate the guest's logical partition ID */
2730
2731 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08002732 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00002733 return -ENOMEM;
2734 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002735
Paul Mackerras1b400ba2012-11-21 23:28:08 +00002736 /*
2737 * Since we don't flush the TLB when tearing down a VM,
2738 * and this lpid might have previously been used,
2739 * make sure we flush on each core before running the new VM.
2740 */
2741 cpumask_setall(&kvm->arch.need_tlb_flush);
2742
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002743 /* Start out with the default set of hcalls enabled */
2744 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
2745 sizeof(kvm->arch.enabled_hcalls));
2746
Paul Mackerras9e368f22011-06-29 00:40:08 +00002747 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002748
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002749 /* Init LPCR for virtual RMA mode */
2750 kvm->arch.host_lpid = mfspr(SPRN_LPID);
2751 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
2752 lpcr &= LPCR_PECE | LPCR_LPES;
2753 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
2754 LPCR_VPM0 | LPCR_VPM1;
2755 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
2756 (VRMA_VSID << SLB_VSID_SHIFT_1T);
2757 /* On POWER8 turn on online bit to enable PURR/SPURR */
2758 if (cpu_has_feature(CPU_FTR_ARCH_207S))
2759 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002760 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002761
Paul Mackerras512691d2012-10-15 01:15:41 +00002762 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10002763 * Track that we now have a HV mode VM active. This blocks secondary
2764 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00002765 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10002766 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002767
Paul Mackerrase23a8082015-03-28 14:21:01 +11002768 /*
2769 * Create a debugfs directory for the VM
2770 */
2771 snprintf(buf, sizeof(buf), "vm%d", current->pid);
2772 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
2773 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
2774 kvmppc_mmu_debugfs_init(kvm);
2775
David Gibson54738c02011-06-29 00:22:41 +00002776 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002777}
2778
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302779static void kvmppc_free_vcores(struct kvm *kvm)
2780{
2781 long int i;
2782
Stewart Smith9678cda2014-07-18 14:18:43 +10002783 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2784 if (kvm->arch.vcores[i] && kvm->arch.vcores[i]->mpp_buffer) {
2785 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2786 free_pages((unsigned long)vc->mpp_buffer,
2787 MPP_BUFFER_ORDER);
2788 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302789 kfree(kvm->arch.vcores[i]);
Stewart Smith9678cda2014-07-18 14:18:43 +10002790 }
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302791 kvm->arch.online_vcores = 0;
2792}
2793
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302794static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002795{
Paul Mackerrase23a8082015-03-28 14:21:01 +11002796 debugfs_remove_recursive(kvm->arch.debugfs_dir);
2797
Michael Ellerman441c19c2014-05-23 18:15:25 +10002798 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00002799
Paul Mackerrasf1378b12013-09-27 15:33:43 +05302800 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00002801
Paul Mackerrasde56a942011-06-29 00:21:34 +00002802 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002803}
2804
2805/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302806static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
2807 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002808{
2809 return EMULATE_FAIL;
2810}
2811
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302812static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
2813 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002814{
2815 return EMULATE_FAIL;
2816}
2817
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302818static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
2819 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002820{
2821 return EMULATE_FAIL;
2822}
2823
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302824static int kvmppc_core_check_processor_compat_hv(void)
2825{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002826 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
2827 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302828 return -EIO;
2829 return 0;
2830}
2831
2832static long kvm_arch_vm_ioctl_hv(struct file *filp,
2833 unsigned int ioctl, unsigned long arg)
2834{
2835 struct kvm *kvm __maybe_unused = filp->private_data;
2836 void __user *argp = (void __user *)arg;
2837 long r;
2838
2839 switch (ioctl) {
2840
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302841 case KVM_PPC_ALLOCATE_HTAB: {
2842 u32 htab_order;
2843
2844 r = -EFAULT;
2845 if (get_user(htab_order, (u32 __user *)argp))
2846 break;
2847 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
2848 if (r)
2849 break;
2850 r = -EFAULT;
2851 if (put_user(htab_order, (u32 __user *)argp))
2852 break;
2853 r = 0;
2854 break;
2855 }
2856
2857 case KVM_PPC_GET_HTAB_FD: {
2858 struct kvm_get_htab_fd ghf;
2859
2860 r = -EFAULT;
2861 if (copy_from_user(&ghf, argp, sizeof(ghf)))
2862 break;
2863 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
2864 break;
2865 }
2866
2867 default:
2868 r = -ENOTTY;
2869 }
2870
2871 return r;
2872}
2873
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002874/*
2875 * List of hcall numbers to enable by default.
2876 * For compatibility with old userspace, we enable by default
2877 * all hcalls that were implemented before the hcall-enabling
2878 * facility was added. Note this list should not include H_RTAS.
2879 */
2880static unsigned int default_hcall_list[] = {
2881 H_REMOVE,
2882 H_ENTER,
2883 H_READ,
2884 H_PROTECT,
2885 H_BULK_REMOVE,
2886 H_GET_TCE,
2887 H_PUT_TCE,
2888 H_SET_DABR,
2889 H_SET_XDABR,
2890 H_CEDE,
2891 H_PROD,
2892 H_CONFER,
2893 H_REGISTER_VPA,
2894#ifdef CONFIG_KVM_XICS
2895 H_EOI,
2896 H_CPPR,
2897 H_IPI,
2898 H_IPOLL,
2899 H_XIRR,
2900 H_XIRR_X,
2901#endif
2902 0
2903};
2904
2905static void init_default_hcalls(void)
2906{
2907 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002908 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002909
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002910 for (i = 0; default_hcall_list[i]; ++i) {
2911 hcall = default_hcall_list[i];
2912 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
2913 __set_bit(hcall / 4, default_enabled_hcalls);
2914 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002915}
2916
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302917static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302918 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
2919 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
2920 .get_one_reg = kvmppc_get_one_reg_hv,
2921 .set_one_reg = kvmppc_set_one_reg_hv,
2922 .vcpu_load = kvmppc_core_vcpu_load_hv,
2923 .vcpu_put = kvmppc_core_vcpu_put_hv,
2924 .set_msr = kvmppc_set_msr_hv,
2925 .vcpu_run = kvmppc_vcpu_run_hv,
2926 .vcpu_create = kvmppc_core_vcpu_create_hv,
2927 .vcpu_free = kvmppc_core_vcpu_free_hv,
2928 .check_requests = kvmppc_core_check_requests_hv,
2929 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
2930 .flush_memslot = kvmppc_core_flush_memslot_hv,
2931 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
2932 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
2933 .unmap_hva = kvm_unmap_hva_hv,
2934 .unmap_hva_range = kvm_unmap_hva_range_hv,
2935 .age_hva = kvm_age_hva_hv,
2936 .test_age_hva = kvm_test_age_hva_hv,
2937 .set_spte_hva = kvm_set_spte_hva_hv,
2938 .mmu_destroy = kvmppc_mmu_destroy_hv,
2939 .free_memslot = kvmppc_core_free_memslot_hv,
2940 .create_memslot = kvmppc_core_create_memslot_hv,
2941 .init_vm = kvmppc_core_init_vm_hv,
2942 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302943 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
2944 .emulate_op = kvmppc_core_emulate_op_hv,
2945 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
2946 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
2947 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
2948 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10002949 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302950};
2951
2952static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002953{
2954 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302955 /*
2956 * FIXME!! Do we need to check on all cpus ?
2957 */
2958 r = kvmppc_core_check_processor_compat_hv();
2959 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11002960 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002961
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302962 kvm_ops_hv.owner = THIS_MODULE;
2963 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002964
Paul Mackerras699a0ea2014-06-02 11:02:59 +10002965 init_default_hcalls();
2966
Paul Mackerrasec257162015-06-24 21:18:03 +10002967 init_vcore_lists();
2968
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302969 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002970 return r;
2971}
2972
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302973static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00002974{
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05302975 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002976}
2977
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302978module_init(kvmppc_book3s_init_hv);
2979module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05302980MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01002981MODULE_ALIAS_MISCDEV(KVM_MINOR);
2982MODULE_ALIAS("devname:kvm");