Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
| 4 | * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Paul Mackerras <paulus@au1.ibm.com> |
| 8 | * Alexander Graf <agraf@suse.de> |
| 9 | * Kevin Wolf <mail@kevin-wolf.de> |
| 10 | * |
| 11 | * Description: KVM functions specific to running on Book 3S |
| 12 | * processors in hypervisor mode (specifically POWER7 and later). |
| 13 | * |
| 14 | * This file is derived from arch/powerpc/kvm/book3s.c, |
| 15 | * by Alexander Graf <agraf@suse.de>. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
| 18 | #include <linux/kvm_host.h> |
Thomas Meyer | 4bb817ed | 2017-09-03 14:19:31 +0200 | [diff] [blame] | 19 | #include <linux/kernel.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 20 | #include <linux/err.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/preempt.h> |
Ingo Molnar | 174cd4b | 2017-02-02 19:15:33 +0100 | [diff] [blame] | 23 | #include <linux/sched/signal.h> |
Ingo Molnar | 03441a3 | 2017-02-08 18:51:35 +0100 | [diff] [blame] | 24 | #include <linux/sched/stat.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 25 | #include <linux/delay.h> |
Paul Gortmaker | 66b15db | 2011-05-27 10:46:24 -0400 | [diff] [blame] | 26 | #include <linux/export.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 27 | #include <linux/fs.h> |
| 28 | #include <linux/anon_inodes.h> |
Gavin Shan | 07f8ab25 | 2016-05-11 11:15:55 +1000 | [diff] [blame] | 29 | #include <linux/cpu.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 30 | #include <linux/cpumask.h> |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 31 | #include <linux/spinlock.h> |
| 32 | #include <linux/page-flags.h> |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 33 | #include <linux/srcu.h> |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 34 | #include <linux/miscdevice.h> |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 35 | #include <linux/debugfs.h> |
Benjamin Herrenschmidt | d398914 | 2017-04-05 17:54:51 +1000 | [diff] [blame] | 36 | #include <linux/gfp.h> |
| 37 | #include <linux/vmalloc.h> |
| 38 | #include <linux/highmem.h> |
| 39 | #include <linux/hugetlb.h> |
| 40 | #include <linux/kvm_irqfd.h> |
| 41 | #include <linux/irqbypass.h> |
| 42 | #include <linux/module.h> |
| 43 | #include <linux/compiler.h> |
| 44 | #include <linux/of.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 45 | |
Luke Dashjr | d6ee76d | 2018-08-16 21:36:26 +0000 | [diff] [blame] | 46 | #include <asm/ftrace.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 47 | #include <asm/reg.h> |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 48 | #include <asm/ppc-opcode.h> |
Nicholas Piggin | 6de6638 | 2017-11-05 23:33:55 +1100 | [diff] [blame] | 49 | #include <asm/asm-prototypes.h> |
Paul Mackerras | 4bad777 | 2018-10-08 16:31:06 +1100 | [diff] [blame] | 50 | #include <asm/archrandom.h> |
Nicholas Piggin | e303c08 | 2018-04-01 15:50:35 +1000 | [diff] [blame] | 51 | #include <asm/debug.h> |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 52 | #include <asm/disassemble.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 53 | #include <asm/cputable.h> |
| 54 | #include <asm/cacheflush.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> |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame] | 74 | #include <asm/hw_breakpoint.h> |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 75 | #include <asm/kvm_book3s_uvmem.h> |
Bharata B Rao | c326225 | 2019-11-25 08:36:29 +0530 | [diff] [blame] | 76 | #include <asm/ultravisor.h> |
Peter Zijlstra | d6bdceb | 2020-05-29 22:41:01 +0200 | [diff] [blame] | 77 | #include <asm/dtl.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 78 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 79 | #include "book3s.h" |
| 80 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 81 | #define CREATE_TRACE_POINTS |
| 82 | #include "trace_hv.h" |
| 83 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 84 | /* #define EXIT_DEBUG */ |
| 85 | /* #define EXIT_DEBUG_SIMPLE */ |
| 86 | /* #define EXIT_DEBUG_INT */ |
| 87 | |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 88 | /* Used to indicate that a guest page fault needs to be handled */ |
| 89 | #define RESUME_PAGE_FAULT (RESUME_GUEST | RESUME_FLAG_ARCH1) |
Suresh Warrier | f7af520 | 2016-08-19 15:35:52 +1000 | [diff] [blame] | 90 | /* Used to indicate that a guest passthrough interrupt needs to be handled */ |
| 91 | #define RESUME_PASSTHROUGH (RESUME_GUEST | RESUME_FLAG_ARCH2) |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 92 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 93 | /* Used as a "null" value for timebase values */ |
| 94 | #define TB_NIL (~(u64)0) |
| 95 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 96 | static DECLARE_BITMAP(default_enabled_hcalls, MAX_HCALL_OPCODE/4 + 1); |
| 97 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 98 | static int dynamic_mt_modes = 6; |
Russell Currey | 57ad583f | 2017-01-12 14:54:13 +1100 | [diff] [blame] | 99 | module_param(dynamic_mt_modes, int, 0644); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 100 | 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] | 101 | static int target_smt_mode; |
Russell Currey | 57ad583f | 2017-01-12 14:54:13 +1100 | [diff] [blame] | 102 | module_param(target_smt_mode, int, 0644); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 103 | MODULE_PARM_DESC(target_smt_mode, "Target threads per core (0 = max)"); |
Stewart Smith | 9678cda | 2014-07-18 14:18:43 +1000 | [diff] [blame] | 104 | |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 105 | static bool indep_threads_mode = true; |
| 106 | module_param(indep_threads_mode, bool, S_IRUGO | S_IWUSR); |
| 107 | MODULE_PARM_DESC(indep_threads_mode, "Independent-threads mode (only on POWER9)"); |
| 108 | |
Paul Mackerras | aa22786 | 2018-09-12 10:42:12 +1000 | [diff] [blame] | 109 | static bool one_vm_per_core; |
| 110 | module_param(one_vm_per_core, bool, S_IRUGO | S_IWUSR); |
| 111 | MODULE_PARM_DESC(one_vm_per_core, "Only run vCPUs from the same VM on a core (requires indep_threads_mode=N)"); |
| 112 | |
Suresh E. Warrier | 520fe9c | 2015-12-21 16:33:57 -0600 | [diff] [blame] | 113 | #ifdef CONFIG_KVM_XICS |
| 114 | static struct kernel_param_ops module_param_ops = { |
| 115 | .set = param_set_int, |
| 116 | .get = param_get_int, |
| 117 | }; |
| 118 | |
Russell Currey | 57ad583f | 2017-01-12 14:54:13 +1100 | [diff] [blame] | 119 | module_param_cb(kvm_irq_bypass, &module_param_ops, &kvm_irq_bypass, 0644); |
Suresh Warrier | 644abbb | 2016-08-19 15:35:54 +1000 | [diff] [blame] | 120 | MODULE_PARM_DESC(kvm_irq_bypass, "Bypass passthrough interrupt optimization"); |
| 121 | |
Russell Currey | 57ad583f | 2017-01-12 14:54:13 +1100 | [diff] [blame] | 122 | module_param_cb(h_ipi_redirect, &module_param_ops, &h_ipi_redirect, 0644); |
Suresh E. Warrier | 520fe9c | 2015-12-21 16:33:57 -0600 | [diff] [blame] | 123 | MODULE_PARM_DESC(h_ipi_redirect, "Redirect H_IPI wakeup to a free host core"); |
| 124 | #endif |
| 125 | |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 126 | /* If set, guests are allowed to create and control nested guests */ |
| 127 | static bool nested = true; |
| 128 | module_param(nested, bool, S_IRUGO | S_IWUSR); |
| 129 | MODULE_PARM_DESC(nested, "Enable nested virtualization (only on POWER9)"); |
| 130 | |
| 131 | static inline bool nesting_enabled(struct kvm *kvm) |
| 132 | { |
| 133 | return kvm->arch.nested_enable && kvm_is_radix(kvm); |
| 134 | } |
| 135 | |
Paul Mackerras | 00608e1 | 2018-01-11 16:54:26 +1100 | [diff] [blame] | 136 | /* If set, the threads on each CPU core have to be in the same MMU mode */ |
| 137 | static bool no_mixing_hpt_and_radix; |
| 138 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 139 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 140 | |
Paul Mackerras | 7aa1584 | 2018-04-20 19:53:22 +1000 | [diff] [blame] | 141 | /* |
| 142 | * RWMR values for POWER8. These control the rate at which PURR |
| 143 | * and SPURR count and should be set according to the number of |
| 144 | * online threads in the vcore being run. |
| 145 | */ |
Nicholas Mc Guire | 0abb75b | 2018-07-07 11:07:25 +0200 | [diff] [blame] | 146 | #define RWMR_RPA_P8_1THREAD 0x164520C62609AECAUL |
| 147 | #define RWMR_RPA_P8_2THREAD 0x7FFF2908450D8DA9UL |
| 148 | #define RWMR_RPA_P8_3THREAD 0x164520C62609AECAUL |
| 149 | #define RWMR_RPA_P8_4THREAD 0x199A421245058DA9UL |
| 150 | #define RWMR_RPA_P8_5THREAD 0x164520C62609AECAUL |
| 151 | #define RWMR_RPA_P8_6THREAD 0x164520C62609AECAUL |
| 152 | #define RWMR_RPA_P8_7THREAD 0x164520C62609AECAUL |
| 153 | #define RWMR_RPA_P8_8THREAD 0x164520C62609AECAUL |
Paul Mackerras | 7aa1584 | 2018-04-20 19:53:22 +1000 | [diff] [blame] | 154 | |
| 155 | static unsigned long p8_rwmr_values[MAX_SMT_THREADS + 1] = { |
| 156 | RWMR_RPA_P8_1THREAD, |
| 157 | RWMR_RPA_P8_1THREAD, |
| 158 | RWMR_RPA_P8_2THREAD, |
| 159 | RWMR_RPA_P8_3THREAD, |
| 160 | RWMR_RPA_P8_4THREAD, |
| 161 | RWMR_RPA_P8_5THREAD, |
| 162 | RWMR_RPA_P8_6THREAD, |
| 163 | RWMR_RPA_P8_7THREAD, |
| 164 | RWMR_RPA_P8_8THREAD, |
| 165 | }; |
| 166 | |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 167 | static inline struct kvm_vcpu *next_runnable_thread(struct kvmppc_vcore *vc, |
| 168 | int *ip) |
| 169 | { |
| 170 | int i = *ip; |
| 171 | struct kvm_vcpu *vcpu; |
| 172 | |
| 173 | while (++i < MAX_SMT_THREADS) { |
| 174 | vcpu = READ_ONCE(vc->runnable_threads[i]); |
| 175 | if (vcpu) { |
| 176 | *ip = i; |
| 177 | return vcpu; |
| 178 | } |
| 179 | } |
| 180 | return NULL; |
| 181 | } |
| 182 | |
| 183 | /* Used to traverse the list of runnable threads for a given vcore */ |
| 184 | #define for_each_runnable_thread(i, vcpu, vc) \ |
| 185 | for (i = -1; (vcpu = next_runnable_thread(vc, &i)); ) |
| 186 | |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 187 | static bool kvmppc_ipi_thread(int cpu) |
| 188 | { |
Paul Mackerras | 1704a81 | 2016-11-18 08:47:08 +1100 | [diff] [blame] | 189 | unsigned long msg = PPC_DBELL_TYPE(PPC_DBELL_SERVER); |
| 190 | |
Paul Mackerras | f3c18e9 | 2018-10-08 16:31:05 +1100 | [diff] [blame] | 191 | /* If we're a nested hypervisor, fall back to ordinary IPIs for now */ |
| 192 | if (kvmhv_on_pseries()) |
| 193 | return false; |
| 194 | |
Paul Mackerras | 1704a81 | 2016-11-18 08:47:08 +1100 | [diff] [blame] | 195 | /* On POWER9 we can use msgsnd to IPI any cpu */ |
| 196 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
| 197 | msg |= get_hard_smp_processor_id(cpu); |
| 198 | smp_mb(); |
| 199 | __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); |
| 200 | return true; |
| 201 | } |
| 202 | |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 203 | /* On POWER8 for IPIs to threads in the same core, use msgsnd */ |
| 204 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 205 | preempt_disable(); |
| 206 | if (cpu_first_thread_sibling(cpu) == |
| 207 | cpu_first_thread_sibling(smp_processor_id())) { |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 208 | msg |= cpu_thread_in_core(cpu); |
| 209 | smp_mb(); |
| 210 | __asm__ __volatile__ (PPC_MSGSND(%0) : : "r" (msg)); |
| 211 | preempt_enable(); |
| 212 | return true; |
| 213 | } |
| 214 | preempt_enable(); |
| 215 | } |
| 216 | |
| 217 | #if defined(CONFIG_PPC_ICP_NATIVE) && defined(CONFIG_SMP) |
Paul Mackerras | f725758 | 2016-11-18 09:02:08 +1100 | [diff] [blame] | 218 | if (cpu >= 0 && cpu < nr_cpu_ids) { |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 219 | if (paca_ptrs[cpu]->kvm_hstate.xics_phys) { |
Paul Mackerras | f725758 | 2016-11-18 09:02:08 +1100 | [diff] [blame] | 220 | xics_wake_cpu(cpu); |
| 221 | return true; |
| 222 | } |
| 223 | opal_int_set_mfrr(get_hard_smp_processor_id(cpu), IPI_PRIORITY); |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 224 | return true; |
| 225 | } |
| 226 | #endif |
| 227 | |
| 228 | return false; |
| 229 | } |
| 230 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 231 | static void kvmppc_fast_vcpu_kick_hv(struct kvm_vcpu *vcpu) |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 232 | { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 233 | int cpu; |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 234 | struct rcuwait *waitp; |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 235 | |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 236 | waitp = kvm_arch_vcpu_get_wait(vcpu); |
| 237 | if (rcuwait_wake_up(waitp)) |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 238 | ++vcpu->stat.halt_wakeup; |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 239 | |
Paul Mackerras | 3deda5e | 2016-12-20 14:02:29 +1100 | [diff] [blame] | 240 | cpu = READ_ONCE(vcpu->arch.thread_cpu); |
| 241 | if (cpu >= 0 && kvmppc_ipi_thread(cpu)) |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 242 | return; |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 243 | |
| 244 | /* CPU points to the first thread of the core */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 245 | cpu = vcpu->cpu; |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 246 | if (cpu >= 0 && cpu < nr_cpu_ids && cpu_online(cpu)) |
| 247 | smp_send_reschedule(cpu); |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 248 | } |
| 249 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 250 | /* |
| 251 | * We use the vcpu_load/put functions to measure stolen time. |
| 252 | * Stolen time is counted as time when either the vcpu is able to |
| 253 | * run as part of a virtual core, but the task running the vcore |
| 254 | * is preempted or sleeping, or when the vcpu needs something done |
| 255 | * in the kernel by the task running the vcpu, but that task is |
| 256 | * preempted or sleeping. Those two things have to be counted |
| 257 | * separately, since one of the vcpu tasks will take on the job |
| 258 | * of running the core, and the other vcpu tasks in the vcore will |
| 259 | * sleep waiting for it to do that, but that sleep shouldn't count |
| 260 | * as stolen time. |
| 261 | * |
| 262 | * Hence we accumulate stolen time when the vcpu can run as part of |
| 263 | * a vcore using vc->stolen_tb, and the stolen time when the vcpu |
| 264 | * needs its task to do other things in the kernel (for example, |
| 265 | * service a page fault) in busy_stolen. We don't accumulate |
| 266 | * stolen time for a vcore when it is inactive, or for a vcpu |
| 267 | * when it is in state RUNNING or NOTREADY. NOTREADY is a bit of |
| 268 | * a misnomer; it means that the vcpu task is not executing in |
| 269 | * the KVM_VCPU_RUN ioctl, i.e. it is in userspace or elsewhere in |
| 270 | * the kernel. We don't have any way of dividing up that time |
| 271 | * between time that the vcpu is genuinely stopped, time that |
| 272 | * the task is actively working on behalf of the vcpu, and time |
| 273 | * that the task is preempted, so we don't count any of it as |
| 274 | * stolen. |
| 275 | * |
| 276 | * Updates to busy_stolen are protected by arch.tbacct_lock; |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 277 | * updates to vc->stolen_tb are protected by the vcore->stoltb_lock |
| 278 | * lock. The stolen times are measured in units of timebase ticks. |
| 279 | * (Note that the != TB_NIL checks below are purely defensive; |
| 280 | * they should never fail.) |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 281 | */ |
| 282 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 283 | static void kvmppc_core_start_stolen(struct kvmppc_vcore *vc) |
| 284 | { |
| 285 | unsigned long flags; |
| 286 | |
| 287 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 288 | vc->preempt_tb = mftb(); |
| 289 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
| 290 | } |
| 291 | |
| 292 | static void kvmppc_core_end_stolen(struct kvmppc_vcore *vc) |
| 293 | { |
| 294 | unsigned long flags; |
| 295 | |
| 296 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 297 | if (vc->preempt_tb != TB_NIL) { |
| 298 | vc->stolen_tb += mftb() - vc->preempt_tb; |
| 299 | vc->preempt_tb = TB_NIL; |
| 300 | } |
| 301 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
| 302 | } |
| 303 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 304 | 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] | 305 | { |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 306 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 307 | unsigned long flags; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 308 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 309 | /* |
| 310 | * We can test vc->runner without taking the vcore lock, |
| 311 | * because only this task ever sets vc->runner to this |
| 312 | * vcpu, and once it is set to this vcpu, only this task |
| 313 | * ever sets it to NULL. |
| 314 | */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 315 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
| 316 | kvmppc_core_end_stolen(vc); |
| 317 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 318 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 319 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST && |
| 320 | vcpu->arch.busy_preempt != TB_NIL) { |
| 321 | vcpu->arch.busy_stolen += mftb() - vcpu->arch.busy_preempt; |
| 322 | vcpu->arch.busy_preempt = TB_NIL; |
| 323 | } |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 324 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 327 | static void kvmppc_core_vcpu_put_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 328 | { |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 329 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 330 | unsigned long flags; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 331 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 332 | if (vc->runner == vcpu && vc->vcore_state >= VCORE_SLEEPING) |
| 333 | kvmppc_core_start_stolen(vc); |
| 334 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 335 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 336 | if (vcpu->arch.state == KVMPPC_VCPU_BUSY_IN_HOST) |
| 337 | vcpu->arch.busy_preempt = mftb(); |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 338 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 341 | static void kvmppc_set_pvr_hv(struct kvm_vcpu *vcpu, u32 pvr) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 342 | { |
| 343 | vcpu->arch.pvr = pvr; |
| 344 | } |
| 345 | |
Alistair Popple | 4cb4ade | 2020-06-02 15:53:25 +1000 | [diff] [blame] | 346 | /* Dummy value used in computing PCR value below */ |
| 347 | #define PCR_ARCH_31 (PCR_ARCH_300 << 1) |
| 348 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 349 | 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] | 350 | { |
Suraj Jitindar Singh | 2ee13be3 | 2016-11-14 11:35:08 +1100 | [diff] [blame] | 351 | unsigned long host_pcr_bit = 0, guest_pcr_bit = 0; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 352 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 353 | |
Suraj Jitindar Singh | 2ee13be3 | 2016-11-14 11:35:08 +1100 | [diff] [blame] | 354 | /* We can (emulate) our own architecture version and anything older */ |
Alistair Popple | 4cb4ade | 2020-06-02 15:53:25 +1000 | [diff] [blame] | 355 | if (cpu_has_feature(CPU_FTR_ARCH_31)) |
| 356 | host_pcr_bit = PCR_ARCH_31; |
| 357 | else if (cpu_has_feature(CPU_FTR_ARCH_300)) |
Suraj Jitindar Singh | 2ee13be3 | 2016-11-14 11:35:08 +1100 | [diff] [blame] | 358 | host_pcr_bit = PCR_ARCH_300; |
| 359 | else if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 360 | host_pcr_bit = PCR_ARCH_207; |
| 361 | else if (cpu_has_feature(CPU_FTR_ARCH_206)) |
| 362 | host_pcr_bit = PCR_ARCH_206; |
| 363 | else |
| 364 | host_pcr_bit = PCR_ARCH_205; |
| 365 | |
| 366 | /* Determine lowest PCR bit needed to run guest in given PVR level */ |
| 367 | guest_pcr_bit = host_pcr_bit; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 368 | if (arch_compat) { |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 369 | switch (arch_compat) { |
| 370 | case PVR_ARCH_205: |
Suraj Jitindar Singh | 2ee13be3 | 2016-11-14 11:35:08 +1100 | [diff] [blame] | 371 | guest_pcr_bit = PCR_ARCH_205; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 372 | break; |
| 373 | case PVR_ARCH_206: |
| 374 | case PVR_ARCH_206p: |
Suraj Jitindar Singh | 2ee13be3 | 2016-11-14 11:35:08 +1100 | [diff] [blame] | 375 | guest_pcr_bit = PCR_ARCH_206; |
Paul Mackerras | 5557ae0 | 2014-01-08 21:25:24 +1100 | [diff] [blame] | 376 | break; |
| 377 | case PVR_ARCH_207: |
Suraj Jitindar Singh | 2ee13be3 | 2016-11-14 11:35:08 +1100 | [diff] [blame] | 378 | guest_pcr_bit = PCR_ARCH_207; |
| 379 | break; |
| 380 | case PVR_ARCH_300: |
| 381 | guest_pcr_bit = PCR_ARCH_300; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 382 | break; |
Alistair Popple | 4cb4ade | 2020-06-02 15:53:25 +1000 | [diff] [blame] | 383 | case PVR_ARCH_31: |
| 384 | guest_pcr_bit = PCR_ARCH_31; |
| 385 | break; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 386 | default: |
| 387 | return -EINVAL; |
| 388 | } |
| 389 | } |
| 390 | |
Suraj Jitindar Singh | 2ee13be3 | 2016-11-14 11:35:08 +1100 | [diff] [blame] | 391 | /* Check requested PCR bits don't exceed our capabilities */ |
| 392 | if (guest_pcr_bit > host_pcr_bit) |
| 393 | return -EINVAL; |
| 394 | |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 395 | spin_lock(&vc->lock); |
| 396 | vc->arch_compat = arch_compat; |
Jordan Niethe | 13c7bb3 | 2019-09-17 10:46:05 +1000 | [diff] [blame] | 397 | /* |
| 398 | * Set all PCR bits for which guest_pcr_bit <= bit < host_pcr_bit |
| 399 | * Also set all reserved PCR bits |
| 400 | */ |
| 401 | vc->pcr = (host_pcr_bit - guest_pcr_bit) | PCR_MASK; |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 402 | spin_unlock(&vc->lock); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 407 | static void kvmppc_dump_regs(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 408 | { |
| 409 | int r; |
| 410 | |
| 411 | pr_err("vcpu %p (%d):\n", vcpu, vcpu->vcpu_id); |
| 412 | pr_err("pc = %.16lx msr = %.16llx trap = %x\n", |
Simon Guo | 173c520 | 2018-05-07 14:20:08 +0800 | [diff] [blame] | 413 | vcpu->arch.regs.nip, vcpu->arch.shregs.msr, vcpu->arch.trap); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 414 | for (r = 0; r < 16; ++r) |
| 415 | pr_err("r%2d = %.16lx r%d = %.16lx\n", |
| 416 | r, kvmppc_get_gpr(vcpu, r), |
| 417 | r+16, kvmppc_get_gpr(vcpu, r+16)); |
| 418 | pr_err("ctr = %.16lx lr = %.16lx\n", |
Simon Guo | 173c520 | 2018-05-07 14:20:08 +0800 | [diff] [blame] | 419 | vcpu->arch.regs.ctr, vcpu->arch.regs.link); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 420 | pr_err("srr0 = %.16llx srr1 = %.16llx\n", |
| 421 | vcpu->arch.shregs.srr0, vcpu->arch.shregs.srr1); |
| 422 | pr_err("sprg0 = %.16llx sprg1 = %.16llx\n", |
| 423 | vcpu->arch.shregs.sprg0, vcpu->arch.shregs.sprg1); |
| 424 | pr_err("sprg2 = %.16llx sprg3 = %.16llx\n", |
| 425 | vcpu->arch.shregs.sprg2, vcpu->arch.shregs.sprg3); |
Paul Mackerras | fd0944b | 2018-10-08 16:30:58 +1100 | [diff] [blame] | 426 | pr_err("cr = %.8lx xer = %.16lx dsisr = %.8x\n", |
| 427 | vcpu->arch.regs.ccr, vcpu->arch.regs.xer, vcpu->arch.shregs.dsisr); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 428 | pr_err("dar = %.16llx\n", vcpu->arch.shregs.dar); |
| 429 | pr_err("fault dar = %.16lx dsisr = %.8x\n", |
| 430 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); |
| 431 | pr_err("SLB (%d entries):\n", vcpu->arch.slb_max); |
| 432 | for (r = 0; r < vcpu->arch.slb_max; ++r) |
| 433 | pr_err(" ESID = %.16llx VSID = %.16llx\n", |
| 434 | vcpu->arch.slb[r].orige, vcpu->arch.slb[r].origv); |
| 435 | pr_err("lpcr = %.16lx sdr1 = %.16lx last_inst = %.8x\n", |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 436 | vcpu->arch.vcore->lpcr, vcpu->kvm->arch.sdr1, |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 437 | vcpu->arch.last_inst); |
| 438 | } |
| 439 | |
Thomas Huth | 5358a96 | 2015-05-22 09:25:02 +0200 | [diff] [blame] | 440 | static struct kvm_vcpu *kvmppc_find_vcpu(struct kvm *kvm, int id) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 441 | { |
Paul Mackerras | 5a3f493 | 2019-05-23 16:36:32 +1000 | [diff] [blame] | 442 | return kvm_get_vcpu_by_id(kvm, id); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | static void init_vpa(struct kvm_vcpu *vcpu, struct lppaca *vpa) |
| 446 | { |
Anton Blanchard | f13c13a | 2013-08-07 02:01:26 +1000 | [diff] [blame] | 447 | vpa->__old_status |= LPPACA_OLD_SHARED_PROC; |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 448 | vpa->yield_count = cpu_to_be32(1); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 451 | static int set_vpa(struct kvm_vcpu *vcpu, struct kvmppc_vpa *v, |
| 452 | unsigned long addr, unsigned long len) |
| 453 | { |
| 454 | /* check address is cacheline aligned */ |
| 455 | if (addr & (L1_CACHE_BYTES - 1)) |
| 456 | return -EINVAL; |
| 457 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 458 | if (v->next_gpa != addr || v->len != len) { |
| 459 | v->next_gpa = addr; |
| 460 | v->len = addr ? len : 0; |
| 461 | v->update_pending = 1; |
| 462 | } |
| 463 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 464 | return 0; |
| 465 | } |
| 466 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 467 | /* Length for a per-processor buffer is passed in at offset 4 in the buffer */ |
| 468 | struct reg_vpa { |
| 469 | u32 dummy; |
| 470 | union { |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 471 | __be16 hword; |
| 472 | __be32 word; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 473 | } length; |
| 474 | }; |
| 475 | |
| 476 | static int vpa_is_registered(struct kvmppc_vpa *vpap) |
| 477 | { |
| 478 | if (vpap->update_pending) |
| 479 | return vpap->next_gpa != 0; |
| 480 | return vpap->pinned_addr != NULL; |
| 481 | } |
| 482 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 483 | static unsigned long do_h_register_vpa(struct kvm_vcpu *vcpu, |
| 484 | unsigned long flags, |
| 485 | unsigned long vcpuid, unsigned long vpa) |
| 486 | { |
| 487 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 488 | unsigned long len, nb; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 489 | void *va; |
| 490 | struct kvm_vcpu *tvcpu; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 491 | int err; |
| 492 | int subfunc; |
| 493 | struct kvmppc_vpa *vpap; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 494 | |
| 495 | tvcpu = kvmppc_find_vcpu(kvm, vcpuid); |
| 496 | if (!tvcpu) |
| 497 | return H_PARAMETER; |
| 498 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 499 | subfunc = (flags >> H_VPA_FUNC_SHIFT) & H_VPA_FUNC_MASK; |
| 500 | if (subfunc == H_VPA_REG_VPA || subfunc == H_VPA_REG_DTL || |
| 501 | subfunc == H_VPA_REG_SLB) { |
| 502 | /* Registering new area - address must be cache-line aligned */ |
| 503 | if ((vpa & (L1_CACHE_BYTES - 1)) || !vpa) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 504 | return H_PARAMETER; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 505 | |
| 506 | /* convert logical addr to kernel addr and read length */ |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 507 | va = kvmppc_pin_guest_page(kvm, vpa, &nb); |
| 508 | if (va == NULL) |
Paul Mackerras | b2b2f16 | 2011-12-12 12:28:21 +0000 | [diff] [blame] | 509 | return H_PARAMETER; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 510 | if (subfunc == H_VPA_REG_VPA) |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 511 | len = be16_to_cpu(((struct reg_vpa *)va)->length.hword); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 512 | else |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 513 | len = be32_to_cpu(((struct reg_vpa *)va)->length.word); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 514 | kvmppc_unpin_guest_page(kvm, va, vpa, false); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 515 | |
| 516 | /* Check length */ |
| 517 | if (len > nb || len < sizeof(struct reg_vpa)) |
| 518 | return H_PARAMETER; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 519 | } else { |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 520 | vpa = 0; |
| 521 | len = 0; |
| 522 | } |
| 523 | |
| 524 | err = H_PARAMETER; |
| 525 | vpap = NULL; |
| 526 | spin_lock(&tvcpu->arch.vpa_update_lock); |
| 527 | |
| 528 | switch (subfunc) { |
| 529 | case H_VPA_REG_VPA: /* register VPA */ |
Nicholas Piggin | eaac112e | 2017-08-13 11:33:38 +1000 | [diff] [blame] | 530 | /* |
| 531 | * The size of our lppaca is 1kB because of the way we align |
| 532 | * it for the guest to avoid crossing a 4kB boundary. We only |
| 533 | * use 640 bytes of the structure though, so we should accept |
| 534 | * clients that set a size of 640. |
| 535 | */ |
Nicholas Piggin | 499dcd4 | 2018-02-14 01:08:13 +1000 | [diff] [blame] | 536 | BUILD_BUG_ON(sizeof(struct lppaca) != 640); |
| 537 | if (len < sizeof(struct lppaca)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 538 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 539 | vpap = &tvcpu->arch.vpa; |
| 540 | err = 0; |
| 541 | break; |
| 542 | |
| 543 | case H_VPA_REG_DTL: /* register DTL */ |
| 544 | if (len < sizeof(struct dtl_entry)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 545 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 546 | len -= len % sizeof(struct dtl_entry); |
| 547 | |
| 548 | /* Check that they have previously registered a VPA */ |
| 549 | err = H_RESOURCE; |
| 550 | if (!vpa_is_registered(&tvcpu->arch.vpa)) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 551 | break; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 552 | |
| 553 | vpap = &tvcpu->arch.dtl; |
| 554 | err = 0; |
| 555 | break; |
| 556 | |
| 557 | case H_VPA_REG_SLB: /* register SLB shadow buffer */ |
| 558 | /* Check that they have previously registered a VPA */ |
| 559 | err = H_RESOURCE; |
| 560 | if (!vpa_is_registered(&tvcpu->arch.vpa)) |
| 561 | break; |
| 562 | |
| 563 | vpap = &tvcpu->arch.slb_shadow; |
| 564 | err = 0; |
| 565 | break; |
| 566 | |
| 567 | case H_VPA_DEREG_VPA: /* deregister VPA */ |
| 568 | /* Check they don't still have a DTL or SLB buf registered */ |
| 569 | err = H_RESOURCE; |
| 570 | if (vpa_is_registered(&tvcpu->arch.dtl) || |
| 571 | vpa_is_registered(&tvcpu->arch.slb_shadow)) |
| 572 | break; |
| 573 | |
| 574 | vpap = &tvcpu->arch.vpa; |
| 575 | err = 0; |
| 576 | break; |
| 577 | |
| 578 | case H_VPA_DEREG_DTL: /* deregister DTL */ |
| 579 | vpap = &tvcpu->arch.dtl; |
| 580 | err = 0; |
| 581 | break; |
| 582 | |
| 583 | case H_VPA_DEREG_SLB: /* deregister SLB shadow buffer */ |
| 584 | vpap = &tvcpu->arch.slb_shadow; |
| 585 | err = 0; |
| 586 | break; |
| 587 | } |
| 588 | |
| 589 | if (vpap) { |
| 590 | vpap->next_gpa = vpa; |
| 591 | vpap->len = len; |
| 592 | vpap->update_pending = 1; |
| 593 | } |
| 594 | |
| 595 | spin_unlock(&tvcpu->arch.vpa_update_lock); |
| 596 | |
| 597 | return err; |
| 598 | } |
| 599 | |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 600 | 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] | 601 | { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 602 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 603 | void *va; |
| 604 | unsigned long nb; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 605 | unsigned long gpa; |
| 606 | |
| 607 | /* |
| 608 | * We need to pin the page pointed to by vpap->next_gpa, |
| 609 | * but we can't call kvmppc_pin_guest_page under the lock |
| 610 | * as it does get_user_pages() and down_read(). So we |
| 611 | * have to drop the lock, pin the page, then get the lock |
| 612 | * again and check that a new area didn't get registered |
| 613 | * in the meantime. |
| 614 | */ |
| 615 | for (;;) { |
| 616 | gpa = vpap->next_gpa; |
| 617 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 618 | va = NULL; |
| 619 | nb = 0; |
| 620 | if (gpa) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 621 | va = kvmppc_pin_guest_page(kvm, gpa, &nb); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 622 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 623 | if (gpa == vpap->next_gpa) |
| 624 | break; |
| 625 | /* sigh... unpin that one and try again */ |
| 626 | if (va) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 627 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 628 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 629 | |
| 630 | vpap->update_pending = 0; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 631 | if (va && nb < vpap->len) { |
| 632 | /* |
| 633 | * If it's now too short, it must be that userspace |
| 634 | * has changed the mappings underlying guest memory, |
| 635 | * so unregister the region. |
| 636 | */ |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 637 | kvmppc_unpin_guest_page(kvm, va, gpa, false); |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 638 | va = NULL; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 639 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 640 | if (vpap->pinned_addr) |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 641 | kvmppc_unpin_guest_page(kvm, vpap->pinned_addr, vpap->gpa, |
| 642 | vpap->dirty); |
| 643 | vpap->gpa = gpa; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 644 | vpap->pinned_addr = va; |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 645 | vpap->dirty = false; |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 646 | if (va) |
| 647 | vpap->pinned_end = va + vpap->len; |
| 648 | } |
Paul Mackerras | 93e6024 | 2011-12-12 12:28:55 +0000 | [diff] [blame] | 649 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 650 | static void kvmppc_update_vpas(struct kvm_vcpu *vcpu) |
| 651 | { |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 652 | if (!(vcpu->arch.vpa.update_pending || |
| 653 | vcpu->arch.slb_shadow.update_pending || |
| 654 | vcpu->arch.dtl.update_pending)) |
| 655 | return; |
| 656 | |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 657 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 658 | if (vcpu->arch.vpa.update_pending) { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 659 | kvmppc_update_vpa(vcpu, &vcpu->arch.vpa); |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 660 | if (vcpu->arch.vpa.pinned_addr) |
| 661 | init_vpa(vcpu, vcpu->arch.vpa.pinned_addr); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 662 | } |
| 663 | if (vcpu->arch.dtl.update_pending) { |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 664 | kvmppc_update_vpa(vcpu, &vcpu->arch.dtl); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 665 | vcpu->arch.dtl_ptr = vcpu->arch.dtl.pinned_addr; |
| 666 | vcpu->arch.dtl_index = 0; |
| 667 | } |
| 668 | if (vcpu->arch.slb_shadow.update_pending) |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 669 | kvmppc_update_vpa(vcpu, &vcpu->arch.slb_shadow); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 670 | spin_unlock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 671 | } |
| 672 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 673 | /* |
| 674 | * Return the accumulated stolen time for the vcore up until `now'. |
| 675 | * The caller should hold the vcore lock. |
| 676 | */ |
| 677 | static u64 vcore_stolen_time(struct kvmppc_vcore *vc, u64 now) |
| 678 | { |
| 679 | u64 p; |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 680 | unsigned long flags; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 681 | |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 682 | spin_lock_irqsave(&vc->stoltb_lock, flags); |
| 683 | p = vc->stolen_tb; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 684 | if (vc->vcore_state != VCORE_INACTIVE && |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 685 | vc->preempt_tb != TB_NIL) |
| 686 | p += now - vc->preempt_tb; |
| 687 | spin_unlock_irqrestore(&vc->stoltb_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 688 | return p; |
| 689 | } |
| 690 | |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 691 | static void kvmppc_create_dtl_entry(struct kvm_vcpu *vcpu, |
| 692 | struct kvmppc_vcore *vc) |
| 693 | { |
| 694 | struct dtl_entry *dt; |
| 695 | struct lppaca *vpa; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 696 | unsigned long stolen; |
| 697 | unsigned long core_stolen; |
| 698 | u64 now; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 699 | unsigned long flags; |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 700 | |
| 701 | dt = vcpu->arch.dtl_ptr; |
| 702 | vpa = vcpu->arch.vpa.pinned_addr; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 703 | now = mftb(); |
| 704 | core_stolen = vcore_stolen_time(vc, now); |
| 705 | stolen = core_stolen - vcpu->arch.stolen_logged; |
| 706 | vcpu->arch.stolen_logged = core_stolen; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 707 | spin_lock_irqsave(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 708 | stolen += vcpu->arch.busy_stolen; |
| 709 | vcpu->arch.busy_stolen = 0; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 710 | spin_unlock_irqrestore(&vcpu->arch.tbacct_lock, flags); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 711 | if (!dt || !vpa) |
| 712 | return; |
| 713 | memset(dt, 0, sizeof(struct dtl_entry)); |
| 714 | dt->dispatch_reason = 7; |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 715 | dt->processor_id = cpu_to_be16(vc->pcpu + vcpu->arch.ptid); |
| 716 | dt->timebase = cpu_to_be64(now + vc->tb_offset); |
| 717 | dt->enqueue_to_dispatch_time = cpu_to_be32(stolen); |
| 718 | dt->srr0 = cpu_to_be64(kvmppc_get_pc(vcpu)); |
| 719 | dt->srr1 = cpu_to_be64(vcpu->arch.shregs.msr); |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 720 | ++dt; |
| 721 | if (dt == vcpu->arch.dtl.pinned_end) |
| 722 | dt = vcpu->arch.dtl.pinned_addr; |
| 723 | vcpu->arch.dtl_ptr = dt; |
| 724 | /* order writing *dt vs. writing vpa->dtl_idx */ |
| 725 | smp_wmb(); |
Alexander Graf | 0240755 | 2014-06-11 10:34:19 +0200 | [diff] [blame] | 726 | vpa->dtl_idx = cpu_to_be64(++vcpu->arch.dtl_index); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 727 | vcpu->arch.dtl.dirty = true; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 728 | } |
| 729 | |
Paul Mackerras | 1da4e2f | 2017-05-19 16:26:16 +1000 | [diff] [blame] | 730 | /* See if there is a doorbell interrupt pending for a vcpu */ |
| 731 | static bool kvmppc_doorbell_pending(struct kvm_vcpu *vcpu) |
| 732 | { |
| 733 | int thr; |
| 734 | struct kvmppc_vcore *vc; |
| 735 | |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 736 | if (vcpu->arch.doorbell_request) |
| 737 | return true; |
| 738 | /* |
| 739 | * Ensure that the read of vcore->dpdes comes after the read |
| 740 | * of vcpu->doorbell_request. This barrier matches the |
Palmer Dabbelt | 6fabc9f | 2019-04-25 12:53:39 -0700 | [diff] [blame] | 741 | * smp_wmb() in kvmppc_guest_entry_inject(). |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 742 | */ |
| 743 | smp_rmb(); |
Paul Mackerras | 1da4e2f | 2017-05-19 16:26:16 +1000 | [diff] [blame] | 744 | vc = vcpu->arch.vcore; |
| 745 | thr = vcpu->vcpu_id - vc->first_vcpuid; |
| 746 | return !!(vc->dpdes & (1 << thr)); |
| 747 | } |
| 748 | |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 749 | static bool kvmppc_power8_compatible(struct kvm_vcpu *vcpu) |
| 750 | { |
| 751 | if (vcpu->arch.vcore->arch_compat >= PVR_ARCH_207) |
| 752 | return true; |
| 753 | if ((!vcpu->arch.vcore->arch_compat) && |
| 754 | cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 755 | return true; |
| 756 | return false; |
| 757 | } |
| 758 | |
| 759 | static int kvmppc_h_set_mode(struct kvm_vcpu *vcpu, unsigned long mflags, |
| 760 | unsigned long resource, unsigned long value1, |
| 761 | unsigned long value2) |
| 762 | { |
| 763 | switch (resource) { |
| 764 | case H_SET_MODE_RESOURCE_SET_CIABR: |
| 765 | if (!kvmppc_power8_compatible(vcpu)) |
| 766 | return H_P2; |
| 767 | if (value2) |
| 768 | return H_P4; |
| 769 | if (mflags) |
| 770 | return H_UNSUPPORTED_FLAG_START; |
| 771 | /* Guests can't breakpoint the hypervisor */ |
| 772 | if ((value1 & CIABR_PRIV) == CIABR_PRIV_HYPER) |
| 773 | return H_P3; |
| 774 | vcpu->arch.ciabr = value1; |
| 775 | return H_SUCCESS; |
Ravi Bangoria | 6f3fe29 | 2020-07-23 14:38:10 +0530 | [diff] [blame] | 776 | case H_SET_MODE_RESOURCE_SET_DAWR0: |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 777 | if (!kvmppc_power8_compatible(vcpu)) |
| 778 | return H_P2; |
Michael Neuling | 398e712 | 2018-03-27 15:37:20 +1100 | [diff] [blame] | 779 | if (!ppc_breakpoint_available()) |
| 780 | return H_P2; |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 781 | if (mflags) |
| 782 | return H_UNSUPPORTED_FLAG_START; |
| 783 | if (value2 & DABRX_HYP) |
| 784 | return H_P4; |
| 785 | vcpu->arch.dawr = value1; |
| 786 | vcpu->arch.dawrx = value2; |
| 787 | return H_SUCCESS; |
Nicholas Piggin | 55d7004 | 2019-10-02 16:00:25 +1000 | [diff] [blame] | 788 | case H_SET_MODE_RESOURCE_ADDR_TRANS_MODE: |
| 789 | /* KVM does not support mflags=2 (AIL=2) */ |
| 790 | if (mflags != 0 && mflags != 3) |
| 791 | return H_UNSUPPORTED_FLAG_START; |
| 792 | return H_TOO_HARD; |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 793 | default: |
| 794 | return H_TOO_HARD; |
| 795 | } |
| 796 | } |
| 797 | |
Suraj Jitindar Singh | 2d34d1c | 2019-03-22 17:05:44 +1100 | [diff] [blame] | 798 | /* Copy guest memory in place - must reside within a single memslot */ |
| 799 | static int kvmppc_copy_guest(struct kvm *kvm, gpa_t to, gpa_t from, |
| 800 | unsigned long len) |
| 801 | { |
| 802 | struct kvm_memory_slot *to_memslot = NULL; |
| 803 | struct kvm_memory_slot *from_memslot = NULL; |
| 804 | unsigned long to_addr, from_addr; |
| 805 | int r; |
| 806 | |
| 807 | /* Get HPA for from address */ |
| 808 | from_memslot = gfn_to_memslot(kvm, from >> PAGE_SHIFT); |
| 809 | if (!from_memslot) |
| 810 | return -EFAULT; |
| 811 | if ((from + len) >= ((from_memslot->base_gfn + from_memslot->npages) |
| 812 | << PAGE_SHIFT)) |
| 813 | return -EINVAL; |
| 814 | from_addr = gfn_to_hva_memslot(from_memslot, from >> PAGE_SHIFT); |
| 815 | if (kvm_is_error_hva(from_addr)) |
| 816 | return -EFAULT; |
| 817 | from_addr |= (from & (PAGE_SIZE - 1)); |
| 818 | |
| 819 | /* Get HPA for to address */ |
| 820 | to_memslot = gfn_to_memslot(kvm, to >> PAGE_SHIFT); |
| 821 | if (!to_memslot) |
| 822 | return -EFAULT; |
| 823 | if ((to + len) >= ((to_memslot->base_gfn + to_memslot->npages) |
| 824 | << PAGE_SHIFT)) |
| 825 | return -EINVAL; |
| 826 | to_addr = gfn_to_hva_memslot(to_memslot, to >> PAGE_SHIFT); |
| 827 | if (kvm_is_error_hva(to_addr)) |
| 828 | return -EFAULT; |
| 829 | to_addr |= (to & (PAGE_SIZE - 1)); |
| 830 | |
| 831 | /* Perform copy */ |
| 832 | r = raw_copy_in_user((void __user *)to_addr, (void __user *)from_addr, |
| 833 | len); |
| 834 | if (r) |
| 835 | return -EFAULT; |
| 836 | mark_page_dirty(kvm, to >> PAGE_SHIFT); |
| 837 | return 0; |
| 838 | } |
| 839 | |
| 840 | static long kvmppc_h_page_init(struct kvm_vcpu *vcpu, unsigned long flags, |
| 841 | unsigned long dest, unsigned long src) |
| 842 | { |
| 843 | u64 pg_sz = SZ_4K; /* 4K page size */ |
| 844 | u64 pg_mask = SZ_4K - 1; |
| 845 | int ret; |
| 846 | |
| 847 | /* Check for invalid flags (H_PAGE_SET_LOANED covers all CMO flags) */ |
| 848 | if (flags & ~(H_ICACHE_INVALIDATE | H_ICACHE_SYNCHRONIZE | |
| 849 | H_ZERO_PAGE | H_COPY_PAGE | H_PAGE_SET_LOANED)) |
| 850 | return H_PARAMETER; |
| 851 | |
| 852 | /* dest (and src if copy_page flag set) must be page aligned */ |
| 853 | if ((dest & pg_mask) || ((flags & H_COPY_PAGE) && (src & pg_mask))) |
| 854 | return H_PARAMETER; |
| 855 | |
| 856 | /* zero and/or copy the page as determined by the flags */ |
| 857 | if (flags & H_COPY_PAGE) { |
| 858 | ret = kvmppc_copy_guest(vcpu->kvm, dest, src, pg_sz); |
| 859 | if (ret < 0) |
| 860 | return H_PARAMETER; |
| 861 | } else if (flags & H_ZERO_PAGE) { |
| 862 | ret = kvm_clear_guest(vcpu->kvm, dest, pg_sz); |
| 863 | if (ret < 0) |
| 864 | return H_PARAMETER; |
| 865 | } |
| 866 | |
| 867 | /* We can ignore the remaining flags */ |
| 868 | |
| 869 | return H_SUCCESS; |
| 870 | } |
| 871 | |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 872 | static int kvm_arch_vcpu_yield_to(struct kvm_vcpu *target) |
| 873 | { |
| 874 | struct kvmppc_vcore *vcore = target->arch.vcore; |
| 875 | |
| 876 | /* |
| 877 | * We expect to have been called by the real mode handler |
| 878 | * (kvmppc_rm_h_confer()) which would have directly returned |
| 879 | * H_SUCCESS if the source vcore wasn't idle (e.g. if it may |
| 880 | * have useful work to do and should not confer) so we don't |
| 881 | * recheck that here. |
| 882 | */ |
| 883 | |
| 884 | spin_lock(&vcore->lock); |
| 885 | if (target->arch.state == KVMPPC_VCPU_RUNNABLE && |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 886 | vcore->vcore_state != VCORE_INACTIVE && |
| 887 | vcore->runner) |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 888 | target = vcore->runner; |
| 889 | spin_unlock(&vcore->lock); |
| 890 | |
| 891 | return kvm_vcpu_yield_to(target); |
| 892 | } |
| 893 | |
| 894 | static int kvmppc_get_yield_count(struct kvm_vcpu *vcpu) |
| 895 | { |
| 896 | int yield_count = 0; |
| 897 | struct lppaca *lppaca; |
| 898 | |
| 899 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 900 | lppaca = (struct lppaca *)vcpu->arch.vpa.pinned_addr; |
| 901 | if (lppaca) |
Paul Mackerras | ecb6d61 | 2015-03-20 20:39:39 +1100 | [diff] [blame] | 902 | yield_count = be32_to_cpu(lppaca->yield_count); |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 903 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 904 | return yield_count; |
| 905 | } |
| 906 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 907 | int kvmppc_pseries_do_hcall(struct kvm_vcpu *vcpu) |
| 908 | { |
| 909 | unsigned long req = kvmppc_get_gpr(vcpu, 3); |
| 910 | unsigned long target, ret = H_SUCCESS; |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 911 | int yield_count; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 912 | struct kvm_vcpu *tvcpu; |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 913 | int idx, rc; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 914 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 915 | if (req <= MAX_HCALL_OPCODE && |
| 916 | !test_bit(req/4, vcpu->kvm->arch.enabled_hcalls)) |
| 917 | return RESUME_HOST; |
| 918 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 919 | switch (req) { |
| 920 | case H_CEDE: |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 921 | break; |
| 922 | case H_PROD: |
| 923 | target = kvmppc_get_gpr(vcpu, 4); |
| 924 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); |
| 925 | if (!tvcpu) { |
| 926 | ret = H_PARAMETER; |
| 927 | break; |
| 928 | } |
| 929 | tvcpu->arch.prodded = 1; |
| 930 | smp_mb(); |
Paul Mackerras | 8464c88 | 2016-12-06 20:42:05 +1100 | [diff] [blame] | 931 | if (tvcpu->arch.ceded) |
| 932 | kvmppc_fast_vcpu_kick_hv(tvcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 933 | break; |
| 934 | case H_CONFER: |
Paul Mackerras | 42d7604 | 2013-09-06 13:23:21 +1000 | [diff] [blame] | 935 | target = kvmppc_get_gpr(vcpu, 4); |
| 936 | if (target == -1) |
| 937 | break; |
| 938 | tvcpu = kvmppc_find_vcpu(vcpu->kvm, target); |
| 939 | if (!tvcpu) { |
| 940 | ret = H_PARAMETER; |
| 941 | break; |
| 942 | } |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 943 | yield_count = kvmppc_get_gpr(vcpu, 5); |
| 944 | if (kvmppc_get_yield_count(tvcpu) != yield_count) |
| 945 | break; |
| 946 | kvm_arch_vcpu_yield_to(tvcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 947 | break; |
| 948 | case H_REGISTER_VPA: |
| 949 | ret = do_h_register_vpa(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 950 | kvmppc_get_gpr(vcpu, 5), |
| 951 | kvmppc_get_gpr(vcpu, 6)); |
| 952 | break; |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 953 | case H_RTAS: |
| 954 | if (list_empty(&vcpu->kvm->arch.rtas_tokens)) |
| 955 | return RESUME_HOST; |
| 956 | |
Paul Mackerras | c943809 | 2013-11-16 17:46:05 +1100 | [diff] [blame] | 957 | idx = srcu_read_lock(&vcpu->kvm->srcu); |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 958 | rc = kvmppc_rtas_hcall(vcpu); |
Paul Mackerras | c943809 | 2013-11-16 17:46:05 +1100 | [diff] [blame] | 959 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Michael Ellerman | 8e591cb | 2013-04-17 20:30:00 +0000 | [diff] [blame] | 960 | |
| 961 | if (rc == -ENOENT) |
| 962 | return RESUME_HOST; |
| 963 | else if (rc == 0) |
| 964 | break; |
| 965 | |
| 966 | /* Send the error out to userspace via KVM_RUN */ |
| 967 | return rc; |
David Gibson | 99342cf8 | 2015-02-05 11:53:25 +1100 | [diff] [blame] | 968 | case H_LOGICAL_CI_LOAD: |
| 969 | ret = kvmppc_h_logical_ci_load(vcpu); |
| 970 | if (ret == H_TOO_HARD) |
| 971 | return RESUME_HOST; |
| 972 | break; |
| 973 | case H_LOGICAL_CI_STORE: |
| 974 | ret = kvmppc_h_logical_ci_store(vcpu); |
| 975 | if (ret == H_TOO_HARD) |
| 976 | return RESUME_HOST; |
| 977 | break; |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 978 | case H_SET_MODE: |
| 979 | ret = kvmppc_h_set_mode(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 980 | kvmppc_get_gpr(vcpu, 5), |
| 981 | kvmppc_get_gpr(vcpu, 6), |
| 982 | kvmppc_get_gpr(vcpu, 7)); |
| 983 | if (ret == H_TOO_HARD) |
| 984 | return RESUME_HOST; |
| 985 | break; |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 986 | case H_XIRR: |
| 987 | case H_CPPR: |
| 988 | case H_EOI: |
| 989 | case H_IPI: |
Paul Mackerras | 8e44ddc | 2013-05-23 15:42:21 +0000 | [diff] [blame] | 990 | case H_IPOLL: |
| 991 | case H_XIRR_X: |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 992 | if (kvmppc_xics_enabled(vcpu)) { |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 993 | if (xics_on_xive()) { |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 994 | ret = H_NOT_AVAILABLE; |
| 995 | return RESUME_GUEST; |
| 996 | } |
Benjamin Herrenschmidt | bc5ad3f | 2013-04-17 20:30:26 +0000 | [diff] [blame] | 997 | ret = kvmppc_xics_hcall(vcpu, req); |
| 998 | break; |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 999 | } |
| 1000 | return RESUME_HOST; |
Paul Mackerras | 4bad777 | 2018-10-08 16:31:06 +1100 | [diff] [blame] | 1001 | case H_SET_DABR: |
| 1002 | ret = kvmppc_h_set_dabr(vcpu, kvmppc_get_gpr(vcpu, 4)); |
| 1003 | break; |
| 1004 | case H_SET_XDABR: |
| 1005 | ret = kvmppc_h_set_xdabr(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 1006 | kvmppc_get_gpr(vcpu, 5)); |
| 1007 | break; |
Jordan Niethe | e40542a | 2019-02-21 14:28:48 +1100 | [diff] [blame] | 1008 | #ifdef CONFIG_SPAPR_TCE_IOMMU |
Paul Mackerras | 4bad777 | 2018-10-08 16:31:06 +1100 | [diff] [blame] | 1009 | case H_GET_TCE: |
| 1010 | ret = kvmppc_h_get_tce(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 1011 | kvmppc_get_gpr(vcpu, 5)); |
| 1012 | if (ret == H_TOO_HARD) |
| 1013 | return RESUME_HOST; |
| 1014 | break; |
Alexey Kardashevskiy | d3695aa | 2016-02-15 12:55:09 +1100 | [diff] [blame] | 1015 | case H_PUT_TCE: |
| 1016 | ret = kvmppc_h_put_tce(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 1017 | kvmppc_get_gpr(vcpu, 5), |
| 1018 | kvmppc_get_gpr(vcpu, 6)); |
| 1019 | if (ret == H_TOO_HARD) |
| 1020 | return RESUME_HOST; |
| 1021 | break; |
| 1022 | case H_PUT_TCE_INDIRECT: |
| 1023 | ret = kvmppc_h_put_tce_indirect(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 1024 | kvmppc_get_gpr(vcpu, 5), |
| 1025 | kvmppc_get_gpr(vcpu, 6), |
| 1026 | kvmppc_get_gpr(vcpu, 7)); |
| 1027 | if (ret == H_TOO_HARD) |
| 1028 | return RESUME_HOST; |
| 1029 | break; |
| 1030 | case H_STUFF_TCE: |
| 1031 | ret = kvmppc_h_stuff_tce(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 1032 | kvmppc_get_gpr(vcpu, 5), |
| 1033 | kvmppc_get_gpr(vcpu, 6), |
| 1034 | kvmppc_get_gpr(vcpu, 7)); |
| 1035 | if (ret == H_TOO_HARD) |
| 1036 | return RESUME_HOST; |
| 1037 | break; |
Jordan Niethe | e40542a | 2019-02-21 14:28:48 +1100 | [diff] [blame] | 1038 | #endif |
Paul Mackerras | 4bad777 | 2018-10-08 16:31:06 +1100 | [diff] [blame] | 1039 | case H_RANDOM: |
| 1040 | if (!powernv_get_random_long(&vcpu->arch.regs.gpr[4])) |
| 1041 | ret = H_HARDWARE; |
| 1042 | break; |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 1043 | |
| 1044 | case H_SET_PARTITION_TABLE: |
| 1045 | ret = H_FUNCTION; |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 1046 | if (nesting_enabled(vcpu->kvm)) |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 1047 | ret = kvmhv_set_partition_table(vcpu); |
| 1048 | break; |
| 1049 | case H_ENTER_NESTED: |
| 1050 | ret = H_FUNCTION; |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 1051 | if (!nesting_enabled(vcpu->kvm)) |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1052 | break; |
| 1053 | ret = kvmhv_enter_nested_guest(vcpu); |
| 1054 | if (ret == H_INTERRUPT) { |
| 1055 | kvmppc_set_gpr(vcpu, 3, 0); |
Michael Roth | 6c08ec1 | 2018-11-08 21:27:23 -0600 | [diff] [blame] | 1056 | vcpu->arch.hcall_needed = 0; |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1057 | return -EINTR; |
Suraj Jitindar Singh | 873db2c | 2018-12-14 16:29:08 +1100 | [diff] [blame] | 1058 | } else if (ret == H_TOO_HARD) { |
| 1059 | kvmppc_set_gpr(vcpu, 3, 0); |
| 1060 | vcpu->arch.hcall_needed = 0; |
| 1061 | return RESUME_HOST; |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1062 | } |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 1063 | break; |
| 1064 | case H_TLB_INVALIDATE: |
| 1065 | ret = H_FUNCTION; |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 1066 | if (nesting_enabled(vcpu->kvm)) |
| 1067 | ret = kvmhv_do_nested_tlbie(vcpu); |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 1068 | break; |
Suraj Jitindar Singh | 6ff887b | 2018-12-14 16:29:09 +1100 | [diff] [blame] | 1069 | case H_COPY_TOFROM_GUEST: |
| 1070 | ret = H_FUNCTION; |
| 1071 | if (nesting_enabled(vcpu->kvm)) |
| 1072 | ret = kvmhv_copy_tofrom_guest_nested(vcpu); |
| 1073 | break; |
Suraj Jitindar Singh | 2d34d1c | 2019-03-22 17:05:44 +1100 | [diff] [blame] | 1074 | case H_PAGE_INIT: |
| 1075 | ret = kvmppc_h_page_init(vcpu, kvmppc_get_gpr(vcpu, 4), |
| 1076 | kvmppc_get_gpr(vcpu, 5), |
| 1077 | kvmppc_get_gpr(vcpu, 6)); |
| 1078 | break; |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 1079 | case H_SVM_PAGE_IN: |
Laurent Dufour | 8c47b6ff | 2020-03-20 11:26:42 +0100 | [diff] [blame] | 1080 | ret = H_UNSUPPORTED; |
| 1081 | if (kvmppc_get_srr1(vcpu) & MSR_S) |
| 1082 | ret = kvmppc_h_svm_page_in(vcpu->kvm, |
| 1083 | kvmppc_get_gpr(vcpu, 4), |
| 1084 | kvmppc_get_gpr(vcpu, 5), |
| 1085 | kvmppc_get_gpr(vcpu, 6)); |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 1086 | break; |
| 1087 | case H_SVM_PAGE_OUT: |
Laurent Dufour | 8c47b6ff | 2020-03-20 11:26:42 +0100 | [diff] [blame] | 1088 | ret = H_UNSUPPORTED; |
| 1089 | if (kvmppc_get_srr1(vcpu) & MSR_S) |
| 1090 | ret = kvmppc_h_svm_page_out(vcpu->kvm, |
| 1091 | kvmppc_get_gpr(vcpu, 4), |
| 1092 | kvmppc_get_gpr(vcpu, 5), |
| 1093 | kvmppc_get_gpr(vcpu, 6)); |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 1094 | break; |
| 1095 | case H_SVM_INIT_START: |
Laurent Dufour | 8c47b6ff | 2020-03-20 11:26:42 +0100 | [diff] [blame] | 1096 | ret = H_UNSUPPORTED; |
| 1097 | if (kvmppc_get_srr1(vcpu) & MSR_S) |
| 1098 | ret = kvmppc_h_svm_init_start(vcpu->kvm); |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 1099 | break; |
| 1100 | case H_SVM_INIT_DONE: |
Laurent Dufour | 8c47b6ff | 2020-03-20 11:26:42 +0100 | [diff] [blame] | 1101 | ret = H_UNSUPPORTED; |
| 1102 | if (kvmppc_get_srr1(vcpu) & MSR_S) |
| 1103 | ret = kvmppc_h_svm_init_done(vcpu->kvm); |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 1104 | break; |
Sukadev Bhattiprolu | 3a43970 | 2020-01-06 18:02:37 -0800 | [diff] [blame] | 1105 | case H_SVM_INIT_ABORT: |
Laurent Dufour | e3326ae | 2020-05-20 19:43:08 +0200 | [diff] [blame] | 1106 | /* |
| 1107 | * Even if that call is made by the Ultravisor, the SSR1 value |
| 1108 | * is the guest context one, with the secure bit clear as it has |
| 1109 | * not yet been secured. So we can't check it here. |
| 1110 | * Instead the kvm->arch.secure_guest flag is checked inside |
| 1111 | * kvmppc_h_svm_init_abort(). |
| 1112 | */ |
| 1113 | ret = kvmppc_h_svm_init_abort(vcpu->kvm); |
Sukadev Bhattiprolu | 3a43970 | 2020-01-06 18:02:37 -0800 | [diff] [blame] | 1114 | break; |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 1115 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1116 | default: |
| 1117 | return RESUME_HOST; |
| 1118 | } |
| 1119 | kvmppc_set_gpr(vcpu, 3, ret); |
| 1120 | vcpu->arch.hcall_needed = 0; |
| 1121 | return RESUME_GUEST; |
| 1122 | } |
| 1123 | |
Paul Mackerras | 4bad777 | 2018-10-08 16:31:06 +1100 | [diff] [blame] | 1124 | /* |
| 1125 | * Handle H_CEDE in the nested virtualization case where we haven't |
| 1126 | * called the real-mode hcall handlers in book3s_hv_rmhandlers.S. |
| 1127 | * This has to be done early, not in kvmppc_pseries_do_hcall(), so |
| 1128 | * that the cede logic in kvmppc_run_single_vcpu() works properly. |
| 1129 | */ |
| 1130 | static void kvmppc_nested_cede(struct kvm_vcpu *vcpu) |
| 1131 | { |
| 1132 | vcpu->arch.shregs.msr |= MSR_EE; |
| 1133 | vcpu->arch.ceded = 1; |
| 1134 | smp_mb(); |
| 1135 | if (vcpu->arch.prodded) { |
| 1136 | vcpu->arch.prodded = 0; |
| 1137 | smp_mb(); |
| 1138 | vcpu->arch.ceded = 0; |
| 1139 | } |
| 1140 | } |
| 1141 | |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 1142 | static int kvmppc_hcall_impl_hv(unsigned long cmd) |
| 1143 | { |
| 1144 | switch (cmd) { |
| 1145 | case H_CEDE: |
| 1146 | case H_PROD: |
| 1147 | case H_CONFER: |
| 1148 | case H_REGISTER_VPA: |
Michael Neuling | 9642382 | 2014-06-02 11:03:01 +1000 | [diff] [blame] | 1149 | case H_SET_MODE: |
David Gibson | 99342cf8 | 2015-02-05 11:53:25 +1100 | [diff] [blame] | 1150 | case H_LOGICAL_CI_LOAD: |
| 1151 | case H_LOGICAL_CI_STORE: |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 1152 | #ifdef CONFIG_KVM_XICS |
| 1153 | case H_XIRR: |
| 1154 | case H_CPPR: |
| 1155 | case H_EOI: |
| 1156 | case H_IPI: |
| 1157 | case H_IPOLL: |
| 1158 | case H_XIRR_X: |
| 1159 | #endif |
Suraj Jitindar Singh | 2d34d1c | 2019-03-22 17:05:44 +1100 | [diff] [blame] | 1160 | case H_PAGE_INIT: |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 1161 | return 1; |
| 1162 | } |
| 1163 | |
| 1164 | /* See if it's in the real-mode table */ |
| 1165 | return kvmppc_hcall_impl_hv_realmode(cmd); |
| 1166 | } |
| 1167 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1168 | static int kvmppc_emulate_debug_inst(struct kvm_vcpu *vcpu) |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1169 | { |
| 1170 | u32 last_inst; |
| 1171 | |
| 1172 | if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst) != |
| 1173 | EMULATE_DONE) { |
| 1174 | /* |
| 1175 | * Fetch failed, so return to guest and |
| 1176 | * try executing it again. |
| 1177 | */ |
| 1178 | return RESUME_GUEST; |
| 1179 | } |
| 1180 | |
| 1181 | if (last_inst == KVMPPC_INST_SW_BREAKPOINT) { |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1182 | vcpu->run->exit_reason = KVM_EXIT_DEBUG; |
| 1183 | vcpu->run->debug.arch.address = kvmppc_get_pc(vcpu); |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1184 | return RESUME_HOST; |
| 1185 | } else { |
| 1186 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 1187 | return RESUME_GUEST; |
| 1188 | } |
| 1189 | } |
| 1190 | |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 1191 | static void do_nothing(void *x) |
| 1192 | { |
| 1193 | } |
| 1194 | |
| 1195 | static unsigned long kvmppc_read_dpdes(struct kvm_vcpu *vcpu) |
| 1196 | { |
| 1197 | int thr, cpu, pcpu, nthreads; |
| 1198 | struct kvm_vcpu *v; |
| 1199 | unsigned long dpdes; |
| 1200 | |
| 1201 | nthreads = vcpu->kvm->arch.emul_smt_mode; |
| 1202 | dpdes = 0; |
| 1203 | cpu = vcpu->vcpu_id & ~(nthreads - 1); |
| 1204 | for (thr = 0; thr < nthreads; ++thr, ++cpu) { |
| 1205 | v = kvmppc_find_vcpu(vcpu->kvm, cpu); |
| 1206 | if (!v) |
| 1207 | continue; |
| 1208 | /* |
| 1209 | * If the vcpu is currently running on a physical cpu thread, |
| 1210 | * interrupt it in order to pull it out of the guest briefly, |
| 1211 | * which will update its vcore->dpdes value. |
| 1212 | */ |
| 1213 | pcpu = READ_ONCE(v->cpu); |
| 1214 | if (pcpu >= 0) |
| 1215 | smp_call_function_single(pcpu, do_nothing, NULL, 1); |
| 1216 | if (kvmppc_doorbell_pending(v)) |
| 1217 | dpdes |= 1 << thr; |
| 1218 | } |
| 1219 | return dpdes; |
| 1220 | } |
| 1221 | |
| 1222 | /* |
| 1223 | * On POWER9, emulate doorbell-related instructions in order to |
| 1224 | * give the guest the illusion of running on a multi-threaded core. |
| 1225 | * The instructions emulated are msgsndp, msgclrp, mfspr TIR, |
| 1226 | * and mfspr DPDES. |
| 1227 | */ |
| 1228 | static int kvmppc_emulate_doorbell_instr(struct kvm_vcpu *vcpu) |
| 1229 | { |
| 1230 | u32 inst, rb, thr; |
| 1231 | unsigned long arg; |
| 1232 | struct kvm *kvm = vcpu->kvm; |
| 1233 | struct kvm_vcpu *tvcpu; |
| 1234 | |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 1235 | if (kvmppc_get_last_inst(vcpu, INST_GENERIC, &inst) != EMULATE_DONE) |
| 1236 | return RESUME_GUEST; |
| 1237 | if (get_op(inst) != 31) |
| 1238 | return EMULATE_FAIL; |
| 1239 | rb = get_rb(inst); |
| 1240 | thr = vcpu->vcpu_id & (kvm->arch.emul_smt_mode - 1); |
| 1241 | switch (get_xop(inst)) { |
| 1242 | case OP_31_XOP_MSGSNDP: |
| 1243 | arg = kvmppc_get_gpr(vcpu, rb); |
| 1244 | if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER) |
| 1245 | break; |
| 1246 | arg &= 0x3f; |
| 1247 | if (arg >= kvm->arch.emul_smt_mode) |
| 1248 | break; |
| 1249 | tvcpu = kvmppc_find_vcpu(kvm, vcpu->vcpu_id - thr + arg); |
| 1250 | if (!tvcpu) |
| 1251 | break; |
| 1252 | if (!tvcpu->arch.doorbell_request) { |
| 1253 | tvcpu->arch.doorbell_request = 1; |
| 1254 | kvmppc_fast_vcpu_kick_hv(tvcpu); |
| 1255 | } |
| 1256 | break; |
| 1257 | case OP_31_XOP_MSGCLRP: |
| 1258 | arg = kvmppc_get_gpr(vcpu, rb); |
| 1259 | if (((arg >> 27) & 0xf) != PPC_DBELL_SERVER) |
| 1260 | break; |
| 1261 | vcpu->arch.vcore->dpdes = 0; |
| 1262 | vcpu->arch.doorbell_request = 0; |
| 1263 | break; |
| 1264 | case OP_31_XOP_MFSPR: |
| 1265 | switch (get_sprn(inst)) { |
| 1266 | case SPRN_TIR: |
| 1267 | arg = thr; |
| 1268 | break; |
| 1269 | case SPRN_DPDES: |
| 1270 | arg = kvmppc_read_dpdes(vcpu); |
| 1271 | break; |
| 1272 | default: |
| 1273 | return EMULATE_FAIL; |
| 1274 | } |
| 1275 | kvmppc_set_gpr(vcpu, get_rt(inst), arg); |
| 1276 | break; |
| 1277 | default: |
| 1278 | return EMULATE_FAIL; |
| 1279 | } |
| 1280 | kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) + 4); |
| 1281 | return RESUME_GUEST; |
| 1282 | } |
| 1283 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1284 | static int kvmppc_handle_exit_hv(struct kvm_vcpu *vcpu, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1285 | struct task_struct *tsk) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1286 | { |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1287 | struct kvm_run *run = vcpu->run; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1288 | int r = RESUME_HOST; |
| 1289 | |
| 1290 | vcpu->stat.sum_exits++; |
| 1291 | |
Paul Mackerras | 1c9e3d5 | 2015-11-12 16:43:48 +1100 | [diff] [blame] | 1292 | /* |
| 1293 | * This can happen if an interrupt occurs in the last stages |
| 1294 | * of guest entry or the first stages of guest exit (i.e. after |
| 1295 | * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV |
| 1296 | * and before setting it to KVM_GUEST_MODE_HOST_HV). |
| 1297 | * That can happen due to a bug, or due to a machine check |
| 1298 | * occurring at just the wrong time. |
| 1299 | */ |
| 1300 | if (vcpu->arch.shregs.msr & MSR_HV) { |
| 1301 | printk(KERN_EMERG "KVM trap in HV mode!\n"); |
| 1302 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", |
| 1303 | vcpu->arch.trap, kvmppc_get_pc(vcpu), |
| 1304 | vcpu->arch.shregs.msr); |
| 1305 | kvmppc_dump_regs(vcpu); |
| 1306 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 1307 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
| 1308 | return RESUME_HOST; |
| 1309 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1310 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1311 | run->ready_for_interrupt_injection = 1; |
| 1312 | switch (vcpu->arch.trap) { |
| 1313 | /* We're good on these - the host merely wanted to get our attention */ |
| 1314 | case BOOK3S_INTERRUPT_HV_DECREMENTER: |
| 1315 | vcpu->stat.dec_exits++; |
| 1316 | r = RESUME_GUEST; |
| 1317 | break; |
| 1318 | case BOOK3S_INTERRUPT_EXTERNAL: |
Paul Mackerras | 5d00f66 | 2014-01-08 21:25:28 +1100 | [diff] [blame] | 1319 | case BOOK3S_INTERRUPT_H_DOORBELL: |
Paul Mackerras | 84f7139 | 2016-11-22 14:30:14 +1100 | [diff] [blame] | 1320 | case BOOK3S_INTERRUPT_H_VIRT: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1321 | vcpu->stat.ext_intr_exits++; |
| 1322 | r = RESUME_GUEST; |
| 1323 | break; |
Nicholas Piggin | 6de6638 | 2017-11-05 23:33:55 +1100 | [diff] [blame] | 1324 | /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/ |
Mahesh Salgaonkar | dee6f24 | 2014-11-03 15:51:57 +1100 | [diff] [blame] | 1325 | case BOOK3S_INTERRUPT_HMI: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1326 | case BOOK3S_INTERRUPT_PERFMON: |
Nicholas Piggin | 6de6638 | 2017-11-05 23:33:55 +1100 | [diff] [blame] | 1327 | case BOOK3S_INTERRUPT_SYSTEM_RESET: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1328 | r = RESUME_GUEST; |
| 1329 | break; |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1330 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
Paul Mackerras | 884dfb7 | 2019-02-21 13:38:49 +1100 | [diff] [blame] | 1331 | /* Print the MCE event to host console. */ |
Paul Mackerras | c057720 | 2019-02-21 13:40:20 +1100 | [diff] [blame] | 1332 | machine_check_print_event_info(&vcpu->arch.mce_evt, false, true); |
Paul Mackerras | 884dfb7 | 2019-02-21 13:38:49 +1100 | [diff] [blame] | 1333 | |
| 1334 | /* |
| 1335 | * If the guest can do FWNMI, exit to userspace so it can |
| 1336 | * deliver a FWNMI to the guest. |
| 1337 | * Otherwise we synthesize a machine check for the guest |
| 1338 | * so that it knows that the machine check occurred. |
| 1339 | */ |
| 1340 | if (!vcpu->kvm->arch.fwnmi_enabled) { |
| 1341 | ulong flags = vcpu->arch.shregs.msr & 0x083c0000; |
| 1342 | kvmppc_core_queue_machine_check(vcpu, flags); |
| 1343 | r = RESUME_GUEST; |
| 1344 | break; |
| 1345 | } |
| 1346 | |
Aravinda Prasad | e20bbd3 | 2017-05-11 16:33:37 +0530 | [diff] [blame] | 1347 | /* Exit to guest with KVM_EXIT_NMI as exit reason */ |
| 1348 | run->exit_reason = KVM_EXIT_NMI; |
| 1349 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
| 1350 | /* Clear out the old NMI status from run->flags */ |
| 1351 | run->flags &= ~KVM_RUN_PPC_NMI_DISP_MASK; |
| 1352 | /* Now set the NMI status */ |
| 1353 | if (vcpu->arch.mce_evt.disposition == MCE_DISPOSITION_RECOVERED) |
| 1354 | run->flags |= KVM_RUN_PPC_NMI_DISP_FULLY_RECOV; |
| 1355 | else |
| 1356 | run->flags |= KVM_RUN_PPC_NMI_DISP_NOT_RECOV; |
| 1357 | |
| 1358 | r = RESUME_HOST; |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1359 | break; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1360 | case BOOK3S_INTERRUPT_PROGRAM: |
| 1361 | { |
| 1362 | ulong flags; |
| 1363 | /* |
| 1364 | * Normally program interrupts are delivered directly |
| 1365 | * to the guest by the hardware, but we can get here |
| 1366 | * as a result of a hypervisor emulation interrupt |
| 1367 | * (e40) getting turned into a 700 by BML RTAS. |
| 1368 | */ |
| 1369 | flags = vcpu->arch.shregs.msr & 0x1f0000ull; |
| 1370 | kvmppc_core_queue_program(vcpu, flags); |
| 1371 | r = RESUME_GUEST; |
| 1372 | break; |
| 1373 | } |
| 1374 | case BOOK3S_INTERRUPT_SYSCALL: |
| 1375 | { |
| 1376 | /* hcall - punt to userspace */ |
| 1377 | int i; |
| 1378 | |
Liu Ping Fan | 27025a6 | 2013-11-19 14:12:48 +0800 | [diff] [blame] | 1379 | /* hypercall with MSR_PR has already been handled in rmode, |
| 1380 | * and never reaches here. |
| 1381 | */ |
| 1382 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1383 | run->papr_hcall.nr = kvmppc_get_gpr(vcpu, 3); |
| 1384 | for (i = 0; i < 9; ++i) |
| 1385 | run->papr_hcall.args[i] = kvmppc_get_gpr(vcpu, 4 + i); |
| 1386 | run->exit_reason = KVM_EXIT_PAPR_HCALL; |
| 1387 | vcpu->arch.hcall_needed = 1; |
| 1388 | r = RESUME_HOST; |
| 1389 | break; |
| 1390 | } |
| 1391 | /* |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1392 | * We get these next two if the guest accesses a page which it thinks |
| 1393 | * it has mapped but which is not actually present, either because |
| 1394 | * it is for an emulated I/O device or because the corresonding |
| 1395 | * host page has been paged out. Any other HDSI/HISI interrupts |
| 1396 | * have been handled already. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1397 | */ |
| 1398 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1399 | r = RESUME_PAGE_FAULT; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1400 | break; |
| 1401 | case BOOK3S_INTERRUPT_H_INST_STORAGE: |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1402 | vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); |
Paul Mackerras | 32eb150 | 2018-10-08 16:30:56 +1100 | [diff] [blame] | 1403 | vcpu->arch.fault_dsisr = vcpu->arch.shregs.msr & |
| 1404 | DSISR_SRR1_MATCH_64S; |
| 1405 | if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE) |
| 1406 | vcpu->arch.fault_dsisr |= DSISR_ISSTORE; |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 1407 | r = RESUME_PAGE_FAULT; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1408 | break; |
| 1409 | /* |
| 1410 | * This occurs if the guest executes an illegal instruction. |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1411 | * If the guest debug is disabled, generate a program interrupt |
| 1412 | * to the guest. If guest debug is enabled, we need to check |
| 1413 | * whether the instruction is a software breakpoint instruction. |
| 1414 | * Accordingly return to Guest or Host. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1415 | */ |
| 1416 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: |
Paul Mackerras | 4a157d6 | 2014-12-03 13:30:39 +1100 | [diff] [blame] | 1417 | if (vcpu->arch.emul_inst != KVM_INST_FETCH_FAILED) |
| 1418 | vcpu->arch.last_inst = kvmppc_need_byteswap(vcpu) ? |
| 1419 | swab32(vcpu->arch.emul_inst) : |
| 1420 | vcpu->arch.emul_inst; |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1421 | if (vcpu->guest_debug & KVM_GUESTDBG_USE_SW_BP) { |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1422 | r = kvmppc_emulate_debug_inst(vcpu); |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1423 | } else { |
| 1424 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 1425 | r = RESUME_GUEST; |
| 1426 | } |
Michael Ellerman | bd3048b | 2014-01-08 21:25:23 +1100 | [diff] [blame] | 1427 | break; |
| 1428 | /* |
| 1429 | * This occurs if the guest (kernel or userspace), does something that |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 1430 | * is prohibited by HFSCR. |
| 1431 | * On POWER9, this could be a doorbell instruction that we need |
| 1432 | * to emulate. |
| 1433 | * Otherwise, we just generate a program interrupt to the guest. |
Michael Ellerman | bd3048b | 2014-01-08 21:25:23 +1100 | [diff] [blame] | 1434 | */ |
| 1435 | case BOOK3S_INTERRUPT_H_FAC_UNAVAIL: |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 1436 | r = EMULATE_FAIL; |
Paul Mackerras | 36ee41d | 2018-01-30 10:51:32 +1100 | [diff] [blame] | 1437 | if (((vcpu->arch.hfscr >> 56) == FSCR_MSGP_LG) && |
Paul Mackerras | 53655dd | 2018-10-08 16:30:54 +1100 | [diff] [blame] | 1438 | cpu_has_feature(CPU_FTR_ARCH_300)) |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 1439 | r = kvmppc_emulate_doorbell_instr(vcpu); |
| 1440 | if (r == EMULATE_FAIL) { |
| 1441 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 1442 | r = RESUME_GUEST; |
| 1443 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1444 | break; |
Paul Mackerras | 4bb3c7a | 2018-03-21 21:32:01 +1100 | [diff] [blame] | 1445 | |
| 1446 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1447 | case BOOK3S_INTERRUPT_HV_SOFTPATCH: |
| 1448 | /* |
| 1449 | * This occurs for various TM-related instructions that |
| 1450 | * we need to emulate on POWER9 DD2.2. We have already |
| 1451 | * handled the cases where the guest was in real-suspend |
| 1452 | * mode and was transitioning to transactional state. |
| 1453 | */ |
| 1454 | r = kvmhv_p9_tm_emulation(vcpu); |
| 1455 | break; |
| 1456 | #endif |
| 1457 | |
Suresh Warrier | f7af520 | 2016-08-19 15:35:52 +1000 | [diff] [blame] | 1458 | case BOOK3S_INTERRUPT_HV_RM_HARD: |
| 1459 | r = RESUME_PASSTHROUGH; |
| 1460 | break; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1461 | default: |
| 1462 | kvmppc_dump_regs(vcpu); |
| 1463 | printk(KERN_EMERG "trap=0x%x | pc=0x%lx | msr=0x%llx\n", |
| 1464 | vcpu->arch.trap, kvmppc_get_pc(vcpu), |
| 1465 | vcpu->arch.shregs.msr); |
Paul Mackerras | f3271d4 | 2013-09-20 14:52:41 +1000 | [diff] [blame] | 1466 | run->hw.hardware_exit_reason = vcpu->arch.trap; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1467 | r = RESUME_HOST; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1468 | break; |
| 1469 | } |
| 1470 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1471 | return r; |
| 1472 | } |
| 1473 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1474 | static int kvmppc_handle_nested_exit(struct kvm_vcpu *vcpu) |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1475 | { |
| 1476 | int r; |
| 1477 | int srcu_idx; |
| 1478 | |
| 1479 | vcpu->stat.sum_exits++; |
| 1480 | |
| 1481 | /* |
| 1482 | * This can happen if an interrupt occurs in the last stages |
| 1483 | * of guest entry or the first stages of guest exit (i.e. after |
| 1484 | * setting paca->kvm_hstate.in_guest to KVM_GUEST_MODE_GUEST_HV |
| 1485 | * and before setting it to KVM_GUEST_MODE_HOST_HV). |
| 1486 | * That can happen due to a bug, or due to a machine check |
| 1487 | * occurring at just the wrong time. |
| 1488 | */ |
| 1489 | if (vcpu->arch.shregs.msr & MSR_HV) { |
| 1490 | pr_emerg("KVM trap in HV mode while nested!\n"); |
| 1491 | pr_emerg("trap=0x%x | pc=0x%lx | msr=0x%llx\n", |
| 1492 | vcpu->arch.trap, kvmppc_get_pc(vcpu), |
| 1493 | vcpu->arch.shregs.msr); |
| 1494 | kvmppc_dump_regs(vcpu); |
| 1495 | return RESUME_HOST; |
| 1496 | } |
| 1497 | switch (vcpu->arch.trap) { |
| 1498 | /* We're good on these - the host merely wanted to get our attention */ |
| 1499 | case BOOK3S_INTERRUPT_HV_DECREMENTER: |
| 1500 | vcpu->stat.dec_exits++; |
| 1501 | r = RESUME_GUEST; |
| 1502 | break; |
| 1503 | case BOOK3S_INTERRUPT_EXTERNAL: |
| 1504 | vcpu->stat.ext_intr_exits++; |
| 1505 | r = RESUME_HOST; |
| 1506 | break; |
| 1507 | case BOOK3S_INTERRUPT_H_DOORBELL: |
| 1508 | case BOOK3S_INTERRUPT_H_VIRT: |
| 1509 | vcpu->stat.ext_intr_exits++; |
| 1510 | r = RESUME_GUEST; |
| 1511 | break; |
| 1512 | /* SR/HMI/PMI are HV interrupts that host has handled. Resume guest.*/ |
| 1513 | case BOOK3S_INTERRUPT_HMI: |
| 1514 | case BOOK3S_INTERRUPT_PERFMON: |
| 1515 | case BOOK3S_INTERRUPT_SYSTEM_RESET: |
| 1516 | r = RESUME_GUEST; |
| 1517 | break; |
| 1518 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
| 1519 | /* Pass the machine check to the L1 guest */ |
| 1520 | r = RESUME_HOST; |
| 1521 | /* Print the MCE event to host console. */ |
Paul Mackerras | c057720 | 2019-02-21 13:40:20 +1100 | [diff] [blame] | 1522 | machine_check_print_event_info(&vcpu->arch.mce_evt, false, true); |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1523 | break; |
| 1524 | /* |
| 1525 | * We get these next two if the guest accesses a page which it thinks |
| 1526 | * it has mapped but which is not actually present, either because |
| 1527 | * it is for an emulated I/O device or because the corresonding |
| 1528 | * host page has been paged out. |
| 1529 | */ |
| 1530 | case BOOK3S_INTERRUPT_H_DATA_STORAGE: |
| 1531 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1532 | r = kvmhv_nested_page_fault(vcpu); |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1533 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); |
| 1534 | break; |
| 1535 | case BOOK3S_INTERRUPT_H_INST_STORAGE: |
| 1536 | vcpu->arch.fault_dar = kvmppc_get_pc(vcpu); |
| 1537 | vcpu->arch.fault_dsisr = kvmppc_get_msr(vcpu) & |
| 1538 | DSISR_SRR1_MATCH_64S; |
| 1539 | if (vcpu->arch.shregs.msr & HSRR1_HISI_WRITE) |
| 1540 | vcpu->arch.fault_dsisr |= DSISR_ISSTORE; |
| 1541 | srcu_idx = srcu_read_lock(&vcpu->kvm->srcu); |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 1542 | r = kvmhv_nested_page_fault(vcpu); |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1543 | srcu_read_unlock(&vcpu->kvm->srcu, srcu_idx); |
| 1544 | break; |
| 1545 | |
| 1546 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1547 | case BOOK3S_INTERRUPT_HV_SOFTPATCH: |
| 1548 | /* |
| 1549 | * This occurs for various TM-related instructions that |
| 1550 | * we need to emulate on POWER9 DD2.2. We have already |
| 1551 | * handled the cases where the guest was in real-suspend |
| 1552 | * mode and was transitioning to transactional state. |
| 1553 | */ |
| 1554 | r = kvmhv_p9_tm_emulation(vcpu); |
| 1555 | break; |
| 1556 | #endif |
| 1557 | |
| 1558 | case BOOK3S_INTERRUPT_HV_RM_HARD: |
| 1559 | vcpu->arch.trap = 0; |
| 1560 | r = RESUME_GUEST; |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 1561 | if (!xics_on_xive()) |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 1562 | kvmppc_xics_rm_complete(vcpu, 0); |
| 1563 | break; |
| 1564 | default: |
| 1565 | r = RESUME_HOST; |
| 1566 | break; |
| 1567 | } |
| 1568 | |
| 1569 | return r; |
| 1570 | } |
| 1571 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1572 | static int kvm_arch_vcpu_ioctl_get_sregs_hv(struct kvm_vcpu *vcpu, |
| 1573 | struct kvm_sregs *sregs) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1574 | { |
| 1575 | int i; |
| 1576 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1577 | memset(sregs, 0, sizeof(struct kvm_sregs)); |
Aneesh Kumar K.V | 8791644 | 2013-08-22 17:08:39 +0530 | [diff] [blame] | 1578 | sregs->pvr = vcpu->arch.pvr; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1579 | for (i = 0; i < vcpu->arch.slb_max; i++) { |
| 1580 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige; |
| 1581 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; |
| 1582 | } |
| 1583 | |
| 1584 | return 0; |
| 1585 | } |
| 1586 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1587 | static int kvm_arch_vcpu_ioctl_set_sregs_hv(struct kvm_vcpu *vcpu, |
| 1588 | struct kvm_sregs *sregs) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1589 | { |
| 1590 | int i, j; |
| 1591 | |
Paul Mackerras | 9333e6c | 2014-09-02 16:14:43 +1000 | [diff] [blame] | 1592 | /* Only accept the same PVR as the host's, since we can't spoof it */ |
| 1593 | if (sregs->pvr != vcpu->arch.pvr) |
| 1594 | return -EINVAL; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1595 | |
| 1596 | j = 0; |
| 1597 | for (i = 0; i < vcpu->arch.slb_nr; i++) { |
| 1598 | if (sregs->u.s.ppc64.slb[i].slbe & SLB_ESID_V) { |
| 1599 | vcpu->arch.slb[j].orige = sregs->u.s.ppc64.slb[i].slbe; |
| 1600 | vcpu->arch.slb[j].origv = sregs->u.s.ppc64.slb[i].slbv; |
| 1601 | ++j; |
| 1602 | } |
| 1603 | } |
| 1604 | vcpu->arch.slb_max = j; |
| 1605 | |
| 1606 | return 0; |
| 1607 | } |
| 1608 | |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1609 | static void kvmppc_set_lpcr(struct kvm_vcpu *vcpu, u64 new_lpcr, |
| 1610 | bool preserve_top32) |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1611 | { |
Paul Mackerras | 8f902b0 | 2015-03-20 20:39:38 +1100 | [diff] [blame] | 1612 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1613 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 1614 | u64 mask; |
| 1615 | |
| 1616 | spin_lock(&vc->lock); |
| 1617 | /* |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1618 | * If ILE (interrupt little-endian) has changed, update the |
| 1619 | * MSR_LE bit in the intr_msr for each vcpu in this vcore. |
| 1620 | */ |
| 1621 | if ((new_lpcr & LPCR_ILE) != (vc->lpcr & LPCR_ILE)) { |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1622 | struct kvm_vcpu *vcpu; |
| 1623 | int i; |
| 1624 | |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1625 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 1626 | if (vcpu->arch.vcore != vc) |
| 1627 | continue; |
| 1628 | if (new_lpcr & LPCR_ILE) |
| 1629 | vcpu->arch.intr_msr |= MSR_LE; |
| 1630 | else |
| 1631 | vcpu->arch.intr_msr &= ~MSR_LE; |
| 1632 | } |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | /* |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1636 | * Userspace can only modify DPFD (default prefetch depth), |
| 1637 | * ILE (interrupt little-endian) and TC (translation control). |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 1638 | * On POWER8 and POWER9 userspace can also modify AIL (alt. interrupt loc.). |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1639 | */ |
| 1640 | mask = LPCR_DPFD | LPCR_ILE | LPCR_TC; |
Paul Mackerras | e0622bd | 2014-01-08 21:25:27 +1100 | [diff] [blame] | 1641 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 1642 | mask |= LPCR_AIL; |
Paul Mackerras | 1bc3fe8 | 2017-05-22 16:55:16 +1000 | [diff] [blame] | 1643 | /* |
| 1644 | * On POWER9, allow userspace to enable large decrementer for the |
| 1645 | * guest, whether or not the host has it enabled. |
| 1646 | */ |
| 1647 | if (cpu_has_feature(CPU_FTR_ARCH_300)) |
| 1648 | mask |= LPCR_LD; |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1649 | |
| 1650 | /* Broken 32-bit version of LPCR must not clear top bits */ |
| 1651 | if (preserve_top32) |
| 1652 | mask &= 0xFFFFFFFF; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1653 | vc->lpcr = (vc->lpcr & ~mask) | (new_lpcr & mask); |
| 1654 | spin_unlock(&vc->lock); |
| 1655 | } |
| 1656 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1657 | static int kvmppc_get_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
| 1658 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1659 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1660 | int r = 0; |
| 1661 | long int i; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1662 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1663 | switch (id) { |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1664 | case KVM_REG_PPC_DEBUG_INST: |
| 1665 | *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT); |
| 1666 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1667 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1668 | *val = get_reg_val(id, 0); |
| 1669 | break; |
| 1670 | case KVM_REG_PPC_DABR: |
| 1671 | *val = get_reg_val(id, vcpu->arch.dabr); |
| 1672 | break; |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 1673 | case KVM_REG_PPC_DABRX: |
| 1674 | *val = get_reg_val(id, vcpu->arch.dabrx); |
| 1675 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1676 | case KVM_REG_PPC_DSCR: |
| 1677 | *val = get_reg_val(id, vcpu->arch.dscr); |
| 1678 | break; |
| 1679 | case KVM_REG_PPC_PURR: |
| 1680 | *val = get_reg_val(id, vcpu->arch.purr); |
| 1681 | break; |
| 1682 | case KVM_REG_PPC_SPURR: |
| 1683 | *val = get_reg_val(id, vcpu->arch.spurr); |
| 1684 | break; |
| 1685 | case KVM_REG_PPC_AMR: |
| 1686 | *val = get_reg_val(id, vcpu->arch.amr); |
| 1687 | break; |
| 1688 | case KVM_REG_PPC_UAMOR: |
| 1689 | *val = get_reg_val(id, vcpu->arch.uamor); |
| 1690 | break; |
Athira Rajeev | 7e4a145 | 2020-07-17 10:38:14 -0400 | [diff] [blame] | 1691 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1692 | i = id - KVM_REG_PPC_MMCR0; |
| 1693 | *val = get_reg_val(id, vcpu->arch.mmcr[i]); |
| 1694 | break; |
Athira Rajeev | 7e4a145 | 2020-07-17 10:38:14 -0400 | [diff] [blame] | 1695 | case KVM_REG_PPC_MMCR2: |
| 1696 | *val = get_reg_val(id, vcpu->arch.mmcr[2]); |
| 1697 | break; |
| 1698 | case KVM_REG_PPC_MMCRA: |
| 1699 | *val = get_reg_val(id, vcpu->arch.mmcra); |
| 1700 | break; |
| 1701 | case KVM_REG_PPC_MMCRS: |
| 1702 | *val = get_reg_val(id, vcpu->arch.mmcrs); |
| 1703 | break; |
Athira Rajeev | 5752fe0 | 2020-07-17 10:38:17 -0400 | [diff] [blame] | 1704 | case KVM_REG_PPC_MMCR3: |
| 1705 | *val = get_reg_val(id, vcpu->arch.mmcr[3]); |
| 1706 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1707 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: |
| 1708 | i = id - KVM_REG_PPC_PMC1; |
| 1709 | *val = get_reg_val(id, vcpu->arch.pmc[i]); |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1710 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1711 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
| 1712 | i = id - KVM_REG_PPC_SPMC1; |
| 1713 | *val = get_reg_val(id, vcpu->arch.spmc[i]); |
| 1714 | break; |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 1715 | case KVM_REG_PPC_SIAR: |
| 1716 | *val = get_reg_val(id, vcpu->arch.siar); |
| 1717 | break; |
| 1718 | case KVM_REG_PPC_SDAR: |
| 1719 | *val = get_reg_val(id, vcpu->arch.sdar); |
| 1720 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1721 | case KVM_REG_PPC_SIER: |
Athira Rajeev | 5752fe0 | 2020-07-17 10:38:17 -0400 | [diff] [blame] | 1722 | *val = get_reg_val(id, vcpu->arch.sier[0]); |
| 1723 | break; |
| 1724 | case KVM_REG_PPC_SIER2: |
| 1725 | *val = get_reg_val(id, vcpu->arch.sier[1]); |
| 1726 | break; |
| 1727 | case KVM_REG_PPC_SIER3: |
| 1728 | *val = get_reg_val(id, vcpu->arch.sier[2]); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1729 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1730 | case KVM_REG_PPC_IAMR: |
| 1731 | *val = get_reg_val(id, vcpu->arch.iamr); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1732 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1733 | case KVM_REG_PPC_PSPB: |
| 1734 | *val = get_reg_val(id, vcpu->arch.pspb); |
| 1735 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1736 | case KVM_REG_PPC_DPDES: |
Paul Mackerras | ff42df4 | 2019-08-27 11:35:40 +1000 | [diff] [blame] | 1737 | /* |
| 1738 | * On POWER9, where we are emulating msgsndp etc., |
| 1739 | * we return 1 bit for each vcpu, which can come from |
| 1740 | * either vcore->dpdes or doorbell_request. |
| 1741 | * On POWER8, doorbell_request is 0. |
| 1742 | */ |
| 1743 | *val = get_reg_val(id, vcpu->arch.vcore->dpdes | |
| 1744 | vcpu->arch.doorbell_request); |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1745 | break; |
Paul Mackerras | 88b02cf9 | 2016-09-15 13:42:52 +1000 | [diff] [blame] | 1746 | case KVM_REG_PPC_VTB: |
| 1747 | *val = get_reg_val(id, vcpu->arch.vcore->vtb); |
| 1748 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1749 | case KVM_REG_PPC_DAWR: |
| 1750 | *val = get_reg_val(id, vcpu->arch.dawr); |
| 1751 | break; |
| 1752 | case KVM_REG_PPC_DAWRX: |
| 1753 | *val = get_reg_val(id, vcpu->arch.dawrx); |
| 1754 | break; |
| 1755 | case KVM_REG_PPC_CIABR: |
| 1756 | *val = get_reg_val(id, vcpu->arch.ciabr); |
| 1757 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1758 | case KVM_REG_PPC_CSIGR: |
| 1759 | *val = get_reg_val(id, vcpu->arch.csigr); |
| 1760 | break; |
| 1761 | case KVM_REG_PPC_TACR: |
| 1762 | *val = get_reg_val(id, vcpu->arch.tacr); |
| 1763 | break; |
| 1764 | case KVM_REG_PPC_TCSCR: |
| 1765 | *val = get_reg_val(id, vcpu->arch.tcscr); |
| 1766 | break; |
| 1767 | case KVM_REG_PPC_PID: |
| 1768 | *val = get_reg_val(id, vcpu->arch.pid); |
| 1769 | break; |
| 1770 | case KVM_REG_PPC_ACOP: |
| 1771 | *val = get_reg_val(id, vcpu->arch.acop); |
| 1772 | break; |
| 1773 | case KVM_REG_PPC_WORT: |
| 1774 | *val = get_reg_val(id, vcpu->arch.wort); |
| 1775 | break; |
Paul Mackerras | e9cf1e0 | 2016-11-18 13:11:42 +1100 | [diff] [blame] | 1776 | case KVM_REG_PPC_TIDR: |
| 1777 | *val = get_reg_val(id, vcpu->arch.tid); |
| 1778 | break; |
| 1779 | case KVM_REG_PPC_PSSCR: |
| 1780 | *val = get_reg_val(id, vcpu->arch.psscr); |
| 1781 | break; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1782 | case KVM_REG_PPC_VPA_ADDR: |
| 1783 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1784 | *val = get_reg_val(id, vcpu->arch.vpa.next_gpa); |
| 1785 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1786 | break; |
| 1787 | case KVM_REG_PPC_VPA_SLB: |
| 1788 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1789 | val->vpaval.addr = vcpu->arch.slb_shadow.next_gpa; |
| 1790 | val->vpaval.length = vcpu->arch.slb_shadow.len; |
| 1791 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1792 | break; |
| 1793 | case KVM_REG_PPC_VPA_DTL: |
| 1794 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 1795 | val->vpaval.addr = vcpu->arch.dtl.next_gpa; |
| 1796 | val->vpaval.length = vcpu->arch.dtl.len; |
| 1797 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 1798 | break; |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 1799 | case KVM_REG_PPC_TB_OFFSET: |
| 1800 | *val = get_reg_val(id, vcpu->arch.vcore->tb_offset); |
| 1801 | break; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1802 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1803 | case KVM_REG_PPC_LPCR_64: |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 1804 | *val = get_reg_val(id, vcpu->arch.vcore->lpcr); |
| 1805 | break; |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1806 | case KVM_REG_PPC_PPR: |
| 1807 | *val = get_reg_val(id, vcpu->arch.ppr); |
| 1808 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 1809 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1810 | case KVM_REG_PPC_TFHAR: |
| 1811 | *val = get_reg_val(id, vcpu->arch.tfhar); |
| 1812 | break; |
| 1813 | case KVM_REG_PPC_TFIAR: |
| 1814 | *val = get_reg_val(id, vcpu->arch.tfiar); |
| 1815 | break; |
| 1816 | case KVM_REG_PPC_TEXASR: |
| 1817 | *val = get_reg_val(id, vcpu->arch.texasr); |
| 1818 | break; |
| 1819 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 1820 | i = id - KVM_REG_PPC_TM_GPR0; |
| 1821 | *val = get_reg_val(id, vcpu->arch.gpr_tm[i]); |
| 1822 | break; |
| 1823 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 1824 | { |
| 1825 | int j; |
| 1826 | i = id - KVM_REG_PPC_TM_VSR0; |
| 1827 | if (i < 32) |
| 1828 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 1829 | val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j]; |
| 1830 | else { |
| 1831 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1832 | val->vval = vcpu->arch.vr_tm.vr[i-32]; |
| 1833 | else |
| 1834 | r = -ENXIO; |
| 1835 | } |
| 1836 | break; |
| 1837 | } |
| 1838 | case KVM_REG_PPC_TM_CR: |
| 1839 | *val = get_reg_val(id, vcpu->arch.cr_tm); |
| 1840 | break; |
Paul Mackerras | 0d808df | 2016-11-07 15:09:58 +1100 | [diff] [blame] | 1841 | case KVM_REG_PPC_TM_XER: |
| 1842 | *val = get_reg_val(id, vcpu->arch.xer_tm); |
| 1843 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 1844 | case KVM_REG_PPC_TM_LR: |
| 1845 | *val = get_reg_val(id, vcpu->arch.lr_tm); |
| 1846 | break; |
| 1847 | case KVM_REG_PPC_TM_CTR: |
| 1848 | *val = get_reg_val(id, vcpu->arch.ctr_tm); |
| 1849 | break; |
| 1850 | case KVM_REG_PPC_TM_FPSCR: |
| 1851 | *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr); |
| 1852 | break; |
| 1853 | case KVM_REG_PPC_TM_AMR: |
| 1854 | *val = get_reg_val(id, vcpu->arch.amr_tm); |
| 1855 | break; |
| 1856 | case KVM_REG_PPC_TM_PPR: |
| 1857 | *val = get_reg_val(id, vcpu->arch.ppr_tm); |
| 1858 | break; |
| 1859 | case KVM_REG_PPC_TM_VRSAVE: |
| 1860 | *val = get_reg_val(id, vcpu->arch.vrsave_tm); |
| 1861 | break; |
| 1862 | case KVM_REG_PPC_TM_VSCR: |
| 1863 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1864 | *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]); |
| 1865 | else |
| 1866 | r = -ENXIO; |
| 1867 | break; |
| 1868 | case KVM_REG_PPC_TM_DSCR: |
| 1869 | *val = get_reg_val(id, vcpu->arch.dscr_tm); |
| 1870 | break; |
| 1871 | case KVM_REG_PPC_TM_TAR: |
| 1872 | *val = get_reg_val(id, vcpu->arch.tar_tm); |
| 1873 | break; |
| 1874 | #endif |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 1875 | case KVM_REG_PPC_ARCH_COMPAT: |
| 1876 | *val = get_reg_val(id, vcpu->arch.vcore->arch_compat); |
| 1877 | break; |
Paul Mackerras | 5855564 | 2018-01-12 20:55:20 +1100 | [diff] [blame] | 1878 | case KVM_REG_PPC_DEC_EXPIRY: |
| 1879 | *val = get_reg_val(id, vcpu->arch.dec_expires + |
| 1880 | vcpu->arch.vcore->tb_offset); |
| 1881 | break; |
Paul Mackerras | a1f1582 | 2018-04-20 15:33:21 +1000 | [diff] [blame] | 1882 | case KVM_REG_PPC_ONLINE: |
| 1883 | *val = get_reg_val(id, vcpu->arch.online); |
| 1884 | break; |
Paul Mackerras | 3032341 | 2018-10-08 16:31:13 +1100 | [diff] [blame] | 1885 | case KVM_REG_PPC_PTCR: |
| 1886 | *val = get_reg_val(id, vcpu->kvm->arch.l1_ptcr); |
| 1887 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1888 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1889 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1890 | break; |
| 1891 | } |
| 1892 | |
| 1893 | return r; |
| 1894 | } |
| 1895 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1896 | static int kvmppc_set_one_reg_hv(struct kvm_vcpu *vcpu, u64 id, |
| 1897 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1898 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1899 | int r = 0; |
| 1900 | long int i; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 1901 | unsigned long addr, len; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1902 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1903 | switch (id) { |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1904 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1905 | /* Only allow this to be set to zero */ |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1906 | if (set_reg_val(id, *val)) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1907 | r = -EINVAL; |
| 1908 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1909 | case KVM_REG_PPC_DABR: |
| 1910 | vcpu->arch.dabr = set_reg_val(id, *val); |
| 1911 | break; |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 1912 | case KVM_REG_PPC_DABRX: |
| 1913 | vcpu->arch.dabrx = set_reg_val(id, *val) & ~DABRX_HYP; |
| 1914 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1915 | case KVM_REG_PPC_DSCR: |
| 1916 | vcpu->arch.dscr = set_reg_val(id, *val); |
| 1917 | break; |
| 1918 | case KVM_REG_PPC_PURR: |
| 1919 | vcpu->arch.purr = set_reg_val(id, *val); |
| 1920 | break; |
| 1921 | case KVM_REG_PPC_SPURR: |
| 1922 | vcpu->arch.spurr = set_reg_val(id, *val); |
| 1923 | break; |
| 1924 | case KVM_REG_PPC_AMR: |
| 1925 | vcpu->arch.amr = set_reg_val(id, *val); |
| 1926 | break; |
| 1927 | case KVM_REG_PPC_UAMOR: |
| 1928 | vcpu->arch.uamor = set_reg_val(id, *val); |
| 1929 | break; |
Athira Rajeev | 7e4a145 | 2020-07-17 10:38:14 -0400 | [diff] [blame] | 1930 | case KVM_REG_PPC_MMCR0 ... KVM_REG_PPC_MMCR1: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1931 | i = id - KVM_REG_PPC_MMCR0; |
| 1932 | vcpu->arch.mmcr[i] = set_reg_val(id, *val); |
| 1933 | break; |
Athira Rajeev | 7e4a145 | 2020-07-17 10:38:14 -0400 | [diff] [blame] | 1934 | case KVM_REG_PPC_MMCR2: |
| 1935 | vcpu->arch.mmcr[2] = set_reg_val(id, *val); |
| 1936 | break; |
| 1937 | case KVM_REG_PPC_MMCRA: |
| 1938 | vcpu->arch.mmcra = set_reg_val(id, *val); |
| 1939 | break; |
| 1940 | case KVM_REG_PPC_MMCRS: |
| 1941 | vcpu->arch.mmcrs = set_reg_val(id, *val); |
| 1942 | break; |
Athira Rajeev | 5752fe0 | 2020-07-17 10:38:17 -0400 | [diff] [blame] | 1943 | case KVM_REG_PPC_MMCR3: |
| 1944 | *val = get_reg_val(id, vcpu->arch.mmcr[3]); |
| 1945 | break; |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1946 | case KVM_REG_PPC_PMC1 ... KVM_REG_PPC_PMC8: |
| 1947 | i = id - KVM_REG_PPC_PMC1; |
| 1948 | vcpu->arch.pmc[i] = set_reg_val(id, *val); |
| 1949 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1950 | case KVM_REG_PPC_SPMC1 ... KVM_REG_PPC_SPMC2: |
| 1951 | i = id - KVM_REG_PPC_SPMC1; |
| 1952 | vcpu->arch.spmc[i] = set_reg_val(id, *val); |
| 1953 | break; |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 1954 | case KVM_REG_PPC_SIAR: |
| 1955 | vcpu->arch.siar = set_reg_val(id, *val); |
| 1956 | break; |
| 1957 | case KVM_REG_PPC_SDAR: |
| 1958 | vcpu->arch.sdar = set_reg_val(id, *val); |
| 1959 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1960 | case KVM_REG_PPC_SIER: |
Athira Rajeev | 5752fe0 | 2020-07-17 10:38:17 -0400 | [diff] [blame] | 1961 | vcpu->arch.sier[0] = set_reg_val(id, *val); |
| 1962 | break; |
| 1963 | case KVM_REG_PPC_SIER2: |
| 1964 | vcpu->arch.sier[1] = set_reg_val(id, *val); |
| 1965 | break; |
| 1966 | case KVM_REG_PPC_SIER3: |
| 1967 | vcpu->arch.sier[2] = set_reg_val(id, *val); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1968 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1969 | case KVM_REG_PPC_IAMR: |
| 1970 | vcpu->arch.iamr = set_reg_val(id, *val); |
Paul Mackerras | a8bd19e | 2012-09-25 20:32:30 +0000 | [diff] [blame] | 1971 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1972 | case KVM_REG_PPC_PSPB: |
| 1973 | vcpu->arch.pspb = set_reg_val(id, *val); |
| 1974 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1975 | case KVM_REG_PPC_DPDES: |
| 1976 | vcpu->arch.vcore->dpdes = set_reg_val(id, *val); |
| 1977 | break; |
Paul Mackerras | 88b02cf9 | 2016-09-15 13:42:52 +1000 | [diff] [blame] | 1978 | case KVM_REG_PPC_VTB: |
| 1979 | vcpu->arch.vcore->vtb = set_reg_val(id, *val); |
| 1980 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1981 | case KVM_REG_PPC_DAWR: |
| 1982 | vcpu->arch.dawr = set_reg_val(id, *val); |
| 1983 | break; |
| 1984 | case KVM_REG_PPC_DAWRX: |
| 1985 | vcpu->arch.dawrx = set_reg_val(id, *val) & ~DAWRX_HYP; |
| 1986 | break; |
| 1987 | case KVM_REG_PPC_CIABR: |
| 1988 | vcpu->arch.ciabr = set_reg_val(id, *val); |
| 1989 | /* Don't allow setting breakpoints in hypervisor code */ |
| 1990 | if ((vcpu->arch.ciabr & CIABR_PRIV) == CIABR_PRIV_HYPER) |
| 1991 | vcpu->arch.ciabr &= ~CIABR_PRIV; /* disable */ |
| 1992 | break; |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1993 | case KVM_REG_PPC_CSIGR: |
| 1994 | vcpu->arch.csigr = set_reg_val(id, *val); |
| 1995 | break; |
| 1996 | case KVM_REG_PPC_TACR: |
| 1997 | vcpu->arch.tacr = set_reg_val(id, *val); |
| 1998 | break; |
| 1999 | case KVM_REG_PPC_TCSCR: |
| 2000 | vcpu->arch.tcscr = set_reg_val(id, *val); |
| 2001 | break; |
| 2002 | case KVM_REG_PPC_PID: |
| 2003 | vcpu->arch.pid = set_reg_val(id, *val); |
| 2004 | break; |
| 2005 | case KVM_REG_PPC_ACOP: |
| 2006 | vcpu->arch.acop = set_reg_val(id, *val); |
| 2007 | break; |
| 2008 | case KVM_REG_PPC_WORT: |
| 2009 | vcpu->arch.wort = set_reg_val(id, *val); |
| 2010 | break; |
Paul Mackerras | e9cf1e0 | 2016-11-18 13:11:42 +1100 | [diff] [blame] | 2011 | case KVM_REG_PPC_TIDR: |
| 2012 | vcpu->arch.tid = set_reg_val(id, *val); |
| 2013 | break; |
| 2014 | case KVM_REG_PPC_PSSCR: |
| 2015 | vcpu->arch.psscr = set_reg_val(id, *val) & PSSCR_GUEST_VIS; |
| 2016 | break; |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 2017 | case KVM_REG_PPC_VPA_ADDR: |
| 2018 | addr = set_reg_val(id, *val); |
| 2019 | r = -EINVAL; |
| 2020 | if (!addr && (vcpu->arch.slb_shadow.next_gpa || |
| 2021 | vcpu->arch.dtl.next_gpa)) |
| 2022 | break; |
| 2023 | r = set_vpa(vcpu, &vcpu->arch.vpa, addr, sizeof(struct lppaca)); |
| 2024 | break; |
| 2025 | case KVM_REG_PPC_VPA_SLB: |
| 2026 | addr = val->vpaval.addr; |
| 2027 | len = val->vpaval.length; |
| 2028 | r = -EINVAL; |
| 2029 | if (addr && !vcpu->arch.vpa.next_gpa) |
| 2030 | break; |
| 2031 | r = set_vpa(vcpu, &vcpu->arch.slb_shadow, addr, len); |
| 2032 | break; |
| 2033 | case KVM_REG_PPC_VPA_DTL: |
| 2034 | addr = val->vpaval.addr; |
| 2035 | len = val->vpaval.length; |
| 2036 | r = -EINVAL; |
Paul Mackerras | 9f8c8c7 | 2012-10-15 01:18:37 +0000 | [diff] [blame] | 2037 | if (addr && (len < sizeof(struct dtl_entry) || |
| 2038 | !vcpu->arch.vpa.next_gpa)) |
Paul Mackerras | 55b665b | 2012-09-25 20:33:06 +0000 | [diff] [blame] | 2039 | break; |
| 2040 | len -= len % sizeof(struct dtl_entry); |
| 2041 | r = set_vpa(vcpu, &vcpu->arch.dtl, addr, len); |
| 2042 | break; |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 2043 | case KVM_REG_PPC_TB_OFFSET: |
| 2044 | /* round up to multiple of 2^24 */ |
| 2045 | vcpu->arch.vcore->tb_offset = |
| 2046 | ALIGN(set_reg_val(id, *val), 1UL << 24); |
| 2047 | break; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2048 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 2049 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), true); |
| 2050 | break; |
| 2051 | case KVM_REG_PPC_LPCR_64: |
| 2052 | kvmppc_set_lpcr(vcpu, set_reg_val(id, *val), false); |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 2053 | break; |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 2054 | case KVM_REG_PPC_PPR: |
| 2055 | vcpu->arch.ppr = set_reg_val(id, *val); |
| 2056 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 2057 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 2058 | case KVM_REG_PPC_TFHAR: |
| 2059 | vcpu->arch.tfhar = set_reg_val(id, *val); |
| 2060 | break; |
| 2061 | case KVM_REG_PPC_TFIAR: |
| 2062 | vcpu->arch.tfiar = set_reg_val(id, *val); |
| 2063 | break; |
| 2064 | case KVM_REG_PPC_TEXASR: |
| 2065 | vcpu->arch.texasr = set_reg_val(id, *val); |
| 2066 | break; |
| 2067 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 2068 | i = id - KVM_REG_PPC_TM_GPR0; |
| 2069 | vcpu->arch.gpr_tm[i] = set_reg_val(id, *val); |
| 2070 | break; |
| 2071 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 2072 | { |
| 2073 | int j; |
| 2074 | i = id - KVM_REG_PPC_TM_VSR0; |
| 2075 | if (i < 32) |
| 2076 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 2077 | vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j]; |
| 2078 | else |
| 2079 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 2080 | vcpu->arch.vr_tm.vr[i-32] = val->vval; |
| 2081 | else |
| 2082 | r = -ENXIO; |
| 2083 | break; |
| 2084 | } |
| 2085 | case KVM_REG_PPC_TM_CR: |
| 2086 | vcpu->arch.cr_tm = set_reg_val(id, *val); |
| 2087 | break; |
Paul Mackerras | 0d808df | 2016-11-07 15:09:58 +1100 | [diff] [blame] | 2088 | case KVM_REG_PPC_TM_XER: |
| 2089 | vcpu->arch.xer_tm = set_reg_val(id, *val); |
| 2090 | break; |
Michael Neuling | a7d80d0 | 2014-03-25 10:47:03 +1100 | [diff] [blame] | 2091 | case KVM_REG_PPC_TM_LR: |
| 2092 | vcpu->arch.lr_tm = set_reg_val(id, *val); |
| 2093 | break; |
| 2094 | case KVM_REG_PPC_TM_CTR: |
| 2095 | vcpu->arch.ctr_tm = set_reg_val(id, *val); |
| 2096 | break; |
| 2097 | case KVM_REG_PPC_TM_FPSCR: |
| 2098 | vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val); |
| 2099 | break; |
| 2100 | case KVM_REG_PPC_TM_AMR: |
| 2101 | vcpu->arch.amr_tm = set_reg_val(id, *val); |
| 2102 | break; |
| 2103 | case KVM_REG_PPC_TM_PPR: |
| 2104 | vcpu->arch.ppr_tm = set_reg_val(id, *val); |
| 2105 | break; |
| 2106 | case KVM_REG_PPC_TM_VRSAVE: |
| 2107 | vcpu->arch.vrsave_tm = set_reg_val(id, *val); |
| 2108 | break; |
| 2109 | case KVM_REG_PPC_TM_VSCR: |
| 2110 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 2111 | vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val); |
| 2112 | else |
| 2113 | r = - ENXIO; |
| 2114 | break; |
| 2115 | case KVM_REG_PPC_TM_DSCR: |
| 2116 | vcpu->arch.dscr_tm = set_reg_val(id, *val); |
| 2117 | break; |
| 2118 | case KVM_REG_PPC_TM_TAR: |
| 2119 | vcpu->arch.tar_tm = set_reg_val(id, *val); |
| 2120 | break; |
| 2121 | #endif |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 2122 | case KVM_REG_PPC_ARCH_COMPAT: |
| 2123 | r = kvmppc_set_arch_compat(vcpu, set_reg_val(id, *val)); |
| 2124 | break; |
Paul Mackerras | 5855564 | 2018-01-12 20:55:20 +1100 | [diff] [blame] | 2125 | case KVM_REG_PPC_DEC_EXPIRY: |
| 2126 | vcpu->arch.dec_expires = set_reg_val(id, *val) - |
| 2127 | vcpu->arch.vcore->tb_offset; |
| 2128 | break; |
Paul Mackerras | a1f1582 | 2018-04-20 15:33:21 +1000 | [diff] [blame] | 2129 | case KVM_REG_PPC_ONLINE: |
Paul Mackerras | 7aa1584 | 2018-04-20 19:53:22 +1000 | [diff] [blame] | 2130 | i = set_reg_val(id, *val); |
| 2131 | if (i && !vcpu->arch.online) |
| 2132 | atomic_inc(&vcpu->arch.vcore->online_count); |
| 2133 | else if (!i && vcpu->arch.online) |
| 2134 | atomic_dec(&vcpu->arch.vcore->online_count); |
| 2135 | vcpu->arch.online = i; |
Paul Mackerras | a1f1582 | 2018-04-20 15:33:21 +1000 | [diff] [blame] | 2136 | break; |
Paul Mackerras | 3032341 | 2018-10-08 16:31:13 +1100 | [diff] [blame] | 2137 | case KVM_REG_PPC_PTCR: |
| 2138 | vcpu->kvm->arch.l1_ptcr = set_reg_val(id, *val); |
| 2139 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 2140 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 2141 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 2142 | break; |
| 2143 | } |
| 2144 | |
| 2145 | return r; |
| 2146 | } |
| 2147 | |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 2148 | /* |
| 2149 | * On POWER9, threads are independent and can be in different partitions. |
| 2150 | * Therefore we consider each thread to be a subcore. |
| 2151 | * There is a restriction that all threads have to be in the same |
| 2152 | * MMU mode (radix or HPT), unfortunately, but since we only support |
| 2153 | * HPT guests on a HPT host so far, that isn't an impediment yet. |
| 2154 | */ |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 2155 | static int threads_per_vcore(struct kvm *kvm) |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 2156 | { |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 2157 | if (kvm->arch.threads_indep) |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 2158 | return 1; |
| 2159 | return threads_per_subcore; |
| 2160 | } |
| 2161 | |
Sam Bobroff | 1e175d2 | 2018-07-25 16:12:02 +1000 | [diff] [blame] | 2162 | static struct kvmppc_vcore *kvmppc_vcore_create(struct kvm *kvm, int id) |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 2163 | { |
| 2164 | struct kvmppc_vcore *vcore; |
| 2165 | |
| 2166 | vcore = kzalloc(sizeof(struct kvmppc_vcore), GFP_KERNEL); |
| 2167 | |
| 2168 | if (vcore == NULL) |
| 2169 | return NULL; |
| 2170 | |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 2171 | spin_lock_init(&vcore->lock); |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 2172 | spin_lock_init(&vcore->stoltb_lock); |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 2173 | rcuwait_init(&vcore->wait); |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 2174 | vcore->preempt_tb = TB_NIL; |
| 2175 | vcore->lpcr = kvm->arch.lpcr; |
Sam Bobroff | 1e175d2 | 2018-07-25 16:12:02 +1000 | [diff] [blame] | 2176 | vcore->first_vcpuid = id; |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 2177 | vcore->kvm = kvm; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2178 | INIT_LIST_HEAD(&vcore->preempt_list); |
Stewart Smith | de9bdd1 | 2014-07-18 14:18:42 +1000 | [diff] [blame] | 2179 | |
| 2180 | return vcore; |
| 2181 | } |
| 2182 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2183 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 2184 | static struct debugfs_timings_element { |
| 2185 | const char *name; |
| 2186 | size_t offset; |
| 2187 | } timings[] = { |
| 2188 | {"rm_entry", offsetof(struct kvm_vcpu, arch.rm_entry)}, |
| 2189 | {"rm_intr", offsetof(struct kvm_vcpu, arch.rm_intr)}, |
| 2190 | {"rm_exit", offsetof(struct kvm_vcpu, arch.rm_exit)}, |
| 2191 | {"guest", offsetof(struct kvm_vcpu, arch.guest_time)}, |
| 2192 | {"cede", offsetof(struct kvm_vcpu, arch.cede_time)}, |
| 2193 | }; |
| 2194 | |
Thomas Meyer | 4bb817ed | 2017-09-03 14:19:31 +0200 | [diff] [blame] | 2195 | #define N_TIMINGS (ARRAY_SIZE(timings)) |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2196 | |
| 2197 | struct debugfs_timings_state { |
| 2198 | struct kvm_vcpu *vcpu; |
| 2199 | unsigned int buflen; |
| 2200 | char buf[N_TIMINGS * 100]; |
| 2201 | }; |
| 2202 | |
| 2203 | static int debugfs_timings_open(struct inode *inode, struct file *file) |
| 2204 | { |
| 2205 | struct kvm_vcpu *vcpu = inode->i_private; |
| 2206 | struct debugfs_timings_state *p; |
| 2207 | |
| 2208 | p = kzalloc(sizeof(*p), GFP_KERNEL); |
| 2209 | if (!p) |
| 2210 | return -ENOMEM; |
| 2211 | |
| 2212 | kvm_get_kvm(vcpu->kvm); |
| 2213 | p->vcpu = vcpu; |
| 2214 | file->private_data = p; |
| 2215 | |
| 2216 | return nonseekable_open(inode, file); |
| 2217 | } |
| 2218 | |
| 2219 | static int debugfs_timings_release(struct inode *inode, struct file *file) |
| 2220 | { |
| 2221 | struct debugfs_timings_state *p = file->private_data; |
| 2222 | |
| 2223 | kvm_put_kvm(p->vcpu->kvm); |
| 2224 | kfree(p); |
| 2225 | return 0; |
| 2226 | } |
| 2227 | |
| 2228 | static ssize_t debugfs_timings_read(struct file *file, char __user *buf, |
| 2229 | size_t len, loff_t *ppos) |
| 2230 | { |
| 2231 | struct debugfs_timings_state *p = file->private_data; |
| 2232 | struct kvm_vcpu *vcpu = p->vcpu; |
| 2233 | char *s, *buf_end; |
| 2234 | struct kvmhv_tb_accumulator tb; |
| 2235 | u64 count; |
| 2236 | loff_t pos; |
| 2237 | ssize_t n; |
| 2238 | int i, loops; |
| 2239 | bool ok; |
| 2240 | |
| 2241 | if (!p->buflen) { |
| 2242 | s = p->buf; |
| 2243 | buf_end = s + sizeof(p->buf); |
| 2244 | for (i = 0; i < N_TIMINGS; ++i) { |
| 2245 | struct kvmhv_tb_accumulator *acc; |
| 2246 | |
| 2247 | acc = (struct kvmhv_tb_accumulator *) |
| 2248 | ((unsigned long)vcpu + timings[i].offset); |
| 2249 | ok = false; |
| 2250 | for (loops = 0; loops < 1000; ++loops) { |
| 2251 | count = acc->seqcount; |
| 2252 | if (!(count & 1)) { |
| 2253 | smp_rmb(); |
| 2254 | tb = *acc; |
| 2255 | smp_rmb(); |
| 2256 | if (count == acc->seqcount) { |
| 2257 | ok = true; |
| 2258 | break; |
| 2259 | } |
| 2260 | } |
| 2261 | udelay(1); |
| 2262 | } |
| 2263 | if (!ok) |
| 2264 | snprintf(s, buf_end - s, "%s: stuck\n", |
| 2265 | timings[i].name); |
| 2266 | else |
| 2267 | snprintf(s, buf_end - s, |
| 2268 | "%s: %llu %llu %llu %llu\n", |
| 2269 | timings[i].name, count / 2, |
| 2270 | tb_to_ns(tb.tb_total), |
| 2271 | tb_to_ns(tb.tb_min), |
| 2272 | tb_to_ns(tb.tb_max)); |
| 2273 | s += strlen(s); |
| 2274 | } |
| 2275 | p->buflen = s - p->buf; |
| 2276 | } |
| 2277 | |
| 2278 | pos = *ppos; |
| 2279 | if (pos >= p->buflen) |
| 2280 | return 0; |
| 2281 | if (len > p->buflen - pos) |
| 2282 | len = p->buflen - pos; |
| 2283 | n = copy_to_user(buf, p->buf + pos, len); |
| 2284 | if (n) { |
| 2285 | if (n == len) |
| 2286 | return -EFAULT; |
| 2287 | len -= n; |
| 2288 | } |
| 2289 | *ppos = pos + len; |
| 2290 | return len; |
| 2291 | } |
| 2292 | |
| 2293 | static ssize_t debugfs_timings_write(struct file *file, const char __user *buf, |
| 2294 | size_t len, loff_t *ppos) |
| 2295 | { |
| 2296 | return -EACCES; |
| 2297 | } |
| 2298 | |
| 2299 | static const struct file_operations debugfs_timings_ops = { |
| 2300 | .owner = THIS_MODULE, |
| 2301 | .open = debugfs_timings_open, |
| 2302 | .release = debugfs_timings_release, |
| 2303 | .read = debugfs_timings_read, |
| 2304 | .write = debugfs_timings_write, |
| 2305 | .llseek = generic_file_llseek, |
| 2306 | }; |
| 2307 | |
| 2308 | /* Create a debugfs directory for the vcpu */ |
| 2309 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) |
| 2310 | { |
| 2311 | char buf[16]; |
| 2312 | struct kvm *kvm = vcpu->kvm; |
| 2313 | |
| 2314 | snprintf(buf, sizeof(buf), "vcpu%u", id); |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2315 | vcpu->arch.debugfs_dir = debugfs_create_dir(buf, kvm->arch.debugfs_dir); |
Greg Kroah-Hartman | c4fd527 | 2020-02-09 11:58:57 +0100 | [diff] [blame] | 2316 | debugfs_create_file("timings", 0444, vcpu->arch.debugfs_dir, vcpu, |
| 2317 | &debugfs_timings_ops); |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2318 | } |
| 2319 | |
| 2320 | #else /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ |
| 2321 | static void debugfs_vcpu_init(struct kvm_vcpu *vcpu, unsigned int id) |
| 2322 | { |
| 2323 | } |
| 2324 | #endif /* CONFIG_KVM_BOOK3S_HV_EXIT_TIMING */ |
| 2325 | |
Sean Christopherson | ff030fd | 2019-12-18 13:55:00 -0800 | [diff] [blame] | 2326 | static int kvmppc_core_vcpu_create_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2327 | { |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2328 | int err; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2329 | int core; |
| 2330 | struct kvmppc_vcore *vcore; |
Sean Christopherson | ff030fd | 2019-12-18 13:55:00 -0800 | [diff] [blame] | 2331 | struct kvm *kvm; |
| 2332 | unsigned int id; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2333 | |
Sean Christopherson | ff030fd | 2019-12-18 13:55:00 -0800 | [diff] [blame] | 2334 | kvm = vcpu->kvm; |
| 2335 | id = vcpu->vcpu_id; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2336 | |
| 2337 | vcpu->arch.shared = &vcpu->arch.shregs; |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 2338 | #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE |
| 2339 | /* |
| 2340 | * The shared struct is never shared on HV, |
| 2341 | * so we can always use host endianness |
| 2342 | */ |
| 2343 | #ifdef __BIG_ENDIAN__ |
| 2344 | vcpu->arch.shared_big_endian = true; |
| 2345 | #else |
| 2346 | vcpu->arch.shared_big_endian = false; |
| 2347 | #endif |
| 2348 | #endif |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2349 | vcpu->arch.mmcr[0] = MMCR0_FC; |
| 2350 | vcpu->arch.ctrl = CTRL_RUNLATCH; |
| 2351 | /* default to host PVR, since we can't spoof it */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2352 | kvmppc_set_pvr_hv(vcpu, mfspr(SPRN_PVR)); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 2353 | spin_lock_init(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2354 | spin_lock_init(&vcpu->arch.tbacct_lock); |
| 2355 | vcpu->arch.busy_preempt = TB_NIL; |
Anton Blanchard | d682916 | 2014-01-08 21:25:30 +1100 | [diff] [blame] | 2356 | vcpu->arch.intr_msr = MSR_SF | MSR_ME; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2357 | |
Paul Mackerras | 769377f | 2017-02-15 14:30:17 +1100 | [diff] [blame] | 2358 | /* |
| 2359 | * Set the default HFSCR for the guest from the host value. |
| 2360 | * This value is only used on POWER9. |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 2361 | * On POWER9, we want to virtualize the doorbell facility, so we |
Paul Mackerras | f3c99f9 | 2018-10-08 16:31:12 +1100 | [diff] [blame] | 2362 | * don't set the HFSCR_MSGP bit, and that causes those instructions |
| 2363 | * to trap and then we emulate them. |
Paul Mackerras | 769377f | 2017-02-15 14:30:17 +1100 | [diff] [blame] | 2364 | */ |
Paul Mackerras | f3c99f9 | 2018-10-08 16:31:12 +1100 | [diff] [blame] | 2365 | vcpu->arch.hfscr = HFSCR_TAR | HFSCR_EBB | HFSCR_PM | HFSCR_BHRB | |
Alistair Popple | 4cb4ade | 2020-06-02 15:53:25 +1000 | [diff] [blame] | 2366 | HFSCR_DSCR | HFSCR_VECVSX | HFSCR_FP | HFSCR_PREFIX; |
Paul Mackerras | f3c99f9 | 2018-10-08 16:31:12 +1100 | [diff] [blame] | 2367 | if (cpu_has_feature(CPU_FTR_HVMODE)) { |
| 2368 | vcpu->arch.hfscr &= mfspr(SPRN_HFSCR); |
| 2369 | if (cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) |
| 2370 | vcpu->arch.hfscr |= HFSCR_TM; |
| 2371 | } |
| 2372 | if (cpu_has_feature(CPU_FTR_TM_COMP)) |
Paul Mackerras | 4bb3c7a | 2018-03-21 21:32:01 +1100 | [diff] [blame] | 2373 | vcpu->arch.hfscr |= HFSCR_TM; |
Paul Mackerras | 769377f | 2017-02-15 14:30:17 +1100 | [diff] [blame] | 2374 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2375 | kvmppc_mmu_book3s_hv_init(vcpu); |
| 2376 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 2377 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2378 | |
| 2379 | init_waitqueue_head(&vcpu->arch.cpu_run); |
| 2380 | |
| 2381 | mutex_lock(&kvm->lock); |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2382 | vcore = NULL; |
| 2383 | err = -EINVAL; |
Sam Bobroff | 1e175d2 | 2018-07-25 16:12:02 +1000 | [diff] [blame] | 2384 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
Paul Mackerras | b5c6f76 | 2018-07-26 15:38:41 +1000 | [diff] [blame] | 2385 | if (id >= (KVM_MAX_VCPUS * kvm->arch.emul_smt_mode)) { |
| 2386 | pr_devel("KVM: VCPU ID too high\n"); |
| 2387 | core = KVM_MAX_VCORES; |
| 2388 | } else { |
| 2389 | BUG_ON(kvm->arch.smt_mode != 1); |
| 2390 | core = kvmppc_pack_vcpu_id(kvm, id); |
| 2391 | } |
Sam Bobroff | 1e175d2 | 2018-07-25 16:12:02 +1000 | [diff] [blame] | 2392 | } else { |
| 2393 | core = id / kvm->arch.smt_mode; |
| 2394 | } |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2395 | if (core < KVM_MAX_VCORES) { |
| 2396 | vcore = kvm->arch.vcores[core]; |
Sam Bobroff | 1e175d2 | 2018-07-25 16:12:02 +1000 | [diff] [blame] | 2397 | if (vcore && cpu_has_feature(CPU_FTR_ARCH_300)) { |
| 2398 | pr_devel("KVM: collision on id %u", id); |
| 2399 | vcore = NULL; |
| 2400 | } else if (!vcore) { |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 2401 | /* |
| 2402 | * Take mmu_setup_lock for mutual exclusion |
| 2403 | * with kvmppc_update_lpcr(). |
| 2404 | */ |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2405 | err = -ENOMEM; |
Sam Bobroff | 1e175d2 | 2018-07-25 16:12:02 +1000 | [diff] [blame] | 2406 | vcore = kvmppc_vcore_create(kvm, |
| 2407 | id & ~(kvm->arch.smt_mode - 1)); |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 2408 | mutex_lock(&kvm->arch.mmu_setup_lock); |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2409 | kvm->arch.vcores[core] = vcore; |
| 2410 | kvm->arch.online_vcores++; |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 2411 | mutex_unlock(&kvm->arch.mmu_setup_lock); |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2412 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2413 | } |
| 2414 | mutex_unlock(&kvm->lock); |
| 2415 | |
| 2416 | if (!vcore) |
Sean Christopherson | ff030fd | 2019-12-18 13:55:00 -0800 | [diff] [blame] | 2417 | return err; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2418 | |
| 2419 | spin_lock(&vcore->lock); |
| 2420 | ++vcore->num_threads; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2421 | spin_unlock(&vcore->lock); |
| 2422 | vcpu->arch.vcore = vcore; |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 2423 | vcpu->arch.ptid = vcpu->vcpu_id - vcore->first_vcpuid; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2424 | vcpu->arch.thread_cpu = -1; |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 2425 | vcpu->arch.prev_cpu = -1; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2426 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 2427 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
| 2428 | kvmppc_sanity_check(vcpu); |
| 2429 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2430 | debugfs_vcpu_init(vcpu, id); |
| 2431 | |
Sean Christopherson | c50bfbd | 2019-12-18 13:54:57 -0800 | [diff] [blame] | 2432 | return 0; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2435 | static int kvmhv_set_smt_mode(struct kvm *kvm, unsigned long smt_mode, |
| 2436 | unsigned long flags) |
| 2437 | { |
| 2438 | int err; |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 2439 | int esmt = 0; |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2440 | |
| 2441 | if (flags) |
| 2442 | return -EINVAL; |
| 2443 | if (smt_mode > MAX_SMT_THREADS || !is_power_of_2(smt_mode)) |
| 2444 | return -EINVAL; |
| 2445 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) { |
| 2446 | /* |
| 2447 | * On POWER8 (or POWER7), the threading mode is "strict", |
| 2448 | * so we pack smt_mode vcpus per vcore. |
| 2449 | */ |
| 2450 | if (smt_mode > threads_per_subcore) |
| 2451 | return -EINVAL; |
| 2452 | } else { |
| 2453 | /* |
| 2454 | * On POWER9, the threading mode is "loose", |
| 2455 | * so each vcpu gets its own vcore. |
| 2456 | */ |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 2457 | esmt = smt_mode; |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2458 | smt_mode = 1; |
| 2459 | } |
| 2460 | mutex_lock(&kvm->lock); |
| 2461 | err = -EBUSY; |
| 2462 | if (!kvm->arch.online_vcores) { |
| 2463 | kvm->arch.smt_mode = smt_mode; |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 2464 | kvm->arch.emul_smt_mode = esmt; |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 2465 | err = 0; |
| 2466 | } |
| 2467 | mutex_unlock(&kvm->lock); |
| 2468 | |
| 2469 | return err; |
| 2470 | } |
| 2471 | |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 2472 | static void unpin_vpa(struct kvm *kvm, struct kvmppc_vpa *vpa) |
| 2473 | { |
| 2474 | if (vpa->pinned_addr) |
| 2475 | kvmppc_unpin_guest_page(kvm, vpa->pinned_addr, vpa->gpa, |
| 2476 | vpa->dirty); |
| 2477 | } |
| 2478 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2479 | static void kvmppc_core_vcpu_free_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2480 | { |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 2481 | spin_lock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 2482 | unpin_vpa(vcpu->kvm, &vcpu->arch.dtl); |
| 2483 | unpin_vpa(vcpu->kvm, &vcpu->arch.slb_shadow); |
| 2484 | unpin_vpa(vcpu->kvm, &vcpu->arch.vpa); |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 2485 | spin_unlock(&vcpu->arch.vpa_update_lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2488 | static int kvmppc_core_check_requests_hv(struct kvm_vcpu *vcpu) |
| 2489 | { |
| 2490 | /* Indicate we want to get back into the guest */ |
| 2491 | return 1; |
| 2492 | } |
| 2493 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2494 | static void kvmppc_set_timer(struct kvm_vcpu *vcpu) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2495 | { |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2496 | unsigned long dec_nsec, now; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2497 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2498 | now = get_tb(); |
| 2499 | if (now > vcpu->arch.dec_expires) { |
| 2500 | /* decrementer has already gone negative */ |
| 2501 | kvmppc_core_queue_dec(vcpu); |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 2502 | kvmppc_core_prepare_to_enter(vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2503 | return; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2504 | } |
Paul Mackerras | c43befc | 2018-10-20 20:54:55 +1100 | [diff] [blame] | 2505 | dec_nsec = tb_to_ns(vcpu->arch.dec_expires - now); |
Thomas Gleixner | 8b0e195 | 2016-12-25 12:30:41 +0100 | [diff] [blame] | 2506 | hrtimer_start(&vcpu->arch.dec_timer, dec_nsec, HRTIMER_MODE_REL); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2507 | vcpu->arch.timer_running = 1; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2510 | extern int __kvmppc_vcore_entry(void); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2511 | |
| 2512 | static void kvmppc_remove_runnable(struct kvmppc_vcore *vc, |
| 2513 | struct kvm_vcpu *vcpu) |
| 2514 | { |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2515 | u64 now; |
| 2516 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2517 | if (vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 2518 | return; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 2519 | spin_lock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 2520 | now = mftb(); |
| 2521 | vcpu->arch.busy_stolen += vcore_stolen_time(vc, now) - |
| 2522 | vcpu->arch.stolen_logged; |
| 2523 | vcpu->arch.busy_preempt = now; |
| 2524 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
Paul Mackerras | bf3d32e | 2013-11-16 17:46:04 +1100 | [diff] [blame] | 2525 | spin_unlock_irq(&vcpu->arch.tbacct_lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2526 | --vc->n_runnable; |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 2527 | WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], NULL); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 2530 | static int kvmppc_grab_hwthread(int cpu) |
| 2531 | { |
| 2532 | struct paca_struct *tpaca; |
Paul Mackerras | b754c73 | 2014-09-02 16:14:42 +1000 | [diff] [blame] | 2533 | long timeout = 10000; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 2534 | |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 2535 | tpaca = paca_ptrs[cpu]; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 2536 | |
| 2537 | /* Ensure the thread won't go into the kernel if it wakes */ |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 2538 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2539 | tpaca->kvm_hstate.kvm_vcore = NULL; |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2540 | tpaca->kvm_hstate.napping = 0; |
| 2541 | smp_wmb(); |
| 2542 | tpaca->kvm_hstate.hwthread_req = 1; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 2543 | |
| 2544 | /* |
| 2545 | * If the thread is already executing in the kernel (e.g. handling |
| 2546 | * a stray interrupt), wait for it to get back to nap mode. |
| 2547 | * The smp_mb() is to ensure that our setting of hwthread_req |
| 2548 | * is visible before we look at hwthread_state, so if this |
| 2549 | * races with the code at system_reset_pSeries and the thread |
| 2550 | * misses our setting of hwthread_req, we are sure to see its |
| 2551 | * setting of hwthread_state, and vice versa. |
| 2552 | */ |
| 2553 | smp_mb(); |
| 2554 | while (tpaca->kvm_hstate.hwthread_state == KVM_HWTHREAD_IN_KERNEL) { |
| 2555 | if (--timeout <= 0) { |
| 2556 | pr_err("KVM: couldn't grab cpu %d\n", cpu); |
| 2557 | return -EBUSY; |
| 2558 | } |
| 2559 | udelay(1); |
| 2560 | } |
| 2561 | return 0; |
| 2562 | } |
| 2563 | |
| 2564 | static void kvmppc_release_hwthread(int cpu) |
| 2565 | { |
| 2566 | struct paca_struct *tpaca; |
| 2567 | |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 2568 | tpaca = paca_ptrs[cpu]; |
Paul Mackerras | 31a4d44 | 2017-10-19 15:14:20 +1100 | [diff] [blame] | 2569 | tpaca->kvm_hstate.hwthread_req = 0; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 2570 | tpaca->kvm_hstate.kvm_vcpu = NULL; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2571 | tpaca->kvm_hstate.kvm_vcore = NULL; |
| 2572 | tpaca->kvm_hstate.kvm_split_mode = NULL; |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 2575 | static void radix_flush_cpu(struct kvm *kvm, int cpu, struct kvm_vcpu *vcpu) |
| 2576 | { |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 2577 | struct kvm_nested_guest *nested = vcpu->arch.nested; |
| 2578 | cpumask_t *cpu_in_guest; |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 2579 | int i; |
| 2580 | |
| 2581 | cpu = cpu_first_thread_sibling(cpu); |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 2582 | if (nested) { |
| 2583 | cpumask_set_cpu(cpu, &nested->need_tlb_flush); |
| 2584 | cpu_in_guest = &nested->cpu_in_guest; |
| 2585 | } else { |
| 2586 | cpumask_set_cpu(cpu, &kvm->arch.need_tlb_flush); |
| 2587 | cpu_in_guest = &kvm->arch.cpu_in_guest; |
| 2588 | } |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 2589 | /* |
| 2590 | * Make sure setting of bit in need_tlb_flush precedes |
| 2591 | * testing of cpu_in_guest bits. The matching barrier on |
| 2592 | * the other side is the first smp_mb() in kvmppc_run_core(). |
| 2593 | */ |
| 2594 | smp_mb(); |
| 2595 | for (i = 0; i < threads_per_core; ++i) |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 2596 | if (cpumask_test_cpu(cpu + i, cpu_in_guest)) |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 2597 | smp_call_function_single(cpu + i, do_nothing, NULL, 1); |
| 2598 | } |
| 2599 | |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2600 | static void kvmppc_prepare_radix_vcpu(struct kvm_vcpu *vcpu, int pcpu) |
| 2601 | { |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 2602 | struct kvm_nested_guest *nested = vcpu->arch.nested; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2603 | struct kvm *kvm = vcpu->kvm; |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 2604 | int prev_cpu; |
| 2605 | |
| 2606 | if (!cpu_has_feature(CPU_FTR_HVMODE)) |
| 2607 | return; |
| 2608 | |
| 2609 | if (nested) |
| 2610 | prev_cpu = nested->prev_cpu[vcpu->arch.nested_vcpu_id]; |
| 2611 | else |
| 2612 | prev_cpu = vcpu->arch.prev_cpu; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2613 | |
| 2614 | /* |
| 2615 | * With radix, the guest can do TLB invalidations itself, |
| 2616 | * and it could choose to use the local form (tlbiel) if |
| 2617 | * it is invalidating a translation that has only ever been |
| 2618 | * used on one vcpu. However, that doesn't mean it has |
| 2619 | * only ever been used on one physical cpu, since vcpus |
| 2620 | * can move around between pcpus. To cope with this, when |
| 2621 | * a vcpu moves from one pcpu to another, we need to tell |
| 2622 | * any vcpus running on the same core as this vcpu previously |
| 2623 | * ran to flush the TLB. The TLB is shared between threads, |
| 2624 | * so we use a single bit in .need_tlb_flush for all 4 threads. |
| 2625 | */ |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 2626 | if (prev_cpu != pcpu) { |
| 2627 | if (prev_cpu >= 0 && |
| 2628 | cpu_first_thread_sibling(prev_cpu) != |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2629 | cpu_first_thread_sibling(pcpu)) |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 2630 | radix_flush_cpu(kvm, prev_cpu, vcpu); |
| 2631 | if (nested) |
| 2632 | nested->prev_cpu[vcpu->arch.nested_vcpu_id] = pcpu; |
| 2633 | else |
| 2634 | vcpu->arch.prev_cpu = pcpu; |
| 2635 | } |
| 2636 | } |
| 2637 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2638 | 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] | 2639 | { |
| 2640 | int cpu; |
| 2641 | struct paca_struct *tpaca; |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 2642 | struct kvm *kvm = vc->kvm; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2643 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2644 | cpu = vc->pcpu; |
| 2645 | if (vcpu) { |
| 2646 | if (vcpu->arch.timer_running) { |
| 2647 | hrtimer_try_to_cancel(&vcpu->arch.dec_timer); |
| 2648 | vcpu->arch.timer_running = 0; |
| 2649 | } |
| 2650 | cpu += vcpu->arch.ptid; |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 2651 | vcpu->cpu = vc->pcpu; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2652 | vcpu->arch.thread_cpu = cpu; |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 2653 | cpumask_set_cpu(cpu, &kvm->arch.cpu_in_guest); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2654 | } |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 2655 | tpaca = paca_ptrs[cpu]; |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2656 | tpaca->kvm_hstate.kvm_vcpu = vcpu; |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 2657 | tpaca->kvm_hstate.ptid = cpu - vc->pcpu; |
Paul Mackerras | 4bb3c7a | 2018-03-21 21:32:01 +1100 | [diff] [blame] | 2658 | tpaca->kvm_hstate.fake_suspend = 0; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2659 | /* Order stores to hstate.kvm_vcpu etc. before store to kvm_vcore */ |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2660 | smp_wmb(); |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 2661 | tpaca->kvm_hstate.kvm_vcore = vc; |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2662 | if (cpu != smp_processor_id()) |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 2663 | kvmppc_ipi_thread(cpu); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2664 | } |
| 2665 | |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 2666 | static void kvmppc_wait_for_nap(int n_threads) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2667 | { |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2668 | int cpu = smp_processor_id(); |
| 2669 | int i, loops; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2670 | |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 2671 | if (n_threads <= 1) |
| 2672 | return; |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2673 | for (loops = 0; loops < 1000000; ++loops) { |
| 2674 | /* |
| 2675 | * Check if all threads are finished. |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2676 | * We set the vcore pointer when starting a thread |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2677 | * and the thread clears it when finished, so we look |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2678 | * for any threads that still have a non-NULL vcore ptr. |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2679 | */ |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 2680 | for (i = 1; i < n_threads; ++i) |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 2681 | if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore) |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2682 | break; |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 2683 | if (i == n_threads) { |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2684 | HMT_medium(); |
| 2685 | return; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2686 | } |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2687 | HMT_low(); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2688 | } |
| 2689 | HMT_medium(); |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 2690 | for (i = 1; i < n_threads; ++i) |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 2691 | if (paca_ptrs[cpu + i]->kvm_hstate.kvm_vcore) |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 2692 | pr_err("KVM: CPU %d seems to be stuck\n", cpu + i); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * 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] | 2697 | * this core are off-line. Then grab the threads so they can't |
| 2698 | * enter the kernel. |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2699 | */ |
| 2700 | static int on_primary_thread(void) |
| 2701 | { |
| 2702 | int cpu = smp_processor_id(); |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 2703 | int thr; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2704 | |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 2705 | /* Are we on a primary subcore? */ |
| 2706 | if (cpu_thread_in_subcore(cpu)) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2707 | return 0; |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 2708 | |
| 2709 | thr = 0; |
| 2710 | while (++thr < threads_per_subcore) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2711 | if (cpu_online(cpu + thr)) |
| 2712 | return 0; |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 2713 | |
| 2714 | /* Grab all hw threads so they can't go into the kernel */ |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 2715 | for (thr = 1; thr < threads_per_subcore; ++thr) { |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 2716 | if (kvmppc_grab_hwthread(cpu + thr)) { |
| 2717 | /* Couldn't grab one; let the others go */ |
| 2718 | do { |
| 2719 | kvmppc_release_hwthread(cpu + thr); |
| 2720 | } while (--thr > 0); |
| 2721 | return 0; |
| 2722 | } |
| 2723 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 2724 | return 1; |
| 2725 | } |
| 2726 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2727 | /* |
| 2728 | * A list of virtual cores for each physical CPU. |
| 2729 | * These are vcores that could run but their runner VCPU tasks are |
| 2730 | * (or may be) preempted. |
| 2731 | */ |
| 2732 | struct preempted_vcore_list { |
| 2733 | struct list_head list; |
| 2734 | spinlock_t lock; |
| 2735 | }; |
| 2736 | |
| 2737 | static DEFINE_PER_CPU(struct preempted_vcore_list, preempted_vcores); |
| 2738 | |
| 2739 | static void init_vcore_lists(void) |
| 2740 | { |
| 2741 | int cpu; |
| 2742 | |
| 2743 | for_each_possible_cpu(cpu) { |
| 2744 | struct preempted_vcore_list *lp = &per_cpu(preempted_vcores, cpu); |
| 2745 | spin_lock_init(&lp->lock); |
| 2746 | INIT_LIST_HEAD(&lp->list); |
| 2747 | } |
| 2748 | } |
| 2749 | |
| 2750 | static void kvmppc_vcore_preempt(struct kvmppc_vcore *vc) |
| 2751 | { |
| 2752 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); |
| 2753 | |
| 2754 | vc->vcore_state = VCORE_PREEMPT; |
| 2755 | vc->pcpu = smp_processor_id(); |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 2756 | if (vc->num_threads < threads_per_vcore(vc->kvm)) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2757 | spin_lock(&lp->lock); |
| 2758 | list_add_tail(&vc->preempt_list, &lp->list); |
| 2759 | spin_unlock(&lp->lock); |
| 2760 | } |
| 2761 | |
| 2762 | /* Start accumulating stolen time */ |
| 2763 | kvmppc_core_start_stolen(vc); |
| 2764 | } |
| 2765 | |
| 2766 | static void kvmppc_vcore_end_preempt(struct kvmppc_vcore *vc) |
| 2767 | { |
Paul Mackerras | 402813f | 2015-07-16 17:11:13 +1000 | [diff] [blame] | 2768 | struct preempted_vcore_list *lp; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2769 | |
| 2770 | kvmppc_core_end_stolen(vc); |
| 2771 | if (!list_empty(&vc->preempt_list)) { |
Paul Mackerras | 402813f | 2015-07-16 17:11:13 +1000 | [diff] [blame] | 2772 | lp = &per_cpu(preempted_vcores, vc->pcpu); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2773 | spin_lock(&lp->lock); |
| 2774 | list_del_init(&vc->preempt_list); |
| 2775 | spin_unlock(&lp->lock); |
| 2776 | } |
| 2777 | vc->vcore_state = VCORE_INACTIVE; |
| 2778 | } |
| 2779 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2780 | /* |
| 2781 | * This stores information about the virtual cores currently |
| 2782 | * assigned to a physical core. |
| 2783 | */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2784 | struct core_info { |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2785 | int n_subcores; |
| 2786 | int max_subcore_threads; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2787 | int total_threads; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2788 | int subcore_threads[MAX_SUBCORES]; |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 2789 | struct kvmppc_vcore *vc[MAX_SUBCORES]; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2790 | }; |
| 2791 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2792 | /* |
| 2793 | * 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] | 2794 | * respectively in 2-way micro-threading (split-core) mode on POWER8. |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2795 | */ |
| 2796 | static int subcore_thread_map[MAX_SUBCORES] = { 0, 4, 2, 6 }; |
| 2797 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2798 | static void init_core_info(struct core_info *cip, struct kvmppc_vcore *vc) |
| 2799 | { |
| 2800 | memset(cip, 0, sizeof(*cip)); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2801 | cip->n_subcores = 1; |
| 2802 | cip->max_subcore_threads = vc->num_threads; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2803 | cip->total_threads = vc->num_threads; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2804 | cip->subcore_threads[0] = vc->num_threads; |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 2805 | cip->vc[0] = vc; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2806 | } |
| 2807 | |
| 2808 | static bool subcore_config_ok(int n_subcores, int n_threads) |
| 2809 | { |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 2810 | /* |
Paul Mackerras | 00608e1 | 2018-01-11 16:54:26 +1100 | [diff] [blame] | 2811 | * POWER9 "SMT4" cores are permanently in what is effectively a 4-way |
| 2812 | * split-core mode, with one thread per subcore. |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 2813 | */ |
| 2814 | if (cpu_has_feature(CPU_FTR_ARCH_300)) |
| 2815 | return n_subcores <= 4 && n_threads == 1; |
| 2816 | |
| 2817 | /* On POWER8, can only dynamically split if unsplit to begin with */ |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2818 | if (n_subcores > 1 && threads_per_subcore < MAX_SMT_THREADS) |
| 2819 | return false; |
| 2820 | if (n_subcores > MAX_SUBCORES) |
| 2821 | return false; |
| 2822 | if (n_subcores > 1) { |
| 2823 | if (!(dynamic_mt_modes & 2)) |
| 2824 | n_subcores = 4; |
| 2825 | if (n_subcores > 2 && !(dynamic_mt_modes & 4)) |
| 2826 | return false; |
| 2827 | } |
| 2828 | |
| 2829 | return n_subcores * roundup_pow_of_two(n_threads) <= MAX_SMT_THREADS; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2830 | } |
| 2831 | |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 2832 | static void init_vcore_to_run(struct kvmppc_vcore *vc) |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2833 | { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2834 | vc->entry_exit_map = 0; |
| 2835 | vc->in_guest = 0; |
| 2836 | vc->napping_threads = 0; |
| 2837 | vc->conferring_threads = 0; |
Paul Mackerras | 57b8daa | 2018-04-20 22:51:11 +1000 | [diff] [blame] | 2838 | vc->tb_offset_applied = 0; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2839 | } |
| 2840 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2841 | static bool can_dynamic_split(struct kvmppc_vcore *vc, struct core_info *cip) |
| 2842 | { |
| 2843 | int n_threads = vc->num_threads; |
| 2844 | int sub; |
| 2845 | |
| 2846 | if (!cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 2847 | return false; |
| 2848 | |
Paul Mackerras | aa22786 | 2018-09-12 10:42:12 +1000 | [diff] [blame] | 2849 | /* In one_vm_per_core mode, require all vcores to be from the same vm */ |
| 2850 | if (one_vm_per_core && vc->kvm != cip->vc[0]->kvm) |
| 2851 | return false; |
| 2852 | |
Paul Mackerras | 00608e1 | 2018-01-11 16:54:26 +1100 | [diff] [blame] | 2853 | /* Some POWER9 chips require all threads to be in the same MMU mode */ |
| 2854 | if (no_mixing_hpt_and_radix && |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 2855 | kvm_is_radix(vc->kvm) != kvm_is_radix(cip->vc[0]->kvm)) |
| 2856 | return false; |
| 2857 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2858 | if (n_threads < cip->max_subcore_threads) |
| 2859 | n_threads = cip->max_subcore_threads; |
Paul Mackerras | b009031 | 2016-09-15 16:27:41 +1000 | [diff] [blame] | 2860 | if (!subcore_config_ok(cip->n_subcores + 1, n_threads)) |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2861 | return false; |
Paul Mackerras | b009031 | 2016-09-15 16:27:41 +1000 | [diff] [blame] | 2862 | cip->max_subcore_threads = n_threads; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2863 | |
| 2864 | sub = cip->n_subcores; |
| 2865 | ++cip->n_subcores; |
| 2866 | cip->total_threads += vc->num_threads; |
| 2867 | cip->subcore_threads[sub] = vc->num_threads; |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 2868 | cip->vc[sub] = vc; |
| 2869 | init_vcore_to_run(vc); |
| 2870 | list_del_init(&vc->preempt_list); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2871 | |
| 2872 | return true; |
| 2873 | } |
| 2874 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2875 | /* |
| 2876 | * Work out whether it is possible to piggyback the execution of |
| 2877 | * vcore *pvc onto the execution of the other vcores described in *cip. |
| 2878 | */ |
| 2879 | static bool can_piggyback(struct kvmppc_vcore *pvc, struct core_info *cip, |
| 2880 | int target_threads) |
| 2881 | { |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2882 | if (cip->total_threads + pvc->num_threads > target_threads) |
| 2883 | return false; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2884 | |
Paul Mackerras | b009031 | 2016-09-15 16:27:41 +1000 | [diff] [blame] | 2885 | return can_dynamic_split(pvc, cip); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 2886 | } |
| 2887 | |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 2888 | static void prepare_threads(struct kvmppc_vcore *vc) |
| 2889 | { |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 2890 | int i; |
| 2891 | struct kvm_vcpu *vcpu; |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 2892 | |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 2893 | for_each_runnable_thread(i, vcpu, vc) { |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 2894 | if (signal_pending(vcpu->arch.run_task)) |
| 2895 | vcpu->arch.ret = -EINTR; |
| 2896 | else if (vcpu->arch.vpa.update_pending || |
| 2897 | vcpu->arch.slb_shadow.update_pending || |
| 2898 | vcpu->arch.dtl.update_pending) |
| 2899 | vcpu->arch.ret = RESUME_GUEST; |
| 2900 | else |
| 2901 | continue; |
| 2902 | kvmppc_remove_runnable(vc, vcpu); |
| 2903 | wake_up(&vcpu->arch.cpu_run); |
| 2904 | } |
| 2905 | } |
| 2906 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2907 | static void collect_piggybacks(struct core_info *cip, int target_threads) |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2908 | { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2909 | struct preempted_vcore_list *lp = this_cpu_ptr(&preempted_vcores); |
| 2910 | struct kvmppc_vcore *pvc, *vcnext; |
| 2911 | |
| 2912 | spin_lock(&lp->lock); |
| 2913 | list_for_each_entry_safe(pvc, vcnext, &lp->list, preempt_list) { |
| 2914 | if (!spin_trylock(&pvc->lock)) |
| 2915 | continue; |
| 2916 | prepare_threads(pvc); |
Paul Mackerras | d28eafc | 2019-08-27 11:31:37 +1000 | [diff] [blame] | 2917 | if (!pvc->n_runnable || !pvc->kvm->arch.mmu_ready) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2918 | list_del_init(&pvc->preempt_list); |
| 2919 | if (pvc->runner == NULL) { |
| 2920 | pvc->vcore_state = VCORE_INACTIVE; |
| 2921 | kvmppc_core_end_stolen(pvc); |
| 2922 | } |
| 2923 | spin_unlock(&pvc->lock); |
| 2924 | continue; |
| 2925 | } |
| 2926 | if (!can_piggyback(pvc, cip, target_threads)) { |
| 2927 | spin_unlock(&pvc->lock); |
| 2928 | continue; |
| 2929 | } |
| 2930 | kvmppc_core_end_stolen(pvc); |
| 2931 | pvc->vcore_state = VCORE_PIGGYBACK; |
| 2932 | if (cip->total_threads >= target_threads) |
| 2933 | break; |
| 2934 | } |
| 2935 | spin_unlock(&lp->lock); |
| 2936 | } |
| 2937 | |
Paul Mackerras | d28eafc | 2019-08-27 11:31:37 +1000 | [diff] [blame] | 2938 | static bool recheck_signals_and_mmu(struct core_info *cip) |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2939 | { |
| 2940 | int sub, i; |
| 2941 | struct kvm_vcpu *vcpu; |
Paul Mackerras | d28eafc | 2019-08-27 11:31:37 +1000 | [diff] [blame] | 2942 | struct kvmppc_vcore *vc; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2943 | |
Paul Mackerras | d28eafc | 2019-08-27 11:31:37 +1000 | [diff] [blame] | 2944 | for (sub = 0; sub < cip->n_subcores; ++sub) { |
| 2945 | vc = cip->vc[sub]; |
| 2946 | if (!vc->kvm->arch.mmu_ready) |
| 2947 | return true; |
| 2948 | for_each_runnable_thread(i, vcpu, vc) |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2949 | if (signal_pending(vcpu->arch.run_task)) |
| 2950 | return true; |
Paul Mackerras | d28eafc | 2019-08-27 11:31:37 +1000 | [diff] [blame] | 2951 | } |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 2952 | return false; |
| 2953 | } |
| 2954 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2955 | static void post_guest_process(struct kvmppc_vcore *vc, bool is_master) |
| 2956 | { |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 2957 | int still_running = 0, i; |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2958 | u64 now; |
| 2959 | long ret; |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 2960 | struct kvm_vcpu *vcpu; |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2961 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2962 | spin_lock(&vc->lock); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2963 | now = get_tb(); |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 2964 | for_each_runnable_thread(i, vcpu, vc) { |
Paul Mackerras | 53655dd | 2018-10-08 16:30:54 +1100 | [diff] [blame] | 2965 | /* |
| 2966 | * It's safe to unlock the vcore in the loop here, because |
| 2967 | * for_each_runnable_thread() is safe against removal of |
| 2968 | * the vcpu, and the vcore state is VCORE_EXITING here, |
| 2969 | * so any vcpus becoming runnable will have their arch.trap |
| 2970 | * set to zero and can't actually run in the guest. |
| 2971 | */ |
| 2972 | spin_unlock(&vc->lock); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2973 | /* cancel pending dec exception if dec is positive */ |
| 2974 | if (now < vcpu->arch.dec_expires && |
| 2975 | kvmppc_core_pending_dec(vcpu)) |
| 2976 | kvmppc_core_dequeue_dec(vcpu); |
| 2977 | |
| 2978 | trace_kvm_guest_exit(vcpu); |
| 2979 | |
| 2980 | ret = RESUME_GUEST; |
| 2981 | if (vcpu->arch.trap) |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 2982 | ret = kvmppc_handle_exit_hv(vcpu, |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2983 | vcpu->arch.run_task); |
| 2984 | |
| 2985 | vcpu->arch.ret = ret; |
| 2986 | vcpu->arch.trap = 0; |
| 2987 | |
Paul Mackerras | 53655dd | 2018-10-08 16:30:54 +1100 | [diff] [blame] | 2988 | spin_lock(&vc->lock); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2989 | if (is_kvmppc_resume_guest(vcpu->arch.ret)) { |
| 2990 | if (vcpu->arch.pending_exceptions) |
| 2991 | kvmppc_core_prepare_to_enter(vcpu); |
| 2992 | if (vcpu->arch.ceded) |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2993 | kvmppc_set_timer(vcpu); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 2994 | else |
| 2995 | ++still_running; |
| 2996 | } else { |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 2997 | kvmppc_remove_runnable(vc, vcpu); |
| 2998 | wake_up(&vcpu->arch.cpu_run); |
| 2999 | } |
| 3000 | } |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3001 | if (!is_master) { |
Paul Mackerras | 563a1e9 | 2015-07-16 17:11:14 +1000 | [diff] [blame] | 3002 | if (still_running > 0) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3003 | kvmppc_vcore_preempt(vc); |
Paul Mackerras | 563a1e9 | 2015-07-16 17:11:14 +1000 | [diff] [blame] | 3004 | } else if (vc->runner) { |
| 3005 | vc->vcore_state = VCORE_PREEMPT; |
| 3006 | kvmppc_core_start_stolen(vc); |
| 3007 | } else { |
| 3008 | vc->vcore_state = VCORE_INACTIVE; |
| 3009 | } |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3010 | if (vc->n_runnable > 0 && vc->runner == NULL) { |
| 3011 | /* make sure there's a candidate runner awake */ |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 3012 | i = -1; |
| 3013 | vcpu = next_runnable_thread(vc, &i); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3014 | wake_up(&vcpu->arch.cpu_run); |
| 3015 | } |
| 3016 | } |
| 3017 | spin_unlock(&vc->lock); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 3018 | } |
| 3019 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3020 | /* |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3021 | * Clear core from the list of active host cores as we are about to |
| 3022 | * enter the guest. Only do this if it is the primary thread of the |
| 3023 | * core (not if a subcore) that is entering the guest. |
| 3024 | */ |
Anna-Maria Gleixner | 3f7cd91 | 2016-11-27 00:13:45 +0100 | [diff] [blame] | 3025 | static inline int kvmppc_clear_host_core(unsigned int cpu) |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3026 | { |
| 3027 | int core; |
| 3028 | |
| 3029 | if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu)) |
Anna-Maria Gleixner | 3f7cd91 | 2016-11-27 00:13:45 +0100 | [diff] [blame] | 3030 | return 0; |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3031 | /* |
| 3032 | * Memory barrier can be omitted here as we will do a smp_wmb() |
| 3033 | * later in kvmppc_start_thread and we need ensure that state is |
| 3034 | * visible to other CPUs only after we enter guest. |
| 3035 | */ |
| 3036 | core = cpu >> threads_shift; |
| 3037 | 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] | 3038 | return 0; |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3039 | } |
| 3040 | |
| 3041 | /* |
| 3042 | * Advertise this core as an active host core since we exited the guest |
| 3043 | * Only need to do this if it is the primary thread of the core that is |
| 3044 | * exiting. |
| 3045 | */ |
Anna-Maria Gleixner | 3f7cd91 | 2016-11-27 00:13:45 +0100 | [diff] [blame] | 3046 | static inline int kvmppc_set_host_core(unsigned int cpu) |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3047 | { |
| 3048 | int core; |
| 3049 | |
| 3050 | if (!kvmppc_host_rm_ops_hv || cpu_thread_in_core(cpu)) |
Anna-Maria Gleixner | 3f7cd91 | 2016-11-27 00:13:45 +0100 | [diff] [blame] | 3051 | return 0; |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3052 | |
| 3053 | /* |
| 3054 | * Memory barrier can be omitted here because we do a spin_unlock |
| 3055 | * immediately after this which provides the memory barrier. |
| 3056 | */ |
| 3057 | core = cpu >> threads_shift; |
| 3058 | 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] | 3059 | return 0; |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3060 | } |
| 3061 | |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3062 | static void set_irq_happened(int trap) |
| 3063 | { |
| 3064 | switch (trap) { |
| 3065 | case BOOK3S_INTERRUPT_EXTERNAL: |
| 3066 | local_paca->irq_happened |= PACA_IRQ_EE; |
| 3067 | break; |
| 3068 | case BOOK3S_INTERRUPT_H_DOORBELL: |
| 3069 | local_paca->irq_happened |= PACA_IRQ_DBELL; |
| 3070 | break; |
| 3071 | case BOOK3S_INTERRUPT_HMI: |
| 3072 | local_paca->irq_happened |= PACA_IRQ_HMI; |
| 3073 | break; |
Nicholas Piggin | 6de6638 | 2017-11-05 23:33:55 +1100 | [diff] [blame] | 3074 | case BOOK3S_INTERRUPT_SYSTEM_RESET: |
| 3075 | replay_system_reset(); |
| 3076 | break; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3077 | } |
| 3078 | } |
| 3079 | |
Suresh Warrier | b8e6a87 | 2015-12-17 14:59:07 -0600 | [diff] [blame] | 3080 | /* |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3081 | * Run a set of guest threads on a physical core. |
| 3082 | * Called with vc->lock held. |
| 3083 | */ |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 3084 | static noinline void kvmppc_run_core(struct kvmppc_vcore *vc) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3085 | { |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 3086 | struct kvm_vcpu *vcpu; |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 3087 | int i; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 3088 | int srcu_idx; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3089 | struct core_info core_info; |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 3090 | struct kvmppc_vcore *pvc; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3091 | struct kvm_split_mode split_info, *sip; |
| 3092 | int split, subcore_size, active; |
| 3093 | int sub; |
| 3094 | bool thr0_done; |
| 3095 | unsigned long cmd_bit, stat_bit; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3096 | int pcpu, thr; |
| 3097 | int target_threads; |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 3098 | int controlled_threads; |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3099 | int trap; |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3100 | bool is_power8; |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3101 | bool hpt_on_radix; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 3102 | |
| 3103 | /* |
Paul Mackerras | d911f0b | 2015-03-28 14:21:03 +1100 | [diff] [blame] | 3104 | * Remove from the list any threads that have a signal pending |
| 3105 | * or need a VPA update done |
| 3106 | */ |
| 3107 | prepare_threads(vc); |
| 3108 | |
| 3109 | /* if the runner is no longer runnable, let the caller pick a new one */ |
| 3110 | if (vc->runner->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 3111 | return; |
| 3112 | |
| 3113 | /* |
| 3114 | * Initialize *vc. |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 3115 | */ |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 3116 | init_vcore_to_run(vc); |
Paul Mackerras | 2711e24 | 2014-12-04 16:43:28 +1100 | [diff] [blame] | 3117 | vc->preempt_tb = TB_NIL; |
Paul Mackerras | 081f323 | 2012-06-01 20:20:24 +1000 | [diff] [blame] | 3118 | |
| 3119 | /* |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 3120 | * Number of threads that we will be controlling: the same as |
| 3121 | * the number of threads per subcore, except on POWER9, |
| 3122 | * where it's 1 because the threads are (mostly) independent. |
| 3123 | */ |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3124 | controlled_threads = threads_per_vcore(vc->kvm); |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 3125 | |
| 3126 | /* |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 3127 | * Make sure we are running on primary threads, and that secondary |
| 3128 | * threads are offline. Also check if the number of threads in this |
| 3129 | * guest are greater than the current system threads per guest. |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3130 | * On POWER9, we need to be not in independent-threads mode if |
Paul Mackerras | 00608e1 | 2018-01-11 16:54:26 +1100 | [diff] [blame] | 3131 | * this is a HPT guest on a radix host machine where the |
| 3132 | * CPU threads may not be in different MMU modes. |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 3133 | */ |
Paul Mackerras | 00608e1 | 2018-01-11 16:54:26 +1100 | [diff] [blame] | 3134 | hpt_on_radix = no_mixing_hpt_and_radix && radix_enabled() && |
| 3135 | !kvm_is_radix(vc->kvm); |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3136 | if (((controlled_threads > 1) && |
| 3137 | ((vc->num_threads > threads_per_subcore) || !on_primary_thread())) || |
| 3138 | (hpt_on_radix && vc->kvm->arch.threads_indep)) { |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 3139 | for_each_runnable_thread(i, vcpu, vc) { |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 3140 | vcpu->arch.ret = -EBUSY; |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 3141 | kvmppc_remove_runnable(vc, vcpu); |
| 3142 | wake_up(&vcpu->arch.cpu_run); |
| 3143 | } |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 3144 | goto out; |
| 3145 | } |
| 3146 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3147 | /* |
| 3148 | * See if we could run any other vcores on the physical core |
| 3149 | * along with this one. |
| 3150 | */ |
| 3151 | init_core_info(&core_info, vc); |
| 3152 | pcpu = smp_processor_id(); |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 3153 | target_threads = controlled_threads; |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3154 | if (target_smt_mode && target_smt_mode < target_threads) |
| 3155 | target_threads = target_smt_mode; |
| 3156 | if (vc->num_threads < target_threads) |
| 3157 | collect_piggybacks(&core_info, target_threads); |
Michael Ellerman | 3102f78 | 2014-05-23 18:15:29 +1000 | [diff] [blame] | 3158 | |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3159 | /* |
| 3160 | * On radix, arrange for TLB flushing if necessary. |
| 3161 | * This has to be done before disabling interrupts since |
| 3162 | * it uses smp_call_function(). |
| 3163 | */ |
| 3164 | pcpu = smp_processor_id(); |
| 3165 | if (kvm_is_radix(vc->kvm)) { |
| 3166 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
| 3167 | for_each_runnable_thread(i, vcpu, core_info.vc[sub]) |
| 3168 | kvmppc_prepare_radix_vcpu(vcpu, pcpu); |
| 3169 | } |
| 3170 | |
| 3171 | /* |
| 3172 | * Hard-disable interrupts, and check resched flag and signals. |
| 3173 | * If we need to reschedule or deliver a signal, clean up |
| 3174 | * and return without going into the guest(s). |
Paul Mackerras | 072df81 | 2017-11-09 14:30:24 +1100 | [diff] [blame] | 3175 | * If the mmu_ready flag has been cleared, don't go into the |
Paul Mackerras | 38c53af | 2017-11-08 14:44:04 +1100 | [diff] [blame] | 3176 | * guest because that means a HPT resize operation is in progress. |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3177 | */ |
| 3178 | local_irq_disable(); |
| 3179 | hard_irq_disable(); |
| 3180 | if (lazy_irq_pending() || need_resched() || |
Paul Mackerras | d28eafc | 2019-08-27 11:31:37 +1000 | [diff] [blame] | 3181 | recheck_signals_and_mmu(&core_info)) { |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3182 | local_irq_enable(); |
| 3183 | vc->vcore_state = VCORE_INACTIVE; |
| 3184 | /* Unlock all except the primary vcore */ |
| 3185 | for (sub = 1; sub < core_info.n_subcores; ++sub) { |
| 3186 | pvc = core_info.vc[sub]; |
| 3187 | /* Put back on to the preempted vcores list */ |
| 3188 | kvmppc_vcore_preempt(pvc); |
| 3189 | spin_unlock(&pvc->lock); |
| 3190 | } |
| 3191 | for (i = 0; i < controlled_threads; ++i) |
| 3192 | kvmppc_release_hwthread(pcpu + i); |
| 3193 | return; |
| 3194 | } |
| 3195 | |
| 3196 | kvmppc_clear_host_core(pcpu); |
| 3197 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3198 | /* Decide on micro-threading (split-core) mode */ |
| 3199 | subcore_size = threads_per_subcore; |
| 3200 | cmd_bit = stat_bit = 0; |
| 3201 | split = core_info.n_subcores; |
| 3202 | sip = NULL; |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3203 | is_power8 = cpu_has_feature(CPU_FTR_ARCH_207S) |
| 3204 | && !cpu_has_feature(CPU_FTR_ARCH_300); |
| 3205 | |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3206 | if (split > 1 || hpt_on_radix) { |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3207 | sip = &split_info; |
| 3208 | memset(&split_info, 0, sizeof(split_info)); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3209 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 3210 | split_info.vc[sub] = core_info.vc[sub]; |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3211 | |
| 3212 | if (is_power8) { |
| 3213 | if (split == 2 && (dynamic_mt_modes & 2)) { |
| 3214 | cmd_bit = HID0_POWER8_1TO2LPAR; |
| 3215 | stat_bit = HID0_POWER8_2LPARMODE; |
| 3216 | } else { |
| 3217 | split = 4; |
| 3218 | cmd_bit = HID0_POWER8_1TO4LPAR; |
| 3219 | stat_bit = HID0_POWER8_4LPARMODE; |
| 3220 | } |
| 3221 | subcore_size = MAX_SMT_THREADS / split; |
| 3222 | split_info.rpr = mfspr(SPRN_RPR); |
| 3223 | split_info.pmmar = mfspr(SPRN_PMMAR); |
| 3224 | split_info.ldbar = mfspr(SPRN_LDBAR); |
| 3225 | split_info.subcore_size = subcore_size; |
| 3226 | } else { |
| 3227 | split_info.subcore_size = 1; |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3228 | if (hpt_on_radix) { |
| 3229 | /* Use the split_info for LPCR/LPIDR changes */ |
| 3230 | split_info.lpcr_req = vc->lpcr; |
| 3231 | split_info.lpidr_req = vc->kvm->arch.lpid; |
| 3232 | split_info.host_lpcr = vc->kvm->arch.host_lpcr; |
| 3233 | split_info.do_set = 1; |
| 3234 | } |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3235 | } |
| 3236 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3237 | /* order writes to split_info before kvm_split_mode pointer */ |
| 3238 | smp_wmb(); |
| 3239 | } |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3240 | |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3241 | for (thr = 0; thr < controlled_threads; ++thr) { |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 3242 | struct paca_struct *paca = paca_ptrs[pcpu + thr]; |
| 3243 | |
| 3244 | paca->kvm_hstate.tid = thr; |
| 3245 | paca->kvm_hstate.napping = 0; |
| 3246 | paca->kvm_hstate.kvm_split_mode = sip; |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3247 | } |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3248 | |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3249 | /* Initiate micro-threading (split-core) on POWER8 if required */ |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3250 | if (cmd_bit) { |
| 3251 | unsigned long hid0 = mfspr(SPRN_HID0); |
| 3252 | |
| 3253 | hid0 |= cmd_bit | HID0_POWER8_DYNLPARDIS; |
| 3254 | mb(); |
| 3255 | mtspr(SPRN_HID0, hid0); |
| 3256 | isync(); |
| 3257 | for (;;) { |
| 3258 | hid0 = mfspr(SPRN_HID0); |
| 3259 | if (hid0 & stat_bit) |
| 3260 | break; |
| 3261 | cpu_relax(); |
| 3262 | } |
Paul Mackerras | 2e25aa5 | 2012-02-19 17:46:32 +0000 | [diff] [blame] | 3263 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3264 | |
Paul Mackerras | 7aa1584 | 2018-04-20 19:53:22 +1000 | [diff] [blame] | 3265 | /* |
| 3266 | * On POWER8, set RWMR register. |
| 3267 | * Since it only affects PURR and SPURR, it doesn't affect |
| 3268 | * the host, so we don't save/restore the host value. |
| 3269 | */ |
| 3270 | if (is_power8) { |
| 3271 | unsigned long rwmr_val = RWMR_RPA_P8_8THREAD; |
| 3272 | int n_online = atomic_read(&vc->online_count); |
| 3273 | |
| 3274 | /* |
| 3275 | * Use the 8-thread value if we're doing split-core |
| 3276 | * or if the vcore's online count looks bogus. |
| 3277 | */ |
| 3278 | if (split == 1 && threads_per_subcore == MAX_SMT_THREADS && |
| 3279 | n_online >= 1 && n_online <= MAX_SMT_THREADS) |
| 3280 | rwmr_val = p8_rwmr_values[n_online]; |
| 3281 | mtspr(SPRN_RWMR, rwmr_val); |
| 3282 | } |
| 3283 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3284 | /* Start all the threads */ |
| 3285 | active = 0; |
| 3286 | for (sub = 0; sub < core_info.n_subcores; ++sub) { |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3287 | thr = is_power8 ? subcore_thread_map[sub] : sub; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3288 | thr0_done = false; |
| 3289 | active |= 1 << thr; |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 3290 | pvc = core_info.vc[sub]; |
| 3291 | pvc->pcpu = pcpu + thr; |
| 3292 | for_each_runnable_thread(i, vcpu, pvc) { |
| 3293 | kvmppc_start_thread(vcpu, pvc); |
| 3294 | kvmppc_create_dtl_entry(vcpu, pvc); |
| 3295 | trace_kvm_guest_enter(vcpu); |
| 3296 | if (!vcpu->arch.ptid) |
| 3297 | thr0_done = true; |
| 3298 | active |= 1 << (thr + vcpu->arch.ptid); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3299 | } |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 3300 | /* |
| 3301 | * We need to start the first thread of each subcore |
| 3302 | * even if it doesn't have a vcpu. |
| 3303 | */ |
| 3304 | if (!thr0_done) |
| 3305 | kvmppc_start_thread(NULL, pvc); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3306 | } |
Gautham R. Shenoy | 7f23532 | 2015-09-02 21:48:58 +0530 | [diff] [blame] | 3307 | |
| 3308 | /* |
| 3309 | * Ensure that split_info.do_nap is set after setting |
| 3310 | * the vcore pointer in the PACA of the secondaries. |
| 3311 | */ |
| 3312 | smp_mb(); |
Gautham R. Shenoy | 7f23532 | 2015-09-02 21:48:58 +0530 | [diff] [blame] | 3313 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3314 | /* |
| 3315 | * When doing micro-threading, poke the inactive threads as well. |
| 3316 | * This gets them to the nap instruction after kvm_do_nap, |
| 3317 | * which reduces the time taken to unsplit later. |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3318 | * For POWER9 HPT guest on radix host, we need all the secondary |
| 3319 | * threads woken up so they can do the LPCR/LPIDR change. |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3320 | */ |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3321 | if (cmd_bit || hpt_on_radix) { |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3322 | split_info.do_nap = 1; /* ask secondaries to nap when done */ |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3323 | for (thr = 1; thr < threads_per_subcore; ++thr) |
| 3324 | if (!(active & (1 << thr))) |
| 3325 | kvmppc_ipi_thread(pcpu + thr); |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3326 | } |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 3327 | |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 3328 | vc->vcore_state = VCORE_RUNNING; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3329 | preempt_disable(); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 3330 | |
| 3331 | trace_kvmppc_run_core(vc, 0); |
| 3332 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3333 | for (sub = 0; sub < core_info.n_subcores; ++sub) |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 3334 | spin_unlock(&core_info.vc[sub]->lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3335 | |
Laurent Vivier | 61bd0f66 | 2018-03-02 11:51:56 +0100 | [diff] [blame] | 3336 | guest_enter_irqoff(); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 3337 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 3338 | srcu_idx = srcu_read_lock(&vc->kvm->srcu); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 3339 | |
Naveen N. Rao | a4bc64d | 2018-04-19 12:34:05 +0530 | [diff] [blame] | 3340 | this_cpu_disable_ftrace(); |
| 3341 | |
Alexey Kardashevskiy | 3309bec | 2019-03-29 16:40:13 +1100 | [diff] [blame] | 3342 | /* |
| 3343 | * Interrupts will be enabled once we get into the guest, |
| 3344 | * so tell lockdep that we're about to enable interrupts. |
| 3345 | */ |
| 3346 | trace_hardirqs_on(); |
| 3347 | |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3348 | trap = __kvmppc_vcore_entry(); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3349 | |
Alexey Kardashevskiy | 3309bec | 2019-03-29 16:40:13 +1100 | [diff] [blame] | 3350 | trace_hardirqs_off(); |
| 3351 | |
Naveen N. Rao | a4bc64d | 2018-04-19 12:34:05 +0530 | [diff] [blame] | 3352 | this_cpu_enable_ftrace(); |
| 3353 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3354 | srcu_read_unlock(&vc->kvm->srcu, srcu_idx); |
| 3355 | |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3356 | set_irq_happened(trap); |
| 3357 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3358 | spin_lock(&vc->lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3359 | /* prevent other vcpu threads from doing kvmppc_start_thread() now */ |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3360 | vc->vcore_state = VCORE_EXITING; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3361 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3362 | /* wait for secondary threads to finish writing their state to memory */ |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3363 | kvmppc_wait_for_nap(controlled_threads); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3364 | |
| 3365 | /* Return to whole-core mode if we split the core earlier */ |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 3366 | if (cmd_bit) { |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3367 | unsigned long hid0 = mfspr(SPRN_HID0); |
| 3368 | unsigned long loops = 0; |
| 3369 | |
| 3370 | hid0 &= ~HID0_POWER8_DYNLPARDIS; |
| 3371 | stat_bit = HID0_POWER8_2LPARMODE | HID0_POWER8_4LPARMODE; |
| 3372 | mb(); |
| 3373 | mtspr(SPRN_HID0, hid0); |
| 3374 | isync(); |
| 3375 | for (;;) { |
| 3376 | hid0 = mfspr(SPRN_HID0); |
| 3377 | if (!(hid0 & stat_bit)) |
| 3378 | break; |
| 3379 | cpu_relax(); |
| 3380 | ++loops; |
| 3381 | } |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3382 | } else if (hpt_on_radix) { |
| 3383 | /* Wait for all threads to have seen final sync */ |
| 3384 | for (thr = 1; thr < controlled_threads; ++thr) { |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 3385 | struct paca_struct *paca = paca_ptrs[pcpu + thr]; |
| 3386 | |
| 3387 | while (paca->kvm_hstate.kvm_split_mode) { |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3388 | HMT_low(); |
| 3389 | barrier(); |
| 3390 | } |
| 3391 | HMT_medium(); |
| 3392 | } |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3393 | } |
Paul Mackerras | c010150 | 2017-10-19 14:11:23 +1100 | [diff] [blame] | 3394 | split_info.do_nap = 0; |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3395 | |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3396 | kvmppc_set_host_core(pcpu); |
| 3397 | |
| 3398 | local_irq_enable(); |
Laurent Vivier | 61bd0f66 | 2018-03-02 11:51:56 +0100 | [diff] [blame] | 3399 | guest_exit(); |
Paul Mackerras | 8b24e69 | 2017-06-26 15:45:51 +1000 | [diff] [blame] | 3400 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3401 | /* Let secondaries go back to the offline loop */ |
Paul Mackerras | 45c940b | 2016-11-18 17:43:30 +1100 | [diff] [blame] | 3402 | for (i = 0; i < controlled_threads; ++i) { |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3403 | kvmppc_release_hwthread(pcpu + i); |
| 3404 | if (sip && sip->napped[i]) |
| 3405 | kvmppc_ipi_thread(pcpu + i); |
Paul Mackerras | a29ebea | 2017-01-30 21:21:50 +1100 | [diff] [blame] | 3406 | cpumask_clear_cpu(pcpu + i, &vc->kvm->arch.cpu_in_guest); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3407 | } |
| 3408 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3409 | spin_unlock(&vc->lock); |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 3410 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3411 | /* make sure updates to secondary vcpu structs are visible now */ |
| 3412 | smp_mb(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3413 | |
Paul Mackerras | 36ee41d | 2018-01-30 10:51:32 +1100 | [diff] [blame] | 3414 | preempt_enable(); |
| 3415 | |
Paul Mackerras | 898b25b | 2017-06-22 15:08:42 +1000 | [diff] [blame] | 3416 | for (sub = 0; sub < core_info.n_subcores; ++sub) { |
| 3417 | pvc = core_info.vc[sub]; |
| 3418 | post_guest_process(pvc, pvc == vc); |
| 3419 | } |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3420 | |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 3421 | spin_lock(&vc->lock); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3422 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 3423 | out: |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3424 | vc->vcore_state = VCORE_INACTIVE; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 3425 | trace_kvmppc_run_core(vc, 1); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3428 | /* |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3429 | * Load up hypervisor-mode registers on P9. |
| 3430 | */ |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3431 | static int kvmhv_load_hv_regs_and_go(struct kvm_vcpu *vcpu, u64 time_limit, |
| 3432 | unsigned long lpcr) |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3433 | { |
| 3434 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 3435 | s64 hdec; |
| 3436 | u64 tb, purr, spurr; |
| 3437 | int trap; |
| 3438 | unsigned long host_hfscr = mfspr(SPRN_HFSCR); |
| 3439 | unsigned long host_ciabr = mfspr(SPRN_CIABR); |
Ravi Bangoria | 09f82b0 | 2020-05-14 16:47:26 +0530 | [diff] [blame] | 3440 | unsigned long host_dawr = mfspr(SPRN_DAWR0); |
| 3441 | unsigned long host_dawrx = mfspr(SPRN_DAWRX0); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3442 | unsigned long host_psscr = mfspr(SPRN_PSSCR); |
| 3443 | unsigned long host_pidr = mfspr(SPRN_PID); |
| 3444 | |
| 3445 | hdec = time_limit - mftb(); |
| 3446 | if (hdec < 0) |
| 3447 | return BOOK3S_INTERRUPT_HV_DECREMENTER; |
| 3448 | mtspr(SPRN_HDEC, hdec); |
| 3449 | |
| 3450 | if (vc->tb_offset) { |
| 3451 | u64 new_tb = mftb() + vc->tb_offset; |
| 3452 | mtspr(SPRN_TBU40, new_tb); |
| 3453 | tb = mftb(); |
| 3454 | if ((tb & 0xffffff) < (new_tb & 0xffffff)) |
| 3455 | mtspr(SPRN_TBU40, new_tb + 0x1000000); |
| 3456 | vc->tb_offset_applied = vc->tb_offset; |
| 3457 | } |
| 3458 | |
| 3459 | if (vc->pcr) |
Jordan Niethe | 13c7bb3 | 2019-09-17 10:46:05 +1000 | [diff] [blame] | 3460 | mtspr(SPRN_PCR, vc->pcr | PCR_MASK); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3461 | mtspr(SPRN_DPDES, vc->dpdes); |
| 3462 | mtspr(SPRN_VTB, vc->vtb); |
| 3463 | |
| 3464 | local_paca->kvm_hstate.host_purr = mfspr(SPRN_PURR); |
| 3465 | local_paca->kvm_hstate.host_spurr = mfspr(SPRN_SPURR); |
| 3466 | mtspr(SPRN_PURR, vcpu->arch.purr); |
| 3467 | mtspr(SPRN_SPURR, vcpu->arch.spurr); |
| 3468 | |
Michael Neuling | c1fe190 | 2019-04-01 17:03:12 +1100 | [diff] [blame] | 3469 | if (dawr_enabled()) { |
Ravi Bangoria | 09f82b0 | 2020-05-14 16:47:26 +0530 | [diff] [blame] | 3470 | mtspr(SPRN_DAWR0, vcpu->arch.dawr); |
| 3471 | mtspr(SPRN_DAWRX0, vcpu->arch.dawrx); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3472 | } |
| 3473 | mtspr(SPRN_CIABR, vcpu->arch.ciabr); |
| 3474 | mtspr(SPRN_IC, vcpu->arch.ic); |
| 3475 | mtspr(SPRN_PID, vcpu->arch.pid); |
| 3476 | |
| 3477 | mtspr(SPRN_PSSCR, vcpu->arch.psscr | PSSCR_EC | |
| 3478 | (local_paca->kvm_hstate.fake_suspend << PSSCR_FAKE_SUSPEND_LG)); |
| 3479 | |
| 3480 | mtspr(SPRN_HFSCR, vcpu->arch.hfscr); |
| 3481 | |
| 3482 | mtspr(SPRN_SPRG0, vcpu->arch.shregs.sprg0); |
| 3483 | mtspr(SPRN_SPRG1, vcpu->arch.shregs.sprg1); |
| 3484 | mtspr(SPRN_SPRG2, vcpu->arch.shregs.sprg2); |
| 3485 | mtspr(SPRN_SPRG3, vcpu->arch.shregs.sprg3); |
| 3486 | |
| 3487 | mtspr(SPRN_AMOR, ~0UL); |
| 3488 | |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3489 | mtspr(SPRN_LPCR, lpcr); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3490 | isync(); |
| 3491 | |
| 3492 | kvmppc_xive_push_vcpu(vcpu); |
| 3493 | |
| 3494 | mtspr(SPRN_SRR0, vcpu->arch.shregs.srr0); |
| 3495 | mtspr(SPRN_SRR1, vcpu->arch.shregs.srr1); |
| 3496 | |
| 3497 | trap = __kvmhv_vcpu_entry_p9(vcpu); |
| 3498 | |
| 3499 | /* Advance host PURR/SPURR by the amount used by guest */ |
| 3500 | purr = mfspr(SPRN_PURR); |
| 3501 | spurr = mfspr(SPRN_SPURR); |
| 3502 | mtspr(SPRN_PURR, local_paca->kvm_hstate.host_purr + |
| 3503 | purr - vcpu->arch.purr); |
| 3504 | mtspr(SPRN_SPURR, local_paca->kvm_hstate.host_spurr + |
| 3505 | spurr - vcpu->arch.spurr); |
| 3506 | vcpu->arch.purr = purr; |
| 3507 | vcpu->arch.spurr = spurr; |
| 3508 | |
| 3509 | vcpu->arch.ic = mfspr(SPRN_IC); |
| 3510 | vcpu->arch.pid = mfspr(SPRN_PID); |
| 3511 | vcpu->arch.psscr = mfspr(SPRN_PSSCR) & PSSCR_GUEST_VIS; |
| 3512 | |
| 3513 | vcpu->arch.shregs.sprg0 = mfspr(SPRN_SPRG0); |
| 3514 | vcpu->arch.shregs.sprg1 = mfspr(SPRN_SPRG1); |
| 3515 | vcpu->arch.shregs.sprg2 = mfspr(SPRN_SPRG2); |
| 3516 | vcpu->arch.shregs.sprg3 = mfspr(SPRN_SPRG3); |
| 3517 | |
Suraj Jitindar Singh | 7cb9eb1 | 2019-03-18 13:59:46 +1100 | [diff] [blame] | 3518 | /* Preserve PSSCR[FAKE_SUSPEND] until we've called kvmppc_save_tm_hv */ |
| 3519 | mtspr(SPRN_PSSCR, host_psscr | |
| 3520 | (local_paca->kvm_hstate.fake_suspend << PSSCR_FAKE_SUSPEND_LG)); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3521 | mtspr(SPRN_HFSCR, host_hfscr); |
| 3522 | mtspr(SPRN_CIABR, host_ciabr); |
Ravi Bangoria | 09f82b0 | 2020-05-14 16:47:26 +0530 | [diff] [blame] | 3523 | mtspr(SPRN_DAWR0, host_dawr); |
| 3524 | mtspr(SPRN_DAWRX0, host_dawrx); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3525 | mtspr(SPRN_PID, host_pidr); |
| 3526 | |
| 3527 | /* |
| 3528 | * Since this is radix, do a eieio; tlbsync; ptesync sequence in |
| 3529 | * case we interrupted the guest between a tlbie and a ptesync. |
| 3530 | */ |
| 3531 | asm volatile("eieio; tlbsync; ptesync"); |
| 3532 | |
| 3533 | mtspr(SPRN_LPID, vcpu->kvm->arch.host_lpid); /* restore host LPID */ |
| 3534 | isync(); |
| 3535 | |
| 3536 | vc->dpdes = mfspr(SPRN_DPDES); |
| 3537 | vc->vtb = mfspr(SPRN_VTB); |
| 3538 | mtspr(SPRN_DPDES, 0); |
| 3539 | if (vc->pcr) |
Jordan Niethe | 13c7bb3 | 2019-09-17 10:46:05 +1000 | [diff] [blame] | 3540 | mtspr(SPRN_PCR, PCR_MASK); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3541 | |
| 3542 | if (vc->tb_offset_applied) { |
| 3543 | u64 new_tb = mftb() - vc->tb_offset_applied; |
| 3544 | mtspr(SPRN_TBU40, new_tb); |
| 3545 | tb = mftb(); |
| 3546 | if ((tb & 0xffffff) < (new_tb & 0xffffff)) |
| 3547 | mtspr(SPRN_TBU40, new_tb + 0x1000000); |
| 3548 | vc->tb_offset_applied = 0; |
| 3549 | } |
| 3550 | |
| 3551 | mtspr(SPRN_HDEC, 0x7fffffff); |
| 3552 | mtspr(SPRN_LPCR, vcpu->kvm->arch.host_lpcr); |
| 3553 | |
| 3554 | return trap; |
| 3555 | } |
| 3556 | |
| 3557 | /* |
| 3558 | * Virtual-mode guest entry for POWER9 and later when the host and |
| 3559 | * guest are both using the radix MMU. The LPIDR has already been set. |
| 3560 | */ |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3561 | int kvmhv_p9_guest_entry(struct kvm_vcpu *vcpu, u64 time_limit, |
| 3562 | unsigned long lpcr) |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3563 | { |
| 3564 | struct kvmppc_vcore *vc = vcpu->arch.vcore; |
| 3565 | unsigned long host_dscr = mfspr(SPRN_DSCR); |
| 3566 | unsigned long host_tidr = mfspr(SPRN_TIDR); |
| 3567 | unsigned long host_iamr = mfspr(SPRN_IAMR); |
Michael Ellerman | d976f68 | 2019-02-20 19:55:00 +1100 | [diff] [blame] | 3568 | unsigned long host_amr = mfspr(SPRN_AMR); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3569 | s64 dec; |
| 3570 | u64 tb; |
| 3571 | int trap, save_pmu; |
| 3572 | |
| 3573 | dec = mfspr(SPRN_DEC); |
| 3574 | tb = mftb(); |
| 3575 | if (dec < 512) |
| 3576 | return BOOK3S_INTERRUPT_HV_DECREMENTER; |
| 3577 | local_paca->kvm_hstate.dec_expires = dec + tb; |
| 3578 | if (local_paca->kvm_hstate.dec_expires < time_limit) |
| 3579 | time_limit = local_paca->kvm_hstate.dec_expires; |
| 3580 | |
| 3581 | vcpu->arch.ceded = 0; |
| 3582 | |
| 3583 | kvmhv_save_host_pmu(); /* saves it to PACA kvm_hstate */ |
| 3584 | |
| 3585 | kvmppc_subcore_enter_guest(); |
| 3586 | |
| 3587 | vc->entry_exit_map = 1; |
| 3588 | vc->in_guest = 1; |
| 3589 | |
| 3590 | if (vcpu->arch.vpa.pinned_addr) { |
| 3591 | struct lppaca *lp = vcpu->arch.vpa.pinned_addr; |
| 3592 | u32 yield_count = be32_to_cpu(lp->yield_count) + 1; |
| 3593 | lp->yield_count = cpu_to_be32(yield_count); |
| 3594 | vcpu->arch.vpa.dirty = 1; |
| 3595 | } |
| 3596 | |
| 3597 | if (cpu_has_feature(CPU_FTR_TM) || |
| 3598 | cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) |
| 3599 | kvmppc_restore_tm_hv(vcpu, vcpu->arch.shregs.msr, true); |
| 3600 | |
| 3601 | kvmhv_load_guest_pmu(vcpu); |
| 3602 | |
| 3603 | msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX); |
| 3604 | load_fp_state(&vcpu->arch.fp); |
| 3605 | #ifdef CONFIG_ALTIVEC |
| 3606 | load_vr_state(&vcpu->arch.vr); |
| 3607 | #endif |
Suraj Jitindar Singh | 44b198a | 2019-04-30 10:41:23 +1000 | [diff] [blame] | 3608 | mtspr(SPRN_VRSAVE, vcpu->arch.vrsave); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3609 | |
| 3610 | mtspr(SPRN_DSCR, vcpu->arch.dscr); |
| 3611 | mtspr(SPRN_IAMR, vcpu->arch.iamr); |
| 3612 | mtspr(SPRN_PSPB, vcpu->arch.pspb); |
| 3613 | mtspr(SPRN_FSCR, vcpu->arch.fscr); |
| 3614 | mtspr(SPRN_TAR, vcpu->arch.tar); |
| 3615 | mtspr(SPRN_EBBHR, vcpu->arch.ebbhr); |
| 3616 | mtspr(SPRN_EBBRR, vcpu->arch.ebbrr); |
| 3617 | mtspr(SPRN_BESCR, vcpu->arch.bescr); |
| 3618 | mtspr(SPRN_WORT, vcpu->arch.wort); |
| 3619 | mtspr(SPRN_TIDR, vcpu->arch.tid); |
| 3620 | mtspr(SPRN_DAR, vcpu->arch.shregs.dar); |
| 3621 | mtspr(SPRN_DSISR, vcpu->arch.shregs.dsisr); |
| 3622 | mtspr(SPRN_AMR, vcpu->arch.amr); |
| 3623 | mtspr(SPRN_UAMOR, vcpu->arch.uamor); |
| 3624 | |
| 3625 | if (!(vcpu->arch.ctrl & 1)) |
| 3626 | mtspr(SPRN_CTRLT, mfspr(SPRN_CTRLF) & ~1); |
| 3627 | |
| 3628 | mtspr(SPRN_DEC, vcpu->arch.dec_expires - mftb()); |
| 3629 | |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3630 | if (kvmhv_on_pseries()) { |
Suraj Jitindar Singh | c8b4083 | 2019-07-03 11:20:22 +1000 | [diff] [blame] | 3631 | /* |
| 3632 | * We need to save and restore the guest visible part of the |
| 3633 | * psscr (i.e. using SPRN_PSSCR_PR) since the hypervisor |
| 3634 | * doesn't do this for us. Note only required if pseries since |
| 3635 | * this is done in kvmhv_load_hv_regs_and_go() below otherwise. |
| 3636 | */ |
| 3637 | unsigned long host_psscr; |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3638 | /* call our hypervisor to load up HV regs and go */ |
| 3639 | struct hv_guest_state hvregs; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3640 | |
Suraj Jitindar Singh | c8b4083 | 2019-07-03 11:20:22 +1000 | [diff] [blame] | 3641 | host_psscr = mfspr(SPRN_PSSCR_PR); |
| 3642 | mtspr(SPRN_PSSCR_PR, vcpu->arch.psscr); |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3643 | kvmhv_save_hv_regs(vcpu, &hvregs); |
| 3644 | hvregs.lpcr = lpcr; |
| 3645 | vcpu->arch.regs.msr = vcpu->arch.shregs.msr; |
| 3646 | hvregs.version = HV_GUEST_STATE_VERSION; |
| 3647 | if (vcpu->arch.nested) { |
| 3648 | hvregs.lpid = vcpu->arch.nested->shadow_lpid; |
| 3649 | hvregs.vcpu_token = vcpu->arch.nested_vcpu_id; |
| 3650 | } else { |
| 3651 | hvregs.lpid = vcpu->kvm->arch.lpid; |
| 3652 | hvregs.vcpu_token = vcpu->vcpu_id; |
| 3653 | } |
| 3654 | hvregs.hdec_expiry = time_limit; |
| 3655 | trap = plpar_hcall_norets(H_ENTER_NESTED, __pa(&hvregs), |
| 3656 | __pa(&vcpu->arch.regs)); |
| 3657 | kvmhv_restore_hv_return_state(vcpu, &hvregs); |
| 3658 | vcpu->arch.shregs.msr = vcpu->arch.regs.msr; |
| 3659 | vcpu->arch.shregs.dar = mfspr(SPRN_DAR); |
| 3660 | vcpu->arch.shregs.dsisr = mfspr(SPRN_DSISR); |
Suraj Jitindar Singh | c8b4083 | 2019-07-03 11:20:22 +1000 | [diff] [blame] | 3661 | vcpu->arch.psscr = mfspr(SPRN_PSSCR_PR); |
| 3662 | mtspr(SPRN_PSSCR_PR, host_psscr); |
Paul Mackerras | 4bad777 | 2018-10-08 16:31:06 +1100 | [diff] [blame] | 3663 | |
| 3664 | /* H_CEDE has to be handled now, not later */ |
| 3665 | if (trap == BOOK3S_INTERRUPT_SYSCALL && !vcpu->arch.nested && |
| 3666 | kvmppc_get_gpr(vcpu, 3) == H_CEDE) { |
| 3667 | kvmppc_nested_cede(vcpu); |
Michael Roth | 1f50cc1 | 2020-03-10 16:11:28 -0500 | [diff] [blame] | 3668 | kvmppc_set_gpr(vcpu, 3, 0); |
Paul Mackerras | 4bad777 | 2018-10-08 16:31:06 +1100 | [diff] [blame] | 3669 | trap = 0; |
| 3670 | } |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3671 | } else { |
| 3672 | trap = kvmhv_load_hv_regs_and_go(vcpu, time_limit, lpcr); |
| 3673 | } |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3674 | |
| 3675 | vcpu->arch.slb_max = 0; |
| 3676 | dec = mfspr(SPRN_DEC); |
Suraj Jitindar Singh | 8695377 | 2019-06-20 11:46:50 +1000 | [diff] [blame] | 3677 | if (!(lpcr & LPCR_LD)) /* Sign extend if not using large decrementer */ |
| 3678 | dec = (s32) dec; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3679 | tb = mftb(); |
| 3680 | vcpu->arch.dec_expires = dec + tb; |
| 3681 | vcpu->cpu = -1; |
| 3682 | vcpu->arch.thread_cpu = -1; |
| 3683 | vcpu->arch.ctrl = mfspr(SPRN_CTRLF); |
| 3684 | |
| 3685 | vcpu->arch.iamr = mfspr(SPRN_IAMR); |
| 3686 | vcpu->arch.pspb = mfspr(SPRN_PSPB); |
| 3687 | vcpu->arch.fscr = mfspr(SPRN_FSCR); |
| 3688 | vcpu->arch.tar = mfspr(SPRN_TAR); |
| 3689 | vcpu->arch.ebbhr = mfspr(SPRN_EBBHR); |
| 3690 | vcpu->arch.ebbrr = mfspr(SPRN_EBBRR); |
| 3691 | vcpu->arch.bescr = mfspr(SPRN_BESCR); |
| 3692 | vcpu->arch.wort = mfspr(SPRN_WORT); |
| 3693 | vcpu->arch.tid = mfspr(SPRN_TIDR); |
| 3694 | vcpu->arch.amr = mfspr(SPRN_AMR); |
| 3695 | vcpu->arch.uamor = mfspr(SPRN_UAMOR); |
| 3696 | vcpu->arch.dscr = mfspr(SPRN_DSCR); |
| 3697 | |
| 3698 | mtspr(SPRN_PSPB, 0); |
| 3699 | mtspr(SPRN_WORT, 0); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3700 | mtspr(SPRN_UAMOR, 0); |
| 3701 | mtspr(SPRN_DSCR, host_dscr); |
| 3702 | mtspr(SPRN_TIDR, host_tidr); |
| 3703 | mtspr(SPRN_IAMR, host_iamr); |
| 3704 | mtspr(SPRN_PSPB, 0); |
| 3705 | |
Michael Ellerman | d976f68 | 2019-02-20 19:55:00 +1100 | [diff] [blame] | 3706 | if (host_amr != vcpu->arch.amr) |
| 3707 | mtspr(SPRN_AMR, host_amr); |
| 3708 | |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3709 | msr_check_and_set(MSR_FP | MSR_VEC | MSR_VSX); |
| 3710 | store_fp_state(&vcpu->arch.fp); |
| 3711 | #ifdef CONFIG_ALTIVEC |
| 3712 | store_vr_state(&vcpu->arch.vr); |
| 3713 | #endif |
Suraj Jitindar Singh | 44b198a | 2019-04-30 10:41:23 +1000 | [diff] [blame] | 3714 | vcpu->arch.vrsave = mfspr(SPRN_VRSAVE); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3715 | |
| 3716 | if (cpu_has_feature(CPU_FTR_TM) || |
| 3717 | cpu_has_feature(CPU_FTR_P9_TM_HV_ASSIST)) |
| 3718 | kvmppc_save_tm_hv(vcpu, vcpu->arch.shregs.msr, true); |
| 3719 | |
| 3720 | save_pmu = 1; |
| 3721 | if (vcpu->arch.vpa.pinned_addr) { |
| 3722 | struct lppaca *lp = vcpu->arch.vpa.pinned_addr; |
| 3723 | u32 yield_count = be32_to_cpu(lp->yield_count) + 1; |
| 3724 | lp->yield_count = cpu_to_be32(yield_count); |
| 3725 | vcpu->arch.vpa.dirty = 1; |
| 3726 | save_pmu = lp->pmcregs_in_use; |
| 3727 | } |
Suraj Jitindar Singh | 63279ee | 2019-07-03 11:20:20 +1000 | [diff] [blame] | 3728 | /* Must save pmu if this guest is capable of running nested guests */ |
| 3729 | save_pmu |= nesting_enabled(vcpu->kvm); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3730 | |
| 3731 | kvmhv_save_guest_pmu(vcpu, save_pmu); |
| 3732 | |
| 3733 | vc->entry_exit_map = 0x101; |
| 3734 | vc->in_guest = 0; |
| 3735 | |
| 3736 | mtspr(SPRN_DEC, local_paca->kvm_hstate.dec_expires - mftb()); |
Suraj Jitindar Singh | d724c9e | 2019-05-30 12:17:18 +1000 | [diff] [blame] | 3737 | mtspr(SPRN_SPRG_VDSO_WRITE, local_paca->sprg_vdso); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 3738 | |
| 3739 | kvmhv_load_host_pmu(); |
| 3740 | |
| 3741 | kvmppc_subcore_exit_guest(); |
| 3742 | |
| 3743 | return trap; |
| 3744 | } |
| 3745 | |
| 3746 | /* |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3747 | * Wait for some other vcpu thread to execute us, and |
| 3748 | * wake us up when we need to handle something in the host. |
| 3749 | */ |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3750 | static void kvmppc_wait_for_exec(struct kvmppc_vcore *vc, |
| 3751 | struct kvm_vcpu *vcpu, int wait_state) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3752 | { |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3753 | DEFINE_WAIT(wait); |
| 3754 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3755 | prepare_to_wait(&vcpu->arch.cpu_run, &wait, wait_state); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3756 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
| 3757 | spin_unlock(&vc->lock); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3758 | schedule(); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3759 | spin_lock(&vc->lock); |
| 3760 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3761 | finish_wait(&vcpu->arch.cpu_run, &wait); |
| 3762 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3763 | |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3764 | static void grow_halt_poll_ns(struct kvmppc_vcore *vc) |
| 3765 | { |
Nir Weiner | 7fa08e7 | 2019-01-27 12:17:14 +0200 | [diff] [blame] | 3766 | if (!halt_poll_ns_grow) |
| 3767 | return; |
| 3768 | |
Nir Weiner | dee339b | 2019-01-27 12:17:16 +0200 | [diff] [blame] | 3769 | vc->halt_poll_ns *= halt_poll_ns_grow; |
| 3770 | if (vc->halt_poll_ns < halt_poll_ns_grow_start) |
Nir Weiner | 49113d3 | 2019-01-27 12:17:15 +0200 | [diff] [blame] | 3771 | vc->halt_poll_ns = halt_poll_ns_grow_start; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3772 | } |
| 3773 | |
| 3774 | static void shrink_halt_poll_ns(struct kvmppc_vcore *vc) |
| 3775 | { |
| 3776 | if (halt_poll_ns_shrink == 0) |
| 3777 | vc->halt_poll_ns = 0; |
| 3778 | else |
| 3779 | vc->halt_poll_ns /= halt_poll_ns_shrink; |
| 3780 | } |
| 3781 | |
Paul Mackerras | ee3308a | 2017-06-20 15:46:12 +1000 | [diff] [blame] | 3782 | #ifdef CONFIG_KVM_XICS |
| 3783 | static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu) |
| 3784 | { |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 3785 | if (!xics_on_xive()) |
Paul Mackerras | ee3308a | 2017-06-20 15:46:12 +1000 | [diff] [blame] | 3786 | return false; |
Benjamin Herrenschmidt | 2267ea7 | 2018-01-12 13:37:13 +1100 | [diff] [blame] | 3787 | return vcpu->arch.irq_pending || vcpu->arch.xive_saved_state.pipr < |
Paul Mackerras | ee3308a | 2017-06-20 15:46:12 +1000 | [diff] [blame] | 3788 | vcpu->arch.xive_saved_state.cppr; |
| 3789 | } |
| 3790 | #else |
| 3791 | static inline bool xive_interrupt_pending(struct kvm_vcpu *vcpu) |
| 3792 | { |
| 3793 | return false; |
| 3794 | } |
| 3795 | #endif /* CONFIG_KVM_XICS */ |
| 3796 | |
Paul Mackerras | 1da4e2f | 2017-05-19 16:26:16 +1000 | [diff] [blame] | 3797 | static bool kvmppc_vcpu_woken(struct kvm_vcpu *vcpu) |
| 3798 | { |
| 3799 | if (vcpu->arch.pending_exceptions || vcpu->arch.prodded || |
Paul Mackerras | ee3308a | 2017-06-20 15:46:12 +1000 | [diff] [blame] | 3800 | kvmppc_doorbell_pending(vcpu) || xive_interrupt_pending(vcpu)) |
Paul Mackerras | 1da4e2f | 2017-05-19 16:26:16 +1000 | [diff] [blame] | 3801 | return true; |
| 3802 | |
| 3803 | return false; |
| 3804 | } |
| 3805 | |
Suraj Jitindar Singh | 908a093 | 2016-10-14 11:53:23 +1100 | [diff] [blame] | 3806 | /* |
| 3807 | * 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] | 3808 | * exceptions or are no longer ceded |
| 3809 | */ |
| 3810 | static int kvmppc_vcore_check_block(struct kvmppc_vcore *vc) |
| 3811 | { |
| 3812 | struct kvm_vcpu *vcpu; |
| 3813 | int i; |
| 3814 | |
| 3815 | for_each_runnable_thread(i, vcpu, vc) { |
Paul Mackerras | 1da4e2f | 2017-05-19 16:26:16 +1000 | [diff] [blame] | 3816 | if (!vcpu->arch.ceded || kvmppc_vcpu_woken(vcpu)) |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3817 | return 1; |
| 3818 | } |
| 3819 | |
| 3820 | return 0; |
| 3821 | } |
| 3822 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3823 | /* |
| 3824 | * All the vcpus in this vcore are idle, so wait for a decrementer |
| 3825 | * or external interrupt to one of the vcpus. vc->lock is held. |
| 3826 | */ |
| 3827 | static void kvmppc_vcore_blocked(struct kvmppc_vcore *vc) |
| 3828 | { |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3829 | ktime_t cur, start_poll, start_wait; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3830 | int do_sleep = 1; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3831 | u64 block_ns; |
Suresh E. Warrier | 1bc5d59 | 2014-11-03 15:52:00 +1100 | [diff] [blame] | 3832 | |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3833 | /* Poll for pending exceptions and ceded state */ |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3834 | cur = start_poll = ktime_get(); |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3835 | if (vc->halt_poll_ns) { |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3836 | ktime_t stop = ktime_add_ns(start_poll, vc->halt_poll_ns); |
| 3837 | ++vc->runner->stat.halt_attempted_poll; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3838 | |
| 3839 | vc->vcore_state = VCORE_POLLING; |
| 3840 | spin_unlock(&vc->lock); |
| 3841 | |
| 3842 | do { |
| 3843 | if (kvmppc_vcore_check_block(vc)) { |
| 3844 | do_sleep = 0; |
| 3845 | break; |
| 3846 | } |
| 3847 | cur = ktime_get(); |
| 3848 | } while (single_task_running() && ktime_before(cur, stop)); |
| 3849 | |
| 3850 | spin_lock(&vc->lock); |
| 3851 | vc->vcore_state = VCORE_INACTIVE; |
| 3852 | |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3853 | if (!do_sleep) { |
| 3854 | ++vc->runner->stat.halt_successful_poll; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3855 | goto out; |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3856 | } |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3857 | } |
| 3858 | |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 3859 | prepare_to_rcuwait(&vc->wait); |
| 3860 | set_current_state(TASK_INTERRUPTIBLE); |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3861 | if (kvmppc_vcore_check_block(vc)) { |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 3862 | finish_rcuwait(&vc->wait); |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3863 | do_sleep = 0; |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3864 | /* If we polled, count this as a successful poll */ |
| 3865 | if (vc->halt_poll_ns) |
| 3866 | ++vc->runner->stat.halt_successful_poll; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3867 | goto out; |
Suresh E. Warrier | 1bc5d59 | 2014-11-03 15:52:00 +1100 | [diff] [blame] | 3868 | } |
| 3869 | |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3870 | start_wait = ktime_get(); |
| 3871 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3872 | vc->vcore_state = VCORE_SLEEPING; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 3873 | trace_kvmppc_vcore_blocked(vc, 0); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3874 | spin_unlock(&vc->lock); |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 3875 | schedule(); |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 3876 | finish_rcuwait(&vc->wait); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3877 | spin_lock(&vc->lock); |
| 3878 | vc->vcore_state = VCORE_INACTIVE; |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 3879 | trace_kvmppc_vcore_blocked(vc, 1); |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3880 | ++vc->runner->stat.halt_successful_wait; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3881 | |
| 3882 | cur = ktime_get(); |
| 3883 | |
| 3884 | out: |
Suraj Jitindar Singh | 2a27f51 | 2016-08-02 14:03:23 +1000 | [diff] [blame] | 3885 | block_ns = ktime_to_ns(cur) - ktime_to_ns(start_poll); |
| 3886 | |
| 3887 | /* Attribute wait time */ |
| 3888 | if (do_sleep) { |
| 3889 | vc->runner->stat.halt_wait_ns += |
| 3890 | ktime_to_ns(cur) - ktime_to_ns(start_wait); |
| 3891 | /* Attribute failed poll time */ |
| 3892 | if (vc->halt_poll_ns) |
| 3893 | vc->runner->stat.halt_poll_fail_ns += |
| 3894 | ktime_to_ns(start_wait) - |
| 3895 | ktime_to_ns(start_poll); |
| 3896 | } else { |
| 3897 | /* Attribute successful poll time */ |
| 3898 | if (vc->halt_poll_ns) |
| 3899 | vc->runner->stat.halt_poll_success_ns += |
| 3900 | ktime_to_ns(cur) - |
| 3901 | ktime_to_ns(start_poll); |
| 3902 | } |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3903 | |
| 3904 | /* Adjust poll time */ |
Suraj Jitindar Singh | 307d93e4 | 2016-10-14 11:53:20 +1100 | [diff] [blame] | 3905 | if (halt_poll_ns) { |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3906 | if (block_ns <= vc->halt_poll_ns) |
| 3907 | ; |
| 3908 | /* We slept and blocked for longer than the max halt time */ |
Suraj Jitindar Singh | 307d93e4 | 2016-10-14 11:53:20 +1100 | [diff] [blame] | 3909 | else if (vc->halt_poll_ns && block_ns > halt_poll_ns) |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3910 | shrink_halt_poll_ns(vc); |
| 3911 | /* We slept and our poll time is too small */ |
Suraj Jitindar Singh | 307d93e4 | 2016-10-14 11:53:20 +1100 | [diff] [blame] | 3912 | else if (vc->halt_poll_ns < halt_poll_ns && |
| 3913 | block_ns < halt_poll_ns) |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3914 | grow_halt_poll_ns(vc); |
Suraj Jitindar Singh | e03f392 | 2016-10-14 11:53:21 +1100 | [diff] [blame] | 3915 | if (vc->halt_poll_ns > halt_poll_ns) |
| 3916 | vc->halt_poll_ns = halt_poll_ns; |
Suraj Jitindar Singh | 0cda69d | 2016-08-02 14:03:21 +1000 | [diff] [blame] | 3917 | } else |
| 3918 | vc->halt_poll_ns = 0; |
| 3919 | |
| 3920 | trace_kvmppc_vcore_wakeup(do_sleep, block_ns); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3921 | } |
| 3922 | |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 3923 | /* |
| 3924 | * This never fails for a radix guest, as none of the operations it does |
| 3925 | * for a radix guest can fail or have a way to report failure. |
| 3926 | * kvmhv_run_single_vcpu() relies on this fact. |
| 3927 | */ |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 3928 | static int kvmhv_setup_mmu(struct kvm_vcpu *vcpu) |
| 3929 | { |
| 3930 | int r = 0; |
| 3931 | struct kvm *kvm = vcpu->kvm; |
| 3932 | |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 3933 | mutex_lock(&kvm->arch.mmu_setup_lock); |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 3934 | if (!kvm->arch.mmu_ready) { |
| 3935 | if (!kvm_is_radix(kvm)) |
| 3936 | r = kvmppc_hv_setup_htab_rma(vcpu); |
| 3937 | if (!r) { |
| 3938 | if (cpu_has_feature(CPU_FTR_ARCH_300)) |
| 3939 | kvmppc_setup_partition_table(kvm); |
| 3940 | kvm->arch.mmu_ready = 1; |
| 3941 | } |
| 3942 | } |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 3943 | mutex_unlock(&kvm->arch.mmu_setup_lock); |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 3944 | return r; |
| 3945 | } |
| 3946 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 3947 | static int kvmppc_run_vcpu(struct kvm_vcpu *vcpu) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3948 | { |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 3949 | struct kvm_run *run = vcpu->run; |
Paul Mackerras | 38c53af | 2017-11-08 14:44:04 +1100 | [diff] [blame] | 3950 | int n_ceded, i, r; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3951 | struct kvmppc_vcore *vc; |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 3952 | struct kvm_vcpu *v; |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 3953 | |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 3954 | trace_kvmppc_run_vcpu_enter(vcpu); |
| 3955 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 3956 | run->exit_reason = 0; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3957 | vcpu->arch.ret = RESUME_GUEST; |
| 3958 | vcpu->arch.trap = 0; |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 3959 | kvmppc_update_vpas(vcpu); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3960 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3961 | /* |
| 3962 | * Synchronize with other threads in this virtual core |
| 3963 | */ |
| 3964 | vc = vcpu->arch.vcore; |
| 3965 | spin_lock(&vc->lock); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3966 | vcpu->arch.ceded = 0; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3967 | vcpu->arch.run_task = current; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 3968 | vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3969 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 3970 | vcpu->arch.busy_preempt = TB_NIL; |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 3971 | WRITE_ONCE(vc->runnable_threads[vcpu->arch.ptid], vcpu); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3972 | ++vc->n_runnable; |
| 3973 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3974 | /* |
| 3975 | * This happens the first time this is called for a vcpu. |
| 3976 | * If the vcore is already running, we may be able to start |
| 3977 | * this thread straight away and have it join in. |
| 3978 | */ |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 3979 | if (!signal_pending(current)) { |
Paul Mackerras | c0093f1 | 2017-11-20 16:12:25 +1100 | [diff] [blame] | 3980 | if ((vc->vcore_state == VCORE_PIGGYBACK || |
| 3981 | vc->vcore_state == VCORE_RUNNING) && |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 3982 | !VCORE_IS_EXITING(vc)) { |
Paul Mackerras | 2f12f03 | 2012-10-15 01:17:17 +0000 | [diff] [blame] | 3983 | kvmppc_create_dtl_entry(vcpu, vc); |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 3984 | kvmppc_start_thread(vcpu, vc); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 3985 | trace_kvm_guest_enter(vcpu); |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 3986 | } else if (vc->vcore_state == VCORE_SLEEPING) { |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 3987 | rcuwait_wake_up(&vc->wait); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3988 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 3989 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 3990 | } |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 3991 | |
| 3992 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
| 3993 | !signal_pending(current)) { |
Paul Mackerras | 072df81 | 2017-11-09 14:30:24 +1100 | [diff] [blame] | 3994 | /* See if the MMU is ready to go */ |
| 3995 | if (!vcpu->kvm->arch.mmu_ready) { |
Paul Mackerras | 38c53af | 2017-11-08 14:44:04 +1100 | [diff] [blame] | 3996 | spin_unlock(&vc->lock); |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 3997 | r = kvmhv_setup_mmu(vcpu); |
Paul Mackerras | 38c53af | 2017-11-08 14:44:04 +1100 | [diff] [blame] | 3998 | spin_lock(&vc->lock); |
| 3999 | if (r) { |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4000 | run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 4001 | run->fail_entry. |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 4002 | hardware_entry_failure_reason = 0; |
Paul Mackerras | 38c53af | 2017-11-08 14:44:04 +1100 | [diff] [blame] | 4003 | vcpu->arch.ret = r; |
| 4004 | break; |
| 4005 | } |
| 4006 | } |
| 4007 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 4008 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
| 4009 | kvmppc_vcore_end_preempt(vc); |
| 4010 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4011 | if (vc->vcore_state != VCORE_INACTIVE) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 4012 | kvmppc_wait_for_exec(vc, vcpu, TASK_INTERRUPTIBLE); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 4013 | continue; |
| 4014 | } |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 4015 | for_each_runnable_thread(i, v, vc) { |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 4016 | kvmppc_core_prepare_to_enter(v); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 4017 | if (signal_pending(v->arch.run_task)) { |
| 4018 | kvmppc_remove_runnable(vc, v); |
| 4019 | v->stat.signal_exits++; |
Tianjia Zhang | 2610a57 | 2020-04-27 12:35:10 +0800 | [diff] [blame] | 4020 | v->run->exit_reason = KVM_EXIT_INTR; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 4021 | v->arch.ret = -EINTR; |
| 4022 | wake_up(&v->arch.cpu_run); |
| 4023 | } |
| 4024 | } |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4025 | if (!vc->n_runnable || vcpu->arch.state != KVMPPC_VCPU_RUNNABLE) |
| 4026 | break; |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4027 | n_ceded = 0; |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 4028 | for_each_runnable_thread(i, v, vc) { |
Paul Mackerras | 1da4e2f | 2017-05-19 16:26:16 +1000 | [diff] [blame] | 4029 | if (!kvmppc_vcpu_woken(v)) |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4030 | n_ceded += v->arch.ceded; |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 4031 | else |
| 4032 | v->arch.ceded = 0; |
| 4033 | } |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 4034 | vc->runner = vcpu; |
| 4035 | if (n_ceded == vc->n_runnable) { |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4036 | kvmppc_vcore_blocked(vc); |
Konstantin Khlebnikov | c56dadf | 2015-07-15 12:52:03 +0300 | [diff] [blame] | 4037 | } else if (need_resched()) { |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 4038 | kvmppc_vcore_preempt(vc); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 4039 | /* Let something else run */ |
| 4040 | cond_resched_lock(&vc->lock); |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 4041 | if (vc->vcore_state == VCORE_PREEMPT) |
| 4042 | kvmppc_vcore_end_preempt(vc); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 4043 | } else { |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4044 | kvmppc_run_core(vc); |
Paul Mackerras | 25fedfc | 2015-03-28 14:21:05 +1100 | [diff] [blame] | 4045 | } |
Paul Mackerras | 0456ec4 | 2012-02-03 00:56:21 +0000 | [diff] [blame] | 4046 | vc->runner = NULL; |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 4047 | } |
| 4048 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4049 | while (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE && |
| 4050 | (vc->vcore_state == VCORE_RUNNING || |
Paul Mackerras | 5fc3e64 | 2015-09-18 13:13:44 +1000 | [diff] [blame] | 4051 | vc->vcore_state == VCORE_EXITING || |
| 4052 | vc->vcore_state == VCORE_PIGGYBACK)) |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 4053 | kvmppc_wait_for_exec(vc, vcpu, TASK_UNINTERRUPTIBLE); |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4054 | |
Paul Mackerras | 5fc3e64 | 2015-09-18 13:13:44 +1000 | [diff] [blame] | 4055 | if (vc->vcore_state == VCORE_PREEMPT && vc->runner == NULL) |
| 4056 | kvmppc_vcore_end_preempt(vc); |
| 4057 | |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4058 | if (vcpu->arch.state == KVMPPC_VCPU_RUNNABLE) { |
| 4059 | kvmppc_remove_runnable(vc, vcpu); |
| 4060 | vcpu->stat.signal_exits++; |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4061 | run->exit_reason = KVM_EXIT_INTR; |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4062 | vcpu->arch.ret = -EINTR; |
| 4063 | } |
| 4064 | |
| 4065 | if (vc->n_runnable && vc->vcore_state == VCORE_INACTIVE) { |
| 4066 | /* Wake up some vcpu to run the core */ |
Suraj Jitindar Singh | 7b5f827 | 2016-08-02 14:03:20 +1000 | [diff] [blame] | 4067 | i = -1; |
| 4068 | v = next_runnable_thread(vc, &i); |
Paul Mackerras | 8455d79 | 2012-10-15 01:17:42 +0000 | [diff] [blame] | 4069 | wake_up(&v->arch.cpu_run); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 4070 | } |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 4071 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4072 | trace_kvmppc_run_vcpu_exit(vcpu); |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 4073 | spin_unlock(&vc->lock); |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 4074 | return vcpu->arch.ret; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 4075 | } |
| 4076 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4077 | int kvmhv_run_single_vcpu(struct kvm_vcpu *vcpu, u64 time_limit, |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4078 | unsigned long lpcr) |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4079 | { |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4080 | struct kvm_run *run = vcpu->run; |
Suraj Jitindar Singh | 9d0b048 | 2018-10-08 16:31:11 +1100 | [diff] [blame] | 4081 | int trap, r, pcpu; |
Paul Mackerras | 70ea13f | 2019-04-29 19:02:58 +1000 | [diff] [blame] | 4082 | int srcu_idx, lpid; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4083 | struct kvmppc_vcore *vc; |
| 4084 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4085 | struct kvm_nested_guest *nested = vcpu->arch.nested; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4086 | |
| 4087 | trace_kvmppc_run_vcpu_enter(vcpu); |
| 4088 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4089 | run->exit_reason = 0; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4090 | vcpu->arch.ret = RESUME_GUEST; |
| 4091 | vcpu->arch.trap = 0; |
| 4092 | |
| 4093 | vc = vcpu->arch.vcore; |
| 4094 | vcpu->arch.ceded = 0; |
| 4095 | vcpu->arch.run_task = current; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4096 | vcpu->arch.stolen_logged = vcore_stolen_time(vc, mftb()); |
| 4097 | vcpu->arch.state = KVMPPC_VCPU_RUNNABLE; |
| 4098 | vcpu->arch.busy_preempt = TB_NIL; |
| 4099 | vcpu->arch.last_inst = KVM_INST_FETCH_FAILED; |
| 4100 | vc->runnable_threads[0] = vcpu; |
| 4101 | vc->n_runnable = 1; |
| 4102 | vc->runner = vcpu; |
| 4103 | |
| 4104 | /* See if the MMU is ready to go */ |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4105 | if (!kvm->arch.mmu_ready) |
| 4106 | kvmhv_setup_mmu(vcpu); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4107 | |
| 4108 | if (need_resched()) |
| 4109 | cond_resched(); |
| 4110 | |
| 4111 | kvmppc_update_vpas(vcpu); |
| 4112 | |
| 4113 | init_vcore_to_run(vc); |
| 4114 | vc->preempt_tb = TB_NIL; |
| 4115 | |
| 4116 | preempt_disable(); |
| 4117 | pcpu = smp_processor_id(); |
| 4118 | vc->pcpu = pcpu; |
| 4119 | kvmppc_prepare_radix_vcpu(vcpu, pcpu); |
| 4120 | |
| 4121 | local_irq_disable(); |
| 4122 | hard_irq_disable(); |
| 4123 | if (signal_pending(current)) |
| 4124 | goto sigpend; |
| 4125 | if (lazy_irq_pending() || need_resched() || !kvm->arch.mmu_ready) |
| 4126 | goto out; |
| 4127 | |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4128 | if (!nested) { |
| 4129 | kvmppc_core_prepare_to_enter(vcpu); |
| 4130 | if (vcpu->arch.doorbell_request) { |
| 4131 | vc->dpdes = 1; |
| 4132 | smp_wmb(); |
| 4133 | vcpu->arch.doorbell_request = 0; |
| 4134 | } |
| 4135 | if (test_bit(BOOK3S_IRQPRIO_EXTERNAL, |
| 4136 | &vcpu->arch.pending_exceptions)) |
| 4137 | lpcr |= LPCR_MER; |
| 4138 | } else if (vcpu->arch.pending_exceptions || |
| 4139 | vcpu->arch.doorbell_request || |
| 4140 | xive_interrupt_pending(vcpu)) { |
| 4141 | vcpu->arch.ret = RESUME_HOST; |
| 4142 | goto out; |
| 4143 | } |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4144 | |
| 4145 | kvmppc_clear_host_core(pcpu); |
| 4146 | |
| 4147 | local_paca->kvm_hstate.tid = 0; |
| 4148 | local_paca->kvm_hstate.napping = 0; |
| 4149 | local_paca->kvm_hstate.kvm_split_mode = NULL; |
| 4150 | kvmppc_start_thread(vcpu, vc); |
| 4151 | kvmppc_create_dtl_entry(vcpu, vc); |
| 4152 | trace_kvm_guest_enter(vcpu); |
| 4153 | |
| 4154 | vc->vcore_state = VCORE_RUNNING; |
| 4155 | trace_kvmppc_run_core(vc, 0); |
| 4156 | |
Paul Mackerras | 70ea13f | 2019-04-29 19:02:58 +1000 | [diff] [blame] | 4157 | if (cpu_has_feature(CPU_FTR_HVMODE)) { |
| 4158 | lpid = nested ? nested->shadow_lpid : kvm->arch.lpid; |
| 4159 | mtspr(SPRN_LPID, lpid); |
| 4160 | isync(); |
| 4161 | kvmppc_check_need_tlb_flush(kvm, pcpu, nested); |
| 4162 | } |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4163 | |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4164 | guest_enter_irqoff(); |
| 4165 | |
| 4166 | srcu_idx = srcu_read_lock(&kvm->srcu); |
| 4167 | |
| 4168 | this_cpu_disable_ftrace(); |
| 4169 | |
Paul Mackerras | 1b28d55 | 2019-05-28 15:01:59 +1000 | [diff] [blame] | 4170 | /* Tell lockdep that we're about to enable interrupts */ |
| 4171 | trace_hardirqs_on(); |
| 4172 | |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4173 | trap = kvmhv_p9_guest_entry(vcpu, time_limit, lpcr); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4174 | vcpu->arch.trap = trap; |
| 4175 | |
Paul Mackerras | 1b28d55 | 2019-05-28 15:01:59 +1000 | [diff] [blame] | 4176 | trace_hardirqs_off(); |
| 4177 | |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4178 | this_cpu_enable_ftrace(); |
| 4179 | |
| 4180 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 4181 | |
Paul Mackerras | f3c99f9 | 2018-10-08 16:31:12 +1100 | [diff] [blame] | 4182 | if (cpu_has_feature(CPU_FTR_HVMODE)) { |
| 4183 | mtspr(SPRN_LPID, kvm->arch.host_lpid); |
| 4184 | isync(); |
| 4185 | } |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4186 | |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4187 | set_irq_happened(trap); |
| 4188 | |
| 4189 | kvmppc_set_host_core(pcpu); |
| 4190 | |
| 4191 | local_irq_enable(); |
| 4192 | guest_exit(); |
| 4193 | |
| 4194 | cpumask_clear_cpu(pcpu, &kvm->arch.cpu_in_guest); |
| 4195 | |
| 4196 | preempt_enable(); |
| 4197 | |
Suraj Jitindar Singh | 3c25ab3 | 2019-06-20 11:46:51 +1000 | [diff] [blame] | 4198 | /* |
| 4199 | * cancel pending decrementer exception if DEC is now positive, or if |
| 4200 | * entering a nested guest in which case the decrementer is now owned |
| 4201 | * by L2 and the L1 decrementer is provided in hdec_expires |
| 4202 | */ |
| 4203 | if (kvmppc_core_pending_dec(vcpu) && |
| 4204 | ((get_tb() < vcpu->arch.dec_expires) || |
| 4205 | (trap == BOOK3S_INTERRUPT_SYSCALL && |
| 4206 | kvmppc_get_gpr(vcpu, 3) == H_ENTER_NESTED))) |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4207 | kvmppc_core_dequeue_dec(vcpu); |
| 4208 | |
| 4209 | trace_kvm_guest_exit(vcpu); |
| 4210 | r = RESUME_GUEST; |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4211 | if (trap) { |
| 4212 | if (!nested) |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4213 | r = kvmppc_handle_exit_hv(vcpu, current); |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4214 | else |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4215 | r = kvmppc_handle_nested_exit(vcpu); |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4216 | } |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4217 | vcpu->arch.ret = r; |
| 4218 | |
| 4219 | if (is_kvmppc_resume_guest(r) && vcpu->arch.ceded && |
| 4220 | !kvmppc_vcpu_woken(vcpu)) { |
| 4221 | kvmppc_set_timer(vcpu); |
| 4222 | while (vcpu->arch.ceded && !kvmppc_vcpu_woken(vcpu)) { |
| 4223 | if (signal_pending(current)) { |
| 4224 | vcpu->stat.signal_exits++; |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4225 | run->exit_reason = KVM_EXIT_INTR; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4226 | vcpu->arch.ret = -EINTR; |
| 4227 | break; |
| 4228 | } |
| 4229 | spin_lock(&vc->lock); |
| 4230 | kvmppc_vcore_blocked(vc); |
| 4231 | spin_unlock(&vc->lock); |
| 4232 | } |
| 4233 | } |
| 4234 | vcpu->arch.ceded = 0; |
| 4235 | |
| 4236 | vc->vcore_state = VCORE_INACTIVE; |
| 4237 | trace_kvmppc_run_core(vc, 1); |
| 4238 | |
| 4239 | done: |
| 4240 | kvmppc_remove_runnable(vc, vcpu); |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4241 | trace_kvmppc_run_vcpu_exit(vcpu); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4242 | |
| 4243 | return vcpu->arch.ret; |
| 4244 | |
| 4245 | sigpend: |
| 4246 | vcpu->stat.signal_exits++; |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4247 | run->exit_reason = KVM_EXIT_INTR; |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4248 | vcpu->arch.ret = -EINTR; |
| 4249 | out: |
| 4250 | local_irq_enable(); |
| 4251 | preempt_enable(); |
| 4252 | goto done; |
| 4253 | } |
| 4254 | |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4255 | static int kvmppc_vcpu_run_hv(struct kvm_vcpu *vcpu) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 4256 | { |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4257 | struct kvm_run *run = vcpu->run; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 4258 | int r; |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 4259 | int srcu_idx; |
Paul Mackerras | ca8efa1 | 2017-06-06 16:47:22 +1000 | [diff] [blame] | 4260 | unsigned long ebb_regs[3] = {}; /* shut up GCC */ |
Paul Mackerras | 4c3bb4c | 2017-06-15 15:43:17 +1000 | [diff] [blame] | 4261 | unsigned long user_tar = 0; |
| 4262 | unsigned int user_vrsave; |
Paul Mackerras | 1b151ce | 2017-09-13 15:53:48 +1000 | [diff] [blame] | 4263 | struct kvm *kvm; |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 4264 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 4265 | if (!vcpu->arch.sane) { |
| 4266 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 4267 | return -EINVAL; |
| 4268 | } |
| 4269 | |
Paul Mackerras | 46a704f | 2017-06-15 16:10:27 +1000 | [diff] [blame] | 4270 | /* |
| 4271 | * Don't allow entry with a suspended transaction, because |
| 4272 | * the guest entry/exit code will lose it. |
| 4273 | * If the guest has TM enabled, save away their TM-related SPRs |
| 4274 | * (they will get restored by the TM unavailable interrupt). |
| 4275 | */ |
| 4276 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 4277 | if (cpu_has_feature(CPU_FTR_TM) && current->thread.regs && |
| 4278 | (current->thread.regs->msr & MSR_TM)) { |
| 4279 | if (MSR_TM_ACTIVE(current->thread.regs->msr)) { |
| 4280 | run->exit_reason = KVM_EXIT_FAIL_ENTRY; |
| 4281 | run->fail_entry.hardware_entry_failure_reason = 0; |
| 4282 | return -EINVAL; |
| 4283 | } |
Paul Mackerras | e470571 | 2017-07-21 13:57:14 +1000 | [diff] [blame] | 4284 | /* Enable TM so we can read the TM SPRs */ |
| 4285 | mtmsr(mfmsr() | MSR_TM); |
Paul Mackerras | 46a704f | 2017-06-15 16:10:27 +1000 | [diff] [blame] | 4286 | current->thread.tm_tfhar = mfspr(SPRN_TFHAR); |
| 4287 | current->thread.tm_tfiar = mfspr(SPRN_TFIAR); |
| 4288 | current->thread.tm_texasr = mfspr(SPRN_TEXASR); |
| 4289 | current->thread.regs->msr &= ~MSR_TM; |
| 4290 | } |
| 4291 | #endif |
| 4292 | |
Paul Mackerras | 7aa1584 | 2018-04-20 19:53:22 +1000 | [diff] [blame] | 4293 | /* |
| 4294 | * Force online to 1 for the sake of old userspace which doesn't |
| 4295 | * set it. |
| 4296 | */ |
| 4297 | if (!vcpu->arch.online) { |
| 4298 | atomic_inc(&vcpu->arch.vcore->online_count); |
| 4299 | vcpu->arch.online = 1; |
| 4300 | } |
| 4301 | |
Scott Wood | 25051b5a | 2011-11-08 18:23:23 -0600 | [diff] [blame] | 4302 | kvmppc_core_prepare_to_enter(vcpu); |
| 4303 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 4304 | /* No need to go into the guest when all we'll do is come back out */ |
| 4305 | if (signal_pending(current)) { |
| 4306 | run->exit_reason = KVM_EXIT_INTR; |
| 4307 | return -EINTR; |
| 4308 | } |
| 4309 | |
Paul Mackerras | 1b151ce | 2017-09-13 15:53:48 +1000 | [diff] [blame] | 4310 | kvm = vcpu->kvm; |
| 4311 | atomic_inc(&kvm->arch.vcpus_running); |
| 4312 | /* Order vcpus_running vs. mmu_ready, see kvmppc_alloc_reset_hpt */ |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4313 | smp_mb(); |
| 4314 | |
Anton Blanchard | 579e633 | 2015-10-29 11:44:09 +1100 | [diff] [blame] | 4315 | flush_all_to_thread(current); |
| 4316 | |
Paul Mackerras | 4c3bb4c | 2017-06-15 15:43:17 +1000 | [diff] [blame] | 4317 | /* Save userspace EBB and other register values */ |
Paul Mackerras | ca8efa1 | 2017-06-06 16:47:22 +1000 | [diff] [blame] | 4318 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 4319 | ebb_regs[0] = mfspr(SPRN_EBBHR); |
| 4320 | ebb_regs[1] = mfspr(SPRN_EBBRR); |
| 4321 | ebb_regs[2] = mfspr(SPRN_BESCR); |
Paul Mackerras | 4c3bb4c | 2017-06-15 15:43:17 +1000 | [diff] [blame] | 4322 | user_tar = mfspr(SPRN_TAR); |
Paul Mackerras | ca8efa1 | 2017-06-06 16:47:22 +1000 | [diff] [blame] | 4323 | } |
Paul Mackerras | 4c3bb4c | 2017-06-15 15:43:17 +1000 | [diff] [blame] | 4324 | user_vrsave = mfspr(SPRN_VRSAVE); |
Paul Mackerras | ca8efa1 | 2017-06-06 16:47:22 +1000 | [diff] [blame] | 4325 | |
Davidlohr Bueso | da4ad88 | 2020-04-23 22:48:37 -0700 | [diff] [blame] | 4326 | vcpu->arch.waitp = &vcpu->arch.vcore->wait; |
Leonardo Bras | 8a9c892 | 2019-11-26 19:36:30 -0300 | [diff] [blame] | 4327 | vcpu->arch.pgdir = kvm->mm->pgd; |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 4328 | vcpu->arch.state = KVMPPC_VCPU_BUSY_IN_HOST; |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 4329 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 4330 | do { |
Paul Mackerras | 8d9fcac | 2018-10-19 20:44:04 +1100 | [diff] [blame] | 4331 | /* |
| 4332 | * The early POWER9 chips that can't mix radix and HPT threads |
| 4333 | * on the same core also need the workaround for the problem |
| 4334 | * where the TLB would prefetch entries in the guest exit path |
| 4335 | * for radix guests using the guest PIDR value and LPID 0. |
| 4336 | * The workaround is in the old path (kvmppc_run_vcpu()) |
| 4337 | * but not the new path (kvmhv_run_single_vcpu()). |
| 4338 | */ |
| 4339 | if (kvm->arch.threads_indep && kvm_is_radix(kvm) && |
| 4340 | !no_mixing_hpt_and_radix) |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4341 | r = kvmhv_run_single_vcpu(vcpu, ~(u64)0, |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4342 | vcpu->arch.vcore->lpcr); |
Paul Mackerras | 95a6432 | 2018-10-08 16:30:55 +1100 | [diff] [blame] | 4343 | else |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4344 | r = kvmppc_run_vcpu(vcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 4345 | |
| 4346 | if (run->exit_reason == KVM_EXIT_PAPR_HCALL && |
| 4347 | !(vcpu->arch.shregs.msr & MSR_PR)) { |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 4348 | trace_kvm_hcall_enter(vcpu); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 4349 | r = kvmppc_pseries_do_hcall(vcpu); |
Suresh E. Warrier | 3c78f78 | 2014-12-03 18:48:10 -0600 | [diff] [blame] | 4350 | trace_kvm_hcall_exit(vcpu, r); |
Scott Wood | 7e28e60e | 2011-11-08 18:23:20 -0600 | [diff] [blame] | 4351 | kvmppc_core_prepare_to_enter(vcpu); |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 4352 | } else if (r == RESUME_PAGE_FAULT) { |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 4353 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 4354 | r = kvmppc_book3s_hv_page_fault(vcpu, |
Paul Mackerras | 913d3ff9a | 2012-10-15 01:16:48 +0000 | [diff] [blame] | 4355 | vcpu->arch.fault_dar, vcpu->arch.fault_dsisr); |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 4356 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 4357 | } else if (r == RESUME_PASSTHROUGH) { |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 4358 | if (WARN_ON(xics_on_xive())) |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 4359 | r = H_SUCCESS; |
| 4360 | else |
| 4361 | r = kvmppc_xics_rm_complete(vcpu, 0); |
| 4362 | } |
Greg Kurz | e59d24e | 2014-02-06 17:36:56 +0100 | [diff] [blame] | 4363 | } while (is_kvmppc_resume_guest(r)); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4364 | |
Paul Mackerras | 4c3bb4c | 2017-06-15 15:43:17 +1000 | [diff] [blame] | 4365 | /* Restore userspace EBB and other register values */ |
Paul Mackerras | ca8efa1 | 2017-06-06 16:47:22 +1000 | [diff] [blame] | 4366 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) { |
| 4367 | mtspr(SPRN_EBBHR, ebb_regs[0]); |
| 4368 | mtspr(SPRN_EBBRR, ebb_regs[1]); |
| 4369 | mtspr(SPRN_BESCR, ebb_regs[2]); |
Paul Mackerras | 4c3bb4c | 2017-06-15 15:43:17 +1000 | [diff] [blame] | 4370 | mtspr(SPRN_TAR, user_tar); |
| 4371 | mtspr(SPRN_FSCR, current->thread.fscr); |
Paul Mackerras | ca8efa1 | 2017-06-06 16:47:22 +1000 | [diff] [blame] | 4372 | } |
Paul Mackerras | 4c3bb4c | 2017-06-15 15:43:17 +1000 | [diff] [blame] | 4373 | mtspr(SPRN_VRSAVE, user_vrsave); |
Paul Mackerras | ca8efa1 | 2017-06-06 16:47:22 +1000 | [diff] [blame] | 4374 | |
Paul Mackerras | c7b6767 | 2012-10-15 01:18:07 +0000 | [diff] [blame] | 4375 | vcpu->arch.state = KVMPPC_VCPU_NOTREADY; |
Paul Mackerras | 432953b | 2017-11-09 15:37:10 +1100 | [diff] [blame] | 4376 | atomic_dec(&kvm->arch.vcpus_running); |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 4377 | return r; |
| 4378 | } |
| 4379 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 4380 | 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] | 4381 | int shift, int sllp) |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 4382 | { |
Paul Mackerras | 8dc6cca | 2017-09-11 15:29:45 +1000 | [diff] [blame] | 4383 | (*sps)->page_shift = shift; |
| 4384 | (*sps)->slb_enc = sllp; |
| 4385 | (*sps)->enc[0].page_shift = shift; |
| 4386 | (*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] | 4387 | /* |
Paul Mackerras | 8dc6cca | 2017-09-11 15:29:45 +1000 | [diff] [blame] | 4388 | * Add 16MB MPSS support (may get filtered out by userspace) |
Aneesh Kumar K.V | 1f365bb | 2014-05-06 23:31:36 +0530 | [diff] [blame] | 4389 | */ |
Paul Mackerras | 8dc6cca | 2017-09-11 15:29:45 +1000 | [diff] [blame] | 4390 | if (shift != 24) { |
| 4391 | int penc = kvmppc_pgsize_lp_encoding(shift, 24); |
| 4392 | if (penc != -1) { |
| 4393 | (*sps)->enc[1].page_shift = 24; |
| 4394 | (*sps)->enc[1].pte_enc = penc; |
| 4395 | } |
Aneesh Kumar K.V | 1f365bb | 2014-05-06 23:31:36 +0530 | [diff] [blame] | 4396 | } |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 4397 | (*sps)++; |
| 4398 | } |
| 4399 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 4400 | static int kvm_vm_ioctl_get_smmu_info_hv(struct kvm *kvm, |
| 4401 | struct kvm_ppc_smmu_info *info) |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 4402 | { |
| 4403 | struct kvm_ppc_one_seg_page_size *sps; |
| 4404 | |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4405 | /* |
Paul Mackerras | e3bfed1 | 2017-08-25 19:53:39 +1000 | [diff] [blame] | 4406 | * POWER7, POWER8 and POWER9 all support 32 storage keys for data. |
| 4407 | * POWER7 doesn't support keys for instruction accesses, |
| 4408 | * POWER8 and POWER9 do. |
| 4409 | */ |
| 4410 | info->data_keys = 32; |
| 4411 | info->instr_keys = cpu_has_feature(CPU_FTR_ARCH_207S) ? 32 : 0; |
| 4412 | |
Paul Mackerras | 8dc6cca | 2017-09-11 15:29:45 +1000 | [diff] [blame] | 4413 | /* POWER7, 8 and 9 all have 1T segments and 32-entry SLB */ |
| 4414 | info->flags = KVM_PPC_PAGE_SIZES_REAL | KVM_PPC_1T_SEGMENTS; |
| 4415 | info->slb_size = 32; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 4416 | |
| 4417 | /* We only support these sizes for now, and no muti-size segments */ |
| 4418 | sps = &info->sps[0]; |
Paul Mackerras | 8dc6cca | 2017-09-11 15:29:45 +1000 | [diff] [blame] | 4419 | kvmppc_add_seg_page_size(&sps, 12, 0); |
| 4420 | kvmppc_add_seg_page_size(&sps, 16, SLB_VSID_L | SLB_VSID_LP_01); |
| 4421 | kvmppc_add_seg_page_size(&sps, 24, SLB_VSID_L); |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 4422 | |
Paul Mackerras | 901f8c3 | 2018-10-08 14:24:30 +1100 | [diff] [blame] | 4423 | /* If running as a nested hypervisor, we don't support HPT guests */ |
| 4424 | if (kvmhv_on_pseries()) |
| 4425 | info->flags |= KVM_PPC_NO_HASH; |
| 4426 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 4427 | return 0; |
| 4428 | } |
| 4429 | |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4430 | /* |
| 4431 | * Get (and clear) the dirty memory log for a memory slot. |
| 4432 | */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 4433 | static int kvm_vm_ioctl_get_dirty_log_hv(struct kvm *kvm, |
| 4434 | struct kvm_dirty_log *log) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4435 | { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 4436 | struct kvm_memslots *slots; |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4437 | struct kvm_memory_slot *memslot; |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4438 | int i, r; |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4439 | unsigned long n; |
Paul Mackerras | e641a31 | 2017-10-26 16:39:19 +1100 | [diff] [blame] | 4440 | unsigned long *buf, *p; |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4441 | struct kvm_vcpu *vcpu; |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4442 | |
| 4443 | mutex_lock(&kvm->slots_lock); |
| 4444 | |
| 4445 | r = -EINVAL; |
Alex Williamson | bbacc0c | 2012-12-10 10:33:09 -0700 | [diff] [blame] | 4446 | if (log->slot >= KVM_USER_MEM_SLOTS) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4447 | goto out; |
| 4448 | |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 4449 | slots = kvm_memslots(kvm); |
| 4450 | memslot = id_to_memslot(slots, log->slot); |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4451 | r = -ENOENT; |
Sean Christopherson | 0577d1a | 2020-02-18 13:07:31 -0800 | [diff] [blame] | 4452 | if (!memslot || !memslot->dirty_bitmap) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4453 | goto out; |
| 4454 | |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4455 | /* |
Paul Mackerras | e641a31 | 2017-10-26 16:39:19 +1100 | [diff] [blame] | 4456 | * Use second half of bitmap area because both HPT and radix |
| 4457 | * accumulate bits in the first half. |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4458 | */ |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4459 | n = kvm_dirty_bitmap_bytes(memslot); |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4460 | buf = memslot->dirty_bitmap + n / sizeof(long); |
| 4461 | memset(buf, 0, n); |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4462 | |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4463 | if (kvm_is_radix(kvm)) |
| 4464 | r = kvmppc_hv_get_dirty_log_radix(kvm, memslot, buf); |
| 4465 | else |
| 4466 | r = kvmppc_hv_get_dirty_log_hpt(kvm, memslot, buf); |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4467 | if (r) |
| 4468 | goto out; |
| 4469 | |
Paul Mackerras | e641a31 | 2017-10-26 16:39:19 +1100 | [diff] [blame] | 4470 | /* |
| 4471 | * We accumulate dirty bits in the first half of the |
| 4472 | * memslot's dirty_bitmap area, for when pages are paged |
| 4473 | * out or modified by the host directly. Pick up these |
| 4474 | * bits and add them to the map. |
| 4475 | */ |
| 4476 | p = memslot->dirty_bitmap; |
| 4477 | for (i = 0; i < n / sizeof(long); ++i) |
| 4478 | buf[i] |= xchg(&p[i], 0); |
| 4479 | |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4480 | /* Harvest dirty bits from VPA and DTL updates */ |
| 4481 | /* Note: we never modify the SLB shadow buffer areas */ |
| 4482 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 4483 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 4484 | kvmppc_harvest_vpa_dirty(&vcpu->arch.vpa, memslot, buf); |
| 4485 | kvmppc_harvest_vpa_dirty(&vcpu->arch.dtl, memslot, buf); |
| 4486 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 4487 | } |
| 4488 | |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4489 | r = -EFAULT; |
Paul Mackerras | 8f7b79b | 2017-01-30 21:21:48 +1100 | [diff] [blame] | 4490 | if (copy_to_user(log->dirty_bitmap, buf, n)) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 4491 | goto out; |
| 4492 | |
| 4493 | r = 0; |
| 4494 | out: |
| 4495 | mutex_unlock(&kvm->slots_lock); |
| 4496 | return r; |
| 4497 | } |
| 4498 | |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 4499 | static void kvmppc_core_free_memslot_hv(struct kvm_memory_slot *slot) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 4500 | { |
Sean Christopherson | e96c81e | 2020-02-18 13:07:27 -0800 | [diff] [blame] | 4501 | vfree(slot->arch.rmap); |
| 4502 | slot->arch.rmap = NULL; |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 4503 | } |
| 4504 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 4505 | static int kvmppc_core_prepare_memory_region_hv(struct kvm *kvm, |
Sean Christopherson | 82307e6 | 2020-02-18 13:07:18 -0800 | [diff] [blame] | 4506 | struct kvm_memory_slot *slot, |
| 4507 | const struct kvm_userspace_memory_region *mem, |
| 4508 | enum kvm_mr_change change) |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 4509 | { |
Sean Christopherson | 82307e6 | 2020-02-18 13:07:18 -0800 | [diff] [blame] | 4510 | unsigned long npages = mem->memory_size >> PAGE_SHIFT; |
| 4511 | |
| 4512 | if (change == KVM_MR_CREATE) { |
| 4513 | slot->arch.rmap = vzalloc(array_size(npages, |
| 4514 | sizeof(*slot->arch.rmap))); |
| 4515 | if (!slot->arch.rmap) |
| 4516 | return -ENOMEM; |
| 4517 | } |
| 4518 | |
Paul Mackerras | a66b48c | 2012-09-11 13:27:46 +0000 | [diff] [blame] | 4519 | return 0; |
| 4520 | } |
| 4521 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 4522 | static void kvmppc_core_commit_memory_region_hv(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 4523 | const struct kvm_userspace_memory_region *mem, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 4524 | const struct kvm_memory_slot *old, |
Bharata B Rao | f032b73 | 2018-12-12 15:15:30 +1100 | [diff] [blame] | 4525 | const struct kvm_memory_slot *new, |
| 4526 | enum kvm_mr_change change) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4527 | { |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 4528 | unsigned long npages = mem->memory_size >> PAGE_SHIFT; |
Paul Mackerras | dfe49db | 2012-09-11 13:28:18 +0000 | [diff] [blame] | 4529 | |
Yongji Xie | a56ee9f | 2016-11-04 13:55:12 +0800 | [diff] [blame] | 4530 | /* |
| 4531 | * If we are making a new memslot, it might make |
| 4532 | * some address that was previously cached as emulated |
| 4533 | * MMIO be no longer emulated MMIO, so invalidate |
| 4534 | * all the caches of emulated MMIO translations. |
| 4535 | */ |
| 4536 | if (npages) |
| 4537 | atomic64_inc(&kvm->arch.mmio_update); |
Paul Mackerras | 5af3e9d | 2018-12-12 15:17:17 +1100 | [diff] [blame] | 4538 | |
| 4539 | /* |
| 4540 | * For change == KVM_MR_MOVE or KVM_MR_DELETE, higher levels |
| 4541 | * have already called kvm_arch_flush_shadow_memslot() to |
| 4542 | * flush shadow mappings. For KVM_MR_CREATE we have no |
| 4543 | * previous mappings. So the only case to handle is |
| 4544 | * KVM_MR_FLAGS_ONLY when the KVM_MEM_LOG_DIRTY_PAGES bit |
| 4545 | * has been changed. |
| 4546 | * For radix guests, we flush on setting KVM_MEM_LOG_DIRTY_PAGES |
| 4547 | * to get rid of any THP PTEs in the partition-scoped page tables |
| 4548 | * so we can track dirtiness at the page level; we flush when |
| 4549 | * clearing KVM_MEM_LOG_DIRTY_PAGES so that we can go back to |
| 4550 | * using THP PTEs. |
| 4551 | */ |
| 4552 | if (change == KVM_MR_FLAGS_ONLY && kvm_is_radix(kvm) && |
| 4553 | ((new->flags ^ old->flags) & KVM_MEM_LOG_DIRTY_PAGES)) |
| 4554 | kvmppc_radix_flush_memslot(kvm, old); |
Bharata B Rao | c326225 | 2019-11-25 08:36:29 +0530 | [diff] [blame] | 4555 | /* |
| 4556 | * If UV hasn't yet called H_SVM_INIT_START, don't register memslots. |
| 4557 | */ |
| 4558 | if (!kvm->arch.secure_guest) |
| 4559 | return; |
| 4560 | |
| 4561 | switch (change) { |
| 4562 | case KVM_MR_CREATE: |
Laurent Dufour | a2ce720 | 2020-07-27 11:07:18 -0700 | [diff] [blame] | 4563 | /* |
| 4564 | * @TODO kvmppc_uvmem_memslot_create() can fail and |
| 4565 | * return error. Fix this. |
| 4566 | */ |
| 4567 | kvmppc_uvmem_memslot_create(kvm, new); |
Bharata B Rao | c326225 | 2019-11-25 08:36:29 +0530 | [diff] [blame] | 4568 | break; |
| 4569 | case KVM_MR_DELETE: |
Laurent Dufour | a2ce720 | 2020-07-27 11:07:18 -0700 | [diff] [blame] | 4570 | kvmppc_uvmem_memslot_delete(kvm, old); |
Bharata B Rao | c326225 | 2019-11-25 08:36:29 +0530 | [diff] [blame] | 4571 | break; |
| 4572 | default: |
| 4573 | /* TODO: Handle KVM_MR_MOVE */ |
| 4574 | break; |
| 4575 | } |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4576 | } |
| 4577 | |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 4578 | /* |
| 4579 | * Update LPCR values in kvm->arch and in vcores. |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 4580 | * Caller must hold kvm->arch.mmu_setup_lock (for mutual exclusion |
| 4581 | * of kvm->arch.lpcr update). |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 4582 | */ |
| 4583 | void kvmppc_update_lpcr(struct kvm *kvm, unsigned long lpcr, unsigned long mask) |
| 4584 | { |
| 4585 | long int i; |
| 4586 | u32 cores_done = 0; |
| 4587 | |
| 4588 | if ((kvm->arch.lpcr & mask) == lpcr) |
| 4589 | return; |
| 4590 | |
| 4591 | kvm->arch.lpcr = (kvm->arch.lpcr & ~mask) | lpcr; |
| 4592 | |
| 4593 | for (i = 0; i < KVM_MAX_VCORES; ++i) { |
| 4594 | struct kvmppc_vcore *vc = kvm->arch.vcores[i]; |
| 4595 | if (!vc) |
| 4596 | continue; |
| 4597 | spin_lock(&vc->lock); |
| 4598 | vc->lpcr = (vc->lpcr & ~mask) | lpcr; |
| 4599 | spin_unlock(&vc->lock); |
| 4600 | if (++cores_done >= kvm->arch.online_vcores) |
| 4601 | break; |
| 4602 | } |
| 4603 | } |
| 4604 | |
Paul Mackerras | ded13fc | 2017-11-22 14:38:53 +1100 | [diff] [blame] | 4605 | void kvmppc_setup_partition_table(struct kvm *kvm) |
Paul Mackerras | 7a84084 | 2016-11-16 22:25:20 +1100 | [diff] [blame] | 4606 | { |
| 4607 | unsigned long dw0, dw1; |
| 4608 | |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4609 | if (!kvm_is_radix(kvm)) { |
| 4610 | /* PS field - page size for VRMA */ |
| 4611 | dw0 = ((kvm->arch.vrma_slb_v & SLB_VSID_L) >> 1) | |
| 4612 | ((kvm->arch.vrma_slb_v & SLB_VSID_LP) << 1); |
| 4613 | /* HTABSIZE and HTABORG fields */ |
| 4614 | dw0 |= kvm->arch.sdr1; |
Paul Mackerras | 7a84084 | 2016-11-16 22:25:20 +1100 | [diff] [blame] | 4615 | |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4616 | /* Second dword as set by userspace */ |
| 4617 | dw1 = kvm->arch.process_table; |
| 4618 | } else { |
| 4619 | dw0 = PATB_HR | radix__get_tree_size() | |
| 4620 | __pa(kvm->arch.pgtable) | RADIX_PGD_INDEX_SIZE; |
| 4621 | dw1 = PATB_GR | kvm->arch.process_table; |
| 4622 | } |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 4623 | kvmhv_set_ptbl_entry(kvm->arch.lpid, dw0, dw1); |
Paul Mackerras | 7a84084 | 2016-11-16 22:25:20 +1100 | [diff] [blame] | 4624 | } |
| 4625 | |
Paul Mackerras | 1b151ce | 2017-09-13 15:53:48 +1000 | [diff] [blame] | 4626 | /* |
| 4627 | * Set up HPT (hashed page table) and RMA (real-mode area). |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 4628 | * Must be called with kvm->arch.mmu_setup_lock held. |
Paul Mackerras | 1b151ce | 2017-09-13 15:53:48 +1000 | [diff] [blame] | 4629 | */ |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4630 | static int kvmppc_hv_setup_htab_rma(struct kvm_vcpu *vcpu) |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4631 | { |
| 4632 | int err = 0; |
| 4633 | struct kvm *kvm = vcpu->kvm; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4634 | unsigned long hva; |
| 4635 | struct kvm_memory_slot *memslot; |
| 4636 | struct vm_area_struct *vma; |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 4637 | unsigned long lpcr = 0, senc; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4638 | unsigned long psize, porder; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 4639 | int srcu_idx; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4640 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4641 | /* Allocate hashed page table (if not done already) and reset it */ |
David Gibson | 3f9d4f5 | 2016-12-20 16:49:00 +1100 | [diff] [blame] | 4642 | if (!kvm->arch.hpt.virt) { |
David Gibson | aae0777 | 2016-12-20 16:49:02 +1100 | [diff] [blame] | 4643 | int order = KVM_DEFAULT_HPT_ORDER; |
| 4644 | struct kvm_hpt_info info; |
| 4645 | |
| 4646 | err = kvmppc_allocate_hpt(&info, order); |
| 4647 | /* If we get here, it means userspace didn't specify a |
| 4648 | * size explicitly. So, try successively smaller |
| 4649 | * sizes if the default failed. */ |
| 4650 | while ((err == -ENOMEM) && --order >= PPC_MIN_HPT_ORDER) |
| 4651 | err = kvmppc_allocate_hpt(&info, order); |
| 4652 | |
| 4653 | if (err < 0) { |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4654 | pr_err("KVM: Couldn't alloc HPT\n"); |
| 4655 | goto out; |
| 4656 | } |
David Gibson | aae0777 | 2016-12-20 16:49:02 +1100 | [diff] [blame] | 4657 | |
| 4658 | kvmppc_set_hpt(kvm, &info); |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4659 | } |
| 4660 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4661 | /* Look up the memslot for guest physical address 0 */ |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 4662 | srcu_idx = srcu_read_lock(&kvm->srcu); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4663 | memslot = gfn_to_memslot(kvm, 0); |
| 4664 | |
| 4665 | /* We must have some memory at 0 by now */ |
| 4666 | err = -EINVAL; |
| 4667 | if (!memslot || (memslot->flags & KVM_MEMSLOT_INVALID)) |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 4668 | goto out_srcu; |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4669 | |
| 4670 | /* Look up the VMA for the start of this memory slot */ |
| 4671 | hva = memslot->userspace_addr; |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 4672 | mmap_read_lock(kvm->mm); |
Leonardo Bras | 8a9c892 | 2019-11-26 19:36:30 -0300 | [diff] [blame] | 4673 | vma = find_vma(kvm->mm, hva); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4674 | if (!vma || vma->vm_start > hva || (vma->vm_flags & VM_IO)) |
| 4675 | goto up_out; |
| 4676 | |
| 4677 | psize = vma_kernel_pagesize(vma); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 4678 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 4679 | mmap_read_unlock(kvm->mm); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4680 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 4681 | /* We can handle 4k, 64k or 16M pages in the VRMA */ |
Paul Mackerras | debd574 | 2018-03-02 15:38:04 +1100 | [diff] [blame] | 4682 | if (psize >= 0x1000000) |
| 4683 | psize = 0x1000000; |
| 4684 | else if (psize >= 0x10000) |
| 4685 | psize = 0x10000; |
| 4686 | else |
| 4687 | psize = 0x1000; |
| 4688 | porder = __ilog2(psize); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 4689 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 4690 | senc = slb_pgsize_encoding(psize); |
| 4691 | kvm->arch.vrma_slb_v = senc | SLB_VSID_B_1T | |
| 4692 | (VRMA_VSID << SLB_VSID_SHIFT_1T); |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 4693 | /* Create HPTEs in the hash page table for the VRMA */ |
| 4694 | kvmppc_map_vrma(vcpu, memslot, porder); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 4695 | |
Paul Mackerras | 7a84084 | 2016-11-16 22:25:20 +1100 | [diff] [blame] | 4696 | /* Update VRMASD field in the LPCR */ |
| 4697 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) { |
| 4698 | /* the -4 is to account for senc values starting at 0x10 */ |
| 4699 | lpcr = senc << (LPCR_VRMASD_SH - 4); |
| 4700 | kvmppc_update_lpcr(kvm, lpcr, LPCR_VRMASD); |
Paul Mackerras | 7a84084 | 2016-11-16 22:25:20 +1100 | [diff] [blame] | 4701 | } |
Paul Mackerras | a0144e2 | 2013-09-20 14:52:38 +1000 | [diff] [blame] | 4702 | |
Paul Mackerras | 1b151ce | 2017-09-13 15:53:48 +1000 | [diff] [blame] | 4703 | /* Order updates to kvm->arch.lpcr etc. vs. mmu_ready */ |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4704 | smp_wmb(); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4705 | err = 0; |
Paul Mackerras | 2c9097e | 2012-09-11 13:27:01 +0000 | [diff] [blame] | 4706 | out_srcu: |
| 4707 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4708 | out: |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4709 | return err; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 4710 | |
Paul Mackerras | c77162d | 2011-12-12 12:31:00 +0000 | [diff] [blame] | 4711 | up_out: |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 4712 | mmap_read_unlock(kvm->mm); |
Lai Jiangshan | 505d642 | 2013-03-16 00:50:49 +0800 | [diff] [blame] | 4713 | goto out_srcu; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 4714 | } |
| 4715 | |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 4716 | /* |
| 4717 | * Must be called with kvm->arch.mmu_setup_lock held and |
| 4718 | * mmu_ready = 0 and no vcpus running. |
| 4719 | */ |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4720 | int kvmppc_switch_mmu_to_hpt(struct kvm *kvm) |
| 4721 | { |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 4722 | if (nesting_enabled(kvm)) |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 4723 | kvmhv_release_all_nested(kvm); |
Paul Mackerras | 234ff0b | 2018-11-16 21:28:18 +1100 | [diff] [blame] | 4724 | kvmppc_rmap_reset(kvm); |
| 4725 | kvm->arch.process_table = 0; |
| 4726 | /* Mutual exclusion with kvm_unmap_hva_range etc. */ |
| 4727 | spin_lock(&kvm->mmu_lock); |
| 4728 | kvm->arch.radix = 0; |
| 4729 | spin_unlock(&kvm->mmu_lock); |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4730 | kvmppc_free_radix(kvm); |
| 4731 | kvmppc_update_lpcr(kvm, LPCR_VPM1, |
| 4732 | LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR); |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4733 | return 0; |
| 4734 | } |
| 4735 | |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 4736 | /* |
| 4737 | * Must be called with kvm->arch.mmu_setup_lock held and |
| 4738 | * mmu_ready = 0 and no vcpus running. |
| 4739 | */ |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4740 | int kvmppc_switch_mmu_to_radix(struct kvm *kvm) |
| 4741 | { |
| 4742 | int err; |
| 4743 | |
| 4744 | err = kvmppc_init_vm_radix(kvm); |
| 4745 | if (err) |
| 4746 | return err; |
Paul Mackerras | 234ff0b | 2018-11-16 21:28:18 +1100 | [diff] [blame] | 4747 | kvmppc_rmap_reset(kvm); |
| 4748 | /* Mutual exclusion with kvm_unmap_hva_range etc. */ |
| 4749 | spin_lock(&kvm->mmu_lock); |
| 4750 | kvm->arch.radix = 1; |
| 4751 | spin_unlock(&kvm->mmu_lock); |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4752 | kvmppc_free_hpt(&kvm->arch.hpt); |
| 4753 | kvmppc_update_lpcr(kvm, LPCR_UPRT | LPCR_GTSE | LPCR_HR, |
| 4754 | LPCR_VPM1 | LPCR_UPRT | LPCR_GTSE | LPCR_HR); |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4755 | return 0; |
| 4756 | } |
| 4757 | |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4758 | #ifdef CONFIG_KVM_XICS |
| 4759 | /* |
| 4760 | * Allocate a per-core structure for managing state about which cores are |
| 4761 | * running in the host versus the guest and for exchanging data between |
| 4762 | * real mode KVM and CPU running in the host. |
| 4763 | * This is only done for the first VM. |
| 4764 | * The allocated structure stays even if all VMs have stopped. |
| 4765 | * It is only freed when the kvm-hv module is unloaded. |
| 4766 | * It's OK for this routine to fail, we just don't support host |
| 4767 | * core operations like redirecting H_IPI wakeups. |
| 4768 | */ |
| 4769 | void kvmppc_alloc_host_rm_ops(void) |
| 4770 | { |
| 4771 | struct kvmppc_host_rm_ops *ops; |
| 4772 | unsigned long l_ops; |
| 4773 | int cpu, core; |
| 4774 | int size; |
| 4775 | |
| 4776 | /* Not the first time here ? */ |
| 4777 | if (kvmppc_host_rm_ops_hv != NULL) |
| 4778 | return; |
| 4779 | |
| 4780 | ops = kzalloc(sizeof(struct kvmppc_host_rm_ops), GFP_KERNEL); |
| 4781 | if (!ops) |
| 4782 | return; |
| 4783 | |
| 4784 | size = cpu_nr_cores() * sizeof(struct kvmppc_host_rm_core); |
| 4785 | ops->rm_core = kzalloc(size, GFP_KERNEL); |
| 4786 | |
| 4787 | if (!ops->rm_core) { |
| 4788 | kfree(ops); |
| 4789 | return; |
| 4790 | } |
| 4791 | |
Sebastian Andrzej Siewior | 419af25f | 2017-05-24 10:15:21 +0200 | [diff] [blame] | 4792 | cpus_read_lock(); |
Suresh Warrier | 6f3bb80 | 2015-12-17 14:59:08 -0600 | [diff] [blame] | 4793 | |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4794 | for (cpu = 0; cpu < nr_cpu_ids; cpu += threads_per_core) { |
| 4795 | if (!cpu_online(cpu)) |
| 4796 | continue; |
| 4797 | |
| 4798 | core = cpu >> threads_shift; |
| 4799 | ops->rm_core[core].rm_state.in_host = 1; |
| 4800 | } |
| 4801 | |
Suresh Warrier | 0c2a660 | 2015-12-17 14:59:09 -0600 | [diff] [blame] | 4802 | ops->vcpu_kick = kvmppc_fast_vcpu_kick_hv; |
| 4803 | |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4804 | /* |
| 4805 | * Make the contents of the kvmppc_host_rm_ops structure visible |
| 4806 | * to other CPUs before we assign it to the global variable. |
| 4807 | * Do an atomic assignment (no locks used here), but if someone |
| 4808 | * beats us to it, just free our copy and return. |
| 4809 | */ |
| 4810 | smp_wmb(); |
| 4811 | l_ops = (unsigned long) ops; |
| 4812 | |
| 4813 | 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] | 4814 | cpus_read_unlock(); |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4815 | kfree(ops->rm_core); |
| 4816 | kfree(ops); |
Suresh Warrier | 6f3bb80 | 2015-12-17 14:59:08 -0600 | [diff] [blame] | 4817 | return; |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4818 | } |
Suresh Warrier | 6f3bb80 | 2015-12-17 14:59:08 -0600 | [diff] [blame] | 4819 | |
Sebastian Andrzej Siewior | 419af25f | 2017-05-24 10:15:21 +0200 | [diff] [blame] | 4820 | cpuhp_setup_state_nocalls_cpuslocked(CPUHP_KVM_PPC_BOOK3S_PREPARE, |
| 4821 | "ppc/kvm_book3s:prepare", |
| 4822 | kvmppc_set_host_core, |
| 4823 | kvmppc_clear_host_core); |
| 4824 | cpus_read_unlock(); |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4825 | } |
| 4826 | |
| 4827 | void kvmppc_free_host_rm_ops(void) |
| 4828 | { |
| 4829 | if (kvmppc_host_rm_ops_hv) { |
Anna-Maria Gleixner | 3f7cd91 | 2016-11-27 00:13:45 +0100 | [diff] [blame] | 4830 | cpuhp_remove_state_nocalls(CPUHP_KVM_PPC_BOOK3S_PREPARE); |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4831 | kfree(kvmppc_host_rm_ops_hv->rm_core); |
| 4832 | kfree(kvmppc_host_rm_ops_hv); |
| 4833 | kvmppc_host_rm_ops_hv = NULL; |
| 4834 | } |
| 4835 | } |
| 4836 | #endif |
| 4837 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 4838 | static int kvmppc_core_init_vm_hv(struct kvm *kvm) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 4839 | { |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4840 | unsigned long lpcr, lpid; |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 4841 | char buf[32]; |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4842 | int ret; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 4843 | |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 4844 | mutex_init(&kvm->arch.uvmem_lock); |
| 4845 | INIT_LIST_HEAD(&kvm->arch.uvmem_pfns); |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 4846 | mutex_init(&kvm->arch.mmu_setup_lock); |
| 4847 | |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4848 | /* Allocate the guest's logical partition ID */ |
| 4849 | |
| 4850 | lpid = kvmppc_alloc_lpid(); |
Chen Gang | 5d226ae | 2013-07-22 14:32:35 +0800 | [diff] [blame] | 4851 | if ((long)lpid < 0) |
Paul Mackerras | 32fad28 | 2012-05-04 02:32:53 +0000 | [diff] [blame] | 4852 | return -ENOMEM; |
| 4853 | kvm->arch.lpid = lpid; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 4854 | |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 4855 | kvmppc_alloc_host_rm_ops(); |
| 4856 | |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 4857 | kvmhv_vm_nested_init(kvm); |
| 4858 | |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 4859 | /* |
| 4860 | * Since we don't flush the TLB when tearing down a VM, |
| 4861 | * and this lpid might have previously been used, |
| 4862 | * make sure we flush on each core before running the new VM. |
Paul Mackerras | 7c5b06c | 2016-11-18 08:28:51 +1100 | [diff] [blame] | 4863 | * On POWER9, the tlbie in mmu_partition_table_set_entry() |
| 4864 | * does this flush for us. |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 4865 | */ |
Paul Mackerras | 7c5b06c | 2016-11-18 08:28:51 +1100 | [diff] [blame] | 4866 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
| 4867 | cpumask_setall(&kvm->arch.need_tlb_flush); |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 4868 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 4869 | /* Start out with the default set of hcalls enabled */ |
| 4870 | memcpy(kvm->arch.enabled_hcalls, default_enabled_hcalls, |
| 4871 | sizeof(kvm->arch.enabled_hcalls)); |
| 4872 | |
Paul Mackerras | 7a84084 | 2016-11-16 22:25:20 +1100 | [diff] [blame] | 4873 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
| 4874 | kvm->arch.host_sdr1 = mfspr(SPRN_SDR1); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 4875 | |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 4876 | /* Init LPCR for virtual RMA mode */ |
Paul Mackerras | f3c99f9 | 2018-10-08 16:31:12 +1100 | [diff] [blame] | 4877 | if (cpu_has_feature(CPU_FTR_HVMODE)) { |
| 4878 | kvm->arch.host_lpid = mfspr(SPRN_LPID); |
| 4879 | kvm->arch.host_lpcr = lpcr = mfspr(SPRN_LPCR); |
| 4880 | lpcr &= LPCR_PECE | LPCR_LPES; |
| 4881 | } else { |
| 4882 | lpcr = 0; |
| 4883 | } |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 4884 | lpcr |= (4UL << LPCR_DPFD_SH) | LPCR_HDICE | |
| 4885 | LPCR_VPM0 | LPCR_VPM1; |
| 4886 | kvm->arch.vrma_slb_v = SLB_VSID_B_1T | |
| 4887 | (VRMA_VSID << SLB_VSID_SHIFT_1T); |
| 4888 | /* On POWER8 turn on online bit to enable PURR/SPURR */ |
| 4889 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 4890 | lpcr |= LPCR_ONL; |
Paul Mackerras | 84f7139 | 2016-11-22 14:30:14 +1100 | [diff] [blame] | 4891 | /* |
| 4892 | * On POWER9, VPM0 bit is reserved (VPM0=1 behaviour is assumed) |
| 4893 | * Set HVICE bit to enable hypervisor virtualization interrupts. |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 4894 | * Set HEIC to prevent OS interrupts to go to hypervisor (should |
| 4895 | * be unnecessary but better safe than sorry in case we re-enable |
| 4896 | * EE in HV mode with this LPCR still set) |
Paul Mackerras | 84f7139 | 2016-11-22 14:30:14 +1100 | [diff] [blame] | 4897 | */ |
| 4898 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
Paul Mackerras | 7a84084 | 2016-11-16 22:25:20 +1100 | [diff] [blame] | 4899 | lpcr &= ~LPCR_VPM0; |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 4900 | lpcr |= LPCR_HVICE | LPCR_HEIC; |
| 4901 | |
| 4902 | /* |
| 4903 | * If xive is enabled, we route 0x500 interrupts directly |
| 4904 | * to the guest. |
| 4905 | */ |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 4906 | if (xics_on_xive()) |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 4907 | lpcr |= LPCR_LPES; |
Paul Mackerras | 84f7139 | 2016-11-22 14:30:14 +1100 | [diff] [blame] | 4908 | } |
| 4909 | |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4910 | /* |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4911 | * If the host uses radix, the guest starts out as radix. |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4912 | */ |
| 4913 | if (radix_enabled()) { |
| 4914 | kvm->arch.radix = 1; |
Paul Mackerras | 1b151ce | 2017-09-13 15:53:48 +1000 | [diff] [blame] | 4915 | kvm->arch.mmu_ready = 1; |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4916 | lpcr &= ~LPCR_VPM1; |
| 4917 | lpcr |= LPCR_UPRT | LPCR_GTSE | LPCR_HR; |
| 4918 | ret = kvmppc_init_vm_radix(kvm); |
| 4919 | if (ret) { |
| 4920 | kvmppc_free_lpid(kvm->arch.lpid); |
| 4921 | return ret; |
| 4922 | } |
| 4923 | kvmppc_setup_partition_table(kvm); |
| 4924 | } |
| 4925 | |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 4926 | kvm->arch.lpcr = lpcr; |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 4927 | |
David Gibson | 5e98596 | 2016-12-20 16:49:05 +1100 | [diff] [blame] | 4928 | /* Initialization for future HPT resizes */ |
| 4929 | kvm->arch.resize_hpt = NULL; |
| 4930 | |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 4931 | /* |
Paul Mackerras | 7c5b06c | 2016-11-18 08:28:51 +1100 | [diff] [blame] | 4932 | * Work out how many sets the TLB has, for the use of |
| 4933 | * the TLB invalidation loop in book3s_hv_rmhandlers.S. |
| 4934 | */ |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 4935 | if (radix_enabled()) |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4936 | kvm->arch.tlb_sets = POWER9_TLB_SETS_RADIX; /* 128 */ |
| 4937 | else if (cpu_has_feature(CPU_FTR_ARCH_300)) |
Paul Mackerras | 7c5b06c | 2016-11-18 08:28:51 +1100 | [diff] [blame] | 4938 | kvm->arch.tlb_sets = POWER9_TLB_SETS_HASH; /* 256 */ |
| 4939 | else if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 4940 | kvm->arch.tlb_sets = POWER8_TLB_SETS; /* 512 */ |
| 4941 | else |
| 4942 | kvm->arch.tlb_sets = POWER7_TLB_SETS; /* 128 */ |
| 4943 | |
| 4944 | /* |
Michael Ellerman | 441c19c | 2014-05-23 18:15:25 +1000 | [diff] [blame] | 4945 | * Track that we now have a HV mode VM active. This blocks secondary |
| 4946 | * CPU threads from coming online. |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 4947 | * On POWER9, we only need to do this if the "indep_threads_mode" |
| 4948 | * module parameter has been set to N. |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 4949 | */ |
Paul Mackerras | 360cae3 | 2018-10-08 16:31:04 +1100 | [diff] [blame] | 4950 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
| 4951 | if (!indep_threads_mode && !cpu_has_feature(CPU_FTR_HVMODE)) { |
| 4952 | pr_warn("KVM: Ignoring indep_threads_mode=N in nested hypervisor\n"); |
| 4953 | kvm->arch.threads_indep = true; |
| 4954 | } else { |
| 4955 | kvm->arch.threads_indep = indep_threads_mode; |
| 4956 | } |
| 4957 | } |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 4958 | if (!kvm->arch.threads_indep) |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 4959 | kvm_hv_vm_activated(); |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 4960 | |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 4961 | /* |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 4962 | * Initialize smt_mode depending on processor. |
| 4963 | * POWER8 and earlier have to use "strict" threading, where |
| 4964 | * all vCPUs in a vcore have to run on the same (sub)core, |
| 4965 | * whereas on POWER9 the threads can each run a different |
| 4966 | * guest. |
| 4967 | */ |
| 4968 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
| 4969 | kvm->arch.smt_mode = threads_per_subcore; |
| 4970 | else |
| 4971 | kvm->arch.smt_mode = 1; |
Paul Mackerras | 5790069 | 2017-05-16 16:41:20 +1000 | [diff] [blame] | 4972 | kvm->arch.emul_smt_mode = 1; |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 4973 | |
| 4974 | /* |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 4975 | * Create a debugfs directory for the VM |
| 4976 | */ |
| 4977 | snprintf(buf, sizeof(buf), "vm%d", current->pid); |
| 4978 | kvm->arch.debugfs_dir = debugfs_create_dir(buf, kvm_debugfs_dir); |
Greg Kroah-Hartman | 929f45e | 2018-05-29 18:22:04 +0200 | [diff] [blame] | 4979 | kvmppc_mmu_debugfs_init(kvm); |
Paul Mackerras | 9a94d3e | 2018-10-08 16:30:57 +1100 | [diff] [blame] | 4980 | if (radix_enabled()) |
| 4981 | kvmhv_radix_debugfs_init(kvm); |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 4982 | |
David Gibson | 54738c0 | 2011-06-29 00:22:41 +0000 | [diff] [blame] | 4983 | return 0; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 4984 | } |
| 4985 | |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 4986 | static void kvmppc_free_vcores(struct kvm *kvm) |
| 4987 | { |
| 4988 | long int i; |
| 4989 | |
Paul Mackerras | 2331631 | 2015-10-21 16:03:14 +1100 | [diff] [blame] | 4990 | for (i = 0; i < KVM_MAX_VCORES; ++i) |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 4991 | kfree(kvm->arch.vcores[i]); |
| 4992 | kvm->arch.online_vcores = 0; |
| 4993 | } |
| 4994 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 4995 | static void kvmppc_core_destroy_vm_hv(struct kvm *kvm) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 4996 | { |
Paul Mackerras | e23a808 | 2015-03-28 14:21:01 +1100 | [diff] [blame] | 4997 | debugfs_remove_recursive(kvm->arch.debugfs_dir); |
| 4998 | |
Paul Mackerras | 516f789 | 2017-10-16 16:11:57 +1100 | [diff] [blame] | 4999 | if (!kvm->arch.threads_indep) |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 5000 | kvm_hv_vm_deactivated(); |
Paul Mackerras | 512691d | 2012-10-15 01:15:41 +0000 | [diff] [blame] | 5001 | |
Paul Mackerras | f1378b1 | 2013-09-27 15:33:43 +0530 | [diff] [blame] | 5002 | kvmppc_free_vcores(kvm); |
Paul Mackerras | aa04b4c | 2011-06-29 00:25:44 +0000 | [diff] [blame] | 5003 | |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 5004 | |
Paul Mackerras | 5a31935 | 2017-01-30 21:21:46 +1100 | [diff] [blame] | 5005 | if (kvm_is_radix(kvm)) |
| 5006 | kvmppc_free_radix(kvm); |
| 5007 | else |
David Gibson | aae0777 | 2016-12-20 16:49:02 +1100 | [diff] [blame] | 5008 | kvmppc_free_hpt(&kvm->arch.hpt); |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5009 | |
Suraj Jitindar Singh | 89329c0 | 2018-10-08 16:30:59 +1100 | [diff] [blame] | 5010 | /* Perform global invalidation and return lpid to the pool */ |
| 5011 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 5012 | if (nesting_enabled(kvm)) |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 5013 | kvmhv_release_all_nested(kvm); |
Suraj Jitindar Singh | 89329c0 | 2018-10-08 16:30:59 +1100 | [diff] [blame] | 5014 | kvm->arch.process_table = 0; |
Paul Mackerras | d89c69f | 2019-12-18 11:43:06 +1100 | [diff] [blame] | 5015 | if (kvm->arch.secure_guest) |
| 5016 | uv_svm_terminate(kvm->arch.lpid); |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 5017 | kvmhv_set_ptbl_entry(kvm->arch.lpid, 0, 0); |
Suraj Jitindar Singh | 89329c0 | 2018-10-08 16:30:59 +1100 | [diff] [blame] | 5018 | } |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 5019 | |
Suraj Jitindar Singh | 89329c0 | 2018-10-08 16:30:59 +1100 | [diff] [blame] | 5020 | kvmppc_free_lpid(kvm->arch.lpid); |
| 5021 | |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5022 | kvmppc_free_pimap(kvm); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5023 | } |
| 5024 | |
| 5025 | /* We don't need to emulate any privileged instructions or dcbz */ |
Tianjia Zhang | 8c99d34 | 2020-04-27 12:35:11 +0800 | [diff] [blame] | 5026 | static int kvmppc_core_emulate_op_hv(struct kvm_vcpu *vcpu, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5027 | unsigned int inst, int *advance) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5028 | { |
| 5029 | return EMULATE_FAIL; |
| 5030 | } |
| 5031 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5032 | static int kvmppc_core_emulate_mtspr_hv(struct kvm_vcpu *vcpu, int sprn, |
| 5033 | ulong spr_val) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5034 | { |
| 5035 | return EMULATE_FAIL; |
| 5036 | } |
| 5037 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5038 | static int kvmppc_core_emulate_mfspr_hv(struct kvm_vcpu *vcpu, int sprn, |
| 5039 | ulong *spr_val) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5040 | { |
| 5041 | return EMULATE_FAIL; |
| 5042 | } |
| 5043 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5044 | static int kvmppc_core_check_processor_compat_hv(void) |
| 5045 | { |
Paul Mackerras | de760db | 2018-10-08 16:31:16 +1100 | [diff] [blame] | 5046 | if (cpu_has_feature(CPU_FTR_HVMODE) && |
| 5047 | cpu_has_feature(CPU_FTR_ARCH_206)) |
| 5048 | return 0; |
Aneesh Kumar K.V | 50de596 | 2016-04-29 23:25:43 +1000 | [diff] [blame] | 5049 | |
Paul Mackerras | de760db | 2018-10-08 16:31:16 +1100 | [diff] [blame] | 5050 | /* POWER9 in radix mode is capable of being a nested hypervisor. */ |
| 5051 | if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled()) |
| 5052 | return 0; |
| 5053 | |
| 5054 | return -EIO; |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5055 | } |
| 5056 | |
Suresh Warrier | 8daaafc | 2016-08-19 15:35:48 +1000 | [diff] [blame] | 5057 | #ifdef CONFIG_KVM_XICS |
| 5058 | |
| 5059 | void kvmppc_free_pimap(struct kvm *kvm) |
| 5060 | { |
| 5061 | kfree(kvm->arch.pimap); |
| 5062 | } |
| 5063 | |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5064 | static struct kvmppc_passthru_irqmap *kvmppc_alloc_pimap(void) |
Suresh Warrier | 8daaafc | 2016-08-19 15:35:48 +1000 | [diff] [blame] | 5065 | { |
| 5066 | return kzalloc(sizeof(struct kvmppc_passthru_irqmap), GFP_KERNEL); |
| 5067 | } |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5068 | |
| 5069 | static int kvmppc_set_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi) |
| 5070 | { |
| 5071 | struct irq_desc *desc; |
| 5072 | struct kvmppc_irq_map *irq_map; |
| 5073 | struct kvmppc_passthru_irqmap *pimap; |
| 5074 | struct irq_chip *chip; |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 5075 | int i, rc = 0; |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5076 | |
Suresh Warrier | 644abbb | 2016-08-19 15:35:54 +1000 | [diff] [blame] | 5077 | if (!kvm_irq_bypass) |
| 5078 | return 1; |
| 5079 | |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5080 | desc = irq_to_desc(host_irq); |
| 5081 | if (!desc) |
| 5082 | return -EIO; |
| 5083 | |
| 5084 | mutex_lock(&kvm->lock); |
| 5085 | |
| 5086 | pimap = kvm->arch.pimap; |
| 5087 | if (pimap == NULL) { |
| 5088 | /* First call, allocate structure to hold IRQ map */ |
| 5089 | pimap = kvmppc_alloc_pimap(); |
| 5090 | if (pimap == NULL) { |
| 5091 | mutex_unlock(&kvm->lock); |
| 5092 | return -ENOMEM; |
| 5093 | } |
| 5094 | kvm->arch.pimap = pimap; |
| 5095 | } |
| 5096 | |
| 5097 | /* |
| 5098 | * For now, we only support interrupts for which the EOI operation |
| 5099 | * 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] | 5100 | * what our real-mode EOI code does, or a XIVE interrupt |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5101 | */ |
| 5102 | chip = irq_data_get_irq_chip(&desc->irq_data); |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 5103 | if (!chip || !(is_pnv_opal_msi(chip) || is_xive_irq(chip))) { |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5104 | pr_warn("kvmppc_set_passthru_irq_hv: Could not assign IRQ map for (%d,%d)\n", |
| 5105 | host_irq, guest_gsi); |
| 5106 | mutex_unlock(&kvm->lock); |
| 5107 | return -ENOENT; |
| 5108 | } |
| 5109 | |
| 5110 | /* |
| 5111 | * See if we already have an entry for this guest IRQ number. |
| 5112 | * If it's mapped to a hardware IRQ number, that's an error, |
| 5113 | * otherwise re-use this entry. |
| 5114 | */ |
| 5115 | for (i = 0; i < pimap->n_mapped; i++) { |
| 5116 | if (guest_gsi == pimap->mapped[i].v_hwirq) { |
| 5117 | if (pimap->mapped[i].r_hwirq) { |
| 5118 | mutex_unlock(&kvm->lock); |
| 5119 | return -EINVAL; |
| 5120 | } |
| 5121 | break; |
| 5122 | } |
| 5123 | } |
| 5124 | |
| 5125 | if (i == KVMPPC_PIRQ_MAPPED) { |
| 5126 | mutex_unlock(&kvm->lock); |
| 5127 | return -EAGAIN; /* table is full */ |
| 5128 | } |
| 5129 | |
| 5130 | irq_map = &pimap->mapped[i]; |
| 5131 | |
| 5132 | irq_map->v_hwirq = guest_gsi; |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5133 | irq_map->desc = desc; |
| 5134 | |
Suresh Warrier | e3c13e5 | 2016-08-19 15:35:51 +1000 | [diff] [blame] | 5135 | /* |
| 5136 | * Order the above two stores before the next to serialize with |
| 5137 | * the KVM real mode handler. |
| 5138 | */ |
| 5139 | smp_wmb(); |
| 5140 | irq_map->r_hwirq = desc->irq_data.hwirq; |
| 5141 | |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5142 | if (i == pimap->n_mapped) |
| 5143 | pimap->n_mapped++; |
| 5144 | |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 5145 | if (xics_on_xive()) |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 5146 | rc = kvmppc_xive_set_mapped(kvm, guest_gsi, desc); |
| 5147 | else |
| 5148 | kvmppc_xics_set_mapped(kvm, guest_gsi, desc->irq_data.hwirq); |
| 5149 | if (rc) |
| 5150 | irq_map->r_hwirq = 0; |
Paul Mackerras | 5d37519 | 2016-08-19 15:35:56 +1000 | [diff] [blame] | 5151 | |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5152 | mutex_unlock(&kvm->lock); |
| 5153 | |
| 5154 | return 0; |
| 5155 | } |
| 5156 | |
| 5157 | static int kvmppc_clr_passthru_irq(struct kvm *kvm, int host_irq, int guest_gsi) |
| 5158 | { |
| 5159 | struct irq_desc *desc; |
| 5160 | struct kvmppc_passthru_irqmap *pimap; |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 5161 | int i, rc = 0; |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5162 | |
Suresh Warrier | 644abbb | 2016-08-19 15:35:54 +1000 | [diff] [blame] | 5163 | if (!kvm_irq_bypass) |
| 5164 | return 0; |
| 5165 | |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5166 | desc = irq_to_desc(host_irq); |
| 5167 | if (!desc) |
| 5168 | return -EIO; |
| 5169 | |
| 5170 | mutex_lock(&kvm->lock); |
Markus Elfring | a1c52e1 | 2017-01-20 11:00:08 +0100 | [diff] [blame] | 5171 | if (!kvm->arch.pimap) |
| 5172 | goto unlock; |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5173 | |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5174 | pimap = kvm->arch.pimap; |
| 5175 | |
| 5176 | for (i = 0; i < pimap->n_mapped; i++) { |
| 5177 | if (guest_gsi == pimap->mapped[i].v_hwirq) |
| 5178 | break; |
| 5179 | } |
| 5180 | |
| 5181 | if (i == pimap->n_mapped) { |
| 5182 | mutex_unlock(&kvm->lock); |
| 5183 | return -ENODEV; |
| 5184 | } |
| 5185 | |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 5186 | if (xics_on_xive()) |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 5187 | rc = kvmppc_xive_clr_mapped(kvm, guest_gsi, pimap->mapped[i].desc); |
| 5188 | else |
| 5189 | kvmppc_xics_clr_mapped(kvm, guest_gsi, pimap->mapped[i].r_hwirq); |
Paul Mackerras | 5d37519 | 2016-08-19 15:35:56 +1000 | [diff] [blame] | 5190 | |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 5191 | /* invalidate the entry (what do do on error from the above ?) */ |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5192 | pimap->mapped[i].r_hwirq = 0; |
| 5193 | |
| 5194 | /* |
| 5195 | * We don't free this structure even when the count goes to |
| 5196 | * zero. The structure is freed when we destroy the VM. |
| 5197 | */ |
Markus Elfring | a1c52e1 | 2017-01-20 11:00:08 +0100 | [diff] [blame] | 5198 | unlock: |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5199 | mutex_unlock(&kvm->lock); |
Benjamin Herrenschmidt | 5af5099 | 2017-04-05 17:54:56 +1000 | [diff] [blame] | 5200 | return rc; |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5201 | } |
| 5202 | |
| 5203 | static int kvmppc_irq_bypass_add_producer_hv(struct irq_bypass_consumer *cons, |
| 5204 | struct irq_bypass_producer *prod) |
| 5205 | { |
| 5206 | int ret = 0; |
| 5207 | struct kvm_kernel_irqfd *irqfd = |
| 5208 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 5209 | |
| 5210 | irqfd->producer = prod; |
| 5211 | |
| 5212 | ret = kvmppc_set_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi); |
| 5213 | if (ret) |
| 5214 | pr_info("kvmppc_set_passthru_irq (irq %d, gsi %d) fails: %d\n", |
| 5215 | prod->irq, irqfd->gsi, ret); |
| 5216 | |
| 5217 | return ret; |
| 5218 | } |
| 5219 | |
| 5220 | static void kvmppc_irq_bypass_del_producer_hv(struct irq_bypass_consumer *cons, |
| 5221 | struct irq_bypass_producer *prod) |
| 5222 | { |
| 5223 | int ret; |
| 5224 | struct kvm_kernel_irqfd *irqfd = |
| 5225 | container_of(cons, struct kvm_kernel_irqfd, consumer); |
| 5226 | |
| 5227 | irqfd->producer = NULL; |
| 5228 | |
| 5229 | /* |
| 5230 | * When producer of consumer is unregistered, we change back to |
| 5231 | * default external interrupt handling mode - KVM real mode |
| 5232 | * will switch back to host. |
| 5233 | */ |
| 5234 | ret = kvmppc_clr_passthru_irq(irqfd->kvm, prod->irq, irqfd->gsi); |
| 5235 | if (ret) |
| 5236 | pr_warn("kvmppc_clr_passthru_irq (irq %d, gsi %d) fails: %d\n", |
| 5237 | prod->irq, irqfd->gsi, ret); |
| 5238 | } |
Suresh Warrier | 8daaafc | 2016-08-19 15:35:48 +1000 | [diff] [blame] | 5239 | #endif |
| 5240 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5241 | static long kvm_arch_vm_ioctl_hv(struct file *filp, |
| 5242 | unsigned int ioctl, unsigned long arg) |
| 5243 | { |
| 5244 | struct kvm *kvm __maybe_unused = filp->private_data; |
| 5245 | void __user *argp = (void __user *)arg; |
| 5246 | long r; |
| 5247 | |
| 5248 | switch (ioctl) { |
| 5249 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5250 | case KVM_PPC_ALLOCATE_HTAB: { |
| 5251 | u32 htab_order; |
| 5252 | |
Fabiano Rosas | 05e6295 | 2020-09-11 01:16:07 -0300 | [diff] [blame^] | 5253 | /* If we're a nested hypervisor, we currently only support radix */ |
| 5254 | if (kvmhv_on_pseries()) { |
| 5255 | r = -EOPNOTSUPP; |
| 5256 | break; |
| 5257 | } |
| 5258 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5259 | r = -EFAULT; |
| 5260 | if (get_user(htab_order, (u32 __user *)argp)) |
| 5261 | break; |
David Gibson | f98a8bf | 2016-12-20 16:49:03 +1100 | [diff] [blame] | 5262 | r = kvmppc_alloc_reset_hpt(kvm, htab_order); |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5263 | if (r) |
| 5264 | break; |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5265 | r = 0; |
| 5266 | break; |
| 5267 | } |
| 5268 | |
| 5269 | case KVM_PPC_GET_HTAB_FD: { |
| 5270 | struct kvm_get_htab_fd ghf; |
| 5271 | |
| 5272 | r = -EFAULT; |
| 5273 | if (copy_from_user(&ghf, argp, sizeof(ghf))) |
| 5274 | break; |
| 5275 | r = kvm_vm_ioctl_get_htab_fd(kvm, &ghf); |
| 5276 | break; |
| 5277 | } |
| 5278 | |
David Gibson | 5e98596 | 2016-12-20 16:49:05 +1100 | [diff] [blame] | 5279 | case KVM_PPC_RESIZE_HPT_PREPARE: { |
| 5280 | struct kvm_ppc_resize_hpt rhpt; |
| 5281 | |
| 5282 | r = -EFAULT; |
| 5283 | if (copy_from_user(&rhpt, argp, sizeof(rhpt))) |
| 5284 | break; |
| 5285 | |
| 5286 | r = kvm_vm_ioctl_resize_hpt_prepare(kvm, &rhpt); |
| 5287 | break; |
| 5288 | } |
| 5289 | |
| 5290 | case KVM_PPC_RESIZE_HPT_COMMIT: { |
| 5291 | struct kvm_ppc_resize_hpt rhpt; |
| 5292 | |
| 5293 | r = -EFAULT; |
| 5294 | if (copy_from_user(&rhpt, argp, sizeof(rhpt))) |
| 5295 | break; |
| 5296 | |
| 5297 | r = kvm_vm_ioctl_resize_hpt_commit(kvm, &rhpt); |
| 5298 | break; |
| 5299 | } |
| 5300 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5301 | default: |
| 5302 | r = -ENOTTY; |
| 5303 | } |
| 5304 | |
| 5305 | return r; |
| 5306 | } |
| 5307 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 5308 | /* |
| 5309 | * List of hcall numbers to enable by default. |
| 5310 | * For compatibility with old userspace, we enable by default |
| 5311 | * all hcalls that were implemented before the hcall-enabling |
| 5312 | * facility was added. Note this list should not include H_RTAS. |
| 5313 | */ |
| 5314 | static unsigned int default_hcall_list[] = { |
| 5315 | H_REMOVE, |
| 5316 | H_ENTER, |
| 5317 | H_READ, |
| 5318 | H_PROTECT, |
| 5319 | H_BULK_REMOVE, |
| 5320 | H_GET_TCE, |
| 5321 | H_PUT_TCE, |
| 5322 | H_SET_DABR, |
| 5323 | H_SET_XDABR, |
| 5324 | H_CEDE, |
| 5325 | H_PROD, |
| 5326 | H_CONFER, |
| 5327 | H_REGISTER_VPA, |
| 5328 | #ifdef CONFIG_KVM_XICS |
| 5329 | H_EOI, |
| 5330 | H_CPPR, |
| 5331 | H_IPI, |
| 5332 | H_IPOLL, |
| 5333 | H_XIRR, |
| 5334 | H_XIRR_X, |
| 5335 | #endif |
| 5336 | 0 |
| 5337 | }; |
| 5338 | |
| 5339 | static void init_default_hcalls(void) |
| 5340 | { |
| 5341 | int i; |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 5342 | unsigned int hcall; |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 5343 | |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 5344 | for (i = 0; default_hcall_list[i]; ++i) { |
| 5345 | hcall = default_hcall_list[i]; |
| 5346 | WARN_ON(!kvmppc_hcall_impl_hv(hcall)); |
| 5347 | __set_bit(hcall / 4, default_enabled_hcalls); |
| 5348 | } |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 5349 | } |
| 5350 | |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 5351 | static int kvmhv_configure_mmu(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg) |
| 5352 | { |
Paul Mackerras | 468808b | 2017-01-30 21:21:42 +1100 | [diff] [blame] | 5353 | unsigned long lpcr; |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 5354 | int radix; |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 5355 | int err; |
Paul Mackerras | 468808b | 2017-01-30 21:21:42 +1100 | [diff] [blame] | 5356 | |
| 5357 | /* If not on a POWER9, reject it */ |
| 5358 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
| 5359 | return -ENODEV; |
| 5360 | |
| 5361 | /* If any unknown flags set, reject it */ |
| 5362 | if (cfg->flags & ~(KVM_PPC_MMUV3_RADIX | KVM_PPC_MMUV3_GTSE)) |
| 5363 | return -EINVAL; |
| 5364 | |
Paul Mackerras | 468808b | 2017-01-30 21:21:42 +1100 | [diff] [blame] | 5365 | /* GR (guest radix) bit in process_table field must match */ |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 5366 | radix = !!(cfg->flags & KVM_PPC_MMUV3_RADIX); |
Paul Mackerras | 8cf4ecc | 2017-01-30 21:21:53 +1100 | [diff] [blame] | 5367 | if (!!(cfg->process_table & PATB_GR) != radix) |
Paul Mackerras | 468808b | 2017-01-30 21:21:42 +1100 | [diff] [blame] | 5368 | return -EINVAL; |
| 5369 | |
| 5370 | /* Process table size field must be reasonable, i.e. <= 24 */ |
| 5371 | if ((cfg->process_table & PRTS_MASK) > 24) |
| 5372 | return -EINVAL; |
| 5373 | |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 5374 | /* We can change a guest to/from radix now, if the host is radix */ |
| 5375 | if (radix && !radix_enabled()) |
| 5376 | return -EINVAL; |
| 5377 | |
Paul Mackerras | de760db | 2018-10-08 16:31:16 +1100 | [diff] [blame] | 5378 | /* If we're a nested hypervisor, we currently only support radix */ |
| 5379 | if (kvmhv_on_pseries() && !radix) |
| 5380 | return -EINVAL; |
| 5381 | |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 5382 | mutex_lock(&kvm->arch.mmu_setup_lock); |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 5383 | if (radix != kvm_is_radix(kvm)) { |
| 5384 | if (kvm->arch.mmu_ready) { |
| 5385 | kvm->arch.mmu_ready = 0; |
| 5386 | /* order mmu_ready vs. vcpus_running */ |
| 5387 | smp_mb(); |
| 5388 | if (atomic_read(&kvm->arch.vcpus_running)) { |
| 5389 | kvm->arch.mmu_ready = 1; |
| 5390 | err = -EBUSY; |
| 5391 | goto out_unlock; |
| 5392 | } |
| 5393 | } |
| 5394 | if (radix) |
| 5395 | err = kvmppc_switch_mmu_to_radix(kvm); |
| 5396 | else |
| 5397 | err = kvmppc_switch_mmu_to_hpt(kvm); |
| 5398 | if (err) |
| 5399 | goto out_unlock; |
| 5400 | } |
| 5401 | |
Paul Mackerras | 468808b | 2017-01-30 21:21:42 +1100 | [diff] [blame] | 5402 | kvm->arch.process_table = cfg->process_table; |
| 5403 | kvmppc_setup_partition_table(kvm); |
| 5404 | |
| 5405 | lpcr = (cfg->flags & KVM_PPC_MMUV3_GTSE) ? LPCR_GTSE : 0; |
| 5406 | kvmppc_update_lpcr(kvm, lpcr, LPCR_GTSE); |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 5407 | err = 0; |
Paul Mackerras | 468808b | 2017-01-30 21:21:42 +1100 | [diff] [blame] | 5408 | |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 5409 | out_unlock: |
Paul Mackerras | 0d4ee88 | 2019-05-23 16:35:34 +1000 | [diff] [blame] | 5410 | mutex_unlock(&kvm->arch.mmu_setup_lock); |
Paul Mackerras | 18c3640 | 2017-09-13 16:00:10 +1000 | [diff] [blame] | 5411 | return err; |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 5412 | } |
| 5413 | |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 5414 | static int kvmhv_enable_nested(struct kvm *kvm) |
| 5415 | { |
| 5416 | if (!nested) |
| 5417 | return -EPERM; |
Paul Mackerras | 8d9fcac | 2018-10-19 20:44:04 +1100 | [diff] [blame] | 5418 | if (!cpu_has_feature(CPU_FTR_ARCH_300) || no_mixing_hpt_and_radix) |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 5419 | return -ENODEV; |
| 5420 | |
| 5421 | /* kvm == NULL means the caller is testing if the capability exists */ |
| 5422 | if (kvm) |
| 5423 | kvm->arch.nested_enable = true; |
| 5424 | return 0; |
| 5425 | } |
| 5426 | |
Suraj Jitindar Singh | dceadcf | 2018-12-14 16:29:06 +1100 | [diff] [blame] | 5427 | static int kvmhv_load_from_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr, |
| 5428 | int size) |
| 5429 | { |
| 5430 | int rc = -EINVAL; |
| 5431 | |
| 5432 | if (kvmhv_vcpu_is_radix(vcpu)) { |
| 5433 | rc = kvmhv_copy_from_guest_radix(vcpu, *eaddr, ptr, size); |
| 5434 | |
| 5435 | if (rc > 0) |
| 5436 | rc = -EINVAL; |
| 5437 | } |
| 5438 | |
| 5439 | /* For now quadrants are the only way to access nested guest memory */ |
| 5440 | if (rc && vcpu->arch.nested) |
| 5441 | rc = -EAGAIN; |
| 5442 | |
| 5443 | return rc; |
| 5444 | } |
| 5445 | |
| 5446 | static int kvmhv_store_to_eaddr(struct kvm_vcpu *vcpu, ulong *eaddr, void *ptr, |
| 5447 | int size) |
| 5448 | { |
| 5449 | int rc = -EINVAL; |
| 5450 | |
| 5451 | if (kvmhv_vcpu_is_radix(vcpu)) { |
| 5452 | rc = kvmhv_copy_to_guest_radix(vcpu, *eaddr, ptr, size); |
| 5453 | |
| 5454 | if (rc > 0) |
| 5455 | rc = -EINVAL; |
| 5456 | } |
| 5457 | |
| 5458 | /* For now quadrants are the only way to access nested guest memory */ |
| 5459 | if (rc && vcpu->arch.nested) |
| 5460 | rc = -EAGAIN; |
| 5461 | |
| 5462 | return rc; |
| 5463 | } |
| 5464 | |
Bharata B Rao | 2294568 | 2019-11-25 08:36:30 +0530 | [diff] [blame] | 5465 | static void unpin_vpa_reset(struct kvm *kvm, struct kvmppc_vpa *vpa) |
| 5466 | { |
| 5467 | unpin_vpa(kvm, vpa); |
| 5468 | vpa->gpa = 0; |
| 5469 | vpa->pinned_addr = NULL; |
| 5470 | vpa->dirty = false; |
| 5471 | vpa->update_pending = 0; |
| 5472 | } |
| 5473 | |
| 5474 | /* |
Paul Mackerras | 9a5788c | 2020-03-19 15:29:55 +1100 | [diff] [blame] | 5475 | * Enable a guest to become a secure VM, or test whether |
| 5476 | * that could be enabled. |
| 5477 | * Called when the KVM_CAP_PPC_SECURE_GUEST capability is |
| 5478 | * tested (kvm == NULL) or enabled (kvm != NULL). |
| 5479 | */ |
| 5480 | static int kvmhv_enable_svm(struct kvm *kvm) |
| 5481 | { |
| 5482 | if (!kvmppc_uvmem_available()) |
| 5483 | return -EINVAL; |
| 5484 | if (kvm) |
| 5485 | kvm->arch.svm_enabled = 1; |
| 5486 | return 0; |
| 5487 | } |
| 5488 | |
| 5489 | /* |
Bharata B Rao | 2294568 | 2019-11-25 08:36:30 +0530 | [diff] [blame] | 5490 | * IOCTL handler to turn off secure mode of guest |
| 5491 | * |
| 5492 | * - Release all device pages |
| 5493 | * - Issue ucall to terminate the guest on the UV side |
| 5494 | * - Unpin the VPA pages. |
| 5495 | * - Reinit the partition scoped page tables |
| 5496 | */ |
| 5497 | static int kvmhv_svm_off(struct kvm *kvm) |
| 5498 | { |
| 5499 | struct kvm_vcpu *vcpu; |
| 5500 | int mmu_was_ready; |
| 5501 | int srcu_idx; |
| 5502 | int ret = 0; |
| 5503 | int i; |
| 5504 | |
| 5505 | if (!(kvm->arch.secure_guest & KVMPPC_SECURE_INIT_START)) |
| 5506 | return ret; |
| 5507 | |
| 5508 | mutex_lock(&kvm->arch.mmu_setup_lock); |
| 5509 | mmu_was_ready = kvm->arch.mmu_ready; |
| 5510 | if (kvm->arch.mmu_ready) { |
| 5511 | kvm->arch.mmu_ready = 0; |
| 5512 | /* order mmu_ready vs. vcpus_running */ |
| 5513 | smp_mb(); |
| 5514 | if (atomic_read(&kvm->arch.vcpus_running)) { |
| 5515 | kvm->arch.mmu_ready = 1; |
| 5516 | ret = -EBUSY; |
| 5517 | goto out; |
| 5518 | } |
| 5519 | } |
| 5520 | |
| 5521 | srcu_idx = srcu_read_lock(&kvm->srcu); |
| 5522 | for (i = 0; i < KVM_ADDRESS_SPACE_NUM; i++) { |
| 5523 | struct kvm_memory_slot *memslot; |
| 5524 | struct kvm_memslots *slots = __kvm_memslots(kvm, i); |
| 5525 | |
| 5526 | if (!slots) |
| 5527 | continue; |
| 5528 | |
| 5529 | kvm_for_each_memslot(memslot, slots) { |
Sukadev Bhattiprolu | ce477a7 | 2019-12-19 13:51:45 -0800 | [diff] [blame] | 5530 | kvmppc_uvmem_drop_pages(memslot, kvm, true); |
Bharata B Rao | 2294568 | 2019-11-25 08:36:30 +0530 | [diff] [blame] | 5531 | uv_unregister_mem_slot(kvm->arch.lpid, memslot->id); |
| 5532 | } |
| 5533 | } |
| 5534 | srcu_read_unlock(&kvm->srcu, srcu_idx); |
| 5535 | |
| 5536 | ret = uv_svm_terminate(kvm->arch.lpid); |
| 5537 | if (ret != U_SUCCESS) { |
| 5538 | ret = -EINVAL; |
| 5539 | goto out; |
| 5540 | } |
| 5541 | |
| 5542 | /* |
| 5543 | * When secure guest is reset, all the guest pages are sent |
| 5544 | * to UV via UV_PAGE_IN before the non-boot vcpus get a |
| 5545 | * chance to run and unpin their VPA pages. Unpinning of all |
| 5546 | * VPA pages is done here explicitly so that VPA pages |
| 5547 | * can be migrated to the secure side. |
| 5548 | * |
| 5549 | * This is required to for the secure SMP guest to reboot |
| 5550 | * correctly. |
| 5551 | */ |
| 5552 | kvm_for_each_vcpu(i, vcpu, kvm) { |
| 5553 | spin_lock(&vcpu->arch.vpa_update_lock); |
| 5554 | unpin_vpa_reset(kvm, &vcpu->arch.dtl); |
| 5555 | unpin_vpa_reset(kvm, &vcpu->arch.slb_shadow); |
| 5556 | unpin_vpa_reset(kvm, &vcpu->arch.vpa); |
| 5557 | spin_unlock(&vcpu->arch.vpa_update_lock); |
| 5558 | } |
| 5559 | |
| 5560 | kvmppc_setup_partition_table(kvm); |
| 5561 | kvm->arch.secure_guest = 0; |
| 5562 | kvm->arch.mmu_ready = mmu_was_ready; |
| 5563 | out: |
| 5564 | mutex_unlock(&kvm->arch.mmu_setup_lock); |
| 5565 | return ret; |
| 5566 | } |
| 5567 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 5568 | static struct kvmppc_ops kvm_ops_hv = { |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5569 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_hv, |
| 5570 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_hv, |
| 5571 | .get_one_reg = kvmppc_get_one_reg_hv, |
| 5572 | .set_one_reg = kvmppc_set_one_reg_hv, |
| 5573 | .vcpu_load = kvmppc_core_vcpu_load_hv, |
| 5574 | .vcpu_put = kvmppc_core_vcpu_put_hv, |
Nicholas Piggin | 87a45e0 | 2019-10-02 16:00:22 +1000 | [diff] [blame] | 5575 | .inject_interrupt = kvmppc_inject_interrupt_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5576 | .set_msr = kvmppc_set_msr_hv, |
| 5577 | .vcpu_run = kvmppc_vcpu_run_hv, |
| 5578 | .vcpu_create = kvmppc_core_vcpu_create_hv, |
| 5579 | .vcpu_free = kvmppc_core_vcpu_free_hv, |
| 5580 | .check_requests = kvmppc_core_check_requests_hv, |
| 5581 | .get_dirty_log = kvm_vm_ioctl_get_dirty_log_hv, |
| 5582 | .flush_memslot = kvmppc_core_flush_memslot_hv, |
| 5583 | .prepare_memory_region = kvmppc_core_prepare_memory_region_hv, |
| 5584 | .commit_memory_region = kvmppc_core_commit_memory_region_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5585 | .unmap_hva_range = kvm_unmap_hva_range_hv, |
| 5586 | .age_hva = kvm_age_hva_hv, |
| 5587 | .test_age_hva = kvm_test_age_hva_hv, |
| 5588 | .set_spte_hva = kvm_set_spte_hva_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5589 | .free_memslot = kvmppc_core_free_memslot_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5590 | .init_vm = kvmppc_core_init_vm_hv, |
| 5591 | .destroy_vm = kvmppc_core_destroy_vm_hv, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5592 | .get_smmu_info = kvm_vm_ioctl_get_smmu_info_hv, |
| 5593 | .emulate_op = kvmppc_core_emulate_op_hv, |
| 5594 | .emulate_mtspr = kvmppc_core_emulate_mtspr_hv, |
| 5595 | .emulate_mfspr = kvmppc_core_emulate_mfspr_hv, |
| 5596 | .fast_vcpu_kick = kvmppc_fast_vcpu_kick_hv, |
| 5597 | .arch_vm_ioctl = kvm_arch_vm_ioctl_hv, |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 5598 | .hcall_implemented = kvmppc_hcall_impl_hv, |
Suresh Warrier | c57875f | 2016-08-19 15:35:50 +1000 | [diff] [blame] | 5599 | #ifdef CONFIG_KVM_XICS |
| 5600 | .irq_bypass_add_producer = kvmppc_irq_bypass_add_producer_hv, |
| 5601 | .irq_bypass_del_producer = kvmppc_irq_bypass_del_producer_hv, |
| 5602 | #endif |
Paul Mackerras | c927013 | 2017-01-30 21:21:41 +1100 | [diff] [blame] | 5603 | .configure_mmu = kvmhv_configure_mmu, |
| 5604 | .get_rmmu_info = kvmhv_get_rmmu_info, |
Paul Mackerras | 3c31352 | 2017-02-06 13:24:41 +1100 | [diff] [blame] | 5605 | .set_smt_mode = kvmhv_set_smt_mode, |
Paul Mackerras | aa069a9 | 2018-09-21 20:02:01 +1000 | [diff] [blame] | 5606 | .enable_nested = kvmhv_enable_nested, |
Suraj Jitindar Singh | dceadcf | 2018-12-14 16:29:06 +1100 | [diff] [blame] | 5607 | .load_from_eaddr = kvmhv_load_from_eaddr, |
| 5608 | .store_to_eaddr = kvmhv_store_to_eaddr, |
Paul Mackerras | 9a5788c | 2020-03-19 15:29:55 +1100 | [diff] [blame] | 5609 | .enable_svm = kvmhv_enable_svm, |
Bharata B Rao | 2294568 | 2019-11-25 08:36:30 +0530 | [diff] [blame] | 5610 | .svm_off = kvmhv_svm_off, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5611 | }; |
| 5612 | |
Mahesh Salgaonkar | fd7bacb | 2016-05-15 09:44:26 +0530 | [diff] [blame] | 5613 | static int kvm_init_subcore_bitmap(void) |
| 5614 | { |
| 5615 | int i, j; |
| 5616 | int nr_cores = cpu_nr_cores(); |
| 5617 | struct sibling_subcore_state *sibling_subcore_state; |
| 5618 | |
| 5619 | for (i = 0; i < nr_cores; i++) { |
| 5620 | int first_cpu = i * threads_per_core; |
| 5621 | int node = cpu_to_node(first_cpu); |
| 5622 | |
| 5623 | /* Ignore if it is already allocated. */ |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 5624 | if (paca_ptrs[first_cpu]->sibling_subcore_state) |
Mahesh Salgaonkar | fd7bacb | 2016-05-15 09:44:26 +0530 | [diff] [blame] | 5625 | continue; |
| 5626 | |
| 5627 | sibling_subcore_state = |
wangbo | 08434ab | 2019-01-07 20:15:52 +0800 | [diff] [blame] | 5628 | kzalloc_node(sizeof(struct sibling_subcore_state), |
Mahesh Salgaonkar | fd7bacb | 2016-05-15 09:44:26 +0530 | [diff] [blame] | 5629 | GFP_KERNEL, node); |
| 5630 | if (!sibling_subcore_state) |
| 5631 | return -ENOMEM; |
| 5632 | |
Mahesh Salgaonkar | fd7bacb | 2016-05-15 09:44:26 +0530 | [diff] [blame] | 5633 | |
| 5634 | for (j = 0; j < threads_per_core; j++) { |
| 5635 | int cpu = first_cpu + j; |
| 5636 | |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 5637 | paca_ptrs[cpu]->sibling_subcore_state = |
| 5638 | sibling_subcore_state; |
Mahesh Salgaonkar | fd7bacb | 2016-05-15 09:44:26 +0530 | [diff] [blame] | 5639 | } |
| 5640 | } |
| 5641 | return 0; |
| 5642 | } |
| 5643 | |
Paul Mackerras | 5a31935 | 2017-01-30 21:21:46 +1100 | [diff] [blame] | 5644 | static int kvmppc_radix_possible(void) |
| 5645 | { |
| 5646 | return cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled(); |
| 5647 | } |
| 5648 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5649 | static int kvmppc_book3s_init_hv(void) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5650 | { |
| 5651 | int r; |
Nicholas Piggin | 2275d7b | 2019-09-03 01:29:31 +1000 | [diff] [blame] | 5652 | |
| 5653 | if (!tlbie_capable) { |
| 5654 | pr_err("KVM-HV: Host does not support TLBIE\n"); |
| 5655 | return -ENODEV; |
| 5656 | } |
| 5657 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 5658 | /* |
| 5659 | * FIXME!! Do we need to check on all cpus ? |
| 5660 | */ |
| 5661 | r = kvmppc_core_check_processor_compat_hv(); |
| 5662 | if (r < 0) |
Paul Mackerras | 739e242 | 2014-03-25 10:47:05 +1100 | [diff] [blame] | 5663 | return -ENODEV; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5664 | |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 5665 | r = kvmhv_nested_init(); |
| 5666 | if (r) |
| 5667 | return r; |
| 5668 | |
Mahesh Salgaonkar | fd7bacb | 2016-05-15 09:44:26 +0530 | [diff] [blame] | 5669 | r = kvm_init_subcore_bitmap(); |
| 5670 | if (r) |
| 5671 | return r; |
| 5672 | |
Paul Mackerras | f725758 | 2016-11-18 09:02:08 +1100 | [diff] [blame] | 5673 | /* |
| 5674 | * We need a way of accessing the XICS interrupt controller, |
Nicholas Piggin | d2e6007 | 2018-02-14 01:08:12 +1000 | [diff] [blame] | 5675 | * either directly, via paca_ptrs[cpu]->kvm_hstate.xics_phys, or |
Paul Mackerras | f725758 | 2016-11-18 09:02:08 +1100 | [diff] [blame] | 5676 | * indirectly, via OPAL. |
| 5677 | */ |
| 5678 | #ifdef CONFIG_SMP |
Paul Mackerras | 03f9533 | 2019-02-04 22:07:20 +1100 | [diff] [blame] | 5679 | if (!xics_on_xive() && !kvmhv_on_pseries() && |
Paul Mackerras | f3c18e9 | 2018-10-08 16:31:05 +1100 | [diff] [blame] | 5680 | !local_paca->kvm_hstate.xics_phys) { |
Paul Mackerras | f725758 | 2016-11-18 09:02:08 +1100 | [diff] [blame] | 5681 | struct device_node *np; |
| 5682 | |
| 5683 | np = of_find_compatible_node(NULL, NULL, "ibm,opal-intc"); |
| 5684 | if (!np) { |
| 5685 | pr_err("KVM-HV: Cannot determine method for accessing XICS\n"); |
| 5686 | return -ENODEV; |
| 5687 | } |
Nicholas Mc Guire | 51eaa08 | 2018-07-07 08:53:07 +0200 | [diff] [blame] | 5688 | /* presence of intc confirmed - node can be dropped again */ |
| 5689 | of_node_put(np); |
Paul Mackerras | f725758 | 2016-11-18 09:02:08 +1100 | [diff] [blame] | 5690 | } |
| 5691 | #endif |
| 5692 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 5693 | kvm_ops_hv.owner = THIS_MODULE; |
| 5694 | kvmppc_hv_ops = &kvm_ops_hv; |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5695 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 5696 | init_default_hcalls(); |
| 5697 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 5698 | init_vcore_lists(); |
| 5699 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 5700 | r = kvmppc_mmu_hv_init(); |
Paul Mackerras | 5a31935 | 2017-01-30 21:21:46 +1100 | [diff] [blame] | 5701 | if (r) |
| 5702 | return r; |
| 5703 | |
| 5704 | if (kvmppc_radix_possible()) |
| 5705 | r = kvmppc_radix_init(); |
Paul Mackerras | 00608e1 | 2018-01-11 16:54:26 +1100 | [diff] [blame] | 5706 | |
| 5707 | /* |
| 5708 | * POWER9 chips before version 2.02 can't have some threads in |
| 5709 | * HPT mode and some in radix mode on the same core. |
| 5710 | */ |
| 5711 | if (cpu_has_feature(CPU_FTR_ARCH_300)) { |
| 5712 | unsigned int pvr = mfspr(SPRN_PVR); |
| 5713 | if ((pvr >> 16) == PVR_POWER9 && |
| 5714 | (((pvr & 0xe000) == 0 && (pvr & 0xfff) < 0x202) || |
| 5715 | ((pvr & 0xe000) == 0x2000 && (pvr & 0xfff) < 0x101))) |
| 5716 | no_mixing_hpt_and_radix = true; |
| 5717 | } |
| 5718 | |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 5719 | r = kvmppc_uvmem_init(); |
| 5720 | if (r < 0) |
| 5721 | pr_err("KVM-HV: kvmppc_uvmem_init failed %d\n", r); |
| 5722 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5723 | return r; |
| 5724 | } |
| 5725 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5726 | static void kvmppc_book3s_exit_hv(void) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5727 | { |
Bharata B Rao | ca9f494 | 2019-11-25 08:36:26 +0530 | [diff] [blame] | 5728 | kvmppc_uvmem_free(); |
Suresh Warrier | 79b6c24 | 2015-12-17 14:59:06 -0600 | [diff] [blame] | 5729 | kvmppc_free_host_rm_ops(); |
Paul Mackerras | 5a31935 | 2017-01-30 21:21:46 +1100 | [diff] [blame] | 5730 | if (kvmppc_radix_possible()) |
| 5731 | kvmppc_radix_exit(); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 5732 | kvmppc_hv_ops = NULL; |
Paul Mackerras | 8e3f5fc | 2018-10-08 16:31:03 +1100 | [diff] [blame] | 5733 | kvmhv_nested_exit(); |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 5736 | module_init(kvmppc_book3s_init_hv); |
| 5737 | module_exit(kvmppc_book3s_exit_hv); |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 5738 | MODULE_LICENSE("GPL"); |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 5739 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
| 5740 | MODULE_ALIAS("devname:kvm"); |