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