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