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