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