Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1 | /* |
| 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 Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 27 | #include <linux/export.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 28 | #include <linux/fs.h> |
| 29 | #include <linux/anon_inodes.h> |
| 30 | #include <linux/cpumask.h> |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/page-flags.h> |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 33 | #include <linux/srcu.h> |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 34 | #include <linux/miscdevice.h> |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 35 | #include <linux/debugfs.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 36 | |
| 37 | #include <asm/reg.h> |
| 38 | #include <asm/cputable.h> |
| 39 | #include <asm/cacheflush.h> |
| 40 | #include <asm/tlbflush.h> |
| 41 | #include <asm/uaccess.h> |
| 42 | #include <asm/io.h> |
| 43 | #include <asm/kvm_ppc.h> |
| 44 | #include <asm/kvm_book3s.h> |
| 45 | #include <asm/mmu_context.h> |
| 46 | #include <asm/lppaca.h> |
| 47 | #include <asm/processor.h> |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 48 | #include <asm/cputhreads.h> |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 49 | #include <asm/page.h> |
Michael Neuling | de1d924 | 2011-11-09 20:39:49 +0000 | [diff] [blame] | 50 | #include <asm/hvcall.h> |
David Howells | ae3a197 | 2012-03-28 18:30:02 +0100 | [diff] [blame] | 51 | #include <asm/switch_to.h> |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 52 | #include <asm/smp.h> |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 53 | #include <asm/dbell.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 54 | #include <linux/gfp.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 55 | #include <linux/vmalloc.h> |
| 56 | #include <linux/highmem.h> |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 57 | #include <linux/hugetlb.h> |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 58 | #include <linux/module.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 59 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 60 | #include "book3s.h" |
| 61 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 62 | #define CREATE_TRACE_POINTS |
| 63 | #include "trace_hv.h" |
| 64 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 65 | /* #define EXIT_DEBUG */ |
| 66 | /* #define EXIT_DEBUG_SIMPLE */ |
| 67 | /* #define EXIT_DEBUG_INT */ |
| 68 | |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 69 | /* Used to indicate that a guest page fault needs to be handled */ |
| 70 | #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1) |
| 71 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 72 | /* Used as a "null" value for timebase values */ |
| 73 | #define TB_NIL (~(u64)0) |
| 74 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 75 | static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); |
| 76 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 77 | static int dynamic_mt_modes = 6; |
| 78 | module_param(dynamic_mt_modes, int, S_IRUGO | S_IWUSR); |
| 79 | MODULE_PARM_DESC(dynamic_mt_modes, "Set of allowed dynamic micro-threading modes: 0 (= none), 2, 4, or 6 (= 2 or 4)"); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 80 | static int target_smt_mode; |
| 81 | module_param(target_smt_mode, int, S_IRUGO | S_IWUSR); |
| 82 | MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)"); |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 83 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 84 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 85 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 86 | |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 87 | static bool kvmppc_ipi_thread(int cpu) |
| 88 | { |
| 89 | /* On POWER8 for IPIs to threads in the same core, use msgsnd */ |
| 90 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 91 | preempt_disable(); |
| 92 | if (cpu_first_thread_sibling(cpu) == |
| 93 | cpu_first_thread_sibling(smp_processor_id())) { |
| 94 | unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER); |
| 95 | msg |= cpu_thread_in_core(cpu); |
| 96 | smp_mb(); |
| 97 | __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); |
| 98 | preempt_enable(); |
| 99 | return true; |
| 100 | } |
| 101 | preempt_enable(); |
| 102 | } |
| 103 | |
| 104 | #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) |
| 105 | if (cpu >= 0 && cpu < nr_cpu_ids && paca[cpu].kvm_hstate.xics_phys) { |
| 106 | xics_wake_cpu(cpu); |
| 107 | return true; |
| 108 | } |
| 109 | #endif |
| 110 | |
| 111 | return false; |
| 112 | } |
| 113 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 114 | static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 115 | { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 116 | int cpu; |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 117 | wait_queue_head_t *wqp; |
| 118 | |
| 119 | wqp = kvm_arch_vcpu_wq(vcpu); |
| 120 | if (waitqueue_active(wqp)) { |
| 121 | wake_up_interruptible(wqp); |
| 122 | ++vcpu->stat.halt_wakeup; |
| 123 | } |
| 124 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 125 | if (kvmppc_ipi_thread(vcpu->arch.thread_cpu)) |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 126 | return; |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 127 | |
| 128 | /* CPU points to the first thread of the core */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 129 | cpu = vcpu->cpu; |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 130 | if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu)) |
| 131 | smp_send_reschedule(cpu); |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 134 | /* |
| 135 | * We use the vcpu_load/put functions to measure stolen time. |
| 136 | * Stolen time is counted as time when either the vcpu is able to |
| 137 | * run as part of a virtual core, but the task running the vcore |
| 138 | * is preempted or sleeping, or when the vcpu needs something done |
| 139 | * in the kernel by the task running the vcpu, but that task is |
| 140 | * preempted or sleeping. Those two things have to be counted |
| 141 | * separately, since one of the vcpu tasks will take on the job |
| 142 | * of running the core, and the other vcpu tasks in the vcore will |
| 143 | * sleep waiting for it to do that, but that sleep shouldn't count |
| 144 | * as stolen time. |
| 145 | * |
| 146 | * Hence we accumulate stolen time when the vcpu can run as part of |
| 147 | * a vcore using vc->stolen_tb, and the stolen time when the vcpu |
| 148 | * needs its task to do other things in the kernel (for example, |
| 149 | * service a page fault) in busy_stolen. We don't accumulate |
| 150 | * stolen time for a vcore when it is inactive, or for a vcpu |
| 151 | * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of |
| 152 | * a misnomer; it means that the vcpu task is not executing in |
| 153 | * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in |
| 154 | * the kernel. We don't have any way of dividing up that time |
| 155 | * between time that the vcpu is genuinely stopped, time that |
| 156 | * the task is actively working on behalf of the vcpu, and time |
| 157 | * that the task is preempted, so we don't count any of it as |
| 158 | * stolen. |
| 159 | * |
| 160 | * Updates to busy_stolen are protected by arch.tbacct_lock; |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 161 | * updates to vc->stolen_tb are protected by the vcore->stoltb_lock |
| 162 | * lock. The stolen times are measured in units of timebase ticks. |
| 163 | * (Note that the != TB_NIL checks below are purely defensive; |
| 164 | * they should never fail.) |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 165 | */ |
| 166 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 167 | static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc) |
| 168 | { |
| 169 | unsigned long flags; |
| 170 | |
| 171 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 172 | vc->preempt_tb = mftb(); |
| 173 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
| 174 | } |
| 175 | |
| 176 | static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc) |
| 177 | { |
| 178 | unsigned long flags; |
| 179 | |
| 180 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 181 | if (vc->preempt_tb != TB_NIL) { |
| 182 | vc->stolen_tb += mftb() - vc->preempt_tb; |
| 183 | vc->preempt_tb = TB_NIL; |
| 184 | } |
| 185 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
| 186 | } |
| 187 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 188 | static void kvmppc_core_vcpu_load_hv(struct kvm_vcpu *vcpu, int cpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 189 | { |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 190 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 191 | unsigned long flags; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 192 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 193 | /* |
| 194 | * We can test vc->runner without taking the vcore lock, |
| 195 | * because only this task ever sets vc->runner to this |
| 196 | * vcpu, and once it is set to this vcpu, only this task |
| 197 | * ever sets it to NULL. |
| 198 | */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 199 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
| 200 | kvmppc_core_end_stolen(vc); |
| 201 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 202 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 203 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST && |
| 204 | vcpu->arch.busy_preempt != TB_NIL) { |
| 205 | vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt; |
| 206 | vcpu->arch.busy_preempt = TB_NIL; |
| 207 | } |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 208 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 211 | static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 212 | { |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 213 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 214 | unsigned long flags; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 215 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 216 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
| 217 | kvmppc_core_start_stolen(vc); |
| 218 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 219 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 220 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) |
| 221 | vcpu->arch.busy_preempt = mftb(); |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 222 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 225 | static void kvmppc_set_msr_hv(struct kvm_vcpu *vcpu, u64 msr) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 226 | { |
| 227 | vcpu->arch.shregs.msr = msr; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 228 | kvmppc_end_cede(vcpu); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 231 | static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 232 | { |
| 233 | vcpu->arch.pvr = pvr; |
| 234 | } |
| 235 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 236 | static int kvmppc_set_arch_compat(struct kvm_vcpu *vcpu, u32 arch_compat) |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 237 | { |
| 238 | unsigned long pcr = 0; |
| 239 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 240 | |
| 241 | if (arch_compat) { |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 242 | switch (arch_compat) { |
| 243 | case PVR_ARCH_205: |
Paul Mackerras | 5557ae0 | 2014-01-08 21:25:24 +1100 | [diff] [blame] | 244 | /* |
| 245 | * If an arch bit is set in PCR, all the defined |
| 246 | * higher-order arch bits also have to be set. |
| 247 | */ |
| 248 | pcr = PCR_ARCH_206 | PCR_ARCH_205; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 249 | break; |
| 250 | case PVR_ARCH_206: |
| 251 | case PVR_ARCH_206p: |
Paul Mackerras | 5557ae0 | 2014-01-08 21:25:24 +1100 | [diff] [blame] | 252 | pcr = PCR_ARCH_206; |
| 253 | break; |
| 254 | case PVR_ARCH_207: |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 255 | break; |
| 256 | default: |
| 257 | return -EINVAL; |
| 258 | } |
Paul Mackerras | 5557ae0 | 2014-01-08 21:25:24 +1100 | [diff] [blame] | 259 | |
| 260 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 261 | /* POWER7 can't emulate POWER8 */ |
| 262 | if (!(pcr & PCR_ARCH_206)) |
| 263 | return -EINVAL; |
| 264 | pcr &= ~PCR_ARCH_206; |
| 265 | } |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | spin_lock(&vc->lock); |
| 269 | vc->arch_compat = arch_compat; |
| 270 | vc->pcr = pcr; |
| 271 | spin_unlock(&vc->lock); |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 276 | static void kvmppc_dump_regs(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 277 | { |
| 278 | int r; |
| 279 | |
| 280 | pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); |
| 281 | pr_err("pc = %.16lx msr = %.16llx trap = %x\n", |
| 282 | vcpu->arch.pc, vcpu->arch.shregs.msr, vcpu->arch.trap); |
| 283 | for (r = 0; r < 16; ++r) |
| 284 | pr_err("r%2d = %.16lx r%d = %.16lx\n", |
| 285 | r, kvmppc_get_gpr(vcpu, r), |
| 286 | r+16, kvmppc_get_gpr(vcpu, r+16)); |
| 287 | pr_err("ctr = %.16lx lr = %.16lx\n", |
| 288 | vcpu->arch.ctr, vcpu->arch.lr); |
| 289 | pr_err("srr0 = %.16llx srr1 = %.16llx\n", |
| 290 | vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); |
| 291 | pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", |
| 292 | vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); |
| 293 | pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", |
| 294 | vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); |
| 295 | pr_err("cr = %.8x xer = %.16lx dsisr = %.8x\n", |
| 296 | vcpu->arch.cr, vcpu->arch.xer, vcpu->arch.shregs.dsisr); |
| 297 | pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); |
| 298 | pr_err("fault dar = %.16lx dsisr = %.8x\n", |
| 299 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); |
| 300 | pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); |
| 301 | for (r = 0; r < vcpu->arch.slb_max; ++r) |
| 302 | pr_err(" ESID = %.16llx VSID = %.16llx\n", |
| 303 | vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); |
| 304 | pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n", |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 305 | vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1, |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 306 | vcpu->arch.last_inst); |
| 307 | } |
| 308 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 309 | static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 310 | { |
| 311 | int r; |
| 312 | struct kvm_vcpu *v, *ret = NULL; |
| 313 | |
| 314 | mutex_lock(&kvm->lock); |
| 315 | kvm_for_each_vcpu(r, v, kvm) { |
| 316 | if (v->vcpu_id == id) { |
| 317 | ret = v; |
| 318 | break; |
| 319 | } |
| 320 | } |
| 321 | mutex_unlock(&kvm->lock); |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) |
| 326 | { |
Anton Blanchard | f13c13a | 2013-08-07 02:01:26 +1000 | [diff] [blame] | 327 | vpa->__old_status |= LPPACA_OLD_SHARED_PROC; |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 328 | vpa->yield_count = cpu_to_be32(1); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 329 | } |
| 330 | |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 331 | static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v, |
| 332 | unsigned long addr, unsigned long len) |
| 333 | { |
| 334 | /* check address is cacheline aligned */ |
| 335 | if (addr & (L1_CACHE_BYTES - 1)) |
| 336 | return -EINVAL; |
| 337 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 338 | if (v->next_gpa != addr || v->len != len) { |
| 339 | v->next_gpa = addr; |
| 340 | v->len = addr ? len : 0; |
| 341 | v->update_pending = 1; |
| 342 | } |
| 343 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 344 | return 0; |
| 345 | } |
| 346 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 347 | /* Length for a per-processor buffer is passed in at offset 4 in the buffer */ |
| 348 | struct reg_vpa { |
| 349 | u32 dummy; |
| 350 | union { |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 351 | __be16 hword; |
| 352 | __be32 word; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 353 | } length; |
| 354 | }; |
| 355 | |
| 356 | static int vpa_is_registered(struct kvmppc_vpa *vpap) |
| 357 | { |
| 358 | if (vpap->update_pending) |
| 359 | return vpap->next_gpa != 0; |
| 360 | return vpap->pinned_addr != NULL; |
| 361 | } |
| 362 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 363 | static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, |
| 364 | unsigned long flags, |
| 365 | unsigned long vcpuid, unsigned long vpa) |
| 366 | { |
| 367 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 368 | unsigned long len, nb; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 369 | void *va; |
| 370 | struct kvm_vcpu *tvcpu; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 371 | int err; |
| 372 | int subfunc; |
| 373 | struct kvmppc_vpa *vpap; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 374 | |
| 375 | tvcpu = kvmppc_find_vcpu(kvm, vcpuid); |
| 376 | if (!tvcpu) |
| 377 | return H_PARAMETER; |
| 378 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 379 | subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK; |
| 380 | if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL || |
| 381 | subfunc == H_VPA_REG_SLB) { |
| 382 | /* Registering new area - address must be cache-line aligned */ |
| 383 | if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 384 | return H_PARAMETER; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 385 | |
| 386 | /* convert logical addr to kernel addr and read length */ |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 387 | va = kvmppc_pin_guest_page(kvm, vpa, &nb); |
| 388 | if (va == NULL) |
Paul Mackerras | b2b2f16 | 2011-12-12 12:28:21 +0000 | [diff] [blame] | 389 | return H_PARAMETER; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 390 | if (subfunc == H_VPA_REG_VPA) |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 391 | len = be16_to_cpu(((struct reg_vpa *)va)->length.hword); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 392 | else |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 393 | len = be32_to_cpu(((struct reg_vpa *)va)->length.word); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 394 | kvmppc_unpin_guest_page(kvm, va, vpa, false); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 395 | |
| 396 | /* Check length */ |
| 397 | if (len > nb || len < sizeof(struct reg_vpa)) |
| 398 | return H_PARAMETER; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 399 | } else { |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 400 | vpa = 0; |
| 401 | len = 0; |
| 402 | } |
| 403 | |
| 404 | err = H_PARAMETER; |
| 405 | vpap = NULL; |
| 406 | spin_lock(&tvcpu->arch.vpa_update_lock); |
| 407 | |
| 408 | switch (subfunc) { |
| 409 | case H_VPA_REG_VPA: /* register VPA */ |
| 410 | if (len < sizeof(struct lppaca)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 411 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 412 | vpap = &tvcpu->arch.vpa; |
| 413 | err = 0; |
| 414 | break; |
| 415 | |
| 416 | case H_VPA_REG_DTL: /* register DTL */ |
| 417 | if (len < sizeof(struct dtl_entry)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 418 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 419 | len -= len % sizeof(struct dtl_entry); |
| 420 | |
| 421 | /* Check that they have previously registered a VPA */ |
| 422 | err = H_RESOURCE; |
| 423 | if (!vpa_is_registered(&tvcpu->arch.vpa)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 424 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 425 | |
| 426 | vpap = &tvcpu->arch.dtl; |
| 427 | err = 0; |
| 428 | break; |
| 429 | |
| 430 | case H_VPA_REG_SLB: /* register SLB shadow buffer */ |
| 431 | /* Check that they have previously registered a VPA */ |
| 432 | err = H_RESOURCE; |
| 433 | if (!vpa_is_registered(&tvcpu->arch.vpa)) |
| 434 | break; |
| 435 | |
| 436 | vpap = &tvcpu->arch.slb_shadow; |
| 437 | err = 0; |
| 438 | break; |
| 439 | |
| 440 | case H_VPA_DEREG_VPA: /* deregister VPA */ |
| 441 | /* Check they don't still have a DTL or SLB buf registered */ |
| 442 | err = H_RESOURCE; |
| 443 | if (vpa_is_registered(&tvcpu->arch.dtl) || |
| 444 | vpa_is_registered(&tvcpu->arch.slb_shadow)) |
| 445 | break; |
| 446 | |
| 447 | vpap = &tvcpu->arch.vpa; |
| 448 | err = 0; |
| 449 | break; |
| 450 | |
| 451 | case H_VPA_DEREG_DTL: /* deregister DTL */ |
| 452 | vpap = &tvcpu->arch.dtl; |
| 453 | err = 0; |
| 454 | break; |
| 455 | |
| 456 | case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */ |
| 457 | vpap = &tvcpu->arch.slb_shadow; |
| 458 | err = 0; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | if (vpap) { |
| 463 | vpap->next_gpa = vpa; |
| 464 | vpap->len = len; |
| 465 | vpap->update_pending = 1; |
| 466 | } |
| 467 | |
| 468 | spin_unlock(&tvcpu->arch.vpa_update_lock); |
| 469 | |
| 470 | return err; |
| 471 | } |
| 472 | |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 473 | static void kvmppc_update_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *vpap) |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 474 | { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 475 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 476 | void *va; |
| 477 | unsigned long nb; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 478 | unsigned long gpa; |
| 479 | |
| 480 | /* |
| 481 | * We need to pin the page pointed to by vpap->next_gpa, |
| 482 | * but we can't call kvmppc_pin_guest_page under the lock |
| 483 | * as it does get_user_pages() and down_read(). So we |
| 484 | * have to drop the lock, pin the page, then get the lock |
| 485 | * again and check that a new area didn't get registered |
| 486 | * in the meantime. |
| 487 | */ |
| 488 | for (;;) { |
| 489 | gpa = vpap->next_gpa; |
| 490 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 491 | va = NULL; |
| 492 | nb = 0; |
| 493 | if (gpa) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 494 | va = kvmppc_pin_guest_page(kvm, gpa, &nb); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 495 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 496 | if (gpa == vpap->next_gpa) |
| 497 | break; |
| 498 | /* sigh... unpin that one and try again */ |
| 499 | if (va) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 500 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 501 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 502 | |
| 503 | vpap->update_pending = 0; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 504 | if (va && nb < vpap->len) { |
| 505 | /* |
| 506 | * If it's now too short, it must be that userspace |
| 507 | * has changed the mappings underlying guest memory, |
| 508 | * so unregister the region. |
| 509 | */ |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 510 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 511 | va = NULL; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 512 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 513 | if (vpap->pinned_addr) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 514 | kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa, |
| 515 | vpap->dirty); |
| 516 | vpap->gpa = gpa; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 517 | vpap->pinned_addr = va; |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 518 | vpap->dirty = false; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 519 | if (va) |
| 520 | vpap->pinned_end = va + vpap->len; |
| 521 | } |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 522 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 523 | static void kvmppc_update_vpas(struct kvm_vcpu *vcpu) |
| 524 | { |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 525 | if (!(vcpu->arch.vpa.update_pending || |
| 526 | vcpu->arch.slb_shadow.update_pending || |
| 527 | vcpu->arch.dtl.update_pending)) |
| 528 | return; |
| 529 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 530 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 531 | if (vcpu->arch.vpa.update_pending) { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 532 | kvmppc_update_vpa(vcpu, &vcpu->arch.vpa); |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 533 | if (vcpu->arch.vpa.pinned_addr) |
| 534 | init_vpa(vcpu, vcpu->arch.vpa.pinned_addr); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 535 | } |
| 536 | if (vcpu->arch.dtl.update_pending) { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 537 | kvmppc_update_vpa(vcpu, &vcpu->arch.dtl); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 538 | vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr; |
| 539 | vcpu->arch.dtl_index = 0; |
| 540 | } |
| 541 | if (vcpu->arch.slb_shadow.update_pending) |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 542 | kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 543 | spin_unlock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 544 | } |
| 545 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 546 | /* |
| 547 | * Return the accumulated stolen time for the vcore up until `now'. |
| 548 | * The caller should hold the vcore lock. |
| 549 | */ |
| 550 | static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now) |
| 551 | { |
| 552 | u64 p; |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 553 | unsigned long flags; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 554 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 555 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 556 | p = vc->stolen_tb; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 557 | if (vc->vcore_state != VCORE_INACTIVE && |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 558 | vc->preempt_tb != TB_NIL) |
| 559 | p += now - vc->preempt_tb; |
| 560 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 561 | return p; |
| 562 | } |
| 563 | |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 564 | static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, |
| 565 | struct kvmppc_vcore *vc) |
| 566 | { |
| 567 | struct dtl_entry *dt; |
| 568 | struct lppaca *vpa; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 569 | unsigned long stolen; |
| 570 | unsigned long core_stolen; |
| 571 | u64 now; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 572 | |
| 573 | dt = vcpu->arch.dtl_ptr; |
| 574 | vpa = vcpu->arch.vpa.pinned_addr; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 575 | now = mftb(); |
| 576 | core_stolen = vcore_stolen_time(vc, now); |
| 577 | stolen = core_stolen - vcpu->arch.stolen_logged; |
| 578 | vcpu->arch.stolen_logged = core_stolen; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 579 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 580 | stolen += vcpu->arch.busy_stolen; |
| 581 | vcpu->arch.busy_stolen = 0; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 582 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 583 | if (!dt || !vpa) |
| 584 | return; |
| 585 | memset(dt, 0, sizeof(struct dtl_entry)); |
| 586 | dt->dispatch_reason = 7; |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 587 | dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid); |
| 588 | dt->timebase = cpu_to_be64(now + vc->tb_offset); |
| 589 | dt->enqueue_to_dispatch_time = cpu_to_be32(stolen); |
| 590 | dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu)); |
| 591 | dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 592 | ++dt; |
| 593 | if (dt == vcpu->arch.dtl.pinned_end) |
| 594 | dt = vcpu->arch.dtl.pinned_addr; |
| 595 | vcpu->arch.dtl_ptr = dt; |
| 596 | /* order writing *dt vs. writing vpa->dtl_idx */ |
| 597 | smp_wmb(); |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 598 | vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 599 | vcpu->arch.dtl.dirty = true; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 602 | static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu) |
| 603 | { |
| 604 | if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207) |
| 605 | return true; |
| 606 | if ((!vcpu->arch.vcore->arch_compat) && |
| 607 | cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 608 | return true; |
| 609 | return false; |
| 610 | } |
| 611 | |
| 612 | static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags, |
| 613 | unsigned long resource, unsigned long value1, |
| 614 | unsigned long value2) |
| 615 | { |
| 616 | switch (resource) { |
| 617 | case H_SET_MODE_RESOURCE_SET_CIABR: |
| 618 | if (!kvmppc_power8_compatible(vcpu)) |
| 619 | return H_P2; |
| 620 | if (value2) |
| 621 | return H_P4; |
| 622 | if (mflags) |
| 623 | return H_UNSUPPORTED_FLAG_START; |
| 624 | /* Guests can't breakpoint the hypervisor */ |
| 625 | if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER) |
| 626 | return H_P3; |
| 627 | vcpu->arch.ciabr = value1; |
| 628 | return H_SUCCESS; |
| 629 | case H_SET_MODE_RESOURCE_SET_DAWR: |
| 630 | if (!kvmppc_power8_compatible(vcpu)) |
| 631 | return H_P2; |
| 632 | if (mflags) |
| 633 | return H_UNSUPPORTED_FLAG_START; |
| 634 | if (value2 & DABRX_HYP) |
| 635 | return H_P4; |
| 636 | vcpu->arch.dawr = value1; |
| 637 | vcpu->arch.dawrx = value2; |
| 638 | return H_SUCCESS; |
| 639 | default: |
| 640 | return H_TOO_HARD; |
| 641 | } |
| 642 | } |
| 643 | |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 644 | static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target) |
| 645 | { |
| 646 | struct kvmppc_vcore *vcore = target->arch.vcore; |
| 647 | |
| 648 | /* |
| 649 | * We expect to have been called by the real mode handler |
| 650 | * (kvmppc_rm_h_confer()) which would have directly returned |
| 651 | * H_SUCCESS if the source vcore wasn't idle (e.g. if it may |
| 652 | * have useful work to do and should not confer) so we don't |
| 653 | * recheck that here. |
| 654 | */ |
| 655 | |
| 656 | spin_lock(&vcore->lock); |
| 657 | if (target->arch.state == KVMPPC_VCPU_RUNNABLE && |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 658 | vcore->vcore_state != VCORE_INACTIVE && |
| 659 | vcore->runner) |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 660 | target = vcore->runner; |
| 661 | spin_unlock(&vcore->lock); |
| 662 | |
| 663 | return kvm_vcpu_yield_to(target); |
| 664 | } |
| 665 | |
| 666 | static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu) |
| 667 | { |
| 668 | int yield_count = 0; |
| 669 | struct lppaca *lppaca; |
| 670 | |
| 671 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 672 | lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr; |
| 673 | if (lppaca) |
Paul Mackerras | ecb6d61 | 2015-03-20 20:39:39 +1100 | [diff] [blame] | 674 | yield_count = be32_to_cpu(lppaca->yield_count); |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 675 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 676 | return yield_count; |
| 677 | } |
| 678 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 679 | int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) |
| 680 | { |
| 681 | unsigned long req = kvmppc_get_gpr(vcpu, 3); |
| 682 | unsigned long target, ret = H_SUCCESS; |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 683 | int yield_count; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 684 | struct kvm_vcpu *tvcpu; |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 685 | int idx, rc; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 686 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 687 | if (req <= MAX_HCALL_OPCODE && |
| 688 | !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls)) |
| 689 | return RESUME_HOST; |
| 690 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 691 | switch (req) { |
| 692 | case H_CEDE: |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 693 | break; |
| 694 | case H_PROD: |
| 695 | target = kvmppc_get_gpr(vcpu, 4); |
| 696 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); |
| 697 | if (!tvcpu) { |
| 698 | ret = H_PARAMETER; |
| 699 | break; |
| 700 | } |
| 701 | tvcpu->arch.prodded = 1; |
| 702 | smp_mb(); |
| 703 | if (vcpu->arch.ceded) { |
| 704 | if (waitqueue_active(&vcpu->wq)) { |
| 705 | wake_up_interruptible(&vcpu->wq); |
| 706 | vcpu->stat.halt_wakeup++; |
| 707 | } |
| 708 | } |
| 709 | break; |
| 710 | case H_CONFER: |
Paul Mackerras | 42d7604 | 2013-09-06 13:23:21 +1000 | [diff] [blame] | 711 | target = kvmppc_get_gpr(vcpu, 4); |
| 712 | if (target == -1) |
| 713 | break; |
| 714 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); |
| 715 | if (!tvcpu) { |
| 716 | ret = H_PARAMETER; |
| 717 | break; |
| 718 | } |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 719 | yield_count = kvmppc_get_gpr(vcpu, 5); |
| 720 | if (kvmppc_get_yield_count(tvcpu) != yield_count) |
| 721 | break; |
| 722 | kvm_arch_vcpu_yield_to(tvcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 723 | break; |
| 724 | case H_REGISTER_VPA: |
| 725 | ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 726 | kvmppc_get_gpr(vcpu, 5), |
| 727 | kvmppc_get_gpr(vcpu, 6)); |
| 728 | break; |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 729 | case H_RTAS: |
| 730 | if (list_empty(&vcpu->kvm->arch.rtas_tokens)) |
| 731 | return RESUME_HOST; |
| 732 | |
Paul Mackerras | c943809 | 2013-11-16 17:46:05 +1100 | [diff] [blame] | 733 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 734 | rc = kvmppc_rtas_hcall(vcpu); |
Paul Mackerras | c943809 | 2013-11-16 17:46:05 +1100 | [diff] [blame] | 735 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 736 | |
| 737 | if (rc == -ENOENT) |
| 738 | return RESUME_HOST; |
| 739 | else if (rc == 0) |
| 740 | break; |
| 741 | |
| 742 | /* Send the error out to userspace via KVM_RUN */ |
| 743 | return rc; |
David Gibson | 99342cf8 | 2015-02-05 11:53:25 +1100 | [diff] [blame] | 744 | case H_LOGICAL_CI_LOAD: |
| 745 | ret = kvmppc_h_logical_ci_load(vcpu); |
| 746 | if (ret == H_TOO_HARD) |
| 747 | return RESUME_HOST; |
| 748 | break; |
| 749 | case H_LOGICAL_CI_STORE: |
| 750 | ret = kvmppc_h_logical_ci_store(vcpu); |
| 751 | if (ret == H_TOO_HARD) |
| 752 | return RESUME_HOST; |
| 753 | break; |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 754 | case H_SET_MODE: |
| 755 | ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 756 | kvmppc_get_gpr(vcpu, 5), |
| 757 | kvmppc_get_gpr(vcpu, 6), |
| 758 | kvmppc_get_gpr(vcpu, 7)); |
| 759 | if (ret == H_TOO_HARD) |
| 760 | return RESUME_HOST; |
| 761 | break; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 762 | case H_XIRR: |
| 763 | case H_CPPR: |
| 764 | case H_EOI: |
| 765 | case H_IPI: |
Paul Mackerras | 8e44ddc | 2013-05-23 15:42:21 +0000 | [diff] [blame] | 766 | case H_IPOLL: |
| 767 | case H_XIRR_X: |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 768 | if (kvmppc_xics_enabled(vcpu)) { |
| 769 | ret = kvmppc_xics_hcall(vcpu, req); |
| 770 | break; |
| 771 | } /* fallthrough */ |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 772 | default: |
| 773 | return RESUME_HOST; |
| 774 | } |
| 775 | kvmppc_set_gpr(vcpu, 3, ret); |
| 776 | vcpu->arch.hcall_needed = 0; |
| 777 | return RESUME_GUEST; |
| 778 | } |
| 779 | |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 780 | static int kvmppc_hcall_impl_hv(unsigned long cmd) |
| 781 | { |
| 782 | switch (cmd) { |
| 783 | case H_CEDE: |
| 784 | case H_PROD: |
| 785 | case H_CONFER: |
| 786 | case H_REGISTER_VPA: |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 787 | case H_SET_MODE: |
David Gibson | 99342cf8 | 2015-02-05 11:53:25 +1100 | [diff] [blame] | 788 | case H_LOGICAL_CI_LOAD: |
| 789 | case H_LOGICAL_CI_STORE: |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 790 | #ifdef CONFIG_KVM_XICS |
| 791 | case H_XIRR: |
| 792 | case H_CPPR: |
| 793 | case H_EOI: |
| 794 | case H_IPI: |
| 795 | case H_IPOLL: |
| 796 | case H_XIRR_X: |
| 797 | #endif |
| 798 | return 1; |
| 799 | } |
| 800 | |
| 801 | /* See if it's in the real-mode table */ |
| 802 | return kvmppc_hcall_impl_hv_realmode(cmd); |
| 803 | } |
| 804 | |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 805 | static int kvmppc_emulate_debug_inst(struct kvm_run *run, |
| 806 | struct kvm_vcpu *vcpu) |
| 807 | { |
| 808 | u32 last_inst; |
| 809 | |
| 810 | if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) != |
| 811 | EMULATE_DONE) { |
| 812 | /* |
| 813 | * Fetch failed, so return to guest and |
| 814 | * try executing it again. |
| 815 | */ |
| 816 | return RESUME_GUEST; |
| 817 | } |
| 818 | |
| 819 | if (last_inst == KVMPPC_INST_SW_BREAKPOINT) { |
| 820 | run->exit_reason = KVM_EXIT_DEBUG; |
| 821 | run->debug.arch.address = kvmppc_get_pc(vcpu); |
| 822 | return RESUME_HOST; |
| 823 | } else { |
| 824 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 825 | return RESUME_GUEST; |
| 826 | } |
| 827 | } |
| 828 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 829 | static int kvmppc_handle_exit_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 830 | struct task_struct *tsk) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 831 | { |
| 832 | int r = RESUME_HOST; |
| 833 | |
| 834 | vcpu->stat.sum_exits++; |
| 835 | |
| 836 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 837 | run->ready_for_interrupt_injection = 1; |
| 838 | switch (vcpu->arch.trap) { |
| 839 | /* We're good on these - the host merely wanted to get our attention */ |
| 840 | case BOOK3S_INTERRUPT_HV_DECREMENTER: |
| 841 | vcpu->stat.dec_exits++; |
| 842 | r = RESUME_GUEST; |
| 843 | break; |
| 844 | case BOOK3S_INTERRUPT_EXTERNAL: |
Paul Mackerras | 5d00f66 | 2014-01-08 21:25:28 +1100 | [diff] [blame] | 845 | case BOOK3S_INTERRUPT_H_DOORBELL: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 846 | vcpu->stat.ext_intr_exits++; |
| 847 | r = RESUME_GUEST; |
| 848 | break; |
Mahesh Salgaonkar | dee6f24 | 2014-11-03 15:51:57 +1100 | [diff] [blame] | 849 | /* HMI is hypervisor interrupt and host has handled it. Resume guest.*/ |
| 850 | case BOOK3S_INTERRUPT_HMI: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 851 | case BOOK3S_INTERRUPT_PERFMON: |
| 852 | r = RESUME_GUEST; |
| 853 | break; |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 854 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
| 855 | /* |
| 856 | * Deliver a machine check interrupt to the guest. |
| 857 | * We have to do this, even if the host has handled the |
| 858 | * machine check, because machine checks use SRR0/1 and |
| 859 | * the interrupt might have trashed guest state in them. |
| 860 | */ |
| 861 | kvmppc_book3s_queue_irqprio(vcpu, |
| 862 | BOOK3S_INTERRUPT_MACHINE_CHECK); |
| 863 | r = RESUME_GUEST; |
| 864 | break; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 865 | case BOOK3S_INTERRUPT_PROGRAM: |
| 866 | { |
| 867 | ulong flags; |
| 868 | /* |
| 869 | * Normally program interrupts are delivered directly |
| 870 | * to the guest by the hardware, but we can get here |
| 871 | * as a result of a hypervisor emulation interrupt |
| 872 | * (e40) getting turned into a 700 by BML RTAS. |
| 873 | */ |
| 874 | flags = vcpu->arch.shregs.msr & 0x1f0000ull; |
| 875 | kvmppc_core_queue_program(vcpu, flags); |
| 876 | r = RESUME_GUEST; |
| 877 | break; |
| 878 | } |
| 879 | case BOOK3S_INTERRUPT_SYSCALL: |
| 880 | { |
| 881 | /* hcall - punt to userspace */ |
| 882 | int i; |
| 883 | |
Liu Ping Fan | 27025a6 | 2013-11-19 14:12:48 +0800 | [diff] [blame] | 884 | /* hypercall with MSR_PR has already been handled in rmode, |
| 885 | * and never reaches here. |
| 886 | */ |
| 887 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 888 | run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); |
| 889 | for (i = 0; i < 9; ++i) |
| 890 | run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); |
| 891 | run->exit_reason = KVM_EXIT_PAPR_HCALL; |
| 892 | vcpu->arch.hcall_needed = 1; |
| 893 | r = RESUME_HOST; |
| 894 | break; |
| 895 | } |
| 896 | /* |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 897 | * We get these next two if the guest accesses a page which it thinks |
| 898 | * it has mapped but which is not actually present, either because |
| 899 | * it is for an emulated I/O device or because the corresonding |
| 900 | * host page has been paged out. Any other HDSI/HISI interrupts |
| 901 | * have been handled already. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 902 | */ |
| 903 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 904 | r = RESUME_PAGE_FAULT; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 905 | break; |
| 906 | case BOOK3S_INTERRUPT_H_INST_STORAGE: |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 907 | vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); |
| 908 | vcpu->arch.fault_dsisr = 0; |
| 909 | r = RESUME_PAGE_FAULT; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 910 | break; |
| 911 | /* |
| 912 | * This occurs if the guest executes an illegal instruction. |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 913 | * If the guest debug is disabled, generate a program interrupt |
| 914 | * to the guest. If guest debug is enabled, we need to check |
| 915 | * whether the instruction is a software breakpoint instruction. |
| 916 | * Accordingly return to Guest or Host. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 917 | */ |
| 918 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: |
Paul Mackerras | 4a157d6 | 2014-12-03 13:30:39 +1100 | [diff] [blame] | 919 | if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED) |
| 920 | vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ? |
| 921 | swab32(vcpu->arch.emul_inst) : |
| 922 | vcpu->arch.emul_inst; |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 923 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) { |
| 924 | r = kvmppc_emulate_debug_inst(run, vcpu); |
| 925 | } else { |
| 926 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 927 | r = RESUME_GUEST; |
| 928 | } |
Michael Ellerman | bd3048b | 2014-01-08 21:25:23 +1100 | [diff] [blame] | 929 | break; |
| 930 | /* |
| 931 | * This occurs if the guest (kernel or userspace), does something that |
| 932 | * is prohibited by HFSCR. We just generate a program interrupt to |
| 933 | * the guest. |
| 934 | */ |
| 935 | case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: |
| 936 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 937 | r = RESUME_GUEST; |
| 938 | break; |
| 939 | default: |
| 940 | kvmppc_dump_regs(vcpu); |
| 941 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", |
| 942 | vcpu->arch.trap, kvmppc_get_pc(vcpu), |
| 943 | vcpu->arch.shregs.msr); |
Paul Mackerras | f3271d4 | 2013-09-20 14:52:41 +1000 | [diff] [blame] | 944 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 945 | r = RESUME_HOST; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 946 | break; |
| 947 | } |
| 948 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 949 | return r; |
| 950 | } |
| 951 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 952 | static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu, |
| 953 | struct kvm_sregs *sregs) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 954 | { |
| 955 | int i; |
| 956 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 957 | memset(sregs, 0, sizeof(struct kvm_sregs)); |
Aneesh Kumar K.V | 8791644 | 2013-08-22 17:08:39 +0530 | [diff] [blame] | 958 | sregs->pvr = vcpu->arch.pvr; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 959 | for (i = 0; i < vcpu->arch.slb_max; i++) { |
| 960 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; |
| 961 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; |
| 962 | } |
| 963 | |
| 964 | return 0; |
| 965 | } |
| 966 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 967 | static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu, |
| 968 | struct kvm_sregs *sregs) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 969 | { |
| 970 | int i, j; |
| 971 | |
Paul Mackerras | 9333e6c | 2014-09-02 16:14:43 +1000 | [diff] [blame] | 972 | /* Only accept the same PVR as the host's, since we can't spoof it */ |
| 973 | if (sregs->pvr != vcpu->arch.pvr) |
| 974 | return -EINVAL; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 975 | |
| 976 | j = 0; |
| 977 | for (i = 0; i < vcpu->arch.slb_nr; i++) { |
| 978 | if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { |
| 979 | vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; |
| 980 | vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; |
| 981 | ++j; |
| 982 | } |
| 983 | } |
| 984 | vcpu->arch.slb_max = j; |
| 985 | |
| 986 | return 0; |
| 987 | } |
| 988 | |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 989 | static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr, |
| 990 | bool preserve_top32) |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 991 | { |
Paul Mackerras | 8f902b0 | 2015-03-20 20:39:38 +1100 | [diff] [blame] | 992 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 993 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 994 | u64 mask; |
| 995 | |
Paul Mackerras | 8f902b0 | 2015-03-20 20:39:38 +1100 | [diff] [blame] | 996 | mutex_lock(&kvm->lock); |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 997 | spin_lock(&vc->lock); |
| 998 | /* |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 999 | * If ILE (interrupt little-endian) has changed, update the |
| 1000 | * MSR_LE bit in the intr_msr for each vcpu in this vcore. |
| 1001 | */ |
| 1002 | if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) { |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1003 | struct kvm_vcpu *vcpu; |
| 1004 | int i; |
| 1005 | |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1006 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1007 | if (vcpu->arch.vcore != vc) |
| 1008 | continue; |
| 1009 | if (new_lpcr & LPCR_ILE) |
| 1010 | vcpu->arch.intr_msr |= MSR_LE; |
| 1011 | else |
| 1012 | vcpu->arch.intr_msr &= ~MSR_LE; |
| 1013 | } |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | /* |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1017 | * Userspace can only modify DPFD (default prefetch depth), |
| 1018 | * ILE (interrupt little-endian) and TC (translation control). |
Paul Mackerras | e0622bd | 2014-01-08 21:25:27 +1100 | [diff] [blame] | 1019 | * On POWER8 userspace can also modify AIL (alt. interrupt loc.) |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1020 | */ |
| 1021 | mask = LPCR_DPFD | LPCR_ILE | LPCR_TC; |
Paul Mackerras | e0622bd | 2014-01-08 21:25:27 +1100 | [diff] [blame] | 1022 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 1023 | mask |= LPCR_AIL; |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1024 | |
| 1025 | /* Broken 32-bit version of LPCR must not clear top bits */ |
| 1026 | if (preserve_top32) |
| 1027 | mask &= 0xFFFFFFFF; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1028 | vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask); |
| 1029 | spin_unlock(&vc->lock); |
Paul Mackerras | 8f902b0 | 2015-03-20 20:39:38 +1100 | [diff] [blame] | 1030 | mutex_unlock(&kvm->lock); |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1031 | } |
| 1032 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1033 | static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
| 1034 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1035 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1036 | int r = 0; |
| 1037 | long int i; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1038 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1039 | switch (id) { |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1040 | case KVM_REG_PPC_DEBUG_INST: |
| 1041 | *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT); |
| 1042 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1043 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1044 | *val = get_reg_val(id, 0); |
| 1045 | break; |
| 1046 | case KVM_REG_PPC_DABR: |
| 1047 | *val = get_reg_val(id, vcpu->arch.dabr); |
| 1048 | break; |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 1049 | case KVM_REG_PPC_DABRX: |
| 1050 | *val = get_reg_val(id, vcpu->arch.dabrx); |
| 1051 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1052 | case KVM_REG_PPC_DSCR: |
| 1053 | *val = get_reg_val(id, vcpu->arch.dscr); |
| 1054 | break; |
| 1055 | case KVM_REG_PPC_PURR: |
| 1056 | *val = get_reg_val(id, vcpu->arch.purr); |
| 1057 | break; |
| 1058 | case KVM_REG_PPC_SPURR: |
| 1059 | *val = get_reg_val(id, vcpu->arch.spurr); |
| 1060 | break; |
| 1061 | case KVM_REG_PPC_AMR: |
| 1062 | *val = get_reg_val(id, vcpu->arch.amr); |
| 1063 | break; |
| 1064 | case KVM_REG_PPC_UAMOR: |
| 1065 | *val = get_reg_val(id, vcpu->arch.uamor); |
| 1066 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1067 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1068 | i = id - KVM_REG_PPC_MMCR0; |
| 1069 | *val = get_reg_val(id, vcpu->arch.mmcr[i]); |
| 1070 | break; |
| 1071 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: |
| 1072 | i = id - KVM_REG_PPC_PMC1; |
| 1073 | *val = get_reg_val(id, vcpu->arch.pmc[i]); |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1074 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1075 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
| 1076 | i = id - KVM_REG_PPC_SPMC1; |
| 1077 | *val = get_reg_val(id, vcpu->arch.spmc[i]); |
| 1078 | break; |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 1079 | case KVM_REG_PPC_SIAR: |
| 1080 | *val = get_reg_val(id, vcpu->arch.siar); |
| 1081 | break; |
| 1082 | case KVM_REG_PPC_SDAR: |
| 1083 | *val = get_reg_val(id, vcpu->arch.sdar); |
| 1084 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1085 | case KVM_REG_PPC_SIER: |
| 1086 | *val = get_reg_val(id, vcpu->arch.sier); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1087 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1088 | case KVM_REG_PPC_IAMR: |
| 1089 | *val = get_reg_val(id, vcpu->arch.iamr); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1090 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1091 | case KVM_REG_PPC_PSPB: |
| 1092 | *val = get_reg_val(id, vcpu->arch.pspb); |
| 1093 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1094 | case KVM_REG_PPC_DPDES: |
| 1095 | *val = get_reg_val(id, vcpu->arch.vcore->dpdes); |
| 1096 | break; |
| 1097 | case KVM_REG_PPC_DAWR: |
| 1098 | *val = get_reg_val(id, vcpu->arch.dawr); |
| 1099 | break; |
| 1100 | case KVM_REG_PPC_DAWRX: |
| 1101 | *val = get_reg_val(id, vcpu->arch.dawrx); |
| 1102 | break; |
| 1103 | case KVM_REG_PPC_CIABR: |
| 1104 | *val = get_reg_val(id, vcpu->arch.ciabr); |
| 1105 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1106 | case KVM_REG_PPC_CSIGR: |
| 1107 | *val = get_reg_val(id, vcpu->arch.csigr); |
| 1108 | break; |
| 1109 | case KVM_REG_PPC_TACR: |
| 1110 | *val = get_reg_val(id, vcpu->arch.tacr); |
| 1111 | break; |
| 1112 | case KVM_REG_PPC_TCSCR: |
| 1113 | *val = get_reg_val(id, vcpu->arch.tcscr); |
| 1114 | break; |
| 1115 | case KVM_REG_PPC_PID: |
| 1116 | *val = get_reg_val(id, vcpu->arch.pid); |
| 1117 | break; |
| 1118 | case KVM_REG_PPC_ACOP: |
| 1119 | *val = get_reg_val(id, vcpu->arch.acop); |
| 1120 | break; |
| 1121 | case KVM_REG_PPC_WORT: |
| 1122 | *val = get_reg_val(id, vcpu->arch.wort); |
| 1123 | break; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1124 | case KVM_REG_PPC_VPA_ADDR: |
| 1125 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1126 | *val = get_reg_val(id, vcpu->arch.vpa.next_gpa); |
| 1127 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1128 | break; |
| 1129 | case KVM_REG_PPC_VPA_SLB: |
| 1130 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1131 | val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa; |
| 1132 | val->vpaval.length = vcpu->arch.slb_shadow.len; |
| 1133 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1134 | break; |
| 1135 | case KVM_REG_PPC_VPA_DTL: |
| 1136 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1137 | val->vpaval.addr = vcpu->arch.dtl.next_gpa; |
| 1138 | val->vpaval.length = vcpu->arch.dtl.len; |
| 1139 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1140 | break; |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 1141 | case KVM_REG_PPC_TB_OFFSET: |
| 1142 | *val = get_reg_val(id, vcpu->arch.vcore->tb_offset); |
| 1143 | break; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1144 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1145 | case KVM_REG_PPC_LPCR_64: |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1146 | *val = get_reg_val(id, vcpu->arch.vcore->lpcr); |
| 1147 | break; |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1148 | case KVM_REG_PPC_PPR: |
| 1149 | *val = get_reg_val(id, vcpu->arch.ppr); |
| 1150 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 1151 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1152 | case KVM_REG_PPC_TFHAR: |
| 1153 | *val = get_reg_val(id, vcpu->arch.tfhar); |
| 1154 | break; |
| 1155 | case KVM_REG_PPC_TFIAR: |
| 1156 | *val = get_reg_val(id, vcpu->arch.tfiar); |
| 1157 | break; |
| 1158 | case KVM_REG_PPC_TEXASR: |
| 1159 | *val = get_reg_val(id, vcpu->arch.texasr); |
| 1160 | break; |
| 1161 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 1162 | i = id - KVM_REG_PPC_TM_GPR0; |
| 1163 | *val = get_reg_val(id, vcpu->arch.gpr_tm[i]); |
| 1164 | break; |
| 1165 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 1166 | { |
| 1167 | int j; |
| 1168 | i = id - KVM_REG_PPC_TM_VSR0; |
| 1169 | if (i < 32) |
| 1170 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 1171 | val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j]; |
| 1172 | else { |
| 1173 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1174 | val->vval = vcpu->arch.vr_tm.vr[i-32]; |
| 1175 | else |
| 1176 | r = -ENXIO; |
| 1177 | } |
| 1178 | break; |
| 1179 | } |
| 1180 | case KVM_REG_PPC_TM_CR: |
| 1181 | *val = get_reg_val(id, vcpu->arch.cr_tm); |
| 1182 | break; |
| 1183 | case KVM_REG_PPC_TM_LR: |
| 1184 | *val = get_reg_val(id, vcpu->arch.lr_tm); |
| 1185 | break; |
| 1186 | case KVM_REG_PPC_TM_CTR: |
| 1187 | *val = get_reg_val(id, vcpu->arch.ctr_tm); |
| 1188 | break; |
| 1189 | case KVM_REG_PPC_TM_FPSCR: |
| 1190 | *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr); |
| 1191 | break; |
| 1192 | case KVM_REG_PPC_TM_AMR: |
| 1193 | *val = get_reg_val(id, vcpu->arch.amr_tm); |
| 1194 | break; |
| 1195 | case KVM_REG_PPC_TM_PPR: |
| 1196 | *val = get_reg_val(id, vcpu->arch.ppr_tm); |
| 1197 | break; |
| 1198 | case KVM_REG_PPC_TM_VRSAVE: |
| 1199 | *val = get_reg_val(id, vcpu->arch.vrsave_tm); |
| 1200 | break; |
| 1201 | case KVM_REG_PPC_TM_VSCR: |
| 1202 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1203 | *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]); |
| 1204 | else |
| 1205 | r = -ENXIO; |
| 1206 | break; |
| 1207 | case KVM_REG_PPC_TM_DSCR: |
| 1208 | *val = get_reg_val(id, vcpu->arch.dscr_tm); |
| 1209 | break; |
| 1210 | case KVM_REG_PPC_TM_TAR: |
| 1211 | *val = get_reg_val(id, vcpu->arch.tar_tm); |
| 1212 | break; |
| 1213 | #endif |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 1214 | case KVM_REG_PPC_ARCH_COMPAT: |
| 1215 | *val = get_reg_val(id, vcpu->arch.vcore->arch_compat); |
| 1216 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1217 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1218 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1219 | break; |
| 1220 | } |
| 1221 | |
| 1222 | return r; |
| 1223 | } |
| 1224 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1225 | static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
| 1226 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1227 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1228 | int r = 0; |
| 1229 | long int i; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1230 | unsigned long addr, len; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1231 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1232 | switch (id) { |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1233 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1234 | /* Only allow this to be set to zero */ |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1235 | if (set_reg_val(id, *val)) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1236 | r = -EINVAL; |
| 1237 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1238 | case KVM_REG_PPC_DABR: |
| 1239 | vcpu->arch.dabr = set_reg_val(id, *val); |
| 1240 | break; |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 1241 | case KVM_REG_PPC_DABRX: |
| 1242 | vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP; |
| 1243 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1244 | case KVM_REG_PPC_DSCR: |
| 1245 | vcpu->arch.dscr = set_reg_val(id, *val); |
| 1246 | break; |
| 1247 | case KVM_REG_PPC_PURR: |
| 1248 | vcpu->arch.purr = set_reg_val(id, *val); |
| 1249 | break; |
| 1250 | case KVM_REG_PPC_SPURR: |
| 1251 | vcpu->arch.spurr = set_reg_val(id, *val); |
| 1252 | break; |
| 1253 | case KVM_REG_PPC_AMR: |
| 1254 | vcpu->arch.amr = set_reg_val(id, *val); |
| 1255 | break; |
| 1256 | case KVM_REG_PPC_UAMOR: |
| 1257 | vcpu->arch.uamor = set_reg_val(id, *val); |
| 1258 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1259 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCRS: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1260 | i = id - KVM_REG_PPC_MMCR0; |
| 1261 | vcpu->arch.mmcr[i] = set_reg_val(id, *val); |
| 1262 | break; |
| 1263 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: |
| 1264 | i = id - KVM_REG_PPC_PMC1; |
| 1265 | vcpu->arch.pmc[i] = set_reg_val(id, *val); |
| 1266 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1267 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
| 1268 | i = id - KVM_REG_PPC_SPMC1; |
| 1269 | vcpu->arch.spmc[i] = set_reg_val(id, *val); |
| 1270 | break; |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 1271 | case KVM_REG_PPC_SIAR: |
| 1272 | vcpu->arch.siar = set_reg_val(id, *val); |
| 1273 | break; |
| 1274 | case KVM_REG_PPC_SDAR: |
| 1275 | vcpu->arch.sdar = set_reg_val(id, *val); |
| 1276 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1277 | case KVM_REG_PPC_SIER: |
| 1278 | vcpu->arch.sier = set_reg_val(id, *val); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1279 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1280 | case KVM_REG_PPC_IAMR: |
| 1281 | vcpu->arch.iamr = set_reg_val(id, *val); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1282 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1283 | case KVM_REG_PPC_PSPB: |
| 1284 | vcpu->arch.pspb = set_reg_val(id, *val); |
| 1285 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1286 | case KVM_REG_PPC_DPDES: |
| 1287 | vcpu->arch.vcore->dpdes = set_reg_val(id, *val); |
| 1288 | break; |
| 1289 | case KVM_REG_PPC_DAWR: |
| 1290 | vcpu->arch.dawr = set_reg_val(id, *val); |
| 1291 | break; |
| 1292 | case KVM_REG_PPC_DAWRX: |
| 1293 | vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP; |
| 1294 | break; |
| 1295 | case KVM_REG_PPC_CIABR: |
| 1296 | vcpu->arch.ciabr = set_reg_val(id, *val); |
| 1297 | /* Don't allow setting breakpoints in hypervisor code */ |
| 1298 | if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER) |
| 1299 | vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */ |
| 1300 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1301 | case KVM_REG_PPC_CSIGR: |
| 1302 | vcpu->arch.csigr = set_reg_val(id, *val); |
| 1303 | break; |
| 1304 | case KVM_REG_PPC_TACR: |
| 1305 | vcpu->arch.tacr = set_reg_val(id, *val); |
| 1306 | break; |
| 1307 | case KVM_REG_PPC_TCSCR: |
| 1308 | vcpu->arch.tcscr = set_reg_val(id, *val); |
| 1309 | break; |
| 1310 | case KVM_REG_PPC_PID: |
| 1311 | vcpu->arch.pid = set_reg_val(id, *val); |
| 1312 | break; |
| 1313 | case KVM_REG_PPC_ACOP: |
| 1314 | vcpu->arch.acop = set_reg_val(id, *val); |
| 1315 | break; |
| 1316 | case KVM_REG_PPC_WORT: |
| 1317 | vcpu->arch.wort = set_reg_val(id, *val); |
| 1318 | break; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1319 | case KVM_REG_PPC_VPA_ADDR: |
| 1320 | addr = set_reg_val(id, *val); |
| 1321 | r = -EINVAL; |
| 1322 | if (!addr && (vcpu->arch.slb_shadow.next_gpa || |
| 1323 | vcpu->arch.dtl.next_gpa)) |
| 1324 | break; |
| 1325 | r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca)); |
| 1326 | break; |
| 1327 | case KVM_REG_PPC_VPA_SLB: |
| 1328 | addr = val->vpaval.addr; |
| 1329 | len = val->vpaval.length; |
| 1330 | r = -EINVAL; |
| 1331 | if (addr && !vcpu->arch.vpa.next_gpa) |
| 1332 | break; |
| 1333 | r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len); |
| 1334 | break; |
| 1335 | case KVM_REG_PPC_VPA_DTL: |
| 1336 | addr = val->vpaval.addr; |
| 1337 | len = val->vpaval.length; |
| 1338 | r = -EINVAL; |
Paul Mackerras | 9f8c8c7 | 2012-10-15 01:18:37 +0000 | [diff] [blame] | 1339 | if (addr && (len < sizeof(struct dtl_entry) || |
| 1340 | !vcpu->arch.vpa.next_gpa)) |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1341 | break; |
| 1342 | len -= len % sizeof(struct dtl_entry); |
| 1343 | r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len); |
| 1344 | break; |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 1345 | case KVM_REG_PPC_TB_OFFSET: |
| 1346 | /* round up to multiple of 2^24 */ |
| 1347 | vcpu->arch.vcore->tb_offset = |
| 1348 | ALIGN(set_reg_val(id, *val), 1UL << 24); |
| 1349 | break; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1350 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1351 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true); |
| 1352 | break; |
| 1353 | case KVM_REG_PPC_LPCR_64: |
| 1354 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false); |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1355 | break; |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1356 | case KVM_REG_PPC_PPR: |
| 1357 | vcpu->arch.ppr = set_reg_val(id, *val); |
| 1358 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 1359 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1360 | case KVM_REG_PPC_TFHAR: |
| 1361 | vcpu->arch.tfhar = set_reg_val(id, *val); |
| 1362 | break; |
| 1363 | case KVM_REG_PPC_TFIAR: |
| 1364 | vcpu->arch.tfiar = set_reg_val(id, *val); |
| 1365 | break; |
| 1366 | case KVM_REG_PPC_TEXASR: |
| 1367 | vcpu->arch.texasr = set_reg_val(id, *val); |
| 1368 | break; |
| 1369 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 1370 | i = id - KVM_REG_PPC_TM_GPR0; |
| 1371 | vcpu->arch.gpr_tm[i] = set_reg_val(id, *val); |
| 1372 | break; |
| 1373 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 1374 | { |
| 1375 | int j; |
| 1376 | i = id - KVM_REG_PPC_TM_VSR0; |
| 1377 | if (i < 32) |
| 1378 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 1379 | vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j]; |
| 1380 | else |
| 1381 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1382 | vcpu->arch.vr_tm.vr[i-32] = val->vval; |
| 1383 | else |
| 1384 | r = -ENXIO; |
| 1385 | break; |
| 1386 | } |
| 1387 | case KVM_REG_PPC_TM_CR: |
| 1388 | vcpu->arch.cr_tm = set_reg_val(id, *val); |
| 1389 | break; |
| 1390 | case KVM_REG_PPC_TM_LR: |
| 1391 | vcpu->arch.lr_tm = set_reg_val(id, *val); |
| 1392 | break; |
| 1393 | case KVM_REG_PPC_TM_CTR: |
| 1394 | vcpu->arch.ctr_tm = set_reg_val(id, *val); |
| 1395 | break; |
| 1396 | case KVM_REG_PPC_TM_FPSCR: |
| 1397 | vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val); |
| 1398 | break; |
| 1399 | case KVM_REG_PPC_TM_AMR: |
| 1400 | vcpu->arch.amr_tm = set_reg_val(id, *val); |
| 1401 | break; |
| 1402 | case KVM_REG_PPC_TM_PPR: |
| 1403 | vcpu->arch.ppr_tm = set_reg_val(id, *val); |
| 1404 | break; |
| 1405 | case KVM_REG_PPC_TM_VRSAVE: |
| 1406 | vcpu->arch.vrsave_tm = set_reg_val(id, *val); |
| 1407 | break; |
| 1408 | case KVM_REG_PPC_TM_VSCR: |
| 1409 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1410 | vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val); |
| 1411 | else |
| 1412 | r = - ENXIO; |
| 1413 | break; |
| 1414 | case KVM_REG_PPC_TM_DSCR: |
| 1415 | vcpu->arch.dscr_tm = set_reg_val(id, *val); |
| 1416 | break; |
| 1417 | case KVM_REG_PPC_TM_TAR: |
| 1418 | vcpu->arch.tar_tm = set_reg_val(id, *val); |
| 1419 | break; |
| 1420 | #endif |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 1421 | case KVM_REG_PPC_ARCH_COMPAT: |
| 1422 | r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val)); |
| 1423 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1424 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1425 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1426 | break; |
| 1427 | } |
| 1428 | |
| 1429 | return r; |
| 1430 | } |
| 1431 | |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1432 | static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int core) |
| 1433 | { |
| 1434 | struct kvmppc_vcore *vcore; |
| 1435 | |
| 1436 | vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); |
| 1437 | |
| 1438 | if (vcore == NULL) |
| 1439 | return NULL; |
| 1440 | |
| 1441 | INIT_LIST_HEAD(&vcore->runnable_threads); |
| 1442 | spin_lock_init(&vcore->lock); |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 1443 | spin_lock_init(&vcore->stoltb_lock); |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1444 | init_waitqueue_head(&vcore->wq); |
| 1445 | vcore->preempt_tb = TB_NIL; |
| 1446 | vcore->lpcr = kvm->arch.lpcr; |
| 1447 | vcore->first_vcpuid = core * threads_per_subcore; |
| 1448 | vcore->kvm = kvm; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1449 | INIT_LIST_HEAD(&vcore->preempt_list); |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1450 | |
| 1451 | return vcore; |
| 1452 | } |
| 1453 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1454 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 1455 | static struct debugfs_timings_element { |
| 1456 | const char *name; |
| 1457 | size_t offset; |
| 1458 | } timings[] = { |
| 1459 | {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)}, |
| 1460 | {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)}, |
| 1461 | {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)}, |
| 1462 | {"guest", offsetof(struct kvm_vcpu, arch.guest_time)}, |
| 1463 | {"cede", offsetof(struct kvm_vcpu, arch.cede_time)}, |
| 1464 | }; |
| 1465 | |
| 1466 | #define N_TIMINGS (sizeof(timings) / sizeof(timings[0])) |
| 1467 | |
| 1468 | struct debugfs_timings_state { |
| 1469 | struct kvm_vcpu *vcpu; |
| 1470 | unsigned int buflen; |
| 1471 | char buf[N_TIMINGS * 100]; |
| 1472 | }; |
| 1473 | |
| 1474 | static int debugfs_timings_open(struct inode *inode, struct file *file) |
| 1475 | { |
| 1476 | struct kvm_vcpu *vcpu = inode->i_private; |
| 1477 | struct debugfs_timings_state *p; |
| 1478 | |
| 1479 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 1480 | if (!p) |
| 1481 | return -ENOMEM; |
| 1482 | |
| 1483 | kvm_get_kvm(vcpu->kvm); |
| 1484 | p->vcpu = vcpu; |
| 1485 | file->private_data = p; |
| 1486 | |
| 1487 | return nonseekable_open(inode, file); |
| 1488 | } |
| 1489 | |
| 1490 | static int debugfs_timings_release(struct inode *inode, struct file *file) |
| 1491 | { |
| 1492 | struct debugfs_timings_state *p = file->private_data; |
| 1493 | |
| 1494 | kvm_put_kvm(p->vcpu->kvm); |
| 1495 | kfree(p); |
| 1496 | return 0; |
| 1497 | } |
| 1498 | |
| 1499 | static ssize_t debugfs_timings_read(struct file *file, char __user *buf, |
| 1500 | size_t len, loff_t *ppos) |
| 1501 | { |
| 1502 | struct debugfs_timings_state *p = file->private_data; |
| 1503 | struct kvm_vcpu *vcpu = p->vcpu; |
| 1504 | char *s, *buf_end; |
| 1505 | struct kvmhv_tb_accumulator tb; |
| 1506 | u64 count; |
| 1507 | loff_t pos; |
| 1508 | ssize_t n; |
| 1509 | int i, loops; |
| 1510 | bool ok; |
| 1511 | |
| 1512 | if (!p->buflen) { |
| 1513 | s = p->buf; |
| 1514 | buf_end = s + sizeof(p->buf); |
| 1515 | for (i = 0; i < N_TIMINGS; ++i) { |
| 1516 | struct kvmhv_tb_accumulator *acc; |
| 1517 | |
| 1518 | acc = (struct kvmhv_tb_accumulator *) |
| 1519 | ((unsigned long)vcpu + timings[i].offset); |
| 1520 | ok = false; |
| 1521 | for (loops = 0; loops < 1000; ++loops) { |
| 1522 | count = acc->seqcount; |
| 1523 | if (!(count & 1)) { |
| 1524 | smp_rmb(); |
| 1525 | tb = *acc; |
| 1526 | smp_rmb(); |
| 1527 | if (count == acc->seqcount) { |
| 1528 | ok = true; |
| 1529 | break; |
| 1530 | } |
| 1531 | } |
| 1532 | udelay(1); |
| 1533 | } |
| 1534 | if (!ok) |
| 1535 | snprintf(s, buf_end - s, "%s: stuck\n", |
| 1536 | timings[i].name); |
| 1537 | else |
| 1538 | snprintf(s, buf_end - s, |
| 1539 | "%s: %llu %llu %llu %llu\n", |
| 1540 | timings[i].name, count / 2, |
| 1541 | tb_to_ns(tb.tb_total), |
| 1542 | tb_to_ns(tb.tb_min), |
| 1543 | tb_to_ns(tb.tb_max)); |
| 1544 | s += strlen(s); |
| 1545 | } |
| 1546 | p->buflen = s - p->buf; |
| 1547 | } |
| 1548 | |
| 1549 | pos = *ppos; |
| 1550 | if (pos >= p->buflen) |
| 1551 | return 0; |
| 1552 | if (len > p->buflen - pos) |
| 1553 | len = p->buflen - pos; |
| 1554 | n = copy_to_user(buf, p->buf + pos, len); |
| 1555 | if (n) { |
| 1556 | if (n == len) |
| 1557 | return -EFAULT; |
| 1558 | len -= n; |
| 1559 | } |
| 1560 | *ppos = pos + len; |
| 1561 | return len; |
| 1562 | } |
| 1563 | |
| 1564 | static ssize_t debugfs_timings_write(struct file *file, const char __user *buf, |
| 1565 | size_t len, loff_t *ppos) |
| 1566 | { |
| 1567 | return -EACCES; |
| 1568 | } |
| 1569 | |
| 1570 | static const struct file_operations debugfs_timings_ops = { |
| 1571 | .owner = THIS_MODULE, |
| 1572 | .open = debugfs_timings_open, |
| 1573 | .release = debugfs_timings_release, |
| 1574 | .read = debugfs_timings_read, |
| 1575 | .write = debugfs_timings_write, |
| 1576 | .llseek = generic_file_llseek, |
| 1577 | }; |
| 1578 | |
| 1579 | /* Create a debugfs directory for the vcpu */ |
| 1580 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) |
| 1581 | { |
| 1582 | char buf[16]; |
| 1583 | struct kvm *kvm = vcpu->kvm; |
| 1584 | |
| 1585 | snprintf(buf, sizeof(buf), "vcpu%u", id); |
| 1586 | if (IS_ERR_OR_NULL(kvm->arch.debugfs_dir)) |
| 1587 | return; |
| 1588 | vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir); |
| 1589 | if (IS_ERR_OR_NULL(vcpu->arch.debugfs_dir)) |
| 1590 | return; |
| 1591 | vcpu->arch.debugfs_timings = |
| 1592 | debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir, |
| 1593 | vcpu, &debugfs_timings_ops); |
| 1594 | } |
| 1595 | |
| 1596 | #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ |
| 1597 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) |
| 1598 | { |
| 1599 | } |
| 1600 | #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ |
| 1601 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1602 | static struct kvm_vcpu *kvmppc_core_vcpu_create_hv(struct kvm *kvm, |
| 1603 | unsigned int id) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1604 | { |
| 1605 | struct kvm_vcpu *vcpu; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1606 | int err = -EINVAL; |
| 1607 | int core; |
| 1608 | struct kvmppc_vcore *vcore; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1609 | |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1610 | core = id / threads_per_subcore; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1611 | if (core >= KVM_MAX_VCORES) |
| 1612 | goto out; |
| 1613 | |
| 1614 | err = -ENOMEM; |
Sasha Levin | 6b75e6b | 2011-12-07 10:24:56 +0200 | [diff] [blame] | 1615 | vcpu = kmem_cache_zalloc(kvm_vcpu_cache, GFP_KERNEL); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1616 | if (!vcpu) |
| 1617 | goto out; |
| 1618 | |
| 1619 | err = kvm_vcpu_init(vcpu, kvm, id); |
| 1620 | if (err) |
| 1621 | goto free_vcpu; |
| 1622 | |
| 1623 | vcpu->arch.shared = &vcpu->arch.shregs; |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1624 | #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE |
| 1625 | /* |
| 1626 | * The shared struct is never shared on HV, |
| 1627 | * so we can always use host endianness |
| 1628 | */ |
| 1629 | #ifdef __BIG_ENDIAN__ |
| 1630 | vcpu->arch.shared_big_endian = true; |
| 1631 | #else |
| 1632 | vcpu->arch.shared_big_endian = false; |
| 1633 | #endif |
| 1634 | #endif |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1635 | vcpu->arch.mmcr[0] = MMCR0_FC; |
| 1636 | vcpu->arch.ctrl = CTRL_RUNLATCH; |
| 1637 | /* default to host PVR, since we can't spoof it */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1638 | kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR)); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1639 | spin_lock_init(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1640 | spin_lock_init(&vcpu->arch.tbacct_lock); |
| 1641 | vcpu->arch.busy_preempt = TB_NIL; |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1642 | vcpu->arch.intr_msr = MSR_SF | MSR_ME; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1643 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1644 | kvmppc_mmu_book3s_hv_init(vcpu); |
| 1645 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 1646 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1647 | |
| 1648 | init_waitqueue_head(&vcpu->arch.cpu_run); |
| 1649 | |
| 1650 | mutex_lock(&kvm->lock); |
| 1651 | vcore = kvm->arch.vcores[core]; |
| 1652 | if (!vcore) { |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 1653 | vcore = kvmppc_vcore_create(kvm, core); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1654 | kvm->arch.vcores[core] = vcore; |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 1655 | kvm->arch.online_vcores++; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1656 | } |
| 1657 | mutex_unlock(&kvm->lock); |
| 1658 | |
| 1659 | if (!vcore) |
| 1660 | goto free_vcpu; |
| 1661 | |
| 1662 | spin_lock(&vcore->lock); |
| 1663 | ++vcore->num_threads; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1664 | spin_unlock(&vcore->lock); |
| 1665 | vcpu->arch.vcore = vcore; |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1666 | vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1667 | vcpu->arch.thread_cpu = -1; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1668 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1669 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
| 1670 | kvmppc_sanity_check(vcpu); |
| 1671 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1672 | debugfs_vcpu_init(vcpu, id); |
| 1673 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1674 | return vcpu; |
| 1675 | |
| 1676 | free_vcpu: |
Sasha Levin | 6b75e6b | 2011-12-07 10:24:56 +0200 | [diff] [blame] | 1677 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1678 | out: |
| 1679 | return ERR_PTR(err); |
| 1680 | } |
| 1681 | |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1682 | static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) |
| 1683 | { |
| 1684 | if (vpa->pinned_addr) |
| 1685 | kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa, |
| 1686 | vpa->dirty); |
| 1687 | } |
| 1688 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1689 | static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1690 | { |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1691 | spin_lock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1692 | unpin_vpa(vcpu->kvm, &vcpu->arch.dtl); |
| 1693 | unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow); |
| 1694 | unpin_vpa(vcpu->kvm, &vcpu->arch.vpa); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 1695 | spin_unlock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1696 | kvm_vcpu_uninit(vcpu); |
Sasha Levin | 6b75e6b | 2011-12-07 10:24:56 +0200 | [diff] [blame] | 1697 | kmem_cache_free(kvm_vcpu_cache, vcpu); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1700 | static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu) |
| 1701 | { |
| 1702 | /* Indicate we want to get back into the guest */ |
| 1703 | return 1; |
| 1704 | } |
| 1705 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1706 | static void kvmppc_set_timer(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1707 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1708 | unsigned long dec_nsec, now; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1709 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1710 | now = get_tb(); |
| 1711 | if (now > vcpu->arch.dec_expires) { |
| 1712 | /* decrementer has already gone negative */ |
| 1713 | kvmppc_core_queue_dec(vcpu); |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 1714 | kvmppc_core_prepare_to_enter(vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1715 | return; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1716 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1717 | dec_nsec = (vcpu->arch.dec_expires - now) * NSEC_PER_SEC |
| 1718 | / tb_ticks_per_sec; |
| 1719 | hrtimer_start(&vcpu->arch.dec_timer, ktime_set(0, dec_nsec), |
| 1720 | HRTIMER_MODE_REL); |
| 1721 | vcpu->arch.timer_running = 1; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1722 | } |
| 1723 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1724 | static void kvmppc_end_cede(struct kvm_vcpu *vcpu) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1725 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1726 | vcpu->arch.ceded = 0; |
| 1727 | if (vcpu->arch.timer_running) { |
| 1728 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
| 1729 | vcpu->arch.timer_running = 0; |
| 1730 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1733 | extern void __kvmppc_vcore_entry(void); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1734 | |
| 1735 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
| 1736 | struct kvm_vcpu *vcpu) |
| 1737 | { |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1738 | u64 now; |
| 1739 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1740 | if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 1741 | return; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 1742 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 1743 | now = mftb(); |
| 1744 | vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) - |
| 1745 | vcpu->arch.stolen_logged; |
| 1746 | vcpu->arch.busy_preempt = now; |
| 1747 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 1748 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1749 | --vc->n_runnable; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1750 | list_del(&vcpu->arch.run_list); |
| 1751 | } |
| 1752 | |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 1753 | static int kvmppc_grab_hwthread(int cpu) |
| 1754 | { |
| 1755 | struct paca_struct *tpaca; |
Paul Mackerras | b754c73 | 2014-09-02 16:14:42 +1000 | [diff] [blame] | 1756 | long timeout = 10000; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 1757 | |
| 1758 | tpaca = &paca[cpu]; |
| 1759 | |
| 1760 | /* Ensure the thread won't go into the kernel if it wakes */ |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1761 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1762 | tpaca->kvm_hstate.kvm_vcore = NULL; |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1763 | tpaca->kvm_hstate.napping = 0; |
| 1764 | smp_wmb(); |
| 1765 | tpaca->kvm_hstate.hwthread_req = 1; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 1766 | |
| 1767 | /* |
| 1768 | * If the thread is already executing in the kernel (e.g. handling |
| 1769 | * a stray interrupt), wait for it to get back to nap mode. |
| 1770 | * The smp_mb() is to ensure that our setting of hwthread_req |
| 1771 | * is visible before we look at hwthread_state, so if this |
| 1772 | * races with the code at system_reset_pSeries and the thread |
| 1773 | * misses our setting of hwthread_req, we are sure to see its |
| 1774 | * setting of hwthread_state, and vice versa. |
| 1775 | */ |
| 1776 | smp_mb(); |
| 1777 | while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) { |
| 1778 | if (--timeout <= 0) { |
| 1779 | pr_err("KVM: couldn't grab cpu %d\n", cpu); |
| 1780 | return -EBUSY; |
| 1781 | } |
| 1782 | udelay(1); |
| 1783 | } |
| 1784 | return 0; |
| 1785 | } |
| 1786 | |
| 1787 | static void kvmppc_release_hwthread(int cpu) |
| 1788 | { |
| 1789 | struct paca_struct *tpaca; |
| 1790 | |
| 1791 | tpaca = &paca[cpu]; |
| 1792 | tpaca->kvm_hstate.hwthread_req = 0; |
| 1793 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1794 | tpaca->kvm_hstate.kvm_vcore = NULL; |
| 1795 | tpaca->kvm_hstate.kvm_split_mode = NULL; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 1796 | } |
| 1797 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1798 | static void kvmppc_start_thread(struct kvm_vcpu *vcpu, struct kvmppc_vcore *vc) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1799 | { |
| 1800 | int cpu; |
| 1801 | struct paca_struct *tpaca; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1802 | struct kvmppc_vcore *mvc = vc->master_vcore; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1803 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1804 | cpu = vc->pcpu; |
| 1805 | if (vcpu) { |
| 1806 | if (vcpu->arch.timer_running) { |
| 1807 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
| 1808 | vcpu->arch.timer_running = 0; |
| 1809 | } |
| 1810 | cpu += vcpu->arch.ptid; |
| 1811 | vcpu->cpu = mvc->pcpu; |
| 1812 | vcpu->arch.thread_cpu = cpu; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1813 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1814 | tpaca = &paca[cpu]; |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1815 | tpaca->kvm_hstate.kvm_vcpu = vcpu; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1816 | tpaca->kvm_hstate.ptid = cpu - mvc->pcpu; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1817 | /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */ |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 1818 | smp_wmb(); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1819 | tpaca->kvm_hstate.kvm_vcore = mvc; |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1820 | if (cpu != smp_processor_id()) |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 1821 | kvmppc_ipi_thread(cpu); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1822 | } |
| 1823 | |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1824 | static void kvmppc_wait_for_nap(void) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1825 | { |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1826 | int cpu = smp_processor_id(); |
| 1827 | int i, loops; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1828 | |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1829 | for (loops = 0; loops < 1000000; ++loops) { |
| 1830 | /* |
| 1831 | * Check if all threads are finished. |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1832 | * We set the vcore pointer when starting a thread |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1833 | * and the thread clears it when finished, so we look |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1834 | * for any threads that still have a non-NULL vcore ptr. |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1835 | */ |
| 1836 | for (i = 1; i < threads_per_subcore; ++i) |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1837 | if (paca[cpu + i].kvm_hstate.kvm_vcore) |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1838 | break; |
| 1839 | if (i == threads_per_subcore) { |
| 1840 | HMT_medium(); |
| 1841 | return; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1842 | } |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1843 | HMT_low(); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1844 | } |
| 1845 | HMT_medium(); |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1846 | for (i = 1; i < threads_per_subcore; ++i) |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1847 | if (paca[cpu + i].kvm_hstate.kvm_vcore) |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 1848 | pr_err("KVM: CPU %d seems to be stuck\n", cpu + i); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
| 1851 | /* |
| 1852 | * Check that we are on thread 0 and that any other threads in |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1853 | * this core are off-line. Then grab the threads so they can't |
| 1854 | * enter the kernel. |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1855 | */ |
| 1856 | static int on_primary_thread(void) |
| 1857 | { |
| 1858 | int cpu = smp_processor_id(); |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1859 | int thr; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1860 | |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1861 | /* Are we on a primary subcore? */ |
| 1862 | if (cpu_thread_in_subcore(cpu)) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1863 | return 0; |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1864 | |
| 1865 | thr = 0; |
| 1866 | while (++thr < threads_per_subcore) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1867 | if (cpu_online(cpu + thr)) |
| 1868 | return 0; |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1869 | |
| 1870 | /* Grab all hw threads so they can't go into the kernel */ |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 1871 | for (thr = 1; thr < threads_per_subcore; ++thr) { |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 1872 | if (kvmppc_grab_hwthread(cpu + thr)) { |
| 1873 | /* Couldn't grab one; let the others go */ |
| 1874 | do { |
| 1875 | kvmppc_release_hwthread(cpu + thr); |
| 1876 | } while (--thr > 0); |
| 1877 | return 0; |
| 1878 | } |
| 1879 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 1880 | return 1; |
| 1881 | } |
| 1882 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1883 | /* |
| 1884 | * A list of virtual cores for each physical CPU. |
| 1885 | * These are vcores that could run but their runner VCPU tasks are |
| 1886 | * (or may be) preempted. |
| 1887 | */ |
| 1888 | struct preempted_vcore_list { |
| 1889 | struct list_head list; |
| 1890 | spinlock_t lock; |
| 1891 | }; |
| 1892 | |
| 1893 | static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores); |
| 1894 | |
| 1895 | static void init_vcore_lists(void) |
| 1896 | { |
| 1897 | int cpu; |
| 1898 | |
| 1899 | for_each_possible_cpu(cpu) { |
| 1900 | struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu); |
| 1901 | spin_lock_init(&lp->lock); |
| 1902 | INIT_LIST_HEAD(&lp->list); |
| 1903 | } |
| 1904 | } |
| 1905 | |
| 1906 | static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc) |
| 1907 | { |
| 1908 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); |
| 1909 | |
| 1910 | vc->vcore_state = VCORE_PREEMPT; |
| 1911 | vc->pcpu = smp_processor_id(); |
| 1912 | if (vc->num_threads < threads_per_subcore) { |
| 1913 | spin_lock(&lp->lock); |
| 1914 | list_add_tail(&vc->preempt_list, &lp->list); |
| 1915 | spin_unlock(&lp->lock); |
| 1916 | } |
| 1917 | |
| 1918 | /* Start accumulating stolen time */ |
| 1919 | kvmppc_core_start_stolen(vc); |
| 1920 | } |
| 1921 | |
| 1922 | static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc) |
| 1923 | { |
Paul Mackerras | 402813f | 2015-07-16 17:11:13 +1000 | [diff] [blame] | 1924 | struct preempted_vcore_list *lp; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1925 | |
| 1926 | kvmppc_core_end_stolen(vc); |
| 1927 | if (!list_empty(&vc->preempt_list)) { |
Paul Mackerras | 402813f | 2015-07-16 17:11:13 +1000 | [diff] [blame] | 1928 | lp = &per_cpu(preempted_vcores, vc->pcpu); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1929 | spin_lock(&lp->lock); |
| 1930 | list_del_init(&vc->preempt_list); |
| 1931 | spin_unlock(&lp->lock); |
| 1932 | } |
| 1933 | vc->vcore_state = VCORE_INACTIVE; |
| 1934 | } |
| 1935 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1936 | /* |
| 1937 | * This stores information about the virtual cores currently |
| 1938 | * assigned to a physical core. |
| 1939 | */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1940 | struct core_info { |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1941 | int n_subcores; |
| 1942 | int max_subcore_threads; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1943 | int total_threads; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1944 | int subcore_threads[MAX_SUBCORES]; |
| 1945 | struct kvm *subcore_vm[MAX_SUBCORES]; |
| 1946 | struct list_head vcs[MAX_SUBCORES]; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1947 | }; |
| 1948 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1949 | /* |
| 1950 | * This mapping means subcores 0 and 1 can use threads 0-3 and 4-7 |
| 1951 | * respectively in 2-way micro-threading (split-core) mode. |
| 1952 | */ |
| 1953 | static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 }; |
| 1954 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1955 | static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc) |
| 1956 | { |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1957 | int sub; |
| 1958 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1959 | memset(cip, 0, sizeof(*cip)); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1960 | cip->n_subcores = 1; |
| 1961 | cip->max_subcore_threads = vc->num_threads; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1962 | cip->total_threads = vc->num_threads; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1963 | cip->subcore_threads[0] = vc->num_threads; |
| 1964 | cip->subcore_vm[0] = vc->kvm; |
| 1965 | for (sub = 0; sub < MAX_SUBCORES; ++sub) |
| 1966 | INIT_LIST_HEAD(&cip->vcs[sub]); |
| 1967 | list_add_tail(&vc->preempt_list, &cip->vcs[0]); |
| 1968 | } |
| 1969 | |
| 1970 | static bool subcore_config_ok(int n_subcores, int n_threads) |
| 1971 | { |
| 1972 | /* Can only dynamically split if unsplit to begin with */ |
| 1973 | if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS) |
| 1974 | return false; |
| 1975 | if (n_subcores > MAX_SUBCORES) |
| 1976 | return false; |
| 1977 | if (n_subcores > 1) { |
| 1978 | if (!(dynamic_mt_modes & 2)) |
| 1979 | n_subcores = 4; |
| 1980 | if (n_subcores > 2 && !(dynamic_mt_modes & 4)) |
| 1981 | return false; |
| 1982 | } |
| 1983 | |
| 1984 | return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | static void init_master_vcore(struct kvmppc_vcore *vc) |
| 1988 | { |
| 1989 | vc->master_vcore = vc; |
| 1990 | vc->entry_exit_map = 0; |
| 1991 | vc->in_guest = 0; |
| 1992 | vc->napping_threads = 0; |
| 1993 | vc->conferring_threads = 0; |
| 1994 | } |
| 1995 | |
| 1996 | /* |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1997 | * See if the existing subcores can be split into 3 (or fewer) subcores |
| 1998 | * of at most two threads each, so we can fit in another vcore. This |
| 1999 | * assumes there are at most two subcores and at most 6 threads in total. |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2000 | */ |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2001 | static bool can_split_piggybacked_subcores(struct core_info *cip) |
| 2002 | { |
| 2003 | int sub, new_sub; |
| 2004 | int large_sub = -1; |
| 2005 | int thr; |
| 2006 | int n_subcores = cip->n_subcores; |
| 2007 | struct kvmppc_vcore *vc, *vcnext; |
| 2008 | struct kvmppc_vcore *master_vc = NULL; |
| 2009 | |
| 2010 | for (sub = 0; sub < cip->n_subcores; ++sub) { |
| 2011 | if (cip->subcore_threads[sub] <= 2) |
| 2012 | continue; |
| 2013 | if (large_sub >= 0) |
| 2014 | return false; |
| 2015 | large_sub = sub; |
| 2016 | vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore, |
| 2017 | preempt_list); |
| 2018 | if (vc->num_threads > 2) |
| 2019 | return false; |
| 2020 | n_subcores += (cip->subcore_threads[sub] - 1) >> 1; |
| 2021 | } |
| 2022 | if (n_subcores > 3 || large_sub < 0) |
| 2023 | return false; |
| 2024 | |
| 2025 | /* |
| 2026 | * Seems feasible, so go through and move vcores to new subcores. |
| 2027 | * Note that when we have two or more vcores in one subcore, |
| 2028 | * all those vcores must have only one thread each. |
| 2029 | */ |
| 2030 | new_sub = cip->n_subcores; |
| 2031 | thr = 0; |
| 2032 | sub = large_sub; |
| 2033 | list_for_each_entry_safe(vc, vcnext, &cip->vcs[sub], preempt_list) { |
| 2034 | if (thr >= 2) { |
| 2035 | list_del(&vc->preempt_list); |
| 2036 | list_add_tail(&vc->preempt_list, &cip->vcs[new_sub]); |
| 2037 | /* vc->num_threads must be 1 */ |
| 2038 | if (++cip->subcore_threads[new_sub] == 1) { |
| 2039 | cip->subcore_vm[new_sub] = vc->kvm; |
| 2040 | init_master_vcore(vc); |
| 2041 | master_vc = vc; |
| 2042 | ++cip->n_subcores; |
| 2043 | } else { |
| 2044 | vc->master_vcore = master_vc; |
| 2045 | ++new_sub; |
| 2046 | } |
| 2047 | } |
| 2048 | thr += vc->num_threads; |
| 2049 | } |
| 2050 | cip->subcore_threads[large_sub] = 2; |
| 2051 | cip->max_subcore_threads = 2; |
| 2052 | |
| 2053 | return true; |
| 2054 | } |
| 2055 | |
| 2056 | static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip) |
| 2057 | { |
| 2058 | int n_threads = vc->num_threads; |
| 2059 | int sub; |
| 2060 | |
| 2061 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 2062 | return false; |
| 2063 | |
| 2064 | if (n_threads < cip->max_subcore_threads) |
| 2065 | n_threads = cip->max_subcore_threads; |
| 2066 | if (subcore_config_ok(cip->n_subcores + 1, n_threads)) { |
| 2067 | cip->max_subcore_threads = n_threads; |
| 2068 | } else if (cip->n_subcores <= 2 && cip->total_threads <= 6 && |
| 2069 | vc->num_threads <= 2) { |
| 2070 | /* |
| 2071 | * We may be able to fit another subcore in by |
| 2072 | * splitting an existing subcore with 3 or 4 |
| 2073 | * threads into two 2-thread subcores, or one |
| 2074 | * with 5 or 6 threads into three subcores. |
| 2075 | * We can only do this if those subcores have |
| 2076 | * piggybacked virtual cores. |
| 2077 | */ |
| 2078 | if (!can_split_piggybacked_subcores(cip)) |
| 2079 | return false; |
| 2080 | } else { |
| 2081 | return false; |
| 2082 | } |
| 2083 | |
| 2084 | sub = cip->n_subcores; |
| 2085 | ++cip->n_subcores; |
| 2086 | cip->total_threads += vc->num_threads; |
| 2087 | cip->subcore_threads[sub] = vc->num_threads; |
| 2088 | cip->subcore_vm[sub] = vc->kvm; |
| 2089 | init_master_vcore(vc); |
| 2090 | list_del(&vc->preempt_list); |
| 2091 | list_add_tail(&vc->preempt_list, &cip->vcs[sub]); |
| 2092 | |
| 2093 | return true; |
| 2094 | } |
| 2095 | |
| 2096 | static bool can_piggyback_subcore(struct kvmppc_vcore *pvc, |
| 2097 | struct core_info *cip, int sub) |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2098 | { |
| 2099 | struct kvmppc_vcore *vc; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2100 | int n_thr; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2101 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2102 | vc = list_first_entry(&cip->vcs[sub], struct kvmppc_vcore, |
| 2103 | preempt_list); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2104 | |
| 2105 | /* require same VM and same per-core reg values */ |
| 2106 | if (pvc->kvm != vc->kvm || |
| 2107 | pvc->tb_offset != vc->tb_offset || |
| 2108 | pvc->pcr != vc->pcr || |
| 2109 | pvc->lpcr != vc->lpcr) |
| 2110 | return false; |
| 2111 | |
| 2112 | /* P8 guest with > 1 thread per core would see wrong TIR value */ |
| 2113 | if (cpu_has_feature(CPU_FTR_ARCH_207S) && |
| 2114 | (vc->num_threads > 1 || pvc->num_threads > 1)) |
| 2115 | return false; |
| 2116 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2117 | n_thr = cip->subcore_threads[sub] + pvc->num_threads; |
| 2118 | if (n_thr > cip->max_subcore_threads) { |
| 2119 | if (!subcore_config_ok(cip->n_subcores, n_thr)) |
| 2120 | return false; |
| 2121 | cip->max_subcore_threads = n_thr; |
| 2122 | } |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2123 | |
| 2124 | cip->total_threads += pvc->num_threads; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2125 | cip->subcore_threads[sub] = n_thr; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2126 | pvc->master_vcore = vc; |
| 2127 | list_del(&pvc->preempt_list); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2128 | list_add_tail(&pvc->preempt_list, &cip->vcs[sub]); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2129 | |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2133 | /* |
| 2134 | * Work out whether it is possible to piggyback the execution of |
| 2135 | * vcore *pvc onto the execution of the other vcores described in *cip. |
| 2136 | */ |
| 2137 | static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip, |
| 2138 | int target_threads) |
| 2139 | { |
| 2140 | int sub; |
| 2141 | |
| 2142 | if (cip->total_threads + pvc->num_threads > target_threads) |
| 2143 | return false; |
| 2144 | for (sub = 0; sub < cip->n_subcores; ++sub) |
| 2145 | if (cip->subcore_threads[sub] && |
| 2146 | can_piggyback_subcore(pvc, cip, sub)) |
| 2147 | return true; |
| 2148 | |
| 2149 | if (can_dynamic_split(pvc, cip)) |
| 2150 | return true; |
| 2151 | |
| 2152 | return false; |
| 2153 | } |
| 2154 | |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 2155 | static void prepare_threads(struct kvmppc_vcore *vc) |
| 2156 | { |
| 2157 | struct kvm_vcpu *vcpu, *vnext; |
| 2158 | |
| 2159 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, |
| 2160 | arch.run_list) { |
| 2161 | if (signal_pending(vcpu->arch.run_task)) |
| 2162 | vcpu->arch.ret = -EINTR; |
| 2163 | else if (vcpu->arch.vpa.update_pending || |
| 2164 | vcpu->arch.slb_shadow.update_pending || |
| 2165 | vcpu->arch.dtl.update_pending) |
| 2166 | vcpu->arch.ret = RESUME_GUEST; |
| 2167 | else |
| 2168 | continue; |
| 2169 | kvmppc_remove_runnable(vc, vcpu); |
| 2170 | wake_up(&vcpu->arch.cpu_run); |
| 2171 | } |
| 2172 | } |
| 2173 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2174 | static void collect_piggybacks(struct core_info *cip, int target_threads) |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2175 | { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2176 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); |
| 2177 | struct kvmppc_vcore *pvc, *vcnext; |
| 2178 | |
| 2179 | spin_lock(&lp->lock); |
| 2180 | list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) { |
| 2181 | if (!spin_trylock(&pvc->lock)) |
| 2182 | continue; |
| 2183 | prepare_threads(pvc); |
| 2184 | if (!pvc->n_runnable) { |
| 2185 | list_del_init(&pvc->preempt_list); |
| 2186 | if (pvc->runner == NULL) { |
| 2187 | pvc->vcore_state = VCORE_INACTIVE; |
| 2188 | kvmppc_core_end_stolen(pvc); |
| 2189 | } |
| 2190 | spin_unlock(&pvc->lock); |
| 2191 | continue; |
| 2192 | } |
| 2193 | if (!can_piggyback(pvc, cip, target_threads)) { |
| 2194 | spin_unlock(&pvc->lock); |
| 2195 | continue; |
| 2196 | } |
| 2197 | kvmppc_core_end_stolen(pvc); |
| 2198 | pvc->vcore_state = VCORE_PIGGYBACK; |
| 2199 | if (cip->total_threads >= target_threads) |
| 2200 | break; |
| 2201 | } |
| 2202 | spin_unlock(&lp->lock); |
| 2203 | } |
| 2204 | |
| 2205 | static void post_guest_process(struct kvmppc_vcore *vc, bool is_master) |
| 2206 | { |
| 2207 | int still_running = 0; |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2208 | u64 now; |
| 2209 | long ret; |
| 2210 | struct kvm_vcpu *vcpu, *vnext; |
| 2211 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2212 | spin_lock(&vc->lock); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2213 | now = get_tb(); |
| 2214 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, |
| 2215 | arch.run_list) { |
| 2216 | /* cancel pending dec exception if dec is positive */ |
| 2217 | if (now < vcpu->arch.dec_expires && |
| 2218 | kvmppc_core_pending_dec(vcpu)) |
| 2219 | kvmppc_core_dequeue_dec(vcpu); |
| 2220 | |
| 2221 | trace_kvm_guest_exit(vcpu); |
| 2222 | |
| 2223 | ret = RESUME_GUEST; |
| 2224 | if (vcpu->arch.trap) |
| 2225 | ret = kvmppc_handle_exit_hv(vcpu->arch.kvm_run, vcpu, |
| 2226 | vcpu->arch.run_task); |
| 2227 | |
| 2228 | vcpu->arch.ret = ret; |
| 2229 | vcpu->arch.trap = 0; |
| 2230 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2231 | if (is_kvmppc_resume_guest(vcpu->arch.ret)) { |
| 2232 | if (vcpu->arch.pending_exceptions) |
| 2233 | kvmppc_core_prepare_to_enter(vcpu); |
| 2234 | if (vcpu->arch.ceded) |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2235 | kvmppc_set_timer(vcpu); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2236 | else |
| 2237 | ++still_running; |
| 2238 | } else { |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2239 | kvmppc_remove_runnable(vc, vcpu); |
| 2240 | wake_up(&vcpu->arch.cpu_run); |
| 2241 | } |
| 2242 | } |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2243 | list_del_init(&vc->preempt_list); |
| 2244 | if (!is_master) { |
Paul Mackerras | 563a1e9 | 2015-07-16 17:11:14 +1000 | [diff] [blame] | 2245 | if (still_running > 0) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2246 | kvmppc_vcore_preempt(vc); |
Paul Mackerras | 563a1e9 | 2015-07-16 17:11:14 +1000 | [diff] [blame] | 2247 | } else if (vc->runner) { |
| 2248 | vc->vcore_state = VCORE_PREEMPT; |
| 2249 | kvmppc_core_start_stolen(vc); |
| 2250 | } else { |
| 2251 | vc->vcore_state = VCORE_INACTIVE; |
| 2252 | } |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2253 | if (vc->n_runnable > 0 && vc->runner == NULL) { |
| 2254 | /* make sure there's a candidate runner awake */ |
| 2255 | vcpu = list_first_entry(&vc->runnable_threads, |
| 2256 | struct kvm_vcpu, arch.run_list); |
| 2257 | wake_up(&vcpu->arch.cpu_run); |
| 2258 | } |
| 2259 | } |
| 2260 | spin_unlock(&vc->lock); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2261 | } |
| 2262 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2263 | /* |
| 2264 | * Run a set of guest threads on a physical core. |
| 2265 | * Called with vc->lock held. |
| 2266 | */ |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 2267 | static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2268 | { |
Paul Mackerras | 17d4890 | 2015-04-29 14:49:07 +1000 | [diff] [blame] | 2269 | struct kvm_vcpu *vcpu, *vnext; |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 2270 | int i; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2271 | int srcu_idx; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2272 | struct core_info core_info; |
| 2273 | struct kvmppc_vcore *pvc, *vcnext; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2274 | struct kvm_split_mode split_info, *sip; |
| 2275 | int split, subcore_size, active; |
| 2276 | int sub; |
| 2277 | bool thr0_done; |
| 2278 | unsigned long cmd_bit, stat_bit; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2279 | int pcpu, thr; |
| 2280 | int target_threads; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 2281 | |
| 2282 | /* |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 2283 | * Remove from the list any threads that have a signal pending |
| 2284 | * or need a VPA update done |
| 2285 | */ |
| 2286 | prepare_threads(vc); |
| 2287 | |
| 2288 | /* if the runner is no longer runnable, let the caller pick a new one */ |
| 2289 | if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 2290 | return; |
| 2291 | |
| 2292 | /* |
| 2293 | * Initialize *vc. |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 2294 | */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2295 | init_master_vcore(vc); |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 2296 | vc->preempt_tb = TB_NIL; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 2297 | |
| 2298 | /* |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 2299 | * Make sure we are running on primary threads, and that secondary |
| 2300 | * threads are offline. Also check if the number of threads in this |
| 2301 | * guest are greater than the current system threads per guest. |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 2302 | */ |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 2303 | if ((threads_per_core > 1) && |
| 2304 | ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) { |
Paul Mackerras | 17d4890 | 2015-04-29 14:49:07 +1000 | [diff] [blame] | 2305 | list_for_each_entry_safe(vcpu, vnext, &vc->runnable_threads, |
| 2306 | arch.run_list) { |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 2307 | vcpu->arch.ret = -EBUSY; |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2308 | kvmppc_remove_runnable(vc, vcpu); |
| 2309 | wake_up(&vcpu->arch.cpu_run); |
| 2310 | } |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 2311 | goto out; |
| 2312 | } |
| 2313 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2314 | /* |
| 2315 | * See if we could run any other vcores on the physical core |
| 2316 | * along with this one. |
| 2317 | */ |
| 2318 | init_core_info(&core_info, vc); |
| 2319 | pcpu = smp_processor_id(); |
| 2320 | target_threads = threads_per_subcore; |
| 2321 | if (target_smt_mode && target_smt_mode < target_threads) |
| 2322 | target_threads = target_smt_mode; |
| 2323 | if (vc->num_threads < target_threads) |
| 2324 | collect_piggybacks(&core_info, target_threads); |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 2325 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2326 | /* Decide on micro-threading (split-core) mode */ |
| 2327 | subcore_size = threads_per_subcore; |
| 2328 | cmd_bit = stat_bit = 0; |
| 2329 | split = core_info.n_subcores; |
| 2330 | sip = NULL; |
| 2331 | if (split > 1) { |
| 2332 | /* threads_per_subcore must be MAX_SMT_THREADS (8) here */ |
| 2333 | if (split == 2 && (dynamic_mt_modes & 2)) { |
| 2334 | cmd_bit = HID0_POWER8_1TO2LPAR; |
| 2335 | stat_bit = HID0_POWER8_2LPARMODE; |
| 2336 | } else { |
| 2337 | split = 4; |
| 2338 | cmd_bit = HID0_POWER8_1TO4LPAR; |
| 2339 | stat_bit = HID0_POWER8_4LPARMODE; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2340 | } |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2341 | subcore_size = MAX_SMT_THREADS / split; |
| 2342 | sip = &split_info; |
| 2343 | memset(&split_info, 0, sizeof(split_info)); |
| 2344 | split_info.rpr = mfspr(SPRN_RPR); |
| 2345 | split_info.pmmar = mfspr(SPRN_PMMAR); |
| 2346 | split_info.ldbar = mfspr(SPRN_LDBAR); |
| 2347 | split_info.subcore_size = subcore_size; |
| 2348 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
| 2349 | split_info.master_vcs[sub] = |
| 2350 | list_first_entry(&core_info.vcs[sub], |
| 2351 | struct kvmppc_vcore, preempt_list); |
| 2352 | /* order writes to split_info before kvm_split_mode pointer */ |
| 2353 | smp_wmb(); |
| 2354 | } |
| 2355 | pcpu = smp_processor_id(); |
| 2356 | for (thr = 0; thr < threads_per_subcore; ++thr) |
| 2357 | paca[pcpu + thr].kvm_hstate.kvm_split_mode = sip; |
| 2358 | |
| 2359 | /* Initiate micro-threading (split-core) if required */ |
| 2360 | if (cmd_bit) { |
| 2361 | unsigned long hid0 = mfspr(SPRN_HID0); |
| 2362 | |
| 2363 | hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS; |
| 2364 | mb(); |
| 2365 | mtspr(SPRN_HID0, hid0); |
| 2366 | isync(); |
| 2367 | for (;;) { |
| 2368 | hid0 = mfspr(SPRN_HID0); |
| 2369 | if (hid0 & stat_bit) |
| 2370 | break; |
| 2371 | cpu_relax(); |
| 2372 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 2373 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2374 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2375 | /* Start all the threads */ |
| 2376 | active = 0; |
| 2377 | for (sub = 0; sub < core_info.n_subcores; ++sub) { |
| 2378 | thr = subcore_thread_map[sub]; |
| 2379 | thr0_done = false; |
| 2380 | active |= 1 << thr; |
| 2381 | list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) { |
| 2382 | pvc->pcpu = pcpu + thr; |
| 2383 | list_for_each_entry(vcpu, &pvc->runnable_threads, |
| 2384 | arch.run_list) { |
| 2385 | kvmppc_start_thread(vcpu, pvc); |
| 2386 | kvmppc_create_dtl_entry(vcpu, pvc); |
| 2387 | trace_kvm_guest_enter(vcpu); |
| 2388 | if (!vcpu->arch.ptid) |
| 2389 | thr0_done = true; |
| 2390 | active |= 1 << (thr + vcpu->arch.ptid); |
| 2391 | } |
| 2392 | /* |
| 2393 | * We need to start the first thread of each subcore |
| 2394 | * even if it doesn't have a vcpu. |
| 2395 | */ |
| 2396 | if (pvc->master_vcore == pvc && !thr0_done) |
| 2397 | kvmppc_start_thread(NULL, pvc); |
| 2398 | thr += pvc->num_threads; |
| 2399 | } |
| 2400 | } |
Gautham R. Shenoy | 7f23532 | 2015-09-02 21:48:58 +0530 | [diff] [blame] | 2401 | |
| 2402 | /* |
| 2403 | * Ensure that split_info.do_nap is set after setting |
| 2404 | * the vcore pointer in the PACA of the secondaries. |
| 2405 | */ |
| 2406 | smp_mb(); |
| 2407 | if (cmd_bit) |
| 2408 | split_info.do_nap = 1; /* ask secondaries to nap when done */ |
| 2409 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2410 | /* |
| 2411 | * When doing micro-threading, poke the inactive threads as well. |
| 2412 | * This gets them to the nap instruction after kvm_do_nap, |
| 2413 | * which reduces the time taken to unsplit later. |
| 2414 | */ |
| 2415 | if (split > 1) |
| 2416 | for (thr = 1; thr < threads_per_subcore; ++thr) |
| 2417 | if (!(active & (1 << thr))) |
| 2418 | kvmppc_ipi_thread(pcpu + thr); |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 2419 | |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 2420 | vc->vcore_state = VCORE_RUNNING; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2421 | preempt_disable(); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2422 | |
| 2423 | trace_kvmppc_run_core(vc, 0); |
| 2424 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2425 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
| 2426 | list_for_each_entry(pvc, &core_info.vcs[sub], preempt_list) |
| 2427 | spin_unlock(&pvc->lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2428 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2429 | kvm_guest_enter(); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2430 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 2431 | srcu_idx = srcu_read_lock(&vc->kvm->srcu); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2432 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 2433 | __kvmppc_vcore_entry(); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2434 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2435 | srcu_read_unlock(&vc->kvm->srcu, srcu_idx); |
| 2436 | |
| 2437 | spin_lock(&vc->lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2438 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2439 | vc->vcore_state = VCORE_EXITING; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2440 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2441 | /* wait for secondary threads to finish writing their state to memory */ |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 2442 | kvmppc_wait_for_nap(); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2443 | |
| 2444 | /* Return to whole-core mode if we split the core earlier */ |
| 2445 | if (split > 1) { |
| 2446 | unsigned long hid0 = mfspr(SPRN_HID0); |
| 2447 | unsigned long loops = 0; |
| 2448 | |
| 2449 | hid0 &= ~HID0_POWER8_DYNLPARDIS; |
| 2450 | stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE; |
| 2451 | mb(); |
| 2452 | mtspr(SPRN_HID0, hid0); |
| 2453 | isync(); |
| 2454 | for (;;) { |
| 2455 | hid0 = mfspr(SPRN_HID0); |
| 2456 | if (!(hid0 & stat_bit)) |
| 2457 | break; |
| 2458 | cpu_relax(); |
| 2459 | ++loops; |
| 2460 | } |
| 2461 | split_info.do_nap = 0; |
| 2462 | } |
| 2463 | |
| 2464 | /* Let secondaries go back to the offline loop */ |
| 2465 | for (i = 0; i < threads_per_subcore; ++i) { |
| 2466 | kvmppc_release_hwthread(pcpu + i); |
| 2467 | if (sip && sip->napped[i]) |
| 2468 | kvmppc_ipi_thread(pcpu + i); |
| 2469 | } |
| 2470 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2471 | spin_unlock(&vc->lock); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2472 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2473 | /* make sure updates to secondary vcpu structs are visible now */ |
| 2474 | smp_mb(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2475 | kvm_guest_exit(); |
| 2476 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2477 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
| 2478 | list_for_each_entry_safe(pvc, vcnext, &core_info.vcs[sub], |
| 2479 | preempt_list) |
| 2480 | post_guest_process(pvc, pvc == vc); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2481 | |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 2482 | spin_lock(&vc->lock); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2483 | preempt_enable(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2484 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2485 | out: |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2486 | vc->vcore_state = VCORE_INACTIVE; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2487 | trace_kvmppc_run_core(vc, 1); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2490 | /* |
| 2491 | * Wait for some other vcpu thread to execute us, and |
| 2492 | * wake us up when we need to handle something in the host. |
| 2493 | */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2494 | static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc, |
| 2495 | struct kvm_vcpu *vcpu, int wait_state) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2496 | { |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2497 | DEFINE_WAIT(wait); |
| 2498 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2499 | prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2500 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
| 2501 | spin_unlock(&vc->lock); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2502 | schedule(); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2503 | spin_lock(&vc->lock); |
| 2504 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2505 | finish_wait(&vcpu->arch.cpu_run, &wait); |
| 2506 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2507 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2508 | /* |
| 2509 | * All the vcpus in this vcore are idle, so wait for a decrementer |
| 2510 | * or external interrupt to one of the vcpus. vc->lock is held. |
| 2511 | */ |
| 2512 | static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) |
| 2513 | { |
Suresh E. Warrier | 1bc5d59 | 2014-11-03 15:52:00 +1100 | [diff] [blame] | 2514 | struct kvm_vcpu *vcpu; |
| 2515 | int do_sleep = 1; |
| 2516 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2517 | DEFINE_WAIT(wait); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2518 | |
| 2519 | prepare_to_wait(&vc->wq, &wait, TASK_INTERRUPTIBLE); |
Suresh E. Warrier | 1bc5d59 | 2014-11-03 15:52:00 +1100 | [diff] [blame] | 2520 | |
| 2521 | /* |
| 2522 | * Check one last time for pending exceptions and ceded state after |
| 2523 | * we put ourselves on the wait queue |
| 2524 | */ |
| 2525 | list_for_each_entry(vcpu, &vc->runnable_threads, arch.run_list) { |
| 2526 | if (vcpu->arch.pending_exceptions || !vcpu->arch.ceded) { |
| 2527 | do_sleep = 0; |
| 2528 | break; |
| 2529 | } |
| 2530 | } |
| 2531 | |
| 2532 | if (!do_sleep) { |
| 2533 | finish_wait(&vc->wq, &wait); |
| 2534 | return; |
| 2535 | } |
| 2536 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2537 | vc->vcore_state = VCORE_SLEEPING; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2538 | trace_kvmppc_vcore_blocked(vc, 0); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2539 | spin_unlock(&vc->lock); |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 2540 | schedule(); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2541 | finish_wait(&vc->wq, &wait); |
| 2542 | spin_lock(&vc->lock); |
| 2543 | vc->vcore_state = VCORE_INACTIVE; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2544 | trace_kvmppc_vcore_blocked(vc, 1); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | static int kvmppc_run_vcpu(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
| 2548 | { |
| 2549 | int n_ceded; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2550 | struct kvmppc_vcore *vc; |
| 2551 | struct kvm_vcpu *v, *vn; |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 2552 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2553 | trace_kvmppc_run_vcpu_enter(vcpu); |
| 2554 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2555 | kvm_run->exit_reason = 0; |
| 2556 | vcpu->arch.ret = RESUME_GUEST; |
| 2557 | vcpu->arch.trap = 0; |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 2558 | kvmppc_update_vpas(vcpu); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2559 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2560 | /* |
| 2561 | * Synchronize with other threads in this virtual core |
| 2562 | */ |
| 2563 | vc = vcpu->arch.vcore; |
| 2564 | spin_lock(&vc->lock); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2565 | vcpu->arch.ceded = 0; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2566 | vcpu->arch.run_task = current; |
| 2567 | vcpu->arch.kvm_run = kvm_run; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2568 | vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2569 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2570 | vcpu->arch.busy_preempt = TB_NIL; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2571 | list_add_tail(&vcpu->arch.run_list, &vc->runnable_threads); |
| 2572 | ++vc->n_runnable; |
| 2573 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2574 | /* |
| 2575 | * This happens the first time this is called for a vcpu. |
| 2576 | * If the vcore is already running, we may be able to start |
| 2577 | * this thread straight away and have it join in. |
| 2578 | */ |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2579 | if (!signal_pending(current)) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2580 | if (vc->vcore_state == VCORE_PIGGYBACK) { |
| 2581 | struct kvmppc_vcore *mvc = vc->master_vcore; |
| 2582 | if (spin_trylock(&mvc->lock)) { |
| 2583 | if (mvc->vcore_state == VCORE_RUNNING && |
| 2584 | !VCORE_IS_EXITING(mvc)) { |
| 2585 | kvmppc_create_dtl_entry(vcpu, vc); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2586 | kvmppc_start_thread(vcpu, vc); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2587 | trace_kvm_guest_enter(vcpu); |
| 2588 | } |
| 2589 | spin_unlock(&mvc->lock); |
| 2590 | } |
| 2591 | } else if (vc->vcore_state == VCORE_RUNNING && |
| 2592 | !VCORE_IS_EXITING(vc)) { |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 2593 | kvmppc_create_dtl_entry(vcpu, vc); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2594 | kvmppc_start_thread(vcpu, vc); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2595 | trace_kvm_guest_enter(vcpu); |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2596 | } else if (vc->vcore_state == VCORE_SLEEPING) { |
| 2597 | wake_up(&vc->wq); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2598 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2599 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2600 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2601 | |
| 2602 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
| 2603 | !signal_pending(current)) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2604 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
| 2605 | kvmppc_vcore_end_preempt(vc); |
| 2606 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2607 | if (vc->vcore_state != VCORE_INACTIVE) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2608 | kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2609 | continue; |
| 2610 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2611 | list_for_each_entry_safe(v, vn, &vc->runnable_threads, |
| 2612 | arch.run_list) { |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 2613 | kvmppc_core_prepare_to_enter(v); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2614 | if (signal_pending(v->arch.run_task)) { |
| 2615 | kvmppc_remove_runnable(vc, v); |
| 2616 | v->stat.signal_exits++; |
| 2617 | v->arch.kvm_run->exit_reason = KVM_EXIT_INTR; |
| 2618 | v->arch.ret = -EINTR; |
| 2619 | wake_up(&v->arch.cpu_run); |
| 2620 | } |
| 2621 | } |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2622 | if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 2623 | break; |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2624 | n_ceded = 0; |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 2625 | list_for_each_entry(v, &vc->runnable_threads, arch.run_list) { |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2626 | if (!v->arch.pending_exceptions) |
| 2627 | n_ceded += v->arch.ceded; |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 2628 | else |
| 2629 | v->arch.ceded = 0; |
| 2630 | } |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2631 | vc->runner = vcpu; |
| 2632 | if (n_ceded == vc->n_runnable) { |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2633 | kvmppc_vcore_blocked(vc); |
Konstantin Khlebnikov | c56dadf | 2015-07-15 12:52:03 +0300 | [diff] [blame] | 2634 | } else if (need_resched()) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2635 | kvmppc_vcore_preempt(vc); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2636 | /* Let something else run */ |
| 2637 | cond_resched_lock(&vc->lock); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2638 | if (vc->vcore_state == VCORE_PREEMPT) |
| 2639 | kvmppc_vcore_end_preempt(vc); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2640 | } else { |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2641 | kvmppc_run_core(vc); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2642 | } |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 2643 | vc->runner = NULL; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2646 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
| 2647 | (vc->vcore_state == VCORE_RUNNING || |
Paul Mackerras | 5fc3e64 | 2015-09-18 13:13:44 +1000 | [diff] [blame] | 2648 | vc->vcore_state == VCORE_EXITING || |
| 2649 | vc->vcore_state == VCORE_PIGGYBACK)) |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2650 | kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE); |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2651 | |
Paul Mackerras | 5fc3e64 | 2015-09-18 13:13:44 +1000 | [diff] [blame] | 2652 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
| 2653 | kvmppc_vcore_end_preempt(vc); |
| 2654 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2655 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
| 2656 | kvmppc_remove_runnable(vc, vcpu); |
| 2657 | vcpu->stat.signal_exits++; |
| 2658 | kvm_run->exit_reason = KVM_EXIT_INTR; |
| 2659 | vcpu->arch.ret = -EINTR; |
| 2660 | } |
| 2661 | |
| 2662 | if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) { |
| 2663 | /* Wake up some vcpu to run the core */ |
| 2664 | v = list_first_entry(&vc->runnable_threads, |
| 2665 | struct kvm_vcpu, arch.run_list); |
| 2666 | wake_up(&v->arch.cpu_run); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2667 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2668 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2669 | trace_kvmppc_run_vcpu_exit(vcpu, kvm_run); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2670 | spin_unlock(&vc->lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2671 | return vcpu->arch.ret; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2672 | } |
| 2673 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2674 | static int kvmppc_vcpu_run_hv(struct kvm_run *run, struct kvm_vcpu *vcpu) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2675 | { |
| 2676 | int r; |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 2677 | int srcu_idx; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2678 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 2679 | if (!vcpu->arch.sane) { |
| 2680 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 2681 | return -EINVAL; |
| 2682 | } |
| 2683 | |
Scott Wood | 25051b5a | 2011-11-08 18:23:23 -0600 | [diff] [blame] | 2684 | kvmppc_core_prepare_to_enter(vcpu); |
| 2685 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2686 | /* No need to go into the guest when all we'll do is come back out */ |
| 2687 | if (signal_pending(current)) { |
| 2688 | run->exit_reason = KVM_EXIT_INTR; |
| 2689 | return -EINTR; |
| 2690 | } |
| 2691 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2692 | atomic_inc(&vcpu->kvm->arch.vcpus_running); |
Aneesh Kumar K.V | 31037ec | 2015-03-20 20:39:42 +1100 | [diff] [blame] | 2693 | /* Order vcpus_running vs. hpte_setup_done, see kvmppc_alloc_reset_hpt */ |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2694 | smp_mb(); |
| 2695 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 2696 | /* On the first time here, set up HTAB and VRMA */ |
Aneesh Kumar K.V | 31037ec | 2015-03-20 20:39:42 +1100 | [diff] [blame] | 2697 | if (!vcpu->kvm->arch.hpte_setup_done) { |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2698 | r = kvmppc_hv_setup_htab_rma(vcpu); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2699 | if (r) |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2700 | goto out; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2701 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2702 | |
| 2703 | flush_fp_to_thread(current); |
| 2704 | flush_altivec_to_thread(current); |
| 2705 | flush_vsx_to_thread(current); |
| 2706 | vcpu->arch.wqp = &vcpu->arch.vcore->wq; |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 2707 | vcpu->arch.pgdir = current->mm->pgd; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2708 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2709 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2710 | do { |
| 2711 | r = kvmppc_run_vcpu(run, vcpu); |
| 2712 | |
| 2713 | if (run->exit_reason == KVM_EXIT_PAPR_HCALL && |
| 2714 | !(vcpu->arch.shregs.msr & MSR_PR)) { |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2715 | trace_kvm_hcall_enter(vcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2716 | r = kvmppc_pseries_do_hcall(vcpu); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 2717 | trace_kvm_hcall_exit(vcpu, r); |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 2718 | kvmppc_core_prepare_to_enter(vcpu); |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 2719 | } else if (r == RESUME_PAGE_FAULT) { |
| 2720 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
| 2721 | r = kvmppc_book3s_hv_page_fault(run, vcpu, |
| 2722 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); |
| 2723 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2724 | } |
Greg Kurz | e59d24e | 2014-02-06 17:36:56 +0100 | [diff] [blame] | 2725 | } while (is_kvmppc_resume_guest(r)); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2726 | |
| 2727 | out: |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2728 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2729 | atomic_dec(&vcpu->kvm->arch.vcpus_running); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2730 | return r; |
| 2731 | } |
| 2732 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2733 | static void kvmppc_add_seg_page_size(struct kvm_ppc_one_seg_page_size **sps, |
| 2734 | int linux_psize) |
| 2735 | { |
| 2736 | struct mmu_psize_def *def = &mmu_psize_defs[linux_psize]; |
| 2737 | |
| 2738 | if (!def->shift) |
| 2739 | return; |
| 2740 | (*sps)->page_shift = def->shift; |
| 2741 | (*sps)->slb_enc = def->sllp; |
| 2742 | (*sps)->enc[0].page_shift = def->shift; |
Aneesh Kumar K.V | b1022fb | 2013-04-28 09:37:35 +0000 | [diff] [blame] | 2743 | (*sps)->enc[0].pte_enc = def->penc[linux_psize]; |
Aneesh Kumar K.V | 1f365bb | 2014-05-06 23:31:36 +0530 | [diff] [blame] | 2744 | /* |
| 2745 | * Add 16MB MPSS support if host supports it |
| 2746 | */ |
| 2747 | if (linux_psize != MMU_PAGE_16M && def->penc[MMU_PAGE_16M] != -1) { |
| 2748 | (*sps)->enc[1].page_shift = 24; |
| 2749 | (*sps)->enc[1].pte_enc = def->penc[MMU_PAGE_16M]; |
| 2750 | } |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2751 | (*sps)++; |
| 2752 | } |
| 2753 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2754 | static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, |
| 2755 | struct kvm_ppc_smmu_info *info) |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2756 | { |
| 2757 | struct kvm_ppc_one_seg_page_size *sps; |
| 2758 | |
| 2759 | info->flags = KVM_PPC_PAGE_SIZES_REAL; |
| 2760 | if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) |
| 2761 | info->flags |= KVM_PPC_1T_SEGMENTS; |
| 2762 | info->slb_size = mmu_slb_size; |
| 2763 | |
| 2764 | /* We only support these sizes for now, and no muti-size segments */ |
| 2765 | sps = &info->sps[0]; |
| 2766 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_4K); |
| 2767 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_64K); |
| 2768 | kvmppc_add_seg_page_size(&sps, MMU_PAGE_16M); |
| 2769 | |
| 2770 | return 0; |
| 2771 | } |
| 2772 | |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2773 | /* |
| 2774 | * Get (and clear) the dirty memory log for a memory slot. |
| 2775 | */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2776 | static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm, |
| 2777 | struct kvm_dirty_log *log) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2778 | { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 2779 | struct kvm_memslots *slots; |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2780 | struct kvm_memory_slot *memslot; |
| 2781 | int r; |
| 2782 | unsigned long n; |
| 2783 | |
| 2784 | mutex_lock(&kvm->slots_lock); |
| 2785 | |
| 2786 | r = -EINVAL; |
Alex Williamson | bbacc0c | 2012-12-10 10:33:09 -0700 | [diff] [blame] | 2787 | if (log->slot >= KVM_USER_MEM_SLOTS) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2788 | goto out; |
| 2789 | |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 2790 | slots = kvm_memslots(kvm); |
| 2791 | memslot = id_to_memslot(slots, log->slot); |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2792 | r = -ENOENT; |
| 2793 | if (!memslot->dirty_bitmap) |
| 2794 | goto out; |
| 2795 | |
| 2796 | n = kvm_dirty_bitmap_bytes(memslot); |
| 2797 | memset(memslot->dirty_bitmap, 0, n); |
| 2798 | |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2799 | r = kvmppc_hv_get_dirty_log(kvm, memslot, memslot->dirty_bitmap); |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 2800 | if (r) |
| 2801 | goto out; |
| 2802 | |
| 2803 | r = -EFAULT; |
| 2804 | if (copy_to_user(log->dirty_bitmap, memslot->dirty_bitmap, n)) |
| 2805 | goto out; |
| 2806 | |
| 2807 | r = 0; |
| 2808 | out: |
| 2809 | mutex_unlock(&kvm->slots_lock); |
| 2810 | return r; |
| 2811 | } |
| 2812 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2813 | static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *free, |
| 2814 | struct kvm_memory_slot *dont) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2815 | { |
| 2816 | if (!dont || free->arch.rmap != dont->arch.rmap) { |
| 2817 | vfree(free->arch.rmap); |
| 2818 | free->arch.rmap = NULL; |
| 2819 | } |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2820 | } |
| 2821 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2822 | static int kvmppc_core_create_memslot_hv(struct kvm_memory_slot *slot, |
| 2823 | unsigned long npages) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2824 | { |
| 2825 | slot->arch.rmap = vzalloc(npages * sizeof(*slot->arch.rmap)); |
| 2826 | if (!slot->arch.rmap) |
| 2827 | return -ENOMEM; |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2828 | |
| 2829 | return 0; |
| 2830 | } |
| 2831 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2832 | static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, |
| 2833 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 2834 | const struct kvm_userspace_memory_region *mem) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2835 | { |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
| 2838 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2839 | static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 2840 | const struct kvm_userspace_memory_region *mem, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 2841 | const struct kvm_memory_slot *old, |
| 2842 | const struct kvm_memory_slot *new) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2843 | { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2844 | unsigned long npages = mem->memory_size >> PAGE_SHIFT; |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 2845 | struct kvm_memslots *slots; |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2846 | struct kvm_memory_slot *memslot; |
| 2847 | |
Takuya Yoshikawa | 8482644 | 2013-02-27 19:45:25 +0900 | [diff] [blame] | 2848 | if (npages && old->npages) { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2849 | /* |
| 2850 | * If modifying a memslot, reset all the rmap dirty bits. |
| 2851 | * If this is a new memslot, we don't need to do anything |
| 2852 | * since the rmap array starts out as all zeroes, |
| 2853 | * i.e. no pages are dirty. |
| 2854 | */ |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 2855 | slots = kvm_memslots(kvm); |
| 2856 | memslot = id_to_memslot(slots, mem->slot); |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 2857 | kvmppc_hv_get_dirty_log(kvm, memslot, NULL); |
| 2858 | } |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2861 | /* |
| 2862 | * Update LPCR values in kvm->arch and in vcores. |
| 2863 | * Caller must hold kvm->lock. |
| 2864 | */ |
| 2865 | void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask) |
| 2866 | { |
| 2867 | long int i; |
| 2868 | u32 cores_done = 0; |
| 2869 | |
| 2870 | if ((kvm->arch.lpcr & mask) == lpcr) |
| 2871 | return; |
| 2872 | |
| 2873 | kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr; |
| 2874 | |
| 2875 | for (i = 0; i < KVM_MAX_VCORES; ++i) { |
| 2876 | struct kvmppc_vcore *vc = kvm->arch.vcores[i]; |
| 2877 | if (!vc) |
| 2878 | continue; |
| 2879 | spin_lock(&vc->lock); |
| 2880 | vc->lpcr = (vc->lpcr & ~mask) | lpcr; |
| 2881 | spin_unlock(&vc->lock); |
| 2882 | if (++cores_done >= kvm->arch.online_vcores) |
| 2883 | break; |
| 2884 | } |
| 2885 | } |
| 2886 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2887 | static void kvmppc_mmu_destroy_hv(struct kvm_vcpu *vcpu) |
| 2888 | { |
| 2889 | return; |
| 2890 | } |
| 2891 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2892 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2893 | { |
| 2894 | int err = 0; |
| 2895 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2896 | unsigned long hva; |
| 2897 | struct kvm_memory_slot *memslot; |
| 2898 | struct vm_area_struct *vma; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2899 | unsigned long lpcr = 0, senc; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2900 | unsigned long psize, porder; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2901 | int srcu_idx; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2902 | |
| 2903 | mutex_lock(&kvm->lock); |
Aneesh Kumar K.V | 31037ec | 2015-03-20 20:39:42 +1100 | [diff] [blame] | 2904 | if (kvm->arch.hpte_setup_done) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2905 | goto out; /* another vcpu beat us to it */ |
| 2906 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2907 | /* Allocate hashed page table (if not done already) and reset it */ |
| 2908 | if (!kvm->arch.hpt_virt) { |
| 2909 | err = kvmppc_alloc_hpt(kvm, NULL); |
| 2910 | if (err) { |
| 2911 | pr_err("KVM: Couldn't alloc HPT\n"); |
| 2912 | goto out; |
| 2913 | } |
| 2914 | } |
| 2915 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2916 | /* Look up the memslot for guest physical address 0 */ |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2917 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2918 | memslot = gfn_to_memslot(kvm, 0); |
| 2919 | |
| 2920 | /* We must have some memory at 0 by now */ |
| 2921 | err = -EINVAL; |
| 2922 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2923 | goto out_srcu; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2924 | |
| 2925 | /* Look up the VMA for the start of this memory slot */ |
| 2926 | hva = memslot->userspace_addr; |
| 2927 | down_read(¤t->mm->mmap_sem); |
| 2928 | vma = find_vma(current->mm, hva); |
| 2929 | if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO)) |
| 2930 | goto up_out; |
| 2931 | |
| 2932 | psize = vma_kernel_pagesize(vma); |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 2933 | porder = __ilog2(psize); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2934 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2935 | up_read(¤t->mm->mmap_sem); |
| 2936 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 2937 | /* We can handle 4k, 64k or 16M pages in the VRMA */ |
| 2938 | err = -EINVAL; |
| 2939 | if (!(psize == 0x1000 || psize == 0x10000 || |
| 2940 | psize == 0x1000000)) |
| 2941 | goto out_srcu; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2942 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 2943 | /* Update VRMASD field in the LPCR */ |
| 2944 | senc = slb_pgsize_encoding(psize); |
| 2945 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | |
| 2946 | (VRMA_VSID << SLB_VSID_SHIFT_1T); |
| 2947 | /* the -4 is to account for senc values starting at 0x10 */ |
| 2948 | lpcr = senc << (LPCR_VRMASD_SH - 4); |
Paul Mackerras | da9d1d7 | 2011-12-12 12:31:41 +0000 | [diff] [blame] | 2949 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 2950 | /* Create HPTEs in the hash page table for the VRMA */ |
| 2951 | kvmppc_map_vrma(vcpu, memslot, porder); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2952 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 2953 | kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD); |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2954 | |
Aneesh Kumar K.V | 31037ec | 2015-03-20 20:39:42 +1100 | [diff] [blame] | 2955 | /* Order updates to kvm->arch.lpcr etc. vs. hpte_setup_done */ |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2956 | smp_wmb(); |
Aneesh Kumar K.V | 31037ec | 2015-03-20 20:39:42 +1100 | [diff] [blame] | 2957 | kvm->arch.hpte_setup_done = 1; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2958 | err = 0; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 2959 | out_srcu: |
| 2960 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2961 | out: |
| 2962 | mutex_unlock(&kvm->lock); |
| 2963 | return err; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2964 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 2965 | up_out: |
| 2966 | up_read(¤t->mm->mmap_sem); |
Lai Jiangshan | 505d642 | 2013-03-16 00:50:49 +0800 | [diff] [blame] | 2967 | goto out_srcu; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2968 | } |
| 2969 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2970 | static int kvmppc_core_init_vm_hv(struct kvm *kvm) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2971 | { |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2972 | unsigned long lpcr, lpid; |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 2973 | char buf[32]; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2974 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2975 | /* Allocate the guest's logical partition ID */ |
| 2976 | |
| 2977 | lpid = kvmppc_alloc_lpid(); |
Chen Gang | 5d226ae | 2013-07-22 14:32:35 +0800 | [diff] [blame] | 2978 | if ((long)lpid < 0) |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 2979 | return -ENOMEM; |
| 2980 | kvm->arch.lpid = lpid; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2981 | |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 2982 | /* |
| 2983 | * Since we don't flush the TLB when tearing down a VM, |
| 2984 | * and this lpid might have previously been used, |
| 2985 | * make sure we flush on each core before running the new VM. |
| 2986 | */ |
| 2987 | cpumask_setall(&kvm->arch.need_tlb_flush); |
| 2988 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2989 | /* Start out with the default set of hcalls enabled */ |
| 2990 | memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls, |
| 2991 | sizeof(kvm->arch.enabled_hcalls)); |
| 2992 | |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 2993 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 2994 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 2995 | /* Init LPCR for virtual RMA mode */ |
| 2996 | kvm->arch.host_lpid = mfspr(SPRN_LPID); |
| 2997 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); |
| 2998 | lpcr &= LPCR_PECE | LPCR_LPES; |
| 2999 | lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | |
| 3000 | LPCR_VPM0 | LPCR_VPM1; |
| 3001 | kvm->arch.vrma_slb_v = SLB_VSID_B_1T | |
| 3002 | (VRMA_VSID << SLB_VSID_SHIFT_1T); |
| 3003 | /* On POWER8 turn on online bit to enable PURR/SPURR */ |
| 3004 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 3005 | lpcr |= LPCR_ONL; |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 3006 | kvm->arch.lpcr = lpcr; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 3007 | |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 3008 | /* |
Michael Ellerman | 441c19c | 2014-05-23 18:15:25 +1000 | [diff] [blame] | 3009 | * Track that we now have a HV mode VM active. This blocks secondary |
| 3010 | * CPU threads from coming online. |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 3011 | */ |
Michael Ellerman | 441c19c | 2014-05-23 18:15:25 +1000 | [diff] [blame] | 3012 | kvm_hv_vm_activated(); |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 3013 | |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 3014 | /* |
| 3015 | * Create a debugfs directory for the VM |
| 3016 | */ |
| 3017 | snprintf(buf, sizeof(buf), "vm%d", current->pid); |
| 3018 | kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir); |
| 3019 | if (!IS_ERR_OR_NULL(kvm->arch.debugfs_dir)) |
| 3020 | kvmppc_mmu_debugfs_init(kvm); |
| 3021 | |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 3022 | return 0; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3023 | } |
| 3024 | |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 3025 | static void kvmppc_free_vcores(struct kvm *kvm) |
| 3026 | { |
| 3027 | long int i; |
| 3028 | |
Paul Mackerras | 2331631 | 2015-10-21 16:03:14 +1100 | [diff] [blame^] | 3029 | for (i = 0; i < KVM_MAX_VCORES; ++i) |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 3030 | kfree(kvm->arch.vcores[i]); |
| 3031 | kvm->arch.online_vcores = 0; |
| 3032 | } |
| 3033 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3034 | static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3035 | { |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 3036 | debugfs_remove_recursive(kvm->arch.debugfs_dir); |
| 3037 | |
Michael Ellerman | 441c19c | 2014-05-23 18:15:25 +1000 | [diff] [blame] | 3038 | kvm_hv_vm_deactivated(); |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 3039 | |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 3040 | kvmppc_free_vcores(kvm); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 3041 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3042 | kvmppc_free_hpt(kvm); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3043 | } |
| 3044 | |
| 3045 | /* We don't need to emulate any privileged instructions or dcbz */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3046 | static int kvmppc_core_emulate_op_hv(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 3047 | unsigned int inst, int *advance) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3048 | { |
| 3049 | return EMULATE_FAIL; |
| 3050 | } |
| 3051 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3052 | static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn, |
| 3053 | ulong spr_val) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3054 | { |
| 3055 | return EMULATE_FAIL; |
| 3056 | } |
| 3057 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3058 | static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, |
| 3059 | ulong *spr_val) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3060 | { |
| 3061 | return EMULATE_FAIL; |
| 3062 | } |
| 3063 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3064 | static int kvmppc_core_check_processor_compat_hv(void) |
| 3065 | { |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 3066 | if (!cpu_has_feature(CPU_FTR_HVMODE) || |
| 3067 | !cpu_has_feature(CPU_FTR_ARCH_206)) |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3068 | return -EIO; |
| 3069 | return 0; |
| 3070 | } |
| 3071 | |
| 3072 | static long kvm_arch_vm_ioctl_hv(struct file *filp, |
| 3073 | unsigned int ioctl, unsigned long arg) |
| 3074 | { |
| 3075 | struct kvm *kvm __maybe_unused = filp->private_data; |
| 3076 | void __user *argp = (void __user *)arg; |
| 3077 | long r; |
| 3078 | |
| 3079 | switch (ioctl) { |
| 3080 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3081 | case KVM_PPC_ALLOCATE_HTAB: { |
| 3082 | u32 htab_order; |
| 3083 | |
| 3084 | r = -EFAULT; |
| 3085 | if (get_user(htab_order, (u32 __user *)argp)) |
| 3086 | break; |
| 3087 | r = kvmppc_alloc_reset_hpt(kvm, &htab_order); |
| 3088 | if (r) |
| 3089 | break; |
| 3090 | r = -EFAULT; |
| 3091 | if (put_user(htab_order, (u32 __user *)argp)) |
| 3092 | break; |
| 3093 | r = 0; |
| 3094 | break; |
| 3095 | } |
| 3096 | |
| 3097 | case KVM_PPC_GET_HTAB_FD: { |
| 3098 | struct kvm_get_htab_fd ghf; |
| 3099 | |
| 3100 | r = -EFAULT; |
| 3101 | if (copy_from_user(&ghf, argp, sizeof(ghf))) |
| 3102 | break; |
| 3103 | r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf); |
| 3104 | break; |
| 3105 | } |
| 3106 | |
| 3107 | default: |
| 3108 | r = -ENOTTY; |
| 3109 | } |
| 3110 | |
| 3111 | return r; |
| 3112 | } |
| 3113 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 3114 | /* |
| 3115 | * List of hcall numbers to enable by default. |
| 3116 | * For compatibility with old userspace, we enable by default |
| 3117 | * all hcalls that were implemented before the hcall-enabling |
| 3118 | * facility was added. Note this list should not include H_RTAS. |
| 3119 | */ |
| 3120 | static unsigned int default_hcall_list[] = { |
| 3121 | H_REMOVE, |
| 3122 | H_ENTER, |
| 3123 | H_READ, |
| 3124 | H_PROTECT, |
| 3125 | H_BULK_REMOVE, |
| 3126 | H_GET_TCE, |
| 3127 | H_PUT_TCE, |
| 3128 | H_SET_DABR, |
| 3129 | H_SET_XDABR, |
| 3130 | H_CEDE, |
| 3131 | H_PROD, |
| 3132 | H_CONFER, |
| 3133 | H_REGISTER_VPA, |
| 3134 | #ifdef CONFIG_KVM_XICS |
| 3135 | H_EOI, |
| 3136 | H_CPPR, |
| 3137 | H_IPI, |
| 3138 | H_IPOLL, |
| 3139 | H_XIRR, |
| 3140 | H_XIRR_X, |
| 3141 | #endif |
| 3142 | 0 |
| 3143 | }; |
| 3144 | |
| 3145 | static void init_default_hcalls(void) |
| 3146 | { |
| 3147 | int i; |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 3148 | unsigned int hcall; |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 3149 | |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 3150 | for (i = 0; default_hcall_list[i]; ++i) { |
| 3151 | hcall = default_hcall_list[i]; |
| 3152 | WARN_ON(!kvmppc_hcall_impl_hv(hcall)); |
| 3153 | __set_bit(hcall / 4, default_enabled_hcalls); |
| 3154 | } |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 3155 | } |
| 3156 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 3157 | static struct kvmppc_ops kvm_ops_hv = { |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3158 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, |
| 3159 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, |
| 3160 | .get_one_reg = kvmppc_get_one_reg_hv, |
| 3161 | .set_one_reg = kvmppc_set_one_reg_hv, |
| 3162 | .vcpu_load = kvmppc_core_vcpu_load_hv, |
| 3163 | .vcpu_put = kvmppc_core_vcpu_put_hv, |
| 3164 | .set_msr = kvmppc_set_msr_hv, |
| 3165 | .vcpu_run = kvmppc_vcpu_run_hv, |
| 3166 | .vcpu_create = kvmppc_core_vcpu_create_hv, |
| 3167 | .vcpu_free = kvmppc_core_vcpu_free_hv, |
| 3168 | .check_requests = kvmppc_core_check_requests_hv, |
| 3169 | .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv, |
| 3170 | .flush_memslot = kvmppc_core_flush_memslot_hv, |
| 3171 | .prepare_memory_region = kvmppc_core_prepare_memory_region_hv, |
| 3172 | .commit_memory_region = kvmppc_core_commit_memory_region_hv, |
| 3173 | .unmap_hva = kvm_unmap_hva_hv, |
| 3174 | .unmap_hva_range = kvm_unmap_hva_range_hv, |
| 3175 | .age_hva = kvm_age_hva_hv, |
| 3176 | .test_age_hva = kvm_test_age_hva_hv, |
| 3177 | .set_spte_hva = kvm_set_spte_hva_hv, |
| 3178 | .mmu_destroy = kvmppc_mmu_destroy_hv, |
| 3179 | .free_memslot = kvmppc_core_free_memslot_hv, |
| 3180 | .create_memslot = kvmppc_core_create_memslot_hv, |
| 3181 | .init_vm = kvmppc_core_init_vm_hv, |
| 3182 | .destroy_vm = kvmppc_core_destroy_vm_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3183 | .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv, |
| 3184 | .emulate_op = kvmppc_core_emulate_op_hv, |
| 3185 | .emulate_mtspr = kvmppc_core_emulate_mtspr_hv, |
| 3186 | .emulate_mfspr = kvmppc_core_emulate_mfspr_hv, |
| 3187 | .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv, |
| 3188 | .arch_vm_ioctl = kvm_arch_vm_ioctl_hv, |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 3189 | .hcall_implemented = kvmppc_hcall_impl_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3190 | }; |
| 3191 | |
| 3192 | static int kvmppc_book3s_init_hv(void) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3193 | { |
| 3194 | int r; |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 3195 | /* |
| 3196 | * FIXME!! Do we need to check on all cpus ? |
| 3197 | */ |
| 3198 | r = kvmppc_core_check_processor_compat_hv(); |
| 3199 | if (r < 0) |
Paul Mackerras | 739e242 | 2014-03-25 10:47:05 +1100 | [diff] [blame] | 3200 | return -ENODEV; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3201 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 3202 | kvm_ops_hv.owner = THIS_MODULE; |
| 3203 | kvmppc_hv_ops = &kvm_ops_hv; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3204 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 3205 | init_default_hcalls(); |
| 3206 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3207 | init_vcore_lists(); |
| 3208 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 3209 | r = kvmppc_mmu_hv_init(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3210 | return r; |
| 3211 | } |
| 3212 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3213 | static void kvmppc_book3s_exit_hv(void) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3214 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 3215 | kvmppc_hv_ops = NULL; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 3218 | module_init(kvmppc_book3s_init_hv); |
| 3219 | module_exit(kvmppc_book3s_exit_hv); |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 3220 | MODULE_LICENSE("GPL"); |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 3221 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
| 3222 | MODULE_ALIAS("devname:kvm"); |