blob: bc27c4d6383c71dc9a37ecb654acf99546b67eb4 [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>
Gavin Shan07f8ab252016-05-11 11:15:55 +100030#include <linux/cpu.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000031#include <linux/cpumask.h>
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +000032#include <linux/spinlock.h>
33#include <linux/page-flags.h>
Paul Mackerras2c9097e2012-09-11 13:27:01 +000034#include <linux/srcu.h>
Alexander Graf398a76c2013-12-09 13:53:42 +010035#include <linux/miscdevice.h>
Paul Mackerrase23a8082015-03-28 14:21:01 +110036#include <linux/debugfs.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000037
38#include <asm/reg.h>
39#include <asm/cputable.h>
40#include <asm/cacheflush.h>
41#include <asm/tlbflush.h>
42#include <asm/uaccess.h>
43#include <asm/io.h>
44#include <asm/kvm_ppc.h>
45#include <asm/kvm_book3s.h>
46#include <asm/mmu_context.h>
47#include <asm/lppaca.h>
48#include <asm/processor.h>
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>
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +053055#include <asm/hmi.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000056#include <linux/gfp.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000057#include <linux/vmalloc.h>
58#include <linux/highmem.h>
Paul Mackerrasc77162d2011-12-12 12:31:00 +000059#include <linux/hugetlb.h>
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +053060#include <linux/module.h>
Paul Mackerrasde56a942011-06-29 00:21:34 +000061
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +053062#include "book3s.h"
63
Suresh E. Warrier3c78f782014-12-03 18:48:10 -060064#define CREATE_TRACE_POINTS
65#include "trace_hv.h"
66
Paul Mackerrasde56a942011-06-29 00:21:34 +000067/* #define EXIT_DEBUG */
68/* #define EXIT_DEBUG_SIMPLE */
69/* #define EXIT_DEBUG_INT */
70
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +000071/* Used to indicate that a guest page fault needs to be handled */
72#define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1)
73
Paul Mackerrasc7b67672012-10-15 01:18:07 +000074/* Used as a "null" value for timebase values */
75#define TB_NIL (~(u64)0)
76
Paul Mackerras699a0ea2014-06-02 11:02:59 +100077static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1);
78
Paul Mackerrasb4deba52015-07-02 20:38:16 +100079static int dynamic_mt_modes = 6;
80module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR);
81MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)");
Paul Mackerrasec257162015-06-24 21:18:03 +100082static int target_smt_mode;
83module_param(target_smt_mode, int, S_IRUGO | S_IWUSR);
84MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)");
Stewart Smith9678cda2014-07-18 14:18:43 +100085
Suresh E. Warrier520fe9c2015-12-21 16:33:57 -060086#ifdef CONFIG_KVM_XICS
87static struct kernel_param_ops module_param_ops = {
88 .set = param_set_int,
89 .get = param_get_int,
90};
91
92module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect,
93 S_IRUGO | S_IWUSR);
94MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core");
95#endif
96
Paul Mackerras19ccb762011-07-23 17:42:46 +100097static void kvmppc_end_cede(struct kvm_vcpu *vcpu);
Paul Mackerras32fad282012-05-04 02:32:53 +000098static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +100099
Paul Mackerras66feed62015-03-28 14:21:12 +1100100static bool kvmppc_ipi_thread(int cpu)
101{
102 /* On POWER8 for IPIs to threads in the same core, use msgsnd */
103 if (cpu_has_feature(CPU_FTR_ARCH_207S)) {
104 preempt_disable();
105 if (cpu_first_thread_sibling(cpu) ==
106 cpu_first_thread_sibling(smp_processor_id())) {
107 unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER);
108 msg |= cpu_thread_in_core(cpu);
109 smp_mb();
110 __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg));
111 preempt_enable();
112 return true;
113 }
114 preempt_enable();
115 }
116
117#if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP)
118 if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) {
119 xics_wake_cpu(cpu);
120 return true;
121 }
122#endif
123
124 return false;
125}
126
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530127static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu)
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000128{
Paul Mackerrasec257162015-06-24 21:18:03 +1000129 int cpu;
Marcelo Tosatti85773702016-02-19 09:46:39 +0100130 struct swait_queue_head *wqp;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000131
132 wqp = kvm_arch_vcpu_wq(vcpu);
Marcelo Tosatti85773702016-02-19 09:46:39 +0100133 if (swait_active(wqp)) {
134 swake_up(wqp);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000135 ++vcpu->stat.halt_wakeup;
136 }
137
Paul Mackerrasec257162015-06-24 21:18:03 +1000138 if (kvmppc_ipi_thread(vcpu->arch.thread_cpu))
Paul Mackerras66feed62015-03-28 14:21:12 +1100139 return;
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000140
141 /* CPU points to the first thread of the core */
Paul Mackerrasec257162015-06-24 21:18:03 +1000142 cpu = vcpu->cpu;
Paul Mackerras66feed62015-03-28 14:21:12 +1100143 if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu))
144 smp_send_reschedule(cpu);
Benjamin Herrenschmidt54695c32013-04-17 20:30:50 +0000145}
146
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000147/*
148 * We use the vcpu_load/put functions to measure stolen time.
149 * Stolen time is counted as time when either the vcpu is able to
150 * run as part of a virtual core, but the task running the vcore
151 * is preempted or sleeping, or when the vcpu needs something done
152 * in the kernel by the task running the vcpu, but that task is
153 * preempted or sleeping. Those two things have to be counted
154 * separately, since one of the vcpu tasks will take on the job
155 * of running the core, and the other vcpu tasks in the vcore will
156 * sleep waiting for it to do that, but that sleep shouldn't count
157 * as stolen time.
158 *
159 * Hence we accumulate stolen time when the vcpu can run as part of
160 * a vcore using vc->stolen_tb, and the stolen time when the vcpu
161 * needs its task to do other things in the kernel (for example,
162 * service a page fault) in busy_stolen. We don't accumulate
163 * stolen time for a vcore when it is inactive, or for a vcpu
164 * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of
165 * a misnomer; it means that the vcpu task is not executing in
166 * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in
167 * the kernel. We don't have any way of dividing up that time
168 * between time that the vcpu is genuinely stopped, time that
169 * the task is actively working on behalf of the vcpu, and time
170 * that the task is preempted, so we don't count any of it as
171 * stolen.
172 *
173 * Updates to busy_stolen are protected by arch.tbacct_lock;
Paul Mackerras2711e242014-12-04 16:43:28 +1100174 * updates to vc->stolen_tb are protected by the vcore->stoltb_lock
175 * lock. The stolen times are measured in units of timebase ticks.
176 * (Note that the != TB_NIL checks below are purely defensive;
177 * they should never fail.)
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000178 */
179
Paul Mackerrasec257162015-06-24 21:18:03 +1000180static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc)
181{
182 unsigned long flags;
183
184 spin_lock_irqsave(&vc->stoltb_lock, flags);
185 vc->preempt_tb = mftb();
186 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
187}
188
189static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc)
190{
191 unsigned long flags;
192
193 spin_lock_irqsave(&vc->stoltb_lock, flags);
194 if (vc->preempt_tb != TB_NIL) {
195 vc->stolen_tb += mftb() - vc->preempt_tb;
196 vc->preempt_tb = TB_NIL;
197 }
198 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
199}
200
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530201static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000202{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000203 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100204 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000205
Paul Mackerras2711e242014-12-04 16:43:28 +1100206 /*
207 * We can test vc->runner without taking the vcore lock,
208 * because only this task ever sets vc->runner to this
209 * vcpu, and once it is set to this vcpu, only this task
210 * ever sets it to NULL.
211 */
Paul Mackerrasec257162015-06-24 21:18:03 +1000212 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
213 kvmppc_core_end_stolen(vc);
214
Paul Mackerras2711e242014-12-04 16:43:28 +1100215 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000216 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST &&
217 vcpu->arch.busy_preempt != TB_NIL) {
218 vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt;
219 vcpu->arch.busy_preempt = TB_NIL;
220 }
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100221 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000222}
223
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530224static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000225{
Paul Mackerras0456ec42012-02-03 00:56:21 +0000226 struct kvmppc_vcore *vc = vcpu->arch.vcore;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100227 unsigned long flags;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000228
Paul Mackerrasec257162015-06-24 21:18:03 +1000229 if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING)
230 kvmppc_core_start_stolen(vc);
231
Paul Mackerras2711e242014-12-04 16:43:28 +1100232 spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000233 if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST)
234 vcpu->arch.busy_preempt = mftb();
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100235 spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000236}
237
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530238static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000239{
Paul Mackerrasc20875a2015-11-12 16:43:02 +1100240 /*
241 * Check for illegal transactional state bit combination
242 * and if we find it, force the TS field to a safe state.
243 */
244 if ((msr & MSR_TS_MASK) == MSR_TS_MASK)
245 msr &= ~MSR_TS_MASK;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000246 vcpu->arch.shregs.msr = msr;
Paul Mackerras19ccb762011-07-23 17:42:46 +1000247 kvmppc_end_cede(vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000248}
249
Thomas Huth5358a962015-05-22 09:25:02 +0200250static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000251{
252 vcpu->arch.pvr = pvr;
253}
254
Thomas Huth5358a962015-05-22 09:25:02 +0200255static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat)
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000256{
257 unsigned long pcr = 0;
258 struct kvmppc_vcore *vc = vcpu->arch.vcore;
259
260 if (arch_compat) {
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000261 switch (arch_compat) {
262 case PVR_ARCH_205:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100263 /*
264 * If an arch bit is set in PCR, all the defined
265 * higher-order arch bits also have to be set.
266 */
267 pcr = PCR_ARCH_206 | PCR_ARCH_205;
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000268 break;
269 case PVR_ARCH_206:
270 case PVR_ARCH_206p:
Paul Mackerras5557ae02014-01-08 21:25:24 +1100271 pcr = PCR_ARCH_206;
272 break;
273 case PVR_ARCH_207:
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000274 break;
275 default:
276 return -EINVAL;
277 }
Paul Mackerras5557ae02014-01-08 21:25:24 +1100278
279 if (!cpu_has_feature(CPU_FTR_ARCH_207S)) {
280 /* POWER7 can't emulate POWER8 */
281 if (!(pcr & PCR_ARCH_206))
282 return -EINVAL;
283 pcr &= ~PCR_ARCH_206;
284 }
Paul Mackerras388cc6e2013-09-21 14:35:02 +1000285 }
286
287 spin_lock(&vc->lock);
288 vc->arch_compat = arch_compat;
289 vc->pcr = pcr;
290 spin_unlock(&vc->lock);
291
292 return 0;
293}
294
Thomas Huth5358a962015-05-22 09:25:02 +0200295static void kvmppc_dump_regs(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000296{
297 int r;
298
299 pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id);
300 pr_err("pc = %.16lx msr = %.16llx trap = %x\n",
301 vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap);
302 for (r = 0; r < 16; ++r)
303 pr_err("r%2d = %.16lx r%d = %.16lx\n",
304 r, kvmppc_get_gpr(vcpu, r),
305 r+16, kvmppc_get_gpr(vcpu, r+16));
306 pr_err("ctr = %.16lx lr = %.16lx\n",
307 vcpu->arch.ctr, vcpu->arch.lr);
308 pr_err("srr0 = %.16llx srr1 = %.16llx\n",
309 vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1);
310 pr_err("sprg0 = %.16llx sprg1 = %.16llx\n",
311 vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1);
312 pr_err("sprg2 = %.16llx sprg3 = %.16llx\n",
313 vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3);
314 pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n",
315 vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr);
316 pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar);
317 pr_err("fault dar = %.16lx dsisr = %.8x\n",
318 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
319 pr_err("SLB (%d entries):\n", vcpu->arch.slb_max);
320 for (r = 0; r < vcpu->arch.slb_max; ++r)
321 pr_err(" ESID = %.16llx VSID = %.16llx\n",
322 vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv);
323 pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n",
Paul Mackerrasa0144e22013-09-20 14:52:38 +1000324 vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1,
Paul Mackerrasde56a942011-06-29 00:21:34 +0000325 vcpu->arch.last_inst);
326}
327
Thomas Huth5358a962015-05-22 09:25:02 +0200328static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000329{
David Hildenbrande09fefd2015-11-05 09:03:50 +0100330 struct kvm_vcpu *ret;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000331
332 mutex_lock(&kvm->lock);
David Hildenbrande09fefd2015-11-05 09:03:50 +0100333 ret = kvm_get_vcpu_by_id(kvm, id);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000334 mutex_unlock(&kvm->lock);
335 return ret;
336}
337
338static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa)
339{
Anton Blanchardf13c13a2013-08-07 02:01:26 +1000340 vpa->__old_status |= LPPACA_OLD_SHARED_PROC;
Alexander Graf02407552014-06-11 10:34:19 +0200341 vpa->yield_count = cpu_to_be32(1);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000342}
343
Paul Mackerras55b665b2012-09-25 20:33:06 +0000344static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v,
345 unsigned long addr, unsigned long len)
346{
347 /* check address is cacheline aligned */
348 if (addr & (L1_CACHE_BYTES - 1))
349 return -EINVAL;
350 spin_lock(&vcpu->arch.vpa_update_lock);
351 if (v->next_gpa != addr || v->len != len) {
352 v->next_gpa = addr;
353 v->len = addr ? len : 0;
354 v->update_pending = 1;
355 }
356 spin_unlock(&vcpu->arch.vpa_update_lock);
357 return 0;
358}
359
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000360/* Length for a per-processor buffer is passed in at offset 4 in the buffer */
361struct reg_vpa {
362 u32 dummy;
363 union {
Alexander Graf02407552014-06-11 10:34:19 +0200364 __be16 hword;
365 __be32 word;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000366 } length;
367};
368
369static int vpa_is_registered(struct kvmppc_vpa *vpap)
370{
371 if (vpap->update_pending)
372 return vpap->next_gpa != 0;
373 return vpap->pinned_addr != NULL;
374}
375
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000376static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu,
377 unsigned long flags,
378 unsigned long vcpuid, unsigned long vpa)
379{
380 struct kvm *kvm = vcpu->kvm;
Paul Mackerras93e60242011-12-12 12:28:55 +0000381 unsigned long len, nb;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000382 void *va;
383 struct kvm_vcpu *tvcpu;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000384 int err;
385 int subfunc;
386 struct kvmppc_vpa *vpap;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000387
388 tvcpu = kvmppc_find_vcpu(kvm, vcpuid);
389 if (!tvcpu)
390 return H_PARAMETER;
391
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000392 subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK;
393 if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL ||
394 subfunc == H_VPA_REG_SLB) {
395 /* Registering new area - address must be cache-line aligned */
396 if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa)
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000397 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000398
399 /* convert logical addr to kernel addr and read length */
Paul Mackerras93e60242011-12-12 12:28:55 +0000400 va = kvmppc_pin_guest_page(kvm, vpa, &nb);
401 if (va == NULL)
Paul Mackerrasb2b2f162011-12-12 12:28:21 +0000402 return H_PARAMETER;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000403 if (subfunc == H_VPA_REG_VPA)
Alexander Graf02407552014-06-11 10:34:19 +0200404 len = be16_to_cpu(((struct reg_vpa *)va)->length.hword);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000405 else
Alexander Graf02407552014-06-11 10:34:19 +0200406 len = be32_to_cpu(((struct reg_vpa *)va)->length.word);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000407 kvmppc_unpin_guest_page(kvm, va, vpa, false);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000408
409 /* Check length */
410 if (len > nb || len < sizeof(struct reg_vpa))
411 return H_PARAMETER;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000412 } else {
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000413 vpa = 0;
414 len = 0;
415 }
416
417 err = H_PARAMETER;
418 vpap = NULL;
419 spin_lock(&tvcpu->arch.vpa_update_lock);
420
421 switch (subfunc) {
422 case H_VPA_REG_VPA: /* register VPA */
423 if (len < sizeof(struct lppaca))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000424 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000425 vpap = &tvcpu->arch.vpa;
426 err = 0;
427 break;
428
429 case H_VPA_REG_DTL: /* register DTL */
430 if (len < sizeof(struct dtl_entry))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000431 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000432 len -= len % sizeof(struct dtl_entry);
433
434 /* Check that they have previously registered a VPA */
435 err = H_RESOURCE;
436 if (!vpa_is_registered(&tvcpu->arch.vpa))
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000437 break;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000438
439 vpap = &tvcpu->arch.dtl;
440 err = 0;
441 break;
442
443 case H_VPA_REG_SLB: /* register SLB shadow buffer */
444 /* Check that they have previously registered a VPA */
445 err = H_RESOURCE;
446 if (!vpa_is_registered(&tvcpu->arch.vpa))
447 break;
448
449 vpap = &tvcpu->arch.slb_shadow;
450 err = 0;
451 break;
452
453 case H_VPA_DEREG_VPA: /* deregister VPA */
454 /* Check they don't still have a DTL or SLB buf registered */
455 err = H_RESOURCE;
456 if (vpa_is_registered(&tvcpu->arch.dtl) ||
457 vpa_is_registered(&tvcpu->arch.slb_shadow))
458 break;
459
460 vpap = &tvcpu->arch.vpa;
461 err = 0;
462 break;
463
464 case H_VPA_DEREG_DTL: /* deregister DTL */
465 vpap = &tvcpu->arch.dtl;
466 err = 0;
467 break;
468
469 case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */
470 vpap = &tvcpu->arch.slb_shadow;
471 err = 0;
472 break;
473 }
474
475 if (vpap) {
476 vpap->next_gpa = vpa;
477 vpap->len = len;
478 vpap->update_pending = 1;
479 }
480
481 spin_unlock(&tvcpu->arch.vpa_update_lock);
482
483 return err;
484}
485
Paul Mackerras081f3232012-06-01 20:20:24 +1000486static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap)
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000487{
Paul Mackerras081f3232012-06-01 20:20:24 +1000488 struct kvm *kvm = vcpu->kvm;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000489 void *va;
490 unsigned long nb;
Paul Mackerras081f3232012-06-01 20:20:24 +1000491 unsigned long gpa;
492
493 /*
494 * We need to pin the page pointed to by vpap->next_gpa,
495 * but we can't call kvmppc_pin_guest_page under the lock
496 * as it does get_user_pages() and down_read(). So we
497 * have to drop the lock, pin the page, then get the lock
498 * again and check that a new area didn't get registered
499 * in the meantime.
500 */
501 for (;;) {
502 gpa = vpap->next_gpa;
503 spin_unlock(&vcpu->arch.vpa_update_lock);
504 va = NULL;
505 nb = 0;
506 if (gpa)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000507 va = kvmppc_pin_guest_page(kvm, gpa, &nb);
Paul Mackerras081f3232012-06-01 20:20:24 +1000508 spin_lock(&vcpu->arch.vpa_update_lock);
509 if (gpa == vpap->next_gpa)
510 break;
511 /* sigh... unpin that one and try again */
512 if (va)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000513 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000514 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000515
516 vpap->update_pending = 0;
Paul Mackerras081f3232012-06-01 20:20:24 +1000517 if (va && nb < vpap->len) {
518 /*
519 * If it's now too short, it must be that userspace
520 * has changed the mappings underlying guest memory,
521 * so unregister the region.
522 */
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000523 kvmppc_unpin_guest_page(kvm, va, gpa, false);
Paul Mackerras081f3232012-06-01 20:20:24 +1000524 va = NULL;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000525 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000526 if (vpap->pinned_addr)
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000527 kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa,
528 vpap->dirty);
529 vpap->gpa = gpa;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000530 vpap->pinned_addr = va;
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000531 vpap->dirty = false;
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000532 if (va)
533 vpap->pinned_end = va + vpap->len;
534}
Paul Mackerras93e60242011-12-12 12:28:55 +0000535
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000536static void kvmppc_update_vpas(struct kvm_vcpu *vcpu)
537{
Paul Mackerras2f12f032012-10-15 01:17:17 +0000538 if (!(vcpu->arch.vpa.update_pending ||
539 vcpu->arch.slb_shadow.update_pending ||
540 vcpu->arch.dtl.update_pending))
541 return;
542
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000543 spin_lock(&vcpu->arch.vpa_update_lock);
544 if (vcpu->arch.vpa.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000545 kvmppc_update_vpa(vcpu, &vcpu->arch.vpa);
Paul Mackerras55b665b2012-09-25 20:33:06 +0000546 if (vcpu->arch.vpa.pinned_addr)
547 init_vpa(vcpu, vcpu->arch.vpa.pinned_addr);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000548 }
549 if (vcpu->arch.dtl.update_pending) {
Paul Mackerras081f3232012-06-01 20:20:24 +1000550 kvmppc_update_vpa(vcpu, &vcpu->arch.dtl);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000551 vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr;
552 vcpu->arch.dtl_index = 0;
553 }
554 if (vcpu->arch.slb_shadow.update_pending)
Paul Mackerras081f3232012-06-01 20:20:24 +1000555 kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow);
Paul Mackerras2e25aa52012-02-19 17:46:32 +0000556 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000557}
558
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000559/*
560 * Return the accumulated stolen time for the vcore up until `now'.
561 * The caller should hold the vcore lock.
562 */
563static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now)
564{
565 u64 p;
Paul Mackerras2711e242014-12-04 16:43:28 +1100566 unsigned long flags;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000567
Paul Mackerras2711e242014-12-04 16:43:28 +1100568 spin_lock_irqsave(&vc->stoltb_lock, flags);
569 p = vc->stolen_tb;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000570 if (vc->vcore_state != VCORE_INACTIVE &&
Paul Mackerras2711e242014-12-04 16:43:28 +1100571 vc->preempt_tb != TB_NIL)
572 p += now - vc->preempt_tb;
573 spin_unlock_irqrestore(&vc->stoltb_lock, flags);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000574 return p;
575}
576
Paul Mackerras0456ec42012-02-03 00:56:21 +0000577static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu,
578 struct kvmppc_vcore *vc)
579{
580 struct dtl_entry *dt;
581 struct lppaca *vpa;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000582 unsigned long stolen;
583 unsigned long core_stolen;
584 u64 now;
Paul Mackerras0456ec42012-02-03 00:56:21 +0000585
586 dt = vcpu->arch.dtl_ptr;
587 vpa = vcpu->arch.vpa.pinned_addr;
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000588 now = mftb();
589 core_stolen = vcore_stolen_time(vc, now);
590 stolen = core_stolen - vcpu->arch.stolen_logged;
591 vcpu->arch.stolen_logged = core_stolen;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100592 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +0000593 stolen += vcpu->arch.busy_stolen;
594 vcpu->arch.busy_stolen = 0;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +1100595 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000596 if (!dt || !vpa)
597 return;
598 memset(dt, 0, sizeof(struct dtl_entry));
599 dt->dispatch_reason = 7;
Alexander Graf02407552014-06-11 10:34:19 +0200600 dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid);
601 dt->timebase = cpu_to_be64(now + vc->tb_offset);
602 dt->enqueue_to_dispatch_time = cpu_to_be32(stolen);
603 dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu));
604 dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr);
Paul Mackerras0456ec42012-02-03 00:56:21 +0000605 ++dt;
606 if (dt == vcpu->arch.dtl.pinned_end)
607 dt = vcpu->arch.dtl.pinned_addr;
608 vcpu->arch.dtl_ptr = dt;
609 /* order writing *dt vs. writing vpa->dtl_idx */
610 smp_wmb();
Alexander Graf02407552014-06-11 10:34:19 +0200611 vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index);
Paul Mackerrasc35635e2013-04-18 19:51:04 +0000612 vcpu->arch.dtl.dirty = true;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000613}
614
Michael Neuling96423822014-06-02 11:03:01 +1000615static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu)
616{
617 if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207)
618 return true;
619 if ((!vcpu->arch.vcore->arch_compat) &&
620 cpu_has_feature(CPU_FTR_ARCH_207S))
621 return true;
622 return false;
623}
624
625static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags,
626 unsigned long resource, unsigned long value1,
627 unsigned long value2)
628{
629 switch (resource) {
630 case H_SET_MODE_RESOURCE_SET_CIABR:
631 if (!kvmppc_power8_compatible(vcpu))
632 return H_P2;
633 if (value2)
634 return H_P4;
635 if (mflags)
636 return H_UNSUPPORTED_FLAG_START;
637 /* Guests can't breakpoint the hypervisor */
638 if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER)
639 return H_P3;
640 vcpu->arch.ciabr = value1;
641 return H_SUCCESS;
642 case H_SET_MODE_RESOURCE_SET_DAWR:
643 if (!kvmppc_power8_compatible(vcpu))
644 return H_P2;
645 if (mflags)
646 return H_UNSUPPORTED_FLAG_START;
647 if (value2 & DABRX_HYP)
648 return H_P4;
649 vcpu->arch.dawr = value1;
650 vcpu->arch.dawrx = value2;
651 return H_SUCCESS;
652 default:
653 return H_TOO_HARD;
654 }
655}
656
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100657static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target)
658{
659 struct kvmppc_vcore *vcore = target->arch.vcore;
660
661 /*
662 * We expect to have been called by the real mode handler
663 * (kvmppc_rm_h_confer()) which would have directly returned
664 * H_SUCCESS if the source vcore wasn't idle (e.g. if it may
665 * have useful work to do and should not confer) so we don't
666 * recheck that here.
667 */
668
669 spin_lock(&vcore->lock);
670 if (target->arch.state == KVMPPC_VCPU_RUNNABLE &&
Paul Mackerrasec257162015-06-24 21:18:03 +1000671 vcore->vcore_state != VCORE_INACTIVE &&
672 vcore->runner)
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100673 target = vcore->runner;
674 spin_unlock(&vcore->lock);
675
676 return kvm_vcpu_yield_to(target);
677}
678
679static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu)
680{
681 int yield_count = 0;
682 struct lppaca *lppaca;
683
684 spin_lock(&vcpu->arch.vpa_update_lock);
685 lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr;
686 if (lppaca)
Paul Mackerrasecb6d612015-03-20 20:39:39 +1100687 yield_count = be32_to_cpu(lppaca->yield_count);
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100688 spin_unlock(&vcpu->arch.vpa_update_lock);
689 return yield_count;
690}
691
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000692int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu)
693{
694 unsigned long req = kvmppc_get_gpr(vcpu, 3);
695 unsigned long target, ret = H_SUCCESS;
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100696 int yield_count;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000697 struct kvm_vcpu *tvcpu;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000698 int idx, rc;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000699
Paul Mackerras699a0ea2014-06-02 11:02:59 +1000700 if (req <= MAX_HCALL_OPCODE &&
701 !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls))
702 return RESUME_HOST;
703
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000704 switch (req) {
705 case H_CEDE:
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000706 break;
707 case H_PROD:
708 target = kvmppc_get_gpr(vcpu, 4);
709 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
710 if (!tvcpu) {
711 ret = H_PARAMETER;
712 break;
713 }
714 tvcpu->arch.prodded = 1;
715 smp_mb();
716 if (vcpu->arch.ceded) {
Marcelo Tosatti85773702016-02-19 09:46:39 +0100717 if (swait_active(&vcpu->wq)) {
718 swake_up(&vcpu->wq);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000719 vcpu->stat.halt_wakeup++;
720 }
721 }
722 break;
723 case H_CONFER:
Paul Mackerras42d76042013-09-06 13:23:21 +1000724 target = kvmppc_get_gpr(vcpu, 4);
725 if (target == -1)
726 break;
727 tvcpu = kvmppc_find_vcpu(vcpu->kvm, target);
728 if (!tvcpu) {
729 ret = H_PARAMETER;
730 break;
731 }
Sam Bobroff90fd09f2014-12-03 13:30:40 +1100732 yield_count = kvmppc_get_gpr(vcpu, 5);
733 if (kvmppc_get_yield_count(tvcpu) != yield_count)
734 break;
735 kvm_arch_vcpu_yield_to(tvcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000736 break;
737 case H_REGISTER_VPA:
738 ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4),
739 kvmppc_get_gpr(vcpu, 5),
740 kvmppc_get_gpr(vcpu, 6));
741 break;
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000742 case H_RTAS:
743 if (list_empty(&vcpu->kvm->arch.rtas_tokens))
744 return RESUME_HOST;
745
Paul Mackerrasc9438092013-11-16 17:46:05 +1100746 idx = srcu_read_lock(&vcpu->kvm->srcu);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000747 rc = kvmppc_rtas_hcall(vcpu);
Paul Mackerrasc9438092013-11-16 17:46:05 +1100748 srcu_read_unlock(&vcpu->kvm->srcu, idx);
Michael Ellerman8e591cb2013-04-17 20:30:00 +0000749
750 if (rc == -ENOENT)
751 return RESUME_HOST;
752 else if (rc == 0)
753 break;
754
755 /* Send the error out to userspace via KVM_RUN */
756 return rc;
David Gibson99342cf82015-02-05 11:53:25 +1100757 case H_LOGICAL_CI_LOAD:
758 ret = kvmppc_h_logical_ci_load(vcpu);
759 if (ret == H_TOO_HARD)
760 return RESUME_HOST;
761 break;
762 case H_LOGICAL_CI_STORE:
763 ret = kvmppc_h_logical_ci_store(vcpu);
764 if (ret == H_TOO_HARD)
765 return RESUME_HOST;
766 break;
Michael Neuling96423822014-06-02 11:03:01 +1000767 case H_SET_MODE:
768 ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4),
769 kvmppc_get_gpr(vcpu, 5),
770 kvmppc_get_gpr(vcpu, 6),
771 kvmppc_get_gpr(vcpu, 7));
772 if (ret == H_TOO_HARD)
773 return RESUME_HOST;
774 break;
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000775 case H_XIRR:
776 case H_CPPR:
777 case H_EOI:
778 case H_IPI:
Paul Mackerras8e44ddc2013-05-23 15:42:21 +0000779 case H_IPOLL:
780 case H_XIRR_X:
Benjamin Herrenschmidtbc5ad3f2013-04-17 20:30:26 +0000781 if (kvmppc_xics_enabled(vcpu)) {
782 ret = kvmppc_xics_hcall(vcpu, req);
783 break;
Alexey Kardashevskiyd3695aa2016-02-15 12:55:09 +1100784 }
785 return RESUME_HOST;
786 case H_PUT_TCE:
787 ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
788 kvmppc_get_gpr(vcpu, 5),
789 kvmppc_get_gpr(vcpu, 6));
790 if (ret == H_TOO_HARD)
791 return RESUME_HOST;
792 break;
793 case H_PUT_TCE_INDIRECT:
794 ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4),
795 kvmppc_get_gpr(vcpu, 5),
796 kvmppc_get_gpr(vcpu, 6),
797 kvmppc_get_gpr(vcpu, 7));
798 if (ret == H_TOO_HARD)
799 return RESUME_HOST;
800 break;
801 case H_STUFF_TCE:
802 ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4),
803 kvmppc_get_gpr(vcpu, 5),
804 kvmppc_get_gpr(vcpu, 6),
805 kvmppc_get_gpr(vcpu, 7));
806 if (ret == H_TOO_HARD)
807 return RESUME_HOST;
808 break;
Paul Mackerrasa8606e22011-06-29 00:22:05 +0000809 default:
810 return RESUME_HOST;
811 }
812 kvmppc_set_gpr(vcpu, 3, ret);
813 vcpu->arch.hcall_needed = 0;
814 return RESUME_GUEST;
815}
816
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000817static int kvmppc_hcall_impl_hv(unsigned long cmd)
818{
819 switch (cmd) {
820 case H_CEDE:
821 case H_PROD:
822 case H_CONFER:
823 case H_REGISTER_VPA:
Michael Neuling96423822014-06-02 11:03:01 +1000824 case H_SET_MODE:
David Gibson99342cf82015-02-05 11:53:25 +1100825 case H_LOGICAL_CI_LOAD:
826 case H_LOGICAL_CI_STORE:
Paul Mackerrasae2113a2014-06-02 11:03:00 +1000827#ifdef CONFIG_KVM_XICS
828 case H_XIRR:
829 case H_CPPR:
830 case H_EOI:
831 case H_IPI:
832 case H_IPOLL:
833 case H_XIRR_X:
834#endif
835 return 1;
836 }
837
838 /* See if it's in the real-mode table */
839 return kvmppc_hcall_impl_hv_realmode(cmd);
840}
841
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530842static int kvmppc_emulate_debug_inst(struct kvm_run *run,
843 struct kvm_vcpu *vcpu)
844{
845 u32 last_inst;
846
847 if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) !=
848 EMULATE_DONE) {
849 /*
850 * Fetch failed, so return to guest and
851 * try executing it again.
852 */
853 return RESUME_GUEST;
854 }
855
856 if (last_inst == KVMPPC_INST_SW_BREAKPOINT) {
857 run->exit_reason = KVM_EXIT_DEBUG;
858 run->debug.arch.address = kvmppc_get_pc(vcpu);
859 return RESUME_HOST;
860 } else {
861 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
862 return RESUME_GUEST;
863 }
864}
865
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +0530866static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
867 struct task_struct *tsk)
Paul Mackerrasde56a942011-06-29 00:21:34 +0000868{
869 int r = RESUME_HOST;
870
871 vcpu->stat.sum_exits++;
872
Paul Mackerras1c9e3d52015-11-12 16:43:48 +1100873 /*
874 * This can happen if an interrupt occurs in the last stages
875 * of guest entry or the first stages of guest exit (i.e. after
876 * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV
877 * and before setting it to KVM_GUEST_MODE_HOST_HV).
878 * That can happen due to a bug, or due to a machine check
879 * occurring at just the wrong time.
880 */
881 if (vcpu->arch.shregs.msr & MSR_HV) {
882 printk(KERN_EMERG "KVM trap in HV mode!\n");
883 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
884 vcpu->arch.trap, kvmppc_get_pc(vcpu),
885 vcpu->arch.shregs.msr);
886 kvmppc_dump_regs(vcpu);
887 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
888 run->hw.hardware_exit_reason = vcpu->arch.trap;
889 return RESUME_HOST;
890 }
Paul Mackerrasde56a942011-06-29 00:21:34 +0000891 run->exit_reason = KVM_EXIT_UNKNOWN;
892 run->ready_for_interrupt_injection = 1;
893 switch (vcpu->arch.trap) {
894 /* We're good on these - the host merely wanted to get our attention */
895 case BOOK3S_INTERRUPT_HV_DECREMENTER:
896 vcpu->stat.dec_exits++;
897 r = RESUME_GUEST;
898 break;
899 case BOOK3S_INTERRUPT_EXTERNAL:
Paul Mackerras5d00f662014-01-08 21:25:28 +1100900 case BOOK3S_INTERRUPT_H_DOORBELL:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000901 vcpu->stat.ext_intr_exits++;
902 r = RESUME_GUEST;
903 break;
Mahesh Salgaonkardee6f242014-11-03 15:51:57 +1100904 /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/
905 case BOOK3S_INTERRUPT_HMI:
Paul Mackerrasde56a942011-06-29 00:21:34 +0000906 case BOOK3S_INTERRUPT_PERFMON:
907 r = RESUME_GUEST;
908 break;
Paul Mackerrasb4072df2012-11-23 22:37:50 +0000909 case BOOK3S_INTERRUPT_MACHINE_CHECK:
910 /*
911 * Deliver a machine check interrupt to the guest.
912 * We have to do this, even if the host has handled the
913 * machine check, because machine checks use SRR0/1 and
914 * the interrupt might have trashed guest state in them.
915 */
916 kvmppc_book3s_queue_irqprio(vcpu,
917 BOOK3S_INTERRUPT_MACHINE_CHECK);
918 r = RESUME_GUEST;
919 break;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000920 case BOOK3S_INTERRUPT_PROGRAM:
921 {
922 ulong flags;
923 /*
924 * Normally program interrupts are delivered directly
925 * to the guest by the hardware, but we can get here
926 * as a result of a hypervisor emulation interrupt
927 * (e40) getting turned into a 700 by BML RTAS.
928 */
929 flags = vcpu->arch.shregs.msr & 0x1f0000ull;
930 kvmppc_core_queue_program(vcpu, flags);
931 r = RESUME_GUEST;
932 break;
933 }
934 case BOOK3S_INTERRUPT_SYSCALL:
935 {
936 /* hcall - punt to userspace */
937 int i;
938
Liu Ping Fan27025a62013-11-19 14:12:48 +0800939 /* hypercall with MSR_PR has already been handled in rmode,
940 * and never reaches here.
941 */
942
Paul Mackerrasde56a942011-06-29 00:21:34 +0000943 run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3);
944 for (i = 0; i < 9; ++i)
945 run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i);
946 run->exit_reason = KVM_EXIT_PAPR_HCALL;
947 vcpu->arch.hcall_needed = 1;
948 r = RESUME_HOST;
949 break;
950 }
951 /*
Paul Mackerras342d3db2011-12-12 12:38:05 +0000952 * We get these next two if the guest accesses a page which it thinks
953 * it has mapped but which is not actually present, either because
954 * it is for an emulated I/O device or because the corresonding
955 * host page has been paged out. Any other HDSI/HISI interrupts
956 * have been handled already.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000957 */
958 case BOOK3S_INTERRUPT_H_DATA_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000959 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000960 break;
961 case BOOK3S_INTERRUPT_H_INST_STORAGE:
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +0000962 vcpu->arch.fault_dar = kvmppc_get_pc(vcpu);
963 vcpu->arch.fault_dsisr = 0;
964 r = RESUME_PAGE_FAULT;
Paul Mackerrasde56a942011-06-29 00:21:34 +0000965 break;
966 /*
967 * This occurs if the guest executes an illegal instruction.
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530968 * If the guest debug is disabled, generate a program interrupt
969 * to the guest. If guest debug is enabled, we need to check
970 * whether the instruction is a software breakpoint instruction.
971 * Accordingly return to Guest or Host.
Paul Mackerrasde56a942011-06-29 00:21:34 +0000972 */
973 case BOOK3S_INTERRUPT_H_EMUL_ASSIST:
Paul Mackerras4a157d62014-12-03 13:30:39 +1100974 if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED)
975 vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ?
976 swab32(vcpu->arch.emul_inst) :
977 vcpu->arch.emul_inst;
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +0530978 if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) {
979 r = kvmppc_emulate_debug_inst(run, vcpu);
980 } else {
981 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
982 r = RESUME_GUEST;
983 }
Michael Ellermanbd3048b2014-01-08 21:25:23 +1100984 break;
985 /*
986 * This occurs if the guest (kernel or userspace), does something that
987 * is prohibited by HFSCR. We just generate a program interrupt to
988 * the guest.
989 */
990 case BOOK3S_INTERRUPT_H_FAC_UNAVAIL:
991 kvmppc_core_queue_program(vcpu, SRR1_PROGILL);
Paul Mackerrasde56a942011-06-29 00:21:34 +0000992 r = RESUME_GUEST;
993 break;
994 default:
995 kvmppc_dump_regs(vcpu);
996 printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n",
997 vcpu->arch.trap, kvmppc_get_pc(vcpu),
998 vcpu->arch.shregs.msr);
Paul Mackerrasf3271d42013-09-20 14:52:41 +1000999 run->hw.hardware_exit_reason = vcpu->arch.trap;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001000 r = RESUME_HOST;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001001 break;
1002 }
1003
Paul Mackerrasde56a942011-06-29 00:21:34 +00001004 return r;
1005}
1006
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301007static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu,
1008 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001009{
1010 int i;
1011
Paul Mackerrasde56a942011-06-29 00:21:34 +00001012 memset(sregs, 0, sizeof(struct kvm_sregs));
Aneesh Kumar K.V87916442013-08-22 17:08:39 +05301013 sregs->pvr = vcpu->arch.pvr;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001014 for (i = 0; i < vcpu->arch.slb_max; i++) {
1015 sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige;
1016 sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv;
1017 }
1018
1019 return 0;
1020}
1021
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301022static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu,
1023 struct kvm_sregs *sregs)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001024{
1025 int i, j;
1026
Paul Mackerras9333e6c2014-09-02 16:14:43 +10001027 /* Only accept the same PVR as the host's, since we can't spoof it */
1028 if (sregs->pvr != vcpu->arch.pvr)
1029 return -EINVAL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001030
1031 j = 0;
1032 for (i = 0; i < vcpu->arch.slb_nr; i++) {
1033 if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) {
1034 vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe;
1035 vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv;
1036 ++j;
1037 }
1038 }
1039 vcpu->arch.slb_max = j;
1040
1041 return 0;
1042}
1043
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001044static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr,
1045 bool preserve_top32)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001046{
Paul Mackerras8f902b02015-03-20 20:39:38 +11001047 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001048 struct kvmppc_vcore *vc = vcpu->arch.vcore;
1049 u64 mask;
1050
Paul Mackerras8f902b02015-03-20 20:39:38 +11001051 mutex_lock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001052 spin_lock(&vc->lock);
1053 /*
Anton Blanchardd6829162014-01-08 21:25:30 +11001054 * If ILE (interrupt little-endian) has changed, update the
1055 * MSR_LE bit in the intr_msr for each vcpu in this vcore.
1056 */
1057 if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) {
Anton Blanchardd6829162014-01-08 21:25:30 +11001058 struct kvm_vcpu *vcpu;
1059 int i;
1060
Anton Blanchardd6829162014-01-08 21:25:30 +11001061 kvm_for_each_vcpu(i, vcpu, kvm) {
1062 if (vcpu->arch.vcore != vc)
1063 continue;
1064 if (new_lpcr & LPCR_ILE)
1065 vcpu->arch.intr_msr |= MSR_LE;
1066 else
1067 vcpu->arch.intr_msr &= ~MSR_LE;
1068 }
Anton Blanchardd6829162014-01-08 21:25:30 +11001069 }
1070
1071 /*
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001072 * Userspace can only modify DPFD (default prefetch depth),
1073 * ILE (interrupt little-endian) and TC (translation control).
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001074 * On POWER8 userspace can also modify AIL (alt. interrupt loc.)
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001075 */
1076 mask = LPCR_DPFD | LPCR_ILE | LPCR_TC;
Paul Mackerrase0622bd2014-01-08 21:25:27 +11001077 if (cpu_has_feature(CPU_FTR_ARCH_207S))
1078 mask |= LPCR_AIL;
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001079
1080 /* Broken 32-bit version of LPCR must not clear top bits */
1081 if (preserve_top32)
1082 mask &= 0xFFFFFFFF;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001083 vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask);
1084 spin_unlock(&vc->lock);
Paul Mackerras8f902b02015-03-20 20:39:38 +11001085 mutex_unlock(&kvm->lock);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001086}
1087
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301088static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1089 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001090{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001091 int r = 0;
1092 long int i;
Paul Mackerras31f34382011-12-12 12:26:50 +00001093
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001094 switch (id) {
Madhavan Srinivasana59c1d92014-09-09 22:37:35 +05301095 case KVM_REG_PPC_DEBUG_INST:
1096 *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT);
1097 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001098 case KVM_REG_PPC_HIOR:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001099 *val = get_reg_val(id, 0);
1100 break;
1101 case KVM_REG_PPC_DABR:
1102 *val = get_reg_val(id, vcpu->arch.dabr);
1103 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001104 case KVM_REG_PPC_DABRX:
1105 *val = get_reg_val(id, vcpu->arch.dabrx);
1106 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001107 case KVM_REG_PPC_DSCR:
1108 *val = get_reg_val(id, vcpu->arch.dscr);
1109 break;
1110 case KVM_REG_PPC_PURR:
1111 *val = get_reg_val(id, vcpu->arch.purr);
1112 break;
1113 case KVM_REG_PPC_SPURR:
1114 *val = get_reg_val(id, vcpu->arch.spurr);
1115 break;
1116 case KVM_REG_PPC_AMR:
1117 *val = get_reg_val(id, vcpu->arch.amr);
1118 break;
1119 case KVM_REG_PPC_UAMOR:
1120 *val = get_reg_val(id, vcpu->arch.uamor);
1121 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001122 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001123 i = id - KVM_REG_PPC_MMCR0;
1124 *val = get_reg_val(id, vcpu->arch.mmcr[i]);
1125 break;
1126 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1127 i = id - KVM_REG_PPC_PMC1;
1128 *val = get_reg_val(id, vcpu->arch.pmc[i]);
Paul Mackerras31f34382011-12-12 12:26:50 +00001129 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001130 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1131 i = id - KVM_REG_PPC_SPMC1;
1132 *val = get_reg_val(id, vcpu->arch.spmc[i]);
1133 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001134 case KVM_REG_PPC_SIAR:
1135 *val = get_reg_val(id, vcpu->arch.siar);
1136 break;
1137 case KVM_REG_PPC_SDAR:
1138 *val = get_reg_val(id, vcpu->arch.sdar);
1139 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001140 case KVM_REG_PPC_SIER:
1141 *val = get_reg_val(id, vcpu->arch.sier);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001142 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001143 case KVM_REG_PPC_IAMR:
1144 *val = get_reg_val(id, vcpu->arch.iamr);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001145 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001146 case KVM_REG_PPC_PSPB:
1147 *val = get_reg_val(id, vcpu->arch.pspb);
1148 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001149 case KVM_REG_PPC_DPDES:
1150 *val = get_reg_val(id, vcpu->arch.vcore->dpdes);
1151 break;
1152 case KVM_REG_PPC_DAWR:
1153 *val = get_reg_val(id, vcpu->arch.dawr);
1154 break;
1155 case KVM_REG_PPC_DAWRX:
1156 *val = get_reg_val(id, vcpu->arch.dawrx);
1157 break;
1158 case KVM_REG_PPC_CIABR:
1159 *val = get_reg_val(id, vcpu->arch.ciabr);
1160 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001161 case KVM_REG_PPC_CSIGR:
1162 *val = get_reg_val(id, vcpu->arch.csigr);
1163 break;
1164 case KVM_REG_PPC_TACR:
1165 *val = get_reg_val(id, vcpu->arch.tacr);
1166 break;
1167 case KVM_REG_PPC_TCSCR:
1168 *val = get_reg_val(id, vcpu->arch.tcscr);
1169 break;
1170 case KVM_REG_PPC_PID:
1171 *val = get_reg_val(id, vcpu->arch.pid);
1172 break;
1173 case KVM_REG_PPC_ACOP:
1174 *val = get_reg_val(id, vcpu->arch.acop);
1175 break;
1176 case KVM_REG_PPC_WORT:
1177 *val = get_reg_val(id, vcpu->arch.wort);
1178 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001179 case KVM_REG_PPC_VPA_ADDR:
1180 spin_lock(&vcpu->arch.vpa_update_lock);
1181 *val = get_reg_val(id, vcpu->arch.vpa.next_gpa);
1182 spin_unlock(&vcpu->arch.vpa_update_lock);
1183 break;
1184 case KVM_REG_PPC_VPA_SLB:
1185 spin_lock(&vcpu->arch.vpa_update_lock);
1186 val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa;
1187 val->vpaval.length = vcpu->arch.slb_shadow.len;
1188 spin_unlock(&vcpu->arch.vpa_update_lock);
1189 break;
1190 case KVM_REG_PPC_VPA_DTL:
1191 spin_lock(&vcpu->arch.vpa_update_lock);
1192 val->vpaval.addr = vcpu->arch.dtl.next_gpa;
1193 val->vpaval.length = vcpu->arch.dtl.len;
1194 spin_unlock(&vcpu->arch.vpa_update_lock);
1195 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001196 case KVM_REG_PPC_TB_OFFSET:
1197 *val = get_reg_val(id, vcpu->arch.vcore->tb_offset);
1198 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001199 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001200 case KVM_REG_PPC_LPCR_64:
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001201 *val = get_reg_val(id, vcpu->arch.vcore->lpcr);
1202 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001203 case KVM_REG_PPC_PPR:
1204 *val = get_reg_val(id, vcpu->arch.ppr);
1205 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001206#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1207 case KVM_REG_PPC_TFHAR:
1208 *val = get_reg_val(id, vcpu->arch.tfhar);
1209 break;
1210 case KVM_REG_PPC_TFIAR:
1211 *val = get_reg_val(id, vcpu->arch.tfiar);
1212 break;
1213 case KVM_REG_PPC_TEXASR:
1214 *val = get_reg_val(id, vcpu->arch.texasr);
1215 break;
1216 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1217 i = id - KVM_REG_PPC_TM_GPR0;
1218 *val = get_reg_val(id, vcpu->arch.gpr_tm[i]);
1219 break;
1220 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1221 {
1222 int j;
1223 i = id - KVM_REG_PPC_TM_VSR0;
1224 if (i < 32)
1225 for (j = 0; j < TS_FPRWIDTH; j++)
1226 val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j];
1227 else {
1228 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1229 val->vval = vcpu->arch.vr_tm.vr[i-32];
1230 else
1231 r = -ENXIO;
1232 }
1233 break;
1234 }
1235 case KVM_REG_PPC_TM_CR:
1236 *val = get_reg_val(id, vcpu->arch.cr_tm);
1237 break;
1238 case KVM_REG_PPC_TM_LR:
1239 *val = get_reg_val(id, vcpu->arch.lr_tm);
1240 break;
1241 case KVM_REG_PPC_TM_CTR:
1242 *val = get_reg_val(id, vcpu->arch.ctr_tm);
1243 break;
1244 case KVM_REG_PPC_TM_FPSCR:
1245 *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr);
1246 break;
1247 case KVM_REG_PPC_TM_AMR:
1248 *val = get_reg_val(id, vcpu->arch.amr_tm);
1249 break;
1250 case KVM_REG_PPC_TM_PPR:
1251 *val = get_reg_val(id, vcpu->arch.ppr_tm);
1252 break;
1253 case KVM_REG_PPC_TM_VRSAVE:
1254 *val = get_reg_val(id, vcpu->arch.vrsave_tm);
1255 break;
1256 case KVM_REG_PPC_TM_VSCR:
1257 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1258 *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]);
1259 else
1260 r = -ENXIO;
1261 break;
1262 case KVM_REG_PPC_TM_DSCR:
1263 *val = get_reg_val(id, vcpu->arch.dscr_tm);
1264 break;
1265 case KVM_REG_PPC_TM_TAR:
1266 *val = get_reg_val(id, vcpu->arch.tar_tm);
1267 break;
1268#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001269 case KVM_REG_PPC_ARCH_COMPAT:
1270 *val = get_reg_val(id, vcpu->arch.vcore->arch_compat);
1271 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001272 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001273 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001274 break;
1275 }
1276
1277 return r;
1278}
1279
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301280static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id,
1281 union kvmppc_one_reg *val)
Paul Mackerras31f34382011-12-12 12:26:50 +00001282{
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001283 int r = 0;
1284 long int i;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001285 unsigned long addr, len;
Paul Mackerras31f34382011-12-12 12:26:50 +00001286
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001287 switch (id) {
Paul Mackerras31f34382011-12-12 12:26:50 +00001288 case KVM_REG_PPC_HIOR:
Paul Mackerras31f34382011-12-12 12:26:50 +00001289 /* Only allow this to be set to zero */
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001290 if (set_reg_val(id, *val))
Paul Mackerras31f34382011-12-12 12:26:50 +00001291 r = -EINVAL;
1292 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001293 case KVM_REG_PPC_DABR:
1294 vcpu->arch.dabr = set_reg_val(id, *val);
1295 break;
Paul Mackerras8563bf52014-01-08 21:25:29 +11001296 case KVM_REG_PPC_DABRX:
1297 vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP;
1298 break;
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001299 case KVM_REG_PPC_DSCR:
1300 vcpu->arch.dscr = set_reg_val(id, *val);
1301 break;
1302 case KVM_REG_PPC_PURR:
1303 vcpu->arch.purr = set_reg_val(id, *val);
1304 break;
1305 case KVM_REG_PPC_SPURR:
1306 vcpu->arch.spurr = set_reg_val(id, *val);
1307 break;
1308 case KVM_REG_PPC_AMR:
1309 vcpu->arch.amr = set_reg_val(id, *val);
1310 break;
1311 case KVM_REG_PPC_UAMOR:
1312 vcpu->arch.uamor = set_reg_val(id, *val);
1313 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001314 case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001315 i = id - KVM_REG_PPC_MMCR0;
1316 vcpu->arch.mmcr[i] = set_reg_val(id, *val);
1317 break;
1318 case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8:
1319 i = id - KVM_REG_PPC_PMC1;
1320 vcpu->arch.pmc[i] = set_reg_val(id, *val);
1321 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001322 case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2:
1323 i = id - KVM_REG_PPC_SPMC1;
1324 vcpu->arch.spmc[i] = set_reg_val(id, *val);
1325 break;
Paul Mackerras14941782013-09-06 13:11:18 +10001326 case KVM_REG_PPC_SIAR:
1327 vcpu->arch.siar = set_reg_val(id, *val);
1328 break;
1329 case KVM_REG_PPC_SDAR:
1330 vcpu->arch.sdar = set_reg_val(id, *val);
1331 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001332 case KVM_REG_PPC_SIER:
1333 vcpu->arch.sier = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001334 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001335 case KVM_REG_PPC_IAMR:
1336 vcpu->arch.iamr = set_reg_val(id, *val);
Paul Mackerrasa8bd19e2012-09-25 20:32:30 +00001337 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001338 case KVM_REG_PPC_PSPB:
1339 vcpu->arch.pspb = set_reg_val(id, *val);
1340 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001341 case KVM_REG_PPC_DPDES:
1342 vcpu->arch.vcore->dpdes = set_reg_val(id, *val);
1343 break;
1344 case KVM_REG_PPC_DAWR:
1345 vcpu->arch.dawr = set_reg_val(id, *val);
1346 break;
1347 case KVM_REG_PPC_DAWRX:
1348 vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP;
1349 break;
1350 case KVM_REG_PPC_CIABR:
1351 vcpu->arch.ciabr = set_reg_val(id, *val);
1352 /* Don't allow setting breakpoints in hypervisor code */
1353 if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER)
1354 vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */
1355 break;
Michael Neulingb005255e2014-01-08 21:25:21 +11001356 case KVM_REG_PPC_CSIGR:
1357 vcpu->arch.csigr = set_reg_val(id, *val);
1358 break;
1359 case KVM_REG_PPC_TACR:
1360 vcpu->arch.tacr = set_reg_val(id, *val);
1361 break;
1362 case KVM_REG_PPC_TCSCR:
1363 vcpu->arch.tcscr = set_reg_val(id, *val);
1364 break;
1365 case KVM_REG_PPC_PID:
1366 vcpu->arch.pid = set_reg_val(id, *val);
1367 break;
1368 case KVM_REG_PPC_ACOP:
1369 vcpu->arch.acop = set_reg_val(id, *val);
1370 break;
1371 case KVM_REG_PPC_WORT:
1372 vcpu->arch.wort = set_reg_val(id, *val);
1373 break;
Paul Mackerras55b665b2012-09-25 20:33:06 +00001374 case KVM_REG_PPC_VPA_ADDR:
1375 addr = set_reg_val(id, *val);
1376 r = -EINVAL;
1377 if (!addr && (vcpu->arch.slb_shadow.next_gpa ||
1378 vcpu->arch.dtl.next_gpa))
1379 break;
1380 r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca));
1381 break;
1382 case KVM_REG_PPC_VPA_SLB:
1383 addr = val->vpaval.addr;
1384 len = val->vpaval.length;
1385 r = -EINVAL;
1386 if (addr && !vcpu->arch.vpa.next_gpa)
1387 break;
1388 r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len);
1389 break;
1390 case KVM_REG_PPC_VPA_DTL:
1391 addr = val->vpaval.addr;
1392 len = val->vpaval.length;
1393 r = -EINVAL;
Paul Mackerras9f8c8c72012-10-15 01:18:37 +00001394 if (addr && (len < sizeof(struct dtl_entry) ||
1395 !vcpu->arch.vpa.next_gpa))
Paul Mackerras55b665b2012-09-25 20:33:06 +00001396 break;
1397 len -= len % sizeof(struct dtl_entry);
1398 r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len);
1399 break;
Paul Mackerras93b0f4d2013-09-06 13:17:46 +10001400 case KVM_REG_PPC_TB_OFFSET:
1401 /* round up to multiple of 2^24 */
1402 vcpu->arch.vcore->tb_offset =
1403 ALIGN(set_reg_val(id, *val), 1UL << 24);
1404 break;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001405 case KVM_REG_PPC_LPCR:
Alexey Kardashevskiya0840242014-07-19 17:59:34 +10001406 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true);
1407 break;
1408 case KVM_REG_PPC_LPCR_64:
1409 kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10001410 break;
Paul Mackerras4b8473c2013-09-20 14:52:39 +10001411 case KVM_REG_PPC_PPR:
1412 vcpu->arch.ppr = set_reg_val(id, *val);
1413 break;
Michael Neulinga7d80d02014-03-25 10:47:03 +11001414#ifdef CONFIG_PPC_TRANSACTIONAL_MEM
1415 case KVM_REG_PPC_TFHAR:
1416 vcpu->arch.tfhar = set_reg_val(id, *val);
1417 break;
1418 case KVM_REG_PPC_TFIAR:
1419 vcpu->arch.tfiar = set_reg_val(id, *val);
1420 break;
1421 case KVM_REG_PPC_TEXASR:
1422 vcpu->arch.texasr = set_reg_val(id, *val);
1423 break;
1424 case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31:
1425 i = id - KVM_REG_PPC_TM_GPR0;
1426 vcpu->arch.gpr_tm[i] = set_reg_val(id, *val);
1427 break;
1428 case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63:
1429 {
1430 int j;
1431 i = id - KVM_REG_PPC_TM_VSR0;
1432 if (i < 32)
1433 for (j = 0; j < TS_FPRWIDTH; j++)
1434 vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j];
1435 else
1436 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1437 vcpu->arch.vr_tm.vr[i-32] = val->vval;
1438 else
1439 r = -ENXIO;
1440 break;
1441 }
1442 case KVM_REG_PPC_TM_CR:
1443 vcpu->arch.cr_tm = set_reg_val(id, *val);
1444 break;
1445 case KVM_REG_PPC_TM_LR:
1446 vcpu->arch.lr_tm = set_reg_val(id, *val);
1447 break;
1448 case KVM_REG_PPC_TM_CTR:
1449 vcpu->arch.ctr_tm = set_reg_val(id, *val);
1450 break;
1451 case KVM_REG_PPC_TM_FPSCR:
1452 vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val);
1453 break;
1454 case KVM_REG_PPC_TM_AMR:
1455 vcpu->arch.amr_tm = set_reg_val(id, *val);
1456 break;
1457 case KVM_REG_PPC_TM_PPR:
1458 vcpu->arch.ppr_tm = set_reg_val(id, *val);
1459 break;
1460 case KVM_REG_PPC_TM_VRSAVE:
1461 vcpu->arch.vrsave_tm = set_reg_val(id, *val);
1462 break;
1463 case KVM_REG_PPC_TM_VSCR:
1464 if (cpu_has_feature(CPU_FTR_ALTIVEC))
1465 vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val);
1466 else
1467 r = - ENXIO;
1468 break;
1469 case KVM_REG_PPC_TM_DSCR:
1470 vcpu->arch.dscr_tm = set_reg_val(id, *val);
1471 break;
1472 case KVM_REG_PPC_TM_TAR:
1473 vcpu->arch.tar_tm = set_reg_val(id, *val);
1474 break;
1475#endif
Paul Mackerras388cc6e2013-09-21 14:35:02 +10001476 case KVM_REG_PPC_ARCH_COMPAT:
1477 r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val));
1478 break;
Paul Mackerras31f34382011-12-12 12:26:50 +00001479 default:
Paul Mackerrasa136a8b2012-09-25 20:31:56 +00001480 r = -EINVAL;
Paul Mackerras31f34382011-12-12 12:26:50 +00001481 break;
1482 }
1483
1484 return r;
1485}
1486
Stewart Smithde9bdd12014-07-18 14:18:42 +10001487static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core)
1488{
1489 struct kvmppc_vcore *vcore;
1490
1491 vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL);
1492
1493 if (vcore == NULL)
1494 return NULL;
1495
1496 INIT_LIST_HEAD(&vcore->runnable_threads);
1497 spin_lock_init(&vcore->lock);
Paul Mackerras2711e242014-12-04 16:43:28 +11001498 spin_lock_init(&vcore->stoltb_lock);
Marcelo Tosatti85773702016-02-19 09:46:39 +01001499 init_swait_queue_head(&vcore->wq);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001500 vcore->preempt_tb = TB_NIL;
1501 vcore->lpcr = kvm->arch.lpcr;
1502 vcore->first_vcpuid = core * threads_per_subcore;
1503 vcore->kvm = kvm;
Paul Mackerrasec257162015-06-24 21:18:03 +10001504 INIT_LIST_HEAD(&vcore->preempt_list);
Stewart Smithde9bdd12014-07-18 14:18:42 +10001505
1506 return vcore;
1507}
1508
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001509#ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING
1510static struct debugfs_timings_element {
1511 const char *name;
1512 size_t offset;
1513} timings[] = {
1514 {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)},
1515 {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)},
1516 {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)},
1517 {"guest", offsetof(struct kvm_vcpu, arch.guest_time)},
1518 {"cede", offsetof(struct kvm_vcpu, arch.cede_time)},
1519};
1520
1521#define N_TIMINGS (sizeof(timings) / sizeof(timings[0]))
1522
1523struct debugfs_timings_state {
1524 struct kvm_vcpu *vcpu;
1525 unsigned int buflen;
1526 char buf[N_TIMINGS * 100];
1527};
1528
1529static int debugfs_timings_open(struct inode *inode, struct file *file)
1530{
1531 struct kvm_vcpu *vcpu = inode->i_private;
1532 struct debugfs_timings_state *p;
1533
1534 p = kzalloc(sizeof(*p), GFP_KERNEL);
1535 if (!p)
1536 return -ENOMEM;
1537
1538 kvm_get_kvm(vcpu->kvm);
1539 p->vcpu = vcpu;
1540 file->private_data = p;
1541
1542 return nonseekable_open(inode, file);
1543}
1544
1545static int debugfs_timings_release(struct inode *inode, struct file *file)
1546{
1547 struct debugfs_timings_state *p = file->private_data;
1548
1549 kvm_put_kvm(p->vcpu->kvm);
1550 kfree(p);
1551 return 0;
1552}
1553
1554static ssize_t debugfs_timings_read(struct file *file, char __user *buf,
1555 size_t len, loff_t *ppos)
1556{
1557 struct debugfs_timings_state *p = file->private_data;
1558 struct kvm_vcpu *vcpu = p->vcpu;
1559 char *s, *buf_end;
1560 struct kvmhv_tb_accumulator tb;
1561 u64 count;
1562 loff_t pos;
1563 ssize_t n;
1564 int i, loops;
1565 bool ok;
1566
1567 if (!p->buflen) {
1568 s = p->buf;
1569 buf_end = s + sizeof(p->buf);
1570 for (i = 0; i < N_TIMINGS; ++i) {
1571 struct kvmhv_tb_accumulator *acc;
1572
1573 acc = (struct kvmhv_tb_accumulator *)
1574 ((unsigned long)vcpu + timings[i].offset);
1575 ok = false;
1576 for (loops = 0; loops < 1000; ++loops) {
1577 count = acc->seqcount;
1578 if (!(count & 1)) {
1579 smp_rmb();
1580 tb = *acc;
1581 smp_rmb();
1582 if (count == acc->seqcount) {
1583 ok = true;
1584 break;
1585 }
1586 }
1587 udelay(1);
1588 }
1589 if (!ok)
1590 snprintf(s, buf_end - s, "%s: stuck\n",
1591 timings[i].name);
1592 else
1593 snprintf(s, buf_end - s,
1594 "%s: %llu %llu %llu %llu\n",
1595 timings[i].name, count / 2,
1596 tb_to_ns(tb.tb_total),
1597 tb_to_ns(tb.tb_min),
1598 tb_to_ns(tb.tb_max));
1599 s += strlen(s);
1600 }
1601 p->buflen = s - p->buf;
1602 }
1603
1604 pos = *ppos;
1605 if (pos >= p->buflen)
1606 return 0;
1607 if (len > p->buflen - pos)
1608 len = p->buflen - pos;
1609 n = copy_to_user(buf, p->buf + pos, len);
1610 if (n) {
1611 if (n == len)
1612 return -EFAULT;
1613 len -= n;
1614 }
1615 *ppos = pos + len;
1616 return len;
1617}
1618
1619static ssize_t debugfs_timings_write(struct file *file, const char __user *buf,
1620 size_t len, loff_t *ppos)
1621{
1622 return -EACCES;
1623}
1624
1625static const struct file_operations debugfs_timings_ops = {
1626 .owner = THIS_MODULE,
1627 .open = debugfs_timings_open,
1628 .release = debugfs_timings_release,
1629 .read = debugfs_timings_read,
1630 .write = debugfs_timings_write,
1631 .llseek = generic_file_llseek,
1632};
1633
1634/* Create a debugfs directory for the vcpu */
1635static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1636{
1637 char buf[16];
1638 struct kvm *kvm = vcpu->kvm;
1639
1640 snprintf(buf, sizeof(buf), "vcpu%u", id);
1641 if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
1642 return;
1643 vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir);
1644 if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir))
1645 return;
1646 vcpu->arch.debugfs_timings =
1647 debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir,
1648 vcpu, &debugfs_timings_ops);
1649}
1650
1651#else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1652static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id)
1653{
1654}
1655#endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */
1656
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301657static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm,
1658 unsigned int id)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001659{
1660 struct kvm_vcpu *vcpu;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001661 int err = -EINVAL;
1662 int core;
1663 struct kvmppc_vcore *vcore;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001664
Michael Ellerman3102f782014-05-23 18:15:29 +10001665 core = id / threads_per_subcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001666 if (core >= KVM_MAX_VCORES)
1667 goto out;
1668
1669 err = -ENOMEM;
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001670 vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001671 if (!vcpu)
1672 goto out;
1673
1674 err = kvm_vcpu_init(vcpu, kvm, id);
1675 if (err)
1676 goto free_vcpu;
1677
1678 vcpu->arch.shared = &vcpu->arch.shregs;
Alexander Graf5deb8e72014-04-24 13:46:24 +02001679#ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE
1680 /*
1681 * The shared struct is never shared on HV,
1682 * so we can always use host endianness
1683 */
1684#ifdef __BIG_ENDIAN__
1685 vcpu->arch.shared_big_endian = true;
1686#else
1687 vcpu->arch.shared_big_endian = false;
1688#endif
1689#endif
Paul Mackerrasde56a942011-06-29 00:21:34 +00001690 vcpu->arch.mmcr[0] = MMCR0_FC;
1691 vcpu->arch.ctrl = CTRL_RUNLATCH;
1692 /* default to host PVR, since we can't spoof it */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301693 kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR));
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001694 spin_lock_init(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001695 spin_lock_init(&vcpu->arch.tbacct_lock);
1696 vcpu->arch.busy_preempt = TB_NIL;
Anton Blanchardd6829162014-01-08 21:25:30 +11001697 vcpu->arch.intr_msr = MSR_SF | MSR_ME;
Paul Mackerrasde56a942011-06-29 00:21:34 +00001698
Paul Mackerrasde56a942011-06-29 00:21:34 +00001699 kvmppc_mmu_book3s_hv_init(vcpu);
1700
Paul Mackerras8455d792012-10-15 01:17:42 +00001701 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001702
1703 init_waitqueue_head(&vcpu->arch.cpu_run);
1704
1705 mutex_lock(&kvm->lock);
1706 vcore = kvm->arch.vcores[core];
1707 if (!vcore) {
Stewart Smithde9bdd12014-07-18 14:18:42 +10001708 vcore = kvmppc_vcore_create(kvm, core);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001709 kvm->arch.vcores[core] = vcore;
Paul Mackerras1b400ba2012-11-21 23:28:08 +00001710 kvm->arch.online_vcores++;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001711 }
1712 mutex_unlock(&kvm->lock);
1713
1714 if (!vcore)
1715 goto free_vcpu;
1716
1717 spin_lock(&vcore->lock);
1718 ++vcore->num_threads;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001719 spin_unlock(&vcore->lock);
1720 vcpu->arch.vcore = vcore;
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001721 vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid;
Paul Mackerrasec257162015-06-24 21:18:03 +10001722 vcpu->arch.thread_cpu = -1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001723
Alexander Grafaf8f38b2011-08-10 13:57:08 +02001724 vcpu->arch.cpu_type = KVM_CPU_3S_64;
1725 kvmppc_sanity_check(vcpu);
1726
Paul Mackerrasb6c295d2015-03-28 14:21:02 +11001727 debugfs_vcpu_init(vcpu, id);
1728
Paul Mackerrasde56a942011-06-29 00:21:34 +00001729 return vcpu;
1730
1731free_vcpu:
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001732 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001733out:
1734 return ERR_PTR(err);
1735}
1736
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001737static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa)
1738{
1739 if (vpa->pinned_addr)
1740 kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa,
1741 vpa->dirty);
1742}
1743
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301744static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001745{
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001746 spin_lock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasc35635e2013-04-18 19:51:04 +00001747 unpin_vpa(vcpu->kvm, &vcpu->arch.dtl);
1748 unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow);
1749 unpin_vpa(vcpu->kvm, &vcpu->arch.vpa);
Paul Mackerras2e25aa52012-02-19 17:46:32 +00001750 spin_unlock(&vcpu->arch.vpa_update_lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001751 kvm_vcpu_uninit(vcpu);
Sasha Levin6b75e6b2011-12-07 10:24:56 +02001752 kmem_cache_free(kvm_vcpu_cache, vcpu);
Paul Mackerrasde56a942011-06-29 00:21:34 +00001753}
1754
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05301755static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu)
1756{
1757 /* Indicate we want to get back into the guest */
1758 return 1;
1759}
1760
Paul Mackerras19ccb762011-07-23 17:42:46 +10001761static void kvmppc_set_timer(struct kvm_vcpu *vcpu)
Paul Mackerrasde56a942011-06-29 00:21:34 +00001762{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001763 unsigned long dec_nsec, now;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001764
Paul Mackerras19ccb762011-07-23 17:42:46 +10001765 now = get_tb();
1766 if (now > vcpu->arch.dec_expires) {
1767 /* decrementer has already gone negative */
1768 kvmppc_core_queue_dec(vcpu);
Scott Wood7e28e60e2011-11-08 18:23:20 -06001769 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras19ccb762011-07-23 17:42:46 +10001770 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001771 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10001772 dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC
1773 / tb_ticks_per_sec;
1774 hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec),
1775 HRTIMER_MODE_REL);
1776 vcpu->arch.timer_running = 1;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001777}
1778
Paul Mackerras19ccb762011-07-23 17:42:46 +10001779static void kvmppc_end_cede(struct kvm_vcpu *vcpu)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001780{
Paul Mackerras19ccb762011-07-23 17:42:46 +10001781 vcpu->arch.ceded = 0;
1782 if (vcpu->arch.timer_running) {
1783 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1784 vcpu->arch.timer_running = 0;
1785 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001786}
1787
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11001788extern void __kvmppc_vcore_entry(void);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001789
1790static void kvmppc_remove_runnable(struct kvmppc_vcore *vc,
1791 struct kvm_vcpu *vcpu)
1792{
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001793 u64 now;
1794
Paul Mackerras371fefd2011-06-29 00:23:08 +00001795 if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
1796 return;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001797 spin_lock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerrasc7b67672012-10-15 01:18:07 +00001798 now = mftb();
1799 vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) -
1800 vcpu->arch.stolen_logged;
1801 vcpu->arch.busy_preempt = now;
1802 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerrasbf3d32e2013-11-16 17:46:04 +11001803 spin_unlock_irq(&vcpu->arch.tbacct_lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001804 --vc->n_runnable;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001805 list_del(&vcpu->arch.run_list);
1806}
1807
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001808static int kvmppc_grab_hwthread(int cpu)
1809{
1810 struct paca_struct *tpaca;
Paul Mackerrasb754c732014-09-02 16:14:42 +10001811 long timeout = 10000;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001812
1813 tpaca = &paca[cpu];
1814
1815 /* Ensure the thread won't go into the kernel if it wakes */
Paul Mackerras7b444c62012-10-15 01:16:14 +00001816 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001817 tpaca->kvm_hstate.kvm_vcore = NULL;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001818 tpaca->kvm_hstate.napping = 0;
1819 smp_wmb();
1820 tpaca->kvm_hstate.hwthread_req = 1;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001821
1822 /*
1823 * If the thread is already executing in the kernel (e.g. handling
1824 * a stray interrupt), wait for it to get back to nap mode.
1825 * The smp_mb() is to ensure that our setting of hwthread_req
1826 * is visible before we look at hwthread_state, so if this
1827 * races with the code at system_reset_pSeries and the thread
1828 * misses our setting of hwthread_req, we are sure to see its
1829 * setting of hwthread_state, and vice versa.
1830 */
1831 smp_mb();
1832 while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) {
1833 if (--timeout <= 0) {
1834 pr_err("KVM: couldn't grab cpu %d\n", cpu);
1835 return -EBUSY;
1836 }
1837 udelay(1);
1838 }
1839 return 0;
1840}
1841
1842static void kvmppc_release_hwthread(int cpu)
1843{
1844 struct paca_struct *tpaca;
1845
1846 tpaca = &paca[cpu];
1847 tpaca->kvm_hstate.hwthread_req = 0;
1848 tpaca->kvm_hstate.kvm_vcpu = NULL;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001849 tpaca->kvm_hstate.kvm_vcore = NULL;
1850 tpaca->kvm_hstate.kvm_split_mode = NULL;
Paul Mackerrasf0888f72012-02-03 00:54:17 +00001851}
1852
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001853static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001854{
1855 int cpu;
1856 struct paca_struct *tpaca;
Paul Mackerrasec257162015-06-24 21:18:03 +10001857 struct kvmppc_vcore *mvc = vc->master_vcore;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001858
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001859 cpu = vc->pcpu;
1860 if (vcpu) {
1861 if (vcpu->arch.timer_running) {
1862 hrtimer_try_to_cancel(&vcpu->arch.dec_timer);
1863 vcpu->arch.timer_running = 0;
1864 }
1865 cpu += vcpu->arch.ptid;
1866 vcpu->cpu = mvc->pcpu;
1867 vcpu->arch.thread_cpu = cpu;
Paul Mackerras19ccb762011-07-23 17:42:46 +10001868 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001869 tpaca = &paca[cpu];
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001870 tpaca->kvm_hstate.kvm_vcpu = vcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001871 tpaca->kvm_hstate.ptid = cpu - mvc->pcpu;
Paul Mackerrasec257162015-06-24 21:18:03 +10001872 /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */
Paul Mackerras19ccb762011-07-23 17:42:46 +10001873 smp_wmb();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001874 tpaca->kvm_hstate.kvm_vcore = mvc;
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001875 if (cpu != smp_processor_id())
Paul Mackerras66feed62015-03-28 14:21:12 +11001876 kvmppc_ipi_thread(cpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001877}
1878
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001879static void kvmppc_wait_for_nap(void)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001880{
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001881 int cpu = smp_processor_id();
1882 int i, loops;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001883
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001884 for (loops = 0; loops < 1000000; ++loops) {
1885 /*
1886 * Check if all threads are finished.
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001887 * We set the vcore pointer when starting a thread
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001888 * and the thread clears it when finished, so we look
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001889 * for any threads that still have a non-NULL vcore ptr.
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001890 */
1891 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001892 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001893 break;
1894 if (i == threads_per_subcore) {
1895 HMT_medium();
1896 return;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001897 }
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001898 HMT_low();
Paul Mackerras371fefd2011-06-29 00:23:08 +00001899 }
1900 HMT_medium();
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001901 for (i = 1; i < threads_per_subcore; ++i)
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001902 if (paca[cpu + i].kvm_hstate.kvm_vcore)
Paul Mackerras5d5b99c2015-03-28 14:21:06 +11001903 pr_err("KVM: CPU %d seems to be stuck\n", cpu + i);
Paul Mackerras371fefd2011-06-29 00:23:08 +00001904}
1905
1906/*
1907 * Check that we are on thread 0 and that any other threads in
Paul Mackerras7b444c62012-10-15 01:16:14 +00001908 * this core are off-line. Then grab the threads so they can't
1909 * enter the kernel.
Paul Mackerras371fefd2011-06-29 00:23:08 +00001910 */
1911static int on_primary_thread(void)
1912{
1913 int cpu = smp_processor_id();
Michael Ellerman3102f782014-05-23 18:15:29 +10001914 int thr;
Paul Mackerras371fefd2011-06-29 00:23:08 +00001915
Michael Ellerman3102f782014-05-23 18:15:29 +10001916 /* Are we on a primary subcore? */
1917 if (cpu_thread_in_subcore(cpu))
Paul Mackerras371fefd2011-06-29 00:23:08 +00001918 return 0;
Michael Ellerman3102f782014-05-23 18:15:29 +10001919
1920 thr = 0;
1921 while (++thr < threads_per_subcore)
Paul Mackerras371fefd2011-06-29 00:23:08 +00001922 if (cpu_online(cpu + thr))
1923 return 0;
Paul Mackerras7b444c62012-10-15 01:16:14 +00001924
1925 /* Grab all hw threads so they can't go into the kernel */
Michael Ellerman3102f782014-05-23 18:15:29 +10001926 for (thr = 1; thr < threads_per_subcore; ++thr) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00001927 if (kvmppc_grab_hwthread(cpu + thr)) {
1928 /* Couldn't grab one; let the others go */
1929 do {
1930 kvmppc_release_hwthread(cpu + thr);
1931 } while (--thr > 0);
1932 return 0;
1933 }
1934 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00001935 return 1;
1936}
1937
Paul Mackerrasec257162015-06-24 21:18:03 +10001938/*
1939 * A list of virtual cores for each physical CPU.
1940 * These are vcores that could run but their runner VCPU tasks are
1941 * (or may be) preempted.
1942 */
1943struct preempted_vcore_list {
1944 struct list_head list;
1945 spinlock_t lock;
1946};
1947
1948static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores);
1949
1950static void init_vcore_lists(void)
1951{
1952 int cpu;
1953
1954 for_each_possible_cpu(cpu) {
1955 struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu);
1956 spin_lock_init(&lp->lock);
1957 INIT_LIST_HEAD(&lp->list);
1958 }
1959}
1960
1961static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc)
1962{
1963 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
1964
1965 vc->vcore_state = VCORE_PREEMPT;
1966 vc->pcpu = smp_processor_id();
1967 if (vc->num_threads < threads_per_subcore) {
1968 spin_lock(&lp->lock);
1969 list_add_tail(&vc->preempt_list, &lp->list);
1970 spin_unlock(&lp->lock);
1971 }
1972
1973 /* Start accumulating stolen time */
1974 kvmppc_core_start_stolen(vc);
1975}
1976
1977static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc)
1978{
Paul Mackerras402813f2015-07-16 17:11:13 +10001979 struct preempted_vcore_list *lp;
Paul Mackerrasec257162015-06-24 21:18:03 +10001980
1981 kvmppc_core_end_stolen(vc);
1982 if (!list_empty(&vc->preempt_list)) {
Paul Mackerras402813f2015-07-16 17:11:13 +10001983 lp = &per_cpu(preempted_vcores, vc->pcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10001984 spin_lock(&lp->lock);
1985 list_del_init(&vc->preempt_list);
1986 spin_unlock(&lp->lock);
1987 }
1988 vc->vcore_state = VCORE_INACTIVE;
1989}
1990
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001991/*
1992 * This stores information about the virtual cores currently
1993 * assigned to a physical core.
1994 */
Paul Mackerrasec257162015-06-24 21:18:03 +10001995struct core_info {
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001996 int n_subcores;
1997 int max_subcore_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10001998 int total_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10001999 int subcore_threads[MAX_SUBCORES];
2000 struct kvm *subcore_vm[MAX_SUBCORES];
2001 struct list_head vcs[MAX_SUBCORES];
Paul Mackerrasec257162015-06-24 21:18:03 +10002002};
2003
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002004/*
2005 * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7
2006 * respectively in 2-way micro-threading (split-core) mode.
2007 */
2008static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 };
2009
Paul Mackerrasec257162015-06-24 21:18:03 +10002010static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc)
2011{
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002012 int sub;
2013
Paul Mackerrasec257162015-06-24 21:18:03 +10002014 memset(cip, 0, sizeof(*cip));
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002015 cip->n_subcores = 1;
2016 cip->max_subcore_threads = vc->num_threads;
Paul Mackerrasec257162015-06-24 21:18:03 +10002017 cip->total_threads = vc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002018 cip->subcore_threads[0] = vc->num_threads;
2019 cip->subcore_vm[0] = vc->kvm;
2020 for (sub = 0; sub < MAX_SUBCORES; ++sub)
2021 INIT_LIST_HEAD(&cip->vcs[sub]);
2022 list_add_tail(&vc->preempt_list, &cip->vcs[0]);
2023}
2024
2025static bool subcore_config_ok(int n_subcores, int n_threads)
2026{
2027 /* Can only dynamically split if unsplit to begin with */
2028 if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS)
2029 return false;
2030 if (n_subcores > MAX_SUBCORES)
2031 return false;
2032 if (n_subcores > 1) {
2033 if (!(dynamic_mt_modes & 2))
2034 n_subcores = 4;
2035 if (n_subcores > 2 && !(dynamic_mt_modes & 4))
2036 return false;
2037 }
2038
2039 return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS;
Paul Mackerrasec257162015-06-24 21:18:03 +10002040}
2041
2042static void init_master_vcore(struct kvmppc_vcore *vc)
2043{
2044 vc->master_vcore = vc;
2045 vc->entry_exit_map = 0;
2046 vc->in_guest = 0;
2047 vc->napping_threads = 0;
2048 vc->conferring_threads = 0;
2049}
2050
2051/*
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002052 * See if the existing subcores can be split into 3 (or fewer) subcores
2053 * of at most two threads each, so we can fit in another vcore. This
2054 * assumes there are at most two subcores and at most 6 threads in total.
Paul Mackerrasec257162015-06-24 21:18:03 +10002055 */
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002056static bool can_split_piggybacked_subcores(struct core_info *cip)
2057{
2058 int sub, new_sub;
2059 int large_sub = -1;
2060 int thr;
2061 int n_subcores = cip->n_subcores;
2062 struct kvmppc_vcore *vc, *vcnext;
2063 struct kvmppc_vcore *master_vc = NULL;
2064
2065 for (sub = 0; sub < cip->n_subcores; ++sub) {
2066 if (cip->subcore_threads[sub] <= 2)
2067 continue;
2068 if (large_sub >= 0)
2069 return false;
2070 large_sub = sub;
2071 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2072 preempt_list);
2073 if (vc->num_threads > 2)
2074 return false;
2075 n_subcores += (cip->subcore_threads[sub] - 1) >> 1;
2076 }
Paul Mackerrasf74f2e22015-11-03 16:03:30 +11002077 if (large_sub < 0 || !subcore_config_ok(n_subcores + 1, 2))
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002078 return false;
2079
2080 /*
2081 * Seems feasible, so go through and move vcores to new subcores.
2082 * Note that when we have two or more vcores in one subcore,
2083 * all those vcores must have only one thread each.
2084 */
2085 new_sub = cip->n_subcores;
2086 thr = 0;
2087 sub = large_sub;
2088 list_for_each_entry_safe(vc, vcnext, &cip->vcs[sub], preempt_list) {
2089 if (thr >= 2) {
2090 list_del(&vc->preempt_list);
2091 list_add_tail(&vc->preempt_list, &cip->vcs[new_sub]);
2092 /* vc->num_threads must be 1 */
2093 if (++cip->subcore_threads[new_sub] == 1) {
2094 cip->subcore_vm[new_sub] = vc->kvm;
2095 init_master_vcore(vc);
2096 master_vc = vc;
2097 ++cip->n_subcores;
2098 } else {
2099 vc->master_vcore = master_vc;
2100 ++new_sub;
2101 }
2102 }
2103 thr += vc->num_threads;
2104 }
2105 cip->subcore_threads[large_sub] = 2;
2106 cip->max_subcore_threads = 2;
2107
2108 return true;
2109}
2110
2111static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip)
2112{
2113 int n_threads = vc->num_threads;
2114 int sub;
2115
2116 if (!cpu_has_feature(CPU_FTR_ARCH_207S))
2117 return false;
2118
2119 if (n_threads < cip->max_subcore_threads)
2120 n_threads = cip->max_subcore_threads;
2121 if (subcore_config_ok(cip->n_subcores + 1, n_threads)) {
2122 cip->max_subcore_threads = n_threads;
2123 } else if (cip->n_subcores <= 2 && cip->total_threads <= 6 &&
2124 vc->num_threads <= 2) {
2125 /*
2126 * We may be able to fit another subcore in by
2127 * splitting an existing subcore with 3 or 4
2128 * threads into two 2-thread subcores, or one
2129 * with 5 or 6 threads into three subcores.
2130 * We can only do this if those subcores have
2131 * piggybacked virtual cores.
2132 */
2133 if (!can_split_piggybacked_subcores(cip))
2134 return false;
2135 } else {
2136 return false;
2137 }
2138
2139 sub = cip->n_subcores;
2140 ++cip->n_subcores;
2141 cip->total_threads += vc->num_threads;
2142 cip->subcore_threads[sub] = vc->num_threads;
2143 cip->subcore_vm[sub] = vc->kvm;
2144 init_master_vcore(vc);
2145 list_del(&vc->preempt_list);
2146 list_add_tail(&vc->preempt_list, &cip->vcs[sub]);
2147
2148 return true;
2149}
2150
2151static bool can_piggyback_subcore(struct kvmppc_vcore *pvc,
2152 struct core_info *cip, int sub)
Paul Mackerrasec257162015-06-24 21:18:03 +10002153{
2154 struct kvmppc_vcore *vc;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002155 int n_thr;
Paul Mackerrasec257162015-06-24 21:18:03 +10002156
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002157 vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore,
2158 preempt_list);
Paul Mackerrasec257162015-06-24 21:18:03 +10002159
2160 /* require same VM and same per-core reg values */
2161 if (pvc->kvm != vc->kvm ||
2162 pvc->tb_offset != vc->tb_offset ||
2163 pvc->pcr != vc->pcr ||
2164 pvc->lpcr != vc->lpcr)
2165 return false;
2166
2167 /* P8 guest with > 1 thread per core would see wrong TIR value */
2168 if (cpu_has_feature(CPU_FTR_ARCH_207S) &&
2169 (vc->num_threads > 1 || pvc->num_threads > 1))
2170 return false;
2171
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002172 n_thr = cip->subcore_threads[sub] + pvc->num_threads;
2173 if (n_thr > cip->max_subcore_threads) {
2174 if (!subcore_config_ok(cip->n_subcores, n_thr))
2175 return false;
2176 cip->max_subcore_threads = n_thr;
2177 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002178
2179 cip->total_threads += pvc->num_threads;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002180 cip->subcore_threads[sub] = n_thr;
Paul Mackerrasec257162015-06-24 21:18:03 +10002181 pvc->master_vcore = vc;
2182 list_del(&pvc->preempt_list);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002183 list_add_tail(&pvc->preempt_list, &cip->vcs[sub]);
Paul Mackerrasec257162015-06-24 21:18:03 +10002184
2185 return true;
2186}
2187
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002188/*
2189 * Work out whether it is possible to piggyback the execution of
2190 * vcore *pvc onto the execution of the other vcores described in *cip.
2191 */
2192static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip,
2193 int target_threads)
2194{
2195 int sub;
2196
2197 if (cip->total_threads + pvc->num_threads > target_threads)
2198 return false;
2199 for (sub = 0; sub < cip->n_subcores; ++sub)
2200 if (cip->subcore_threads[sub] &&
2201 can_piggyback_subcore(pvc, cip, sub))
2202 return true;
2203
2204 if (can_dynamic_split(pvc, cip))
2205 return true;
2206
2207 return false;
2208}
2209
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002210static void prepare_threads(struct kvmppc_vcore *vc)
2211{
2212 struct kvm_vcpu *vcpu, *vnext;
2213
2214 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2215 arch.run_list) {
2216 if (signal_pending(vcpu->arch.run_task))
2217 vcpu->arch.ret = -EINTR;
2218 else if (vcpu->arch.vpa.update_pending ||
2219 vcpu->arch.slb_shadow.update_pending ||
2220 vcpu->arch.dtl.update_pending)
2221 vcpu->arch.ret = RESUME_GUEST;
2222 else
2223 continue;
2224 kvmppc_remove_runnable(vc, vcpu);
2225 wake_up(&vcpu->arch.cpu_run);
2226 }
2227}
2228
Paul Mackerrasec257162015-06-24 21:18:03 +10002229static void collect_piggybacks(struct core_info *cip, int target_threads)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002230{
Paul Mackerrasec257162015-06-24 21:18:03 +10002231 struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores);
2232 struct kvmppc_vcore *pvc, *vcnext;
2233
2234 spin_lock(&lp->lock);
2235 list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) {
2236 if (!spin_trylock(&pvc->lock))
2237 continue;
2238 prepare_threads(pvc);
2239 if (!pvc->n_runnable) {
2240 list_del_init(&pvc->preempt_list);
2241 if (pvc->runner == NULL) {
2242 pvc->vcore_state = VCORE_INACTIVE;
2243 kvmppc_core_end_stolen(pvc);
2244 }
2245 spin_unlock(&pvc->lock);
2246 continue;
2247 }
2248 if (!can_piggyback(pvc, cip, target_threads)) {
2249 spin_unlock(&pvc->lock);
2250 continue;
2251 }
2252 kvmppc_core_end_stolen(pvc);
2253 pvc->vcore_state = VCORE_PIGGYBACK;
2254 if (cip->total_threads >= target_threads)
2255 break;
2256 }
2257 spin_unlock(&lp->lock);
2258}
2259
2260static void post_guest_process(struct kvmppc_vcore *vc, bool is_master)
2261{
2262 int still_running = 0;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002263 u64 now;
2264 long ret;
2265 struct kvm_vcpu *vcpu, *vnext;
2266
Paul Mackerrasec257162015-06-24 21:18:03 +10002267 spin_lock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002268 now = get_tb();
2269 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2270 arch.run_list) {
2271 /* cancel pending dec exception if dec is positive */
2272 if (now < vcpu->arch.dec_expires &&
2273 kvmppc_core_pending_dec(vcpu))
2274 kvmppc_core_dequeue_dec(vcpu);
2275
2276 trace_kvm_guest_exit(vcpu);
2277
2278 ret = RESUME_GUEST;
2279 if (vcpu->arch.trap)
2280 ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu,
2281 vcpu->arch.run_task);
2282
2283 vcpu->arch.ret = ret;
2284 vcpu->arch.trap = 0;
2285
Paul Mackerrasec257162015-06-24 21:18:03 +10002286 if (is_kvmppc_resume_guest(vcpu->arch.ret)) {
2287 if (vcpu->arch.pending_exceptions)
2288 kvmppc_core_prepare_to_enter(vcpu);
2289 if (vcpu->arch.ceded)
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002290 kvmppc_set_timer(vcpu);
Paul Mackerrasec257162015-06-24 21:18:03 +10002291 else
2292 ++still_running;
2293 } else {
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002294 kvmppc_remove_runnable(vc, vcpu);
2295 wake_up(&vcpu->arch.cpu_run);
2296 }
2297 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002298 list_del_init(&vc->preempt_list);
2299 if (!is_master) {
Paul Mackerras563a1e92015-07-16 17:11:14 +10002300 if (still_running > 0) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002301 kvmppc_vcore_preempt(vc);
Paul Mackerras563a1e92015-07-16 17:11:14 +10002302 } else if (vc->runner) {
2303 vc->vcore_state = VCORE_PREEMPT;
2304 kvmppc_core_start_stolen(vc);
2305 } else {
2306 vc->vcore_state = VCORE_INACTIVE;
2307 }
Paul Mackerrasec257162015-06-24 21:18:03 +10002308 if (vc->n_runnable > 0 && vc->runner == NULL) {
2309 /* make sure there's a candidate runner awake */
2310 vcpu = list_first_entry(&vc->runnable_threads,
2311 struct kvm_vcpu, arch.run_list);
2312 wake_up(&vcpu->arch.cpu_run);
2313 }
2314 }
2315 spin_unlock(&vc->lock);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002316}
2317
Paul Mackerras371fefd2011-06-29 00:23:08 +00002318/*
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002319 * Clear core from the list of active host cores as we are about to
2320 * enter the guest. Only do this if it is the primary thread of the
2321 * core (not if a subcore) that is entering the guest.
2322 */
2323static inline void kvmppc_clear_host_core(int cpu)
2324{
2325 int core;
2326
2327 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2328 return;
2329 /*
2330 * Memory barrier can be omitted here as we will do a smp_wmb()
2331 * later in kvmppc_start_thread and we need ensure that state is
2332 * visible to other CPUs only after we enter guest.
2333 */
2334 core = cpu >> threads_shift;
2335 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 0;
2336}
2337
2338/*
2339 * Advertise this core as an active host core since we exited the guest
2340 * Only need to do this if it is the primary thread of the core that is
2341 * exiting.
2342 */
2343static inline void kvmppc_set_host_core(int cpu)
2344{
2345 int core;
2346
2347 if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu))
2348 return;
2349
2350 /*
2351 * Memory barrier can be omitted here because we do a spin_unlock
2352 * immediately after this which provides the memory barrier.
2353 */
2354 core = cpu >> threads_shift;
2355 kvmppc_host_rm_ops_hv->rm_core[core].rm_state.in_host = 1;
2356}
2357
2358/*
Paul Mackerras371fefd2011-06-29 00:23:08 +00002359 * Run a set of guest threads on a physical core.
2360 * Called with vc->lock held.
2361 */
Paul Mackerras66feed62015-03-28 14:21:12 +11002362static noinline void kvmppc_run_core(struct kvmppc_vcore *vc)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002363{
Paul Mackerras17d48902015-04-29 14:49:07 +10002364 struct kvm_vcpu *vcpu, *vnext;
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002365 int i;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002366 int srcu_idx;
Paul Mackerrasec257162015-06-24 21:18:03 +10002367 struct core_info core_info;
2368 struct kvmppc_vcore *pvc, *vcnext;
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002369 struct kvm_split_mode split_info, *sip;
2370 int split, subcore_size, active;
2371 int sub;
2372 bool thr0_done;
2373 unsigned long cmd_bit, stat_bit;
Paul Mackerrasec257162015-06-24 21:18:03 +10002374 int pcpu, thr;
2375 int target_threads;
Paul Mackerras081f3232012-06-01 20:20:24 +10002376
2377 /*
Paul Mackerrasd911f0b2015-03-28 14:21:03 +11002378 * Remove from the list any threads that have a signal pending
2379 * or need a VPA update done
2380 */
2381 prepare_threads(vc);
2382
2383 /* if the runner is no longer runnable, let the caller pick a new one */
2384 if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE)
2385 return;
2386
2387 /*
2388 * Initialize *vc.
Paul Mackerras081f3232012-06-01 20:20:24 +10002389 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002390 init_master_vcore(vc);
Paul Mackerras2711e242014-12-04 16:43:28 +11002391 vc->preempt_tb = TB_NIL;
Paul Mackerras081f3232012-06-01 20:20:24 +10002392
2393 /*
Michael Ellerman3102f782014-05-23 18:15:29 +10002394 * Make sure we are running on primary threads, and that secondary
2395 * threads are offline. Also check if the number of threads in this
2396 * guest are greater than the current system threads per guest.
Paul Mackerras7b444c62012-10-15 01:16:14 +00002397 */
Michael Ellerman3102f782014-05-23 18:15:29 +10002398 if ((threads_per_core > 1) &&
2399 ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) {
Paul Mackerras17d48902015-04-29 14:49:07 +10002400 list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads,
2401 arch.run_list) {
Paul Mackerras7b444c62012-10-15 01:16:14 +00002402 vcpu->arch.ret = -EBUSY;
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002403 kvmppc_remove_runnable(vc, vcpu);
2404 wake_up(&vcpu->arch.cpu_run);
2405 }
Paul Mackerras7b444c62012-10-15 01:16:14 +00002406 goto out;
2407 }
2408
Paul Mackerrasec257162015-06-24 21:18:03 +10002409 /*
2410 * See if we could run any other vcores on the physical core
2411 * along with this one.
2412 */
2413 init_core_info(&core_info, vc);
2414 pcpu = smp_processor_id();
2415 target_threads = threads_per_subcore;
2416 if (target_smt_mode && target_smt_mode < target_threads)
2417 target_threads = target_smt_mode;
2418 if (vc->num_threads < target_threads)
2419 collect_piggybacks(&core_info, target_threads);
Michael Ellerman3102f782014-05-23 18:15:29 +10002420
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002421 /* Decide on micro-threading (split-core) mode */
2422 subcore_size = threads_per_subcore;
2423 cmd_bit = stat_bit = 0;
2424 split = core_info.n_subcores;
2425 sip = NULL;
2426 if (split > 1) {
2427 /* threads_per_subcore must be MAX_SMT_THREADS (8) here */
2428 if (split == 2 && (dynamic_mt_modes & 2)) {
2429 cmd_bit = HID0_POWER8_1TO2LPAR;
2430 stat_bit = HID0_POWER8_2LPARMODE;
2431 } else {
2432 split = 4;
2433 cmd_bit = HID0_POWER8_1TO4LPAR;
2434 stat_bit = HID0_POWER8_4LPARMODE;
Paul Mackerrasec257162015-06-24 21:18:03 +10002435 }
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002436 subcore_size = MAX_SMT_THREADS / split;
2437 sip = &split_info;
2438 memset(&split_info, 0, sizeof(split_info));
2439 split_info.rpr = mfspr(SPRN_RPR);
2440 split_info.pmmar = mfspr(SPRN_PMMAR);
2441 split_info.ldbar = mfspr(SPRN_LDBAR);
2442 split_info.subcore_size = subcore_size;
2443 for (sub = 0; sub < core_info.n_subcores; ++sub)
2444 split_info.master_vcs[sub] =
2445 list_first_entry(&core_info.vcs[sub],
2446 struct kvmppc_vcore, preempt_list);
2447 /* order writes to split_info before kvm_split_mode pointer */
2448 smp_wmb();
2449 }
2450 pcpu = smp_processor_id();
2451 for (thr = 0; thr < threads_per_subcore; ++thr)
2452 paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip;
2453
2454 /* Initiate micro-threading (split-core) if required */
2455 if (cmd_bit) {
2456 unsigned long hid0 = mfspr(SPRN_HID0);
2457
2458 hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS;
2459 mb();
2460 mtspr(SPRN_HID0, hid0);
2461 isync();
2462 for (;;) {
2463 hid0 = mfspr(SPRN_HID0);
2464 if (hid0 & stat_bit)
2465 break;
2466 cpu_relax();
2467 }
Paul Mackerras2e25aa52012-02-19 17:46:32 +00002468 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002469
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002470 kvmppc_clear_host_core(pcpu);
2471
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002472 /* Start all the threads */
2473 active = 0;
2474 for (sub = 0; sub < core_info.n_subcores; ++sub) {
2475 thr = subcore_thread_map[sub];
2476 thr0_done = false;
2477 active |= 1 << thr;
2478 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) {
2479 pvc->pcpu = pcpu + thr;
2480 list_for_each_entry(vcpu, &pvc->runnable_threads,
2481 arch.run_list) {
2482 kvmppc_start_thread(vcpu, pvc);
2483 kvmppc_create_dtl_entry(vcpu, pvc);
2484 trace_kvm_guest_enter(vcpu);
2485 if (!vcpu->arch.ptid)
2486 thr0_done = true;
2487 active |= 1 << (thr + vcpu->arch.ptid);
2488 }
2489 /*
2490 * We need to start the first thread of each subcore
2491 * even if it doesn't have a vcpu.
2492 */
2493 if (pvc->master_vcore == pvc && !thr0_done)
2494 kvmppc_start_thread(NULL, pvc);
2495 thr += pvc->num_threads;
2496 }
2497 }
Gautham R. Shenoy7f235322015-09-02 21:48:58 +05302498
2499 /*
2500 * Ensure that split_info.do_nap is set after setting
2501 * the vcore pointer in the PACA of the secondaries.
2502 */
2503 smp_mb();
2504 if (cmd_bit)
2505 split_info.do_nap = 1; /* ask secondaries to nap when done */
2506
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002507 /*
2508 * When doing micro-threading, poke the inactive threads as well.
2509 * This gets them to the nap instruction after kvm_do_nap,
2510 * which reduces the time taken to unsplit later.
2511 */
2512 if (split > 1)
2513 for (thr = 1; thr < threads_per_subcore; ++thr)
2514 if (!(active & (1 << thr)))
2515 kvmppc_ipi_thread(pcpu + thr);
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002516
Paul Mackerras2f12f032012-10-15 01:17:17 +00002517 vc->vcore_state = VCORE_RUNNING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002518 preempt_disable();
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002519
2520 trace_kvmppc_run_core(vc, 0);
2521
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002522 for (sub = 0; sub < core_info.n_subcores; ++sub)
2523 list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list)
2524 spin_unlock(&pvc->lock);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002525
Paul Mackerras19ccb762011-07-23 17:42:46 +10002526 kvm_guest_enter();
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002527
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002528 srcu_idx = srcu_read_lock(&vc->kvm->srcu);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002529
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002530 __kvmppc_vcore_entry();
Paul Mackerras19ccb762011-07-23 17:42:46 +10002531
Paul Mackerrasec257162015-06-24 21:18:03 +10002532 srcu_read_unlock(&vc->kvm->srcu, srcu_idx);
2533
2534 spin_lock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002535 /* prevent other vcpu threads from doing kvmppc_start_thread() now */
Paul Mackerras19ccb762011-07-23 17:42:46 +10002536 vc->vcore_state = VCORE_EXITING;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002537
Paul Mackerras371fefd2011-06-29 00:23:08 +00002538 /* wait for secondary threads to finish writing their state to memory */
Paul Mackerrase0b7ec02014-01-08 21:25:20 +11002539 kvmppc_wait_for_nap();
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002540
2541 /* Return to whole-core mode if we split the core earlier */
2542 if (split > 1) {
2543 unsigned long hid0 = mfspr(SPRN_HID0);
2544 unsigned long loops = 0;
2545
2546 hid0 &= ~HID0_POWER8_DYNLPARDIS;
2547 stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE;
2548 mb();
2549 mtspr(SPRN_HID0, hid0);
2550 isync();
2551 for (;;) {
2552 hid0 = mfspr(SPRN_HID0);
2553 if (!(hid0 & stat_bit))
2554 break;
2555 cpu_relax();
2556 ++loops;
2557 }
2558 split_info.do_nap = 0;
2559 }
2560
2561 /* Let secondaries go back to the offline loop */
2562 for (i = 0; i < threads_per_subcore; ++i) {
2563 kvmppc_release_hwthread(pcpu + i);
2564 if (sip && sip->napped[i])
2565 kvmppc_ipi_thread(pcpu + i);
2566 }
2567
Suresh Warrierb8e6a872015-12-17 14:59:07 -06002568 kvmppc_set_host_core(pcpu);
2569
Paul Mackerras371fefd2011-06-29 00:23:08 +00002570 spin_unlock(&vc->lock);
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002571
Paul Mackerras371fefd2011-06-29 00:23:08 +00002572 /* make sure updates to secondary vcpu structs are visible now */
2573 smp_mb();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002574 kvm_guest_exit();
2575
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002576 for (sub = 0; sub < core_info.n_subcores; ++sub)
2577 list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub],
2578 preempt_list)
2579 post_guest_process(pvc, pvc == vc);
Paul Mackerrasde56a942011-06-29 00:21:34 +00002580
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002581 spin_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002582 preempt_enable();
Paul Mackerrasde56a942011-06-29 00:21:34 +00002583
Paul Mackerrasde56a942011-06-29 00:21:34 +00002584 out:
Paul Mackerras19ccb762011-07-23 17:42:46 +10002585 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002586 trace_kvmppc_run_core(vc, 1);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002587}
2588
Paul Mackerras19ccb762011-07-23 17:42:46 +10002589/*
2590 * Wait for some other vcpu thread to execute us, and
2591 * wake us up when we need to handle something in the host.
2592 */
Paul Mackerrasec257162015-06-24 21:18:03 +10002593static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc,
2594 struct kvm_vcpu *vcpu, int wait_state)
Paul Mackerras371fefd2011-06-29 00:23:08 +00002595{
Paul Mackerras371fefd2011-06-29 00:23:08 +00002596 DEFINE_WAIT(wait);
2597
Paul Mackerras19ccb762011-07-23 17:42:46 +10002598 prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state);
Paul Mackerrasec257162015-06-24 21:18:03 +10002599 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2600 spin_unlock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002601 schedule();
Paul Mackerrasec257162015-06-24 21:18:03 +10002602 spin_lock(&vc->lock);
2603 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002604 finish_wait(&vcpu->arch.cpu_run, &wait);
2605}
Paul Mackerras371fefd2011-06-29 00:23:08 +00002606
Paul Mackerras19ccb762011-07-23 17:42:46 +10002607/*
2608 * All the vcpus in this vcore are idle, so wait for a decrementer
2609 * or external interrupt to one of the vcpus. vc->lock is held.
2610 */
2611static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc)
2612{
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002613 struct kvm_vcpu *vcpu;
2614 int do_sleep = 1;
Marcelo Tosatti85773702016-02-19 09:46:39 +01002615 DECLARE_SWAITQUEUE(wait);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002616
Marcelo Tosatti85773702016-02-19 09:46:39 +01002617 prepare_to_swait(&vc->wq, &wait, TASK_INTERRUPTIBLE);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002618
2619 /*
2620 * Check one last time for pending exceptions and ceded state after
2621 * we put ourselves on the wait queue
2622 */
2623 list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) {
2624 if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) {
2625 do_sleep = 0;
2626 break;
2627 }
2628 }
2629
2630 if (!do_sleep) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002631 finish_swait(&vc->wq, &wait);
Suresh E. Warrier1bc5d592014-11-03 15:52:00 +11002632 return;
2633 }
2634
Paul Mackerras19ccb762011-07-23 17:42:46 +10002635 vc->vcore_state = VCORE_SLEEPING;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002636 trace_kvmppc_vcore_blocked(vc, 0);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002637 spin_unlock(&vc->lock);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002638 schedule();
Marcelo Tosatti85773702016-02-19 09:46:39 +01002639 finish_swait(&vc->wq, &wait);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002640 spin_lock(&vc->lock);
2641 vc->vcore_state = VCORE_INACTIVE;
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002642 trace_kvmppc_vcore_blocked(vc, 1);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002643}
2644
2645static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu)
2646{
2647 int n_ceded;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002648 struct kvmppc_vcore *vc;
2649 struct kvm_vcpu *v, *vn;
Paul Mackerras9e368f22011-06-29 00:40:08 +00002650
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002651 trace_kvmppc_run_vcpu_enter(vcpu);
2652
Paul Mackerras371fefd2011-06-29 00:23:08 +00002653 kvm_run->exit_reason = 0;
2654 vcpu->arch.ret = RESUME_GUEST;
2655 vcpu->arch.trap = 0;
Paul Mackerras2f12f032012-10-15 01:17:17 +00002656 kvmppc_update_vpas(vcpu);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002657
Paul Mackerras371fefd2011-06-29 00:23:08 +00002658 /*
2659 * Synchronize with other threads in this virtual core
2660 */
2661 vc = vcpu->arch.vcore;
2662 spin_lock(&vc->lock);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002663 vcpu->arch.ceded = 0;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002664 vcpu->arch.run_task = current;
2665 vcpu->arch.kvm_run = kvm_run;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002666 vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb());
Paul Mackerras19ccb762011-07-23 17:42:46 +10002667 vcpu->arch.state = KVMPPC_VCPU_RUNNABLE;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002668 vcpu->arch.busy_preempt = TB_NIL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002669 list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads);
2670 ++vc->n_runnable;
2671
Paul Mackerras19ccb762011-07-23 17:42:46 +10002672 /*
2673 * This happens the first time this is called for a vcpu.
2674 * If the vcore is already running, we may be able to start
2675 * this thread straight away and have it join in.
2676 */
Paul Mackerras8455d792012-10-15 01:17:42 +00002677 if (!signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002678 if (vc->vcore_state == VCORE_PIGGYBACK) {
2679 struct kvmppc_vcore *mvc = vc->master_vcore;
2680 if (spin_trylock(&mvc->lock)) {
2681 if (mvc->vcore_state == VCORE_RUNNING &&
2682 !VCORE_IS_EXITING(mvc)) {
2683 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002684 kvmppc_start_thread(vcpu, vc);
Paul Mackerrasec257162015-06-24 21:18:03 +10002685 trace_kvm_guest_enter(vcpu);
2686 }
2687 spin_unlock(&mvc->lock);
2688 }
2689 } else if (vc->vcore_state == VCORE_RUNNING &&
2690 !VCORE_IS_EXITING(vc)) {
Paul Mackerras2f12f032012-10-15 01:17:17 +00002691 kvmppc_create_dtl_entry(vcpu, vc);
Paul Mackerrasb4deba52015-07-02 20:38:16 +10002692 kvmppc_start_thread(vcpu, vc);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002693 trace_kvm_guest_enter(vcpu);
Paul Mackerras8455d792012-10-15 01:17:42 +00002694 } else if (vc->vcore_state == VCORE_SLEEPING) {
Marcelo Tosatti85773702016-02-19 09:46:39 +01002695 swake_up(&vc->wq);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002696 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002697
Paul Mackerras8455d792012-10-15 01:17:42 +00002698 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002699
2700 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2701 !signal_pending(current)) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002702 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2703 kvmppc_vcore_end_preempt(vc);
2704
Paul Mackerras8455d792012-10-15 01:17:42 +00002705 if (vc->vcore_state != VCORE_INACTIVE) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002706 kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002707 continue;
2708 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002709 list_for_each_entry_safe(v, vn, &vc->runnable_threads,
2710 arch.run_list) {
Scott Wood7e28e60e2011-11-08 18:23:20 -06002711 kvmppc_core_prepare_to_enter(v);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002712 if (signal_pending(v->arch.run_task)) {
2713 kvmppc_remove_runnable(vc, v);
2714 v->stat.signal_exits++;
2715 v->arch.kvm_run->exit_reason = KVM_EXIT_INTR;
2716 v->arch.ret = -EINTR;
2717 wake_up(&v->arch.cpu_run);
2718 }
2719 }
Paul Mackerras8455d792012-10-15 01:17:42 +00002720 if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE)
2721 break;
Paul Mackerras8455d792012-10-15 01:17:42 +00002722 n_ceded = 0;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002723 list_for_each_entry(v, &vc->runnable_threads, arch.run_list) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002724 if (!v->arch.pending_exceptions)
2725 n_ceded += v->arch.ceded;
Paul Mackerras4619ac82013-04-17 20:31:41 +00002726 else
2727 v->arch.ceded = 0;
2728 }
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002729 vc->runner = vcpu;
2730 if (n_ceded == vc->n_runnable) {
Paul Mackerras8455d792012-10-15 01:17:42 +00002731 kvmppc_vcore_blocked(vc);
Konstantin Khlebnikovc56dadf2015-07-15 12:52:03 +03002732 } else if (need_resched()) {
Paul Mackerrasec257162015-06-24 21:18:03 +10002733 kvmppc_vcore_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002734 /* Let something else run */
2735 cond_resched_lock(&vc->lock);
Paul Mackerrasec257162015-06-24 21:18:03 +10002736 if (vc->vcore_state == VCORE_PREEMPT)
2737 kvmppc_vcore_end_preempt(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002738 } else {
Paul Mackerras8455d792012-10-15 01:17:42 +00002739 kvmppc_run_core(vc);
Paul Mackerras25fedfc2015-03-28 14:21:05 +11002740 }
Paul Mackerras0456ec42012-02-03 00:56:21 +00002741 vc->runner = NULL;
Paul Mackerras371fefd2011-06-29 00:23:08 +00002742 }
2743
Paul Mackerras8455d792012-10-15 01:17:42 +00002744 while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE &&
2745 (vc->vcore_state == VCORE_RUNNING ||
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002746 vc->vcore_state == VCORE_EXITING ||
2747 vc->vcore_state == VCORE_PIGGYBACK))
Paul Mackerrasec257162015-06-24 21:18:03 +10002748 kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE);
Paul Mackerras8455d792012-10-15 01:17:42 +00002749
Paul Mackerras5fc3e642015-09-18 13:13:44 +10002750 if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL)
2751 kvmppc_vcore_end_preempt(vc);
2752
Paul Mackerras8455d792012-10-15 01:17:42 +00002753 if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) {
2754 kvmppc_remove_runnable(vc, vcpu);
2755 vcpu->stat.signal_exits++;
2756 kvm_run->exit_reason = KVM_EXIT_INTR;
2757 vcpu->arch.ret = -EINTR;
2758 }
2759
2760 if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) {
2761 /* Wake up some vcpu to run the core */
2762 v = list_first_entry(&vc->runnable_threads,
2763 struct kvm_vcpu, arch.run_list);
2764 wake_up(&v->arch.cpu_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002765 }
Paul Mackerras371fefd2011-06-29 00:23:08 +00002766
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002767 trace_kvmppc_run_vcpu_exit(vcpu, kvm_run);
Paul Mackerras19ccb762011-07-23 17:42:46 +10002768 spin_unlock(&vc->lock);
Paul Mackerras371fefd2011-06-29 00:23:08 +00002769 return vcpu->arch.ret;
Paul Mackerrasde56a942011-06-29 00:21:34 +00002770}
2771
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302772static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu)
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002773{
2774 int r;
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002775 int srcu_idx;
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002776
Alexander Grafaf8f38b2011-08-10 13:57:08 +02002777 if (!vcpu->arch.sane) {
2778 run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
2779 return -EINVAL;
2780 }
2781
Scott Wood25051b5a2011-11-08 18:23:23 -06002782 kvmppc_core_prepare_to_enter(vcpu);
2783
Paul Mackerras19ccb762011-07-23 17:42:46 +10002784 /* No need to go into the guest when all we'll do is come back out */
2785 if (signal_pending(current)) {
2786 run->exit_reason = KVM_EXIT_INTR;
2787 return -EINTR;
2788 }
2789
Paul Mackerras32fad282012-05-04 02:32:53 +00002790 atomic_inc(&vcpu->kvm->arch.vcpus_running);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002791 /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */
Paul Mackerras32fad282012-05-04 02:32:53 +00002792 smp_mb();
2793
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11002794 /* On the first time here, set up HTAB and VRMA */
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11002795 if (!vcpu->kvm->arch.hpte_setup_done) {
Paul Mackerras32fad282012-05-04 02:32:53 +00002796 r = kvmppc_hv_setup_htab_rma(vcpu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002797 if (r)
Paul Mackerras32fad282012-05-04 02:32:53 +00002798 goto out;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002799 }
Paul Mackerras19ccb762011-07-23 17:42:46 +10002800
Anton Blanchard579e6332015-10-29 11:44:09 +11002801 flush_all_to_thread(current);
2802
Paul Mackerras19ccb762011-07-23 17:42:46 +10002803 vcpu->arch.wqp = &vcpu->arch.vcore->wq;
Paul Mackerras342d3db2011-12-12 12:38:05 +00002804 vcpu->arch.pgdir = current->mm->pgd;
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002805 vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST;
Paul Mackerras19ccb762011-07-23 17:42:46 +10002806
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002807 do {
2808 r = kvmppc_run_vcpu(run, vcpu);
2809
2810 if (run->exit_reason == KVM_EXIT_PAPR_HCALL &&
2811 !(vcpu->arch.shregs.msr & MSR_PR)) {
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002812 trace_kvm_hcall_enter(vcpu);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002813 r = kvmppc_pseries_do_hcall(vcpu);
Suresh E. Warrier3c78f782014-12-03 18:48:10 -06002814 trace_kvm_hcall_exit(vcpu, r);
Scott Wood7e28e60e2011-11-08 18:23:20 -06002815 kvmppc_core_prepare_to_enter(vcpu);
Paul Mackerras913d3ff9a2012-10-15 01:16:48 +00002816 } else if (r == RESUME_PAGE_FAULT) {
2817 srcu_idx = srcu_read_lock(&vcpu->kvm->srcu);
2818 r = kvmppc_book3s_hv_page_fault(run, vcpu,
2819 vcpu->arch.fault_dar, vcpu->arch.fault_dsisr);
2820 srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002821 }
Greg Kurze59d24e2014-02-06 17:36:56 +01002822 } while (is_kvmppc_resume_guest(r));
Paul Mackerras32fad282012-05-04 02:32:53 +00002823
2824 out:
Paul Mackerrasc7b67672012-10-15 01:18:07 +00002825 vcpu->arch.state = KVMPPC_VCPU_NOTREADY;
Paul Mackerras32fad282012-05-04 02:32:53 +00002826 atomic_dec(&vcpu->kvm->arch.vcpus_running);
Paul Mackerrasa8606e22011-06-29 00:22:05 +00002827 return r;
2828}
2829
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002830static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps,
2831 int linux_psize)
2832{
2833 struct mmu_psize_def *def = &mmu_psize_defs[linux_psize];
2834
2835 if (!def->shift)
2836 return;
2837 (*sps)->page_shift = def->shift;
2838 (*sps)->slb_enc = def->sllp;
2839 (*sps)->enc[0].page_shift = def->shift;
Aneesh Kumar K.Vb1022fb2013-04-28 09:37:35 +00002840 (*sps)->enc[0].pte_enc = def->penc[linux_psize];
Aneesh Kumar K.V1f365bb2014-05-06 23:31:36 +05302841 /*
2842 * Add 16MB MPSS support if host supports it
2843 */
2844 if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) {
2845 (*sps)->enc[1].page_shift = 24;
2846 (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M];
2847 }
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002848 (*sps)++;
2849}
2850
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302851static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm,
2852 struct kvm_ppc_smmu_info *info)
Benjamin Herrenschmidt5b747162012-04-26 19:43:42 +00002853{
2854 struct kvm_ppc_one_seg_page_size *sps;
2855
2856 info->flags = KVM_PPC_PAGE_SIZES_REAL;
2857 if (mmu_has_feature(MMU_FTR_1T_SEGMENT))
2858 info->flags |= KVM_PPC_1T_SEGMENTS;
2859 info->slb_size = mmu_slb_size;
2860
2861 /* We only support these sizes for now, and no muti-size segments */
2862 sps = &info->sps[0];
2863 kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K);
2864 kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K);
2865 kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M);
2866
2867 return 0;
2868}
2869
Paul Mackerras82ed3612011-12-15 02:03:22 +00002870/*
2871 * Get (and clear) the dirty memory log for a memory slot.
2872 */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302873static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm,
2874 struct kvm_dirty_log *log)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002875{
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002876 struct kvm_memslots *slots;
Paul Mackerras82ed3612011-12-15 02:03:22 +00002877 struct kvm_memory_slot *memslot;
2878 int r;
2879 unsigned long n;
2880
2881 mutex_lock(&kvm->slots_lock);
2882
2883 r = -EINVAL;
Alex Williamsonbbacc0c2012-12-10 10:33:09 -07002884 if (log->slot >= KVM_USER_MEM_SLOTS)
Paul Mackerras82ed3612011-12-15 02:03:22 +00002885 goto out;
2886
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002887 slots = kvm_memslots(kvm);
2888 memslot = id_to_memslot(slots, log->slot);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002889 r = -ENOENT;
2890 if (!memslot->dirty_bitmap)
2891 goto out;
2892
2893 n = kvm_dirty_bitmap_bytes(memslot);
2894 memset(memslot->dirty_bitmap, 0, n);
2895
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002896 r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap);
Paul Mackerras82ed3612011-12-15 02:03:22 +00002897 if (r)
2898 goto out;
2899
2900 r = -EFAULT;
2901 if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n))
2902 goto out;
2903
2904 r = 0;
2905out:
2906 mutex_unlock(&kvm->slots_lock);
2907 return r;
2908}
2909
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302910static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free,
2911 struct kvm_memory_slot *dont)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002912{
2913 if (!dont || free->arch.rmap != dont->arch.rmap) {
2914 vfree(free->arch.rmap);
2915 free->arch.rmap = NULL;
2916 }
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002917}
2918
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302919static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot,
2920 unsigned long npages)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002921{
2922 slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap));
2923 if (!slot->arch.rmap)
2924 return -ENOMEM;
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002925
2926 return 0;
2927}
2928
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302929static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm,
2930 struct kvm_memory_slot *memslot,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002931 const struct kvm_userspace_memory_region *mem)
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002932{
Paul Mackerrasa66b48c2012-09-11 13:27:46 +00002933 return 0;
2934}
2935
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302936static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm,
Paolo Bonzini09170a42015-05-18 13:59:39 +02002937 const struct kvm_userspace_memory_region *mem,
Paolo Bonzinif36f3f22015-05-18 13:20:23 +02002938 const struct kvm_memory_slot *old,
2939 const struct kvm_memory_slot *new)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002940{
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002941 unsigned long npages = mem->memory_size >> PAGE_SHIFT;
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002942 struct kvm_memslots *slots;
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002943 struct kvm_memory_slot *memslot;
2944
Takuya Yoshikawa84826442013-02-27 19:45:25 +09002945 if (npages && old->npages) {
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002946 /*
2947 * If modifying a memslot, reset all the rmap dirty bits.
2948 * If this is a new memslot, we don't need to do anything
2949 * since the rmap array starts out as all zeroes,
2950 * i.e. no pages are dirty.
2951 */
Paolo Bonzini9f6b8022015-05-17 16:20:07 +02002952 slots = kvm_memslots(kvm);
2953 memslot = id_to_memslot(slots, mem->slot);
Paul Mackerrasdfe49db2012-09-11 13:28:18 +00002954 kvmppc_hv_get_dirty_log(kvm, memslot, NULL);
2955 }
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002956}
2957
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002958/*
2959 * Update LPCR values in kvm->arch and in vcores.
2960 * Caller must hold kvm->lock.
2961 */
2962void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask)
2963{
2964 long int i;
2965 u32 cores_done = 0;
2966
2967 if ((kvm->arch.lpcr & mask) == lpcr)
2968 return;
2969
2970 kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr;
2971
2972 for (i = 0; i < KVM_MAX_VCORES; ++i) {
2973 struct kvmppc_vcore *vc = kvm->arch.vcores[i];
2974 if (!vc)
2975 continue;
2976 spin_lock(&vc->lock);
2977 vc->lpcr = (vc->lpcr & ~mask) | lpcr;
2978 spin_unlock(&vc->lock);
2979 if (++cores_done >= kvm->arch.online_vcores)
2980 break;
2981 }
2982}
2983
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05302984static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu)
2985{
2986 return;
2987}
2988
Paul Mackerras32fad282012-05-04 02:32:53 +00002989static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002990{
2991 int err = 0;
2992 struct kvm *kvm = vcpu->kvm;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002993 unsigned long hva;
2994 struct kvm_memory_slot *memslot;
2995 struct vm_area_struct *vma;
Paul Mackerrasa0144e22013-09-20 14:52:38 +10002996 unsigned long lpcr = 0, senc;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002997 unsigned long psize, porder;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00002998 int srcu_idx;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00002999
3000 mutex_lock(&kvm->lock);
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003001 if (kvm->arch.hpte_setup_done)
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003002 goto out; /* another vcpu beat us to it */
3003
Paul Mackerras32fad282012-05-04 02:32:53 +00003004 /* Allocate hashed page table (if not done already) and reset it */
3005 if (!kvm->arch.hpt_virt) {
3006 err = kvmppc_alloc_hpt(kvm, NULL);
3007 if (err) {
3008 pr_err("KVM: Couldn't alloc HPT\n");
3009 goto out;
3010 }
3011 }
3012
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003013 /* Look up the memslot for guest physical address 0 */
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003014 srcu_idx = srcu_read_lock(&kvm->srcu);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003015 memslot = gfn_to_memslot(kvm, 0);
3016
3017 /* We must have some memory at 0 by now */
3018 err = -EINVAL;
3019 if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID))
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003020 goto out_srcu;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003021
3022 /* Look up the VMA for the start of this memory slot */
3023 hva = memslot->userspace_addr;
3024 down_read(&current->mm->mmap_sem);
3025 vma = find_vma(current->mm, hva);
3026 if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO))
3027 goto up_out;
3028
3029 psize = vma_kernel_pagesize(vma);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003030 porder = __ilog2(psize);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003031
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003032 up_read(&current->mm->mmap_sem);
3033
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003034 /* We can handle 4k, 64k or 16M pages in the VRMA */
3035 err = -EINVAL;
3036 if (!(psize == 0x1000 || psize == 0x10000 ||
3037 psize == 0x1000000))
3038 goto out_srcu;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003039
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003040 /* Update VRMASD field in the LPCR */
3041 senc = slb_pgsize_encoding(psize);
3042 kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T |
3043 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3044 /* the -4 is to account for senc values starting at 0x10 */
3045 lpcr = senc << (LPCR_VRMASD_SH - 4);
Paul Mackerrasda9d1d72011-12-12 12:31:41 +00003046
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003047 /* Create HPTEs in the hash page table for the VRMA */
3048 kvmppc_map_vrma(vcpu, memslot, porder);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003049
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003050 kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD);
Paul Mackerrasa0144e22013-09-20 14:52:38 +10003051
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003052 /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003053 smp_wmb();
Aneesh Kumar K.V31037ec2015-03-20 20:39:42 +11003054 kvm->arch.hpte_setup_done = 1;
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003055 err = 0;
Paul Mackerras2c9097e2012-09-11 13:27:01 +00003056 out_srcu:
3057 srcu_read_unlock(&kvm->srcu, srcu_idx);
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003058 out:
3059 mutex_unlock(&kvm->lock);
3060 return err;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003061
Paul Mackerrasc77162d2011-12-12 12:31:00 +00003062 up_out:
3063 up_read(&current->mm->mmap_sem);
Lai Jiangshan505d6422013-03-16 00:50:49 +08003064 goto out_srcu;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003065}
3066
Suresh Warrier79b6c242015-12-17 14:59:06 -06003067#ifdef CONFIG_KVM_XICS
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003068static int kvmppc_cpu_notify(struct notifier_block *self, unsigned long action,
3069 void *hcpu)
3070{
3071 unsigned long cpu = (long)hcpu;
3072
3073 switch (action) {
3074 case CPU_UP_PREPARE:
3075 case CPU_UP_PREPARE_FROZEN:
3076 kvmppc_set_host_core(cpu);
3077 break;
3078
3079#ifdef CONFIG_HOTPLUG_CPU
3080 case CPU_DEAD:
3081 case CPU_DEAD_FROZEN:
3082 case CPU_UP_CANCELED:
3083 case CPU_UP_CANCELED_FROZEN:
3084 kvmppc_clear_host_core(cpu);
3085 break;
3086#endif
3087 default:
3088 break;
3089 }
3090
3091 return NOTIFY_OK;
3092}
3093
3094static struct notifier_block kvmppc_cpu_notifier = {
3095 .notifier_call = kvmppc_cpu_notify,
3096};
3097
Suresh Warrier79b6c242015-12-17 14:59:06 -06003098/*
3099 * Allocate a per-core structure for managing state about which cores are
3100 * running in the host versus the guest and for exchanging data between
3101 * real mode KVM and CPU running in the host.
3102 * This is only done for the first VM.
3103 * The allocated structure stays even if all VMs have stopped.
3104 * It is only freed when the kvm-hv module is unloaded.
3105 * It's OK for this routine to fail, we just don't support host
3106 * core operations like redirecting H_IPI wakeups.
3107 */
3108void kvmppc_alloc_host_rm_ops(void)
3109{
3110 struct kvmppc_host_rm_ops *ops;
3111 unsigned long l_ops;
3112 int cpu, core;
3113 int size;
3114
3115 /* Not the first time here ? */
3116 if (kvmppc_host_rm_ops_hv != NULL)
3117 return;
3118
3119 ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL);
3120 if (!ops)
3121 return;
3122
3123 size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core);
3124 ops->rm_core = kzalloc(size, GFP_KERNEL);
3125
3126 if (!ops->rm_core) {
3127 kfree(ops);
3128 return;
3129 }
3130
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003131 get_online_cpus();
3132
Suresh Warrier79b6c242015-12-17 14:59:06 -06003133 for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) {
3134 if (!cpu_online(cpu))
3135 continue;
3136
3137 core = cpu >> threads_shift;
3138 ops->rm_core[core].rm_state.in_host = 1;
3139 }
3140
Suresh Warrier0c2a6602015-12-17 14:59:09 -06003141 ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv;
3142
Suresh Warrier79b6c242015-12-17 14:59:06 -06003143 /*
3144 * Make the contents of the kvmppc_host_rm_ops structure visible
3145 * to other CPUs before we assign it to the global variable.
3146 * Do an atomic assignment (no locks used here), but if someone
3147 * beats us to it, just free our copy and return.
3148 */
3149 smp_wmb();
3150 l_ops = (unsigned long) ops;
3151
3152 if (cmpxchg64((unsigned long *)&kvmppc_host_rm_ops_hv, 0, l_ops)) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003153 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003154 kfree(ops->rm_core);
3155 kfree(ops);
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003156 return;
Suresh Warrier79b6c242015-12-17 14:59:06 -06003157 }
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003158
3159 register_cpu_notifier(&kvmppc_cpu_notifier);
3160
3161 put_online_cpus();
Suresh Warrier79b6c242015-12-17 14:59:06 -06003162}
3163
3164void kvmppc_free_host_rm_ops(void)
3165{
3166 if (kvmppc_host_rm_ops_hv) {
Suresh Warrier6f3bb802015-12-17 14:59:08 -06003167 unregister_cpu_notifier(&kvmppc_cpu_notifier);
Suresh Warrier79b6c242015-12-17 14:59:06 -06003168 kfree(kvmppc_host_rm_ops_hv->rm_core);
3169 kfree(kvmppc_host_rm_ops_hv);
3170 kvmppc_host_rm_ops_hv = NULL;
3171 }
3172}
3173#endif
3174
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303175static int kvmppc_core_init_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003176{
Paul Mackerras32fad282012-05-04 02:32:53 +00003177 unsigned long lpcr, lpid;
Paul Mackerrase23a8082015-03-28 14:21:01 +11003178 char buf[32];
Paul Mackerrasde56a942011-06-29 00:21:34 +00003179
Paul Mackerras32fad282012-05-04 02:32:53 +00003180 /* Allocate the guest's logical partition ID */
3181
3182 lpid = kvmppc_alloc_lpid();
Chen Gang5d226ae2013-07-22 14:32:35 +08003183 if ((long)lpid < 0)
Paul Mackerras32fad282012-05-04 02:32:53 +00003184 return -ENOMEM;
3185 kvm->arch.lpid = lpid;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003186
Suresh Warrier79b6c242015-12-17 14:59:06 -06003187 kvmppc_alloc_host_rm_ops();
3188
Paul Mackerras1b400ba2012-11-21 23:28:08 +00003189 /*
3190 * Since we don't flush the TLB when tearing down a VM,
3191 * and this lpid might have previously been used,
3192 * make sure we flush on each core before running the new VM.
3193 */
3194 cpumask_setall(&kvm->arch.need_tlb_flush);
3195
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003196 /* Start out with the default set of hcalls enabled */
3197 memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls,
3198 sizeof(kvm->arch.enabled_hcalls));
3199
Paul Mackerras9e368f22011-06-29 00:40:08 +00003200 kvm->arch.host_sdr1 = mfspr(SPRN_SDR1);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003201
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003202 /* Init LPCR for virtual RMA mode */
3203 kvm->arch.host_lpid = mfspr(SPRN_LPID);
3204 kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR);
3205 lpcr &= LPCR_PECE | LPCR_LPES;
3206 lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE |
3207 LPCR_VPM0 | LPCR_VPM1;
3208 kvm->arch.vrma_slb_v = SLB_VSID_B_1T |
3209 (VRMA_VSID << SLB_VSID_SHIFT_1T);
3210 /* On POWER8 turn on online bit to enable PURR/SPURR */
3211 if (cpu_has_feature(CPU_FTR_ARCH_207S))
3212 lpcr |= LPCR_ONL;
Paul Mackerras9e368f22011-06-29 00:40:08 +00003213 kvm->arch.lpcr = lpcr;
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003214
Paul Mackerras512691d2012-10-15 01:15:41 +00003215 /*
Michael Ellerman441c19c2014-05-23 18:15:25 +10003216 * Track that we now have a HV mode VM active. This blocks secondary
3217 * CPU threads from coming online.
Paul Mackerras512691d2012-10-15 01:15:41 +00003218 */
Michael Ellerman441c19c2014-05-23 18:15:25 +10003219 kvm_hv_vm_activated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003220
Paul Mackerrase23a8082015-03-28 14:21:01 +11003221 /*
3222 * Create a debugfs directory for the VM
3223 */
3224 snprintf(buf, sizeof(buf), "vm%d", current->pid);
3225 kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir);
3226 if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir))
3227 kvmppc_mmu_debugfs_init(kvm);
3228
David Gibson54738c02011-06-29 00:22:41 +00003229 return 0;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003230}
3231
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303232static void kvmppc_free_vcores(struct kvm *kvm)
3233{
3234 long int i;
3235
Paul Mackerras23316312015-10-21 16:03:14 +11003236 for (i = 0; i < KVM_MAX_VCORES; ++i)
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303237 kfree(kvm->arch.vcores[i]);
3238 kvm->arch.online_vcores = 0;
3239}
3240
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303241static void kvmppc_core_destroy_vm_hv(struct kvm *kvm)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003242{
Paul Mackerrase23a8082015-03-28 14:21:01 +11003243 debugfs_remove_recursive(kvm->arch.debugfs_dir);
3244
Michael Ellerman441c19c2014-05-23 18:15:25 +10003245 kvm_hv_vm_deactivated();
Paul Mackerras512691d2012-10-15 01:15:41 +00003246
Paul Mackerrasf1378b12013-09-27 15:33:43 +05303247 kvmppc_free_vcores(kvm);
Paul Mackerrasaa04b4c2011-06-29 00:25:44 +00003248
Paul Mackerrasde56a942011-06-29 00:21:34 +00003249 kvmppc_free_hpt(kvm);
Paul Mackerrasde56a942011-06-29 00:21:34 +00003250}
3251
3252/* We don't need to emulate any privileged instructions or dcbz */
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303253static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu,
3254 unsigned int inst, int *advance)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003255{
3256 return EMULATE_FAIL;
3257}
3258
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303259static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn,
3260 ulong spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003261{
3262 return EMULATE_FAIL;
3263}
3264
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303265static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn,
3266 ulong *spr_val)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003267{
3268 return EMULATE_FAIL;
3269}
3270
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303271static int kvmppc_core_check_processor_compat_hv(void)
3272{
Paul Mackerrasc17b98c2014-12-03 13:30:38 +11003273 if (!cpu_has_feature(CPU_FTR_HVMODE) ||
3274 !cpu_has_feature(CPU_FTR_ARCH_206))
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303275 return -EIO;
Aneesh Kumar K.V50de5962016-04-29 23:25:43 +10003276 /*
3277 * Disable KVM for Power9, untill the required bits merged.
3278 */
3279 if (cpu_has_feature(CPU_FTR_ARCH_300))
3280 return -EIO;
3281
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303282 return 0;
3283}
3284
3285static long kvm_arch_vm_ioctl_hv(struct file *filp,
3286 unsigned int ioctl, unsigned long arg)
3287{
3288 struct kvm *kvm __maybe_unused = filp->private_data;
3289 void __user *argp = (void __user *)arg;
3290 long r;
3291
3292 switch (ioctl) {
3293
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303294 case KVM_PPC_ALLOCATE_HTAB: {
3295 u32 htab_order;
3296
3297 r = -EFAULT;
3298 if (get_user(htab_order, (u32 __user *)argp))
3299 break;
3300 r = kvmppc_alloc_reset_hpt(kvm, &htab_order);
3301 if (r)
3302 break;
3303 r = -EFAULT;
3304 if (put_user(htab_order, (u32 __user *)argp))
3305 break;
3306 r = 0;
3307 break;
3308 }
3309
3310 case KVM_PPC_GET_HTAB_FD: {
3311 struct kvm_get_htab_fd ghf;
3312
3313 r = -EFAULT;
3314 if (copy_from_user(&ghf, argp, sizeof(ghf)))
3315 break;
3316 r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf);
3317 break;
3318 }
3319
3320 default:
3321 r = -ENOTTY;
3322 }
3323
3324 return r;
3325}
3326
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003327/*
3328 * List of hcall numbers to enable by default.
3329 * For compatibility with old userspace, we enable by default
3330 * all hcalls that were implemented before the hcall-enabling
3331 * facility was added. Note this list should not include H_RTAS.
3332 */
3333static unsigned int default_hcall_list[] = {
3334 H_REMOVE,
3335 H_ENTER,
3336 H_READ,
3337 H_PROTECT,
3338 H_BULK_REMOVE,
3339 H_GET_TCE,
3340 H_PUT_TCE,
3341 H_SET_DABR,
3342 H_SET_XDABR,
3343 H_CEDE,
3344 H_PROD,
3345 H_CONFER,
3346 H_REGISTER_VPA,
3347#ifdef CONFIG_KVM_XICS
3348 H_EOI,
3349 H_CPPR,
3350 H_IPI,
3351 H_IPOLL,
3352 H_XIRR,
3353 H_XIRR_X,
3354#endif
3355 0
3356};
3357
3358static void init_default_hcalls(void)
3359{
3360 int i;
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003361 unsigned int hcall;
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003362
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003363 for (i = 0; default_hcall_list[i]; ++i) {
3364 hcall = default_hcall_list[i];
3365 WARN_ON(!kvmppc_hcall_impl_hv(hcall));
3366 __set_bit(hcall / 4, default_enabled_hcalls);
3367 }
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003368}
3369
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303370static struct kvmppc_ops kvm_ops_hv = {
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303371 .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv,
3372 .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv,
3373 .get_one_reg = kvmppc_get_one_reg_hv,
3374 .set_one_reg = kvmppc_set_one_reg_hv,
3375 .vcpu_load = kvmppc_core_vcpu_load_hv,
3376 .vcpu_put = kvmppc_core_vcpu_put_hv,
3377 .set_msr = kvmppc_set_msr_hv,
3378 .vcpu_run = kvmppc_vcpu_run_hv,
3379 .vcpu_create = kvmppc_core_vcpu_create_hv,
3380 .vcpu_free = kvmppc_core_vcpu_free_hv,
3381 .check_requests = kvmppc_core_check_requests_hv,
3382 .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv,
3383 .flush_memslot = kvmppc_core_flush_memslot_hv,
3384 .prepare_memory_region = kvmppc_core_prepare_memory_region_hv,
3385 .commit_memory_region = kvmppc_core_commit_memory_region_hv,
3386 .unmap_hva = kvm_unmap_hva_hv,
3387 .unmap_hva_range = kvm_unmap_hva_range_hv,
3388 .age_hva = kvm_age_hva_hv,
3389 .test_age_hva = kvm_test_age_hva_hv,
3390 .set_spte_hva = kvm_set_spte_hva_hv,
3391 .mmu_destroy = kvmppc_mmu_destroy_hv,
3392 .free_memslot = kvmppc_core_free_memslot_hv,
3393 .create_memslot = kvmppc_core_create_memslot_hv,
3394 .init_vm = kvmppc_core_init_vm_hv,
3395 .destroy_vm = kvmppc_core_destroy_vm_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303396 .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv,
3397 .emulate_op = kvmppc_core_emulate_op_hv,
3398 .emulate_mtspr = kvmppc_core_emulate_mtspr_hv,
3399 .emulate_mfspr = kvmppc_core_emulate_mfspr_hv,
3400 .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv,
3401 .arch_vm_ioctl = kvm_arch_vm_ioctl_hv,
Paul Mackerrasae2113a2014-06-02 11:03:00 +10003402 .hcall_implemented = kvmppc_hcall_impl_hv,
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303403};
3404
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303405static int kvm_init_subcore_bitmap(void)
3406{
3407 int i, j;
3408 int nr_cores = cpu_nr_cores();
3409 struct sibling_subcore_state *sibling_subcore_state;
3410
3411 for (i = 0; i < nr_cores; i++) {
3412 int first_cpu = i * threads_per_core;
3413 int node = cpu_to_node(first_cpu);
3414
3415 /* Ignore if it is already allocated. */
3416 if (paca[first_cpu].sibling_subcore_state)
3417 continue;
3418
3419 sibling_subcore_state =
3420 kmalloc_node(sizeof(struct sibling_subcore_state),
3421 GFP_KERNEL, node);
3422 if (!sibling_subcore_state)
3423 return -ENOMEM;
3424
3425 memset(sibling_subcore_state, 0,
3426 sizeof(struct sibling_subcore_state));
3427
3428 for (j = 0; j < threads_per_core; j++) {
3429 int cpu = first_cpu + j;
3430
3431 paca[cpu].sibling_subcore_state = sibling_subcore_state;
3432 }
3433 }
3434 return 0;
3435}
3436
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303437static int kvmppc_book3s_init_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003438{
3439 int r;
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303440 /*
3441 * FIXME!! Do we need to check on all cpus ?
3442 */
3443 r = kvmppc_core_check_processor_compat_hv();
3444 if (r < 0)
Paul Mackerras739e2422014-03-25 10:47:05 +11003445 return -ENODEV;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003446
Mahesh Salgaonkarfd7bacb2016-05-15 09:44:26 +05303447 r = kvm_init_subcore_bitmap();
3448 if (r)
3449 return r;
3450
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303451 kvm_ops_hv.owner = THIS_MODULE;
3452 kvmppc_hv_ops = &kvm_ops_hv;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003453
Paul Mackerras699a0ea2014-06-02 11:02:59 +10003454 init_default_hcalls();
3455
Paul Mackerrasec257162015-06-24 21:18:03 +10003456 init_vcore_lists();
3457
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303458 r = kvmppc_mmu_hv_init();
Paul Mackerrasde56a942011-06-29 00:21:34 +00003459 return r;
3460}
3461
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303462static void kvmppc_book3s_exit_hv(void)
Paul Mackerrasde56a942011-06-29 00:21:34 +00003463{
Suresh Warrier79b6c242015-12-17 14:59:06 -06003464 kvmppc_free_host_rm_ops();
Aneesh Kumar K.Vcbbc58d2013-10-07 22:18:01 +05303465 kvmppc_hv_ops = NULL;
Paul Mackerrasde56a942011-06-29 00:21:34 +00003466}
3467
Aneesh Kumar K.V3a167bea2013-10-07 22:17:53 +05303468module_init(kvmppc_book3s_init_hv);
3469module_exit(kvmppc_book3s_exit_hv);
Aneesh Kumar K.V2ba9f0d2013-10-07 22:17:59 +05303470MODULE_LICENSE("GPL");
Alexander Graf398a76c2013-12-09 13:53:42 +01003471MODULE_ALIAS_MISCDEV(KVM_MINOR);
3472MODULE_ALIAS("devname:kvm");