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