Thomas Gleixner | d2912cb | 2019-06-04 10:11:33 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2009. SUSE Linux Products GmbH. All rights reserved. |
| 4 | * |
| 5 | * Authors: |
| 6 | * Alexander Graf <agraf@suse.de> |
| 7 | * Kevin Wolf <mail@kevin-wolf.de> |
| 8 | * Paul Mackerras <paulus@samba.org> |
| 9 | * |
| 10 | * Description: |
| 11 | * Functions relating to running KVM on Book 3S processors where |
| 12 | * we don't have access to hypervisor mode, and we run the guest |
| 13 | * in problem state (user mode). |
| 14 | * |
| 15 | * This file is derived from arch/powerpc/kvm/44x.c, |
| 16 | * by Hollis Blanchard <hollisb@us.ibm.com>. |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 17 | */ |
| 18 | |
| 19 | #include <linux/kvm_host.h> |
Paul Gortmaker | 9308794 | 2011-07-29 16:19:31 +1000 | [diff] [blame] | 20 | #include <linux/export.h> |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 21 | #include <linux/err.h> |
| 22 | #include <linux/slab.h> |
| 23 | |
| 24 | #include <asm/reg.h> |
| 25 | #include <asm/cputable.h> |
| 26 | #include <asm/cacheflush.h> |
Linus Torvalds | 7c0f6ba | 2016-12-24 11:46:01 -0800 | [diff] [blame] | 27 | #include <linux/uaccess.h> |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 28 | #include <asm/io.h> |
| 29 | #include <asm/kvm_ppc.h> |
| 30 | #include <asm/kvm_book3s.h> |
| 31 | #include <asm/mmu_context.h> |
Benjamin Herrenschmidt | 95327d0 | 2012-04-01 17:35:53 +0000 | [diff] [blame] | 32 | #include <asm/switch_to.h> |
Ian Munsie | a413f47 | 2012-12-03 18:36:13 +0000 | [diff] [blame] | 33 | #include <asm/firmware.h> |
Benjamin Herrenschmidt | d3cbff1 | 2016-07-05 15:03:49 +1000 | [diff] [blame] | 34 | #include <asm/setup.h> |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 35 | #include <linux/gfp.h> |
| 36 | #include <linux/sched.h> |
| 37 | #include <linux/vmalloc.h> |
| 38 | #include <linux/highmem.h> |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 39 | #include <linux/module.h> |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 40 | #include <linux/miscdevice.h> |
Simon Guo | 66c33e7 | 2018-05-23 15:01:57 +0800 | [diff] [blame] | 41 | #include <asm/asm-prototypes.h> |
Simon Guo | 8d2e2fc | 2018-05-23 15:01:58 +0800 | [diff] [blame] | 42 | #include <asm/tm.h> |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 43 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 44 | #include "book3s.h" |
Aneesh Kumar K.V | 72c1253 | 2013-10-07 22:17:57 +0530 | [diff] [blame] | 45 | |
| 46 | #define CREATE_TRACE_POINTS |
| 47 | #include "trace_pr.h" |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 48 | |
| 49 | /* #define EXIT_DEBUG */ |
| 50 | /* #define DEBUG_EXT */ |
| 51 | |
| 52 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, |
| 53 | ulong msr); |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 54 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 55 | static int kvmppc_handle_fac(struct kvm_vcpu *vcpu, ulong fac); |
| 56 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 57 | |
| 58 | /* Some compatibility defines */ |
| 59 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 60 | #define MSR_USER32 MSR_USER |
| 61 | #define MSR_USER64 MSR_USER |
| 62 | #define HW_PAGE_SIZE PAGE_SIZE |
Alexey Kardashevskiy | 6c7d47c | 2017-11-22 14:42:21 +1100 | [diff] [blame] | 63 | #define HPTE_R_M _PAGE_COHERENT |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 64 | #endif |
| 65 | |
Alexander Graf | c01e3f6 | 2014-07-11 02:58:58 +0200 | [diff] [blame] | 66 | static bool kvmppc_is_split_real(struct kvm_vcpu *vcpu) |
| 67 | { |
| 68 | ulong msr = kvmppc_get_msr(vcpu); |
| 69 | return (msr & (MSR_IR|MSR_DR)) == MSR_DR; |
| 70 | } |
| 71 | |
| 72 | static void kvmppc_fixup_split_real(struct kvm_vcpu *vcpu) |
| 73 | { |
| 74 | ulong msr = kvmppc_get_msr(vcpu); |
| 75 | ulong pc = kvmppc_get_pc(vcpu); |
| 76 | |
| 77 | /* We are in DR only split real mode */ |
| 78 | if ((msr & (MSR_IR|MSR_DR)) != MSR_DR) |
| 79 | return; |
| 80 | |
| 81 | /* We have not fixed up the guest already */ |
| 82 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SPLIT_HACK) |
| 83 | return; |
| 84 | |
| 85 | /* The code is in fixupable address space */ |
| 86 | if (pc & SPLIT_HACK_MASK) |
| 87 | return; |
| 88 | |
| 89 | vcpu->arch.hflags |= BOOK3S_HFLAG_SPLIT_HACK; |
| 90 | kvmppc_set_pc(vcpu, pc | SPLIT_HACK_OFFS); |
| 91 | } |
| 92 | |
Nicholas Piggin | 87a45e0 | 2019-10-02 16:00:22 +1000 | [diff] [blame] | 93 | static void kvmppc_unfixup_split_real(struct kvm_vcpu *vcpu) |
| 94 | { |
| 95 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SPLIT_HACK) { |
| 96 | ulong pc = kvmppc_get_pc(vcpu); |
| 97 | ulong lr = kvmppc_get_lr(vcpu); |
| 98 | if ((pc & SPLIT_HACK_MASK) == SPLIT_HACK_OFFS) |
| 99 | kvmppc_set_pc(vcpu, pc & ~SPLIT_HACK_MASK); |
| 100 | if ((lr & SPLIT_HACK_MASK) == SPLIT_HACK_OFFS) |
| 101 | kvmppc_set_lr(vcpu, lr & ~SPLIT_HACK_MASK); |
| 102 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_SPLIT_HACK; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | static void kvmppc_inject_interrupt_pr(struct kvm_vcpu *vcpu, int vec, u64 srr1_flags) |
| 107 | { |
| 108 | unsigned long msr, pc, new_msr, new_pc; |
| 109 | |
| 110 | kvmppc_unfixup_split_real(vcpu); |
| 111 | |
| 112 | msr = kvmppc_get_msr(vcpu); |
| 113 | pc = kvmppc_get_pc(vcpu); |
| 114 | new_msr = vcpu->arch.intr_msr; |
| 115 | new_pc = to_book3s(vcpu)->hior + vec; |
| 116 | |
| 117 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 118 | /* If transactional, change to suspend mode on IRQ delivery */ |
| 119 | if (MSR_TM_TRANSACTIONAL(msr)) |
| 120 | new_msr |= MSR_TS_S; |
| 121 | else |
| 122 | new_msr |= msr & MSR_TS_MASK; |
| 123 | #endif |
| 124 | |
| 125 | kvmppc_set_srr0(vcpu, pc); |
| 126 | kvmppc_set_srr1(vcpu, (msr & SRR1_MSR_BITS) | srr1_flags); |
| 127 | kvmppc_set_pc(vcpu, new_pc); |
| 128 | kvmppc_set_msr(vcpu, new_msr); |
| 129 | } |
Alexander Graf | c01e3f6 | 2014-07-11 02:58:58 +0200 | [diff] [blame] | 130 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 131 | static void kvmppc_core_vcpu_load_pr(struct kvm_vcpu *vcpu, int cpu) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 132 | { |
| 133 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 134 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 135 | memcpy(svcpu->slb, to_book3s(vcpu)->slb_shadow, sizeof(svcpu->slb)); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 136 | svcpu->slb_max = to_book3s(vcpu)->slb_shadow_max; |
Alexander Graf | 40fdd8c | 2013-11-29 02:29:00 +0100 | [diff] [blame] | 137 | svcpu->in_use = 0; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 138 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 139 | #endif |
Alexander Graf | fb4188b | 2014-06-09 01:16:32 +0200 | [diff] [blame] | 140 | |
| 141 | /* Disable AIL if supported */ |
| 142 | if (cpu_has_feature(CPU_FTR_HVMODE) && |
| 143 | cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 144 | mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) & ~LPCR_AIL); |
| 145 | |
Paul Mackerras | a47d72f | 2012-09-20 19:35:51 +0000 | [diff] [blame] | 146 | vcpu->cpu = smp_processor_id(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 147 | #ifdef CONFIG_PPC_BOOK3S_32 |
Paul Mackerras | 3ff9550 | 2013-09-20 14:52:49 +1000 | [diff] [blame] | 148 | current->thread.kvm_shadow_vcpu = vcpu->arch.shadow_vcpu; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 149 | #endif |
Alexander Graf | c01e3f6 | 2014-07-11 02:58:58 +0200 | [diff] [blame] | 150 | |
| 151 | if (kvmppc_is_split_real(vcpu)) |
| 152 | kvmppc_fixup_split_real(vcpu); |
Simon Guo | 8d2e2fc | 2018-05-23 15:01:58 +0800 | [diff] [blame] | 153 | |
| 154 | kvmppc_restore_tm_pr(vcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 157 | static void kvmppc_core_vcpu_put_pr(struct kvm_vcpu *vcpu) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 158 | { |
| 159 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 160 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Alexander Graf | 40fdd8c | 2013-11-29 02:29:00 +0100 | [diff] [blame] | 161 | if (svcpu->in_use) { |
Alexander Graf | 07ae538 | 2018-01-31 22:24:58 +0100 | [diff] [blame] | 162 | kvmppc_copy_from_svcpu(vcpu); |
Alexander Graf | 40fdd8c | 2013-11-29 02:29:00 +0100 | [diff] [blame] | 163 | } |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 164 | memcpy(to_book3s(vcpu)->slb_shadow, svcpu->slb, sizeof(svcpu->slb)); |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 165 | to_book3s(vcpu)->slb_shadow_max = svcpu->slb_max; |
| 166 | svcpu_put(svcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 167 | #endif |
| 168 | |
Alexander Graf | c01e3f6 | 2014-07-11 02:58:58 +0200 | [diff] [blame] | 169 | if (kvmppc_is_split_real(vcpu)) |
| 170 | kvmppc_unfixup_split_real(vcpu); |
| 171 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 172 | kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX); |
Alexander Graf | e14e7a1 | 2014-04-22 12:26:58 +0200 | [diff] [blame] | 173 | kvmppc_giveup_fac(vcpu, FSCR_TAR_LG); |
Simon Guo | 8d2e2fc | 2018-05-23 15:01:58 +0800 | [diff] [blame] | 174 | kvmppc_save_tm_pr(vcpu); |
Alexander Graf | fb4188b | 2014-06-09 01:16:32 +0200 | [diff] [blame] | 175 | |
| 176 | /* Enable AIL if supported */ |
| 177 | if (cpu_has_feature(CPU_FTR_HVMODE) && |
| 178 | cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 179 | mtspr(SPRN_LPCR, mfspr(SPRN_LPCR) | LPCR_AIL_3); |
| 180 | |
Paul Mackerras | a47d72f | 2012-09-20 19:35:51 +0000 | [diff] [blame] | 181 | vcpu->cpu = -1; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 182 | } |
| 183 | |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 184 | /* Copy data needed by real-mode code from vcpu to shadow vcpu */ |
Alexander Graf | 07ae538 | 2018-01-31 22:24:58 +0100 | [diff] [blame] | 185 | void kvmppc_copy_to_svcpu(struct kvm_vcpu *vcpu) |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 186 | { |
Alexander Graf | 07ae538 | 2018-01-31 22:24:58 +0100 | [diff] [blame] | 187 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
| 188 | |
Simon Guo | 1143a70 | 2018-05-07 14:20:07 +0800 | [diff] [blame] | 189 | svcpu->gpr[0] = vcpu->arch.regs.gpr[0]; |
| 190 | svcpu->gpr[1] = vcpu->arch.regs.gpr[1]; |
| 191 | svcpu->gpr[2] = vcpu->arch.regs.gpr[2]; |
| 192 | svcpu->gpr[3] = vcpu->arch.regs.gpr[3]; |
| 193 | svcpu->gpr[4] = vcpu->arch.regs.gpr[4]; |
| 194 | svcpu->gpr[5] = vcpu->arch.regs.gpr[5]; |
| 195 | svcpu->gpr[6] = vcpu->arch.regs.gpr[6]; |
| 196 | svcpu->gpr[7] = vcpu->arch.regs.gpr[7]; |
| 197 | svcpu->gpr[8] = vcpu->arch.regs.gpr[8]; |
| 198 | svcpu->gpr[9] = vcpu->arch.regs.gpr[9]; |
| 199 | svcpu->gpr[10] = vcpu->arch.regs.gpr[10]; |
| 200 | svcpu->gpr[11] = vcpu->arch.regs.gpr[11]; |
| 201 | svcpu->gpr[12] = vcpu->arch.regs.gpr[12]; |
| 202 | svcpu->gpr[13] = vcpu->arch.regs.gpr[13]; |
Paul Mackerras | fd0944b | 2018-10-08 16:30:58 +1100 | [diff] [blame] | 203 | svcpu->cr = vcpu->arch.regs.ccr; |
Simon Guo | 173c520 | 2018-05-07 14:20:08 +0800 | [diff] [blame] | 204 | svcpu->xer = vcpu->arch.regs.xer; |
| 205 | svcpu->ctr = vcpu->arch.regs.ctr; |
| 206 | svcpu->lr = vcpu->arch.regs.link; |
| 207 | svcpu->pc = vcpu->arch.regs.nip; |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 208 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 209 | svcpu->shadow_fscr = vcpu->arch.shadow_fscr; |
| 210 | #endif |
Aneesh Kumar K.V | 3cd60e3 | 2014-06-04 16:47:55 +0530 | [diff] [blame] | 211 | /* |
| 212 | * Now also save the current time base value. We use this |
| 213 | * to find the guest purr and spurr value. |
| 214 | */ |
| 215 | vcpu->arch.entry_tb = get_tb(); |
Aneesh Kumar K.V | 8f42ab2 | 2014-06-05 17:38:02 +0530 | [diff] [blame] | 216 | vcpu->arch.entry_vtb = get_vtb(); |
Aneesh Kumar K.V | 06da28e | 2014-06-05 17:38:05 +0530 | [diff] [blame] | 217 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 218 | vcpu->arch.entry_ic = mfspr(SPRN_IC); |
Alexander Graf | 40fdd8c | 2013-11-29 02:29:00 +0100 | [diff] [blame] | 219 | svcpu->in_use = true; |
Alexander Graf | 07ae538 | 2018-01-31 22:24:58 +0100 | [diff] [blame] | 220 | |
| 221 | svcpu_put(svcpu); |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 222 | } |
| 223 | |
Simon Guo | 95757bf | 2018-05-23 15:01:53 +0800 | [diff] [blame] | 224 | static void kvmppc_recalc_shadow_msr(struct kvm_vcpu *vcpu) |
| 225 | { |
| 226 | ulong guest_msr = kvmppc_get_msr(vcpu); |
| 227 | ulong smsr = guest_msr; |
| 228 | |
| 229 | /* Guest MSR values */ |
| 230 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 231 | smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_LE | |
| 232 | MSR_TM | MSR_TS_MASK; |
| 233 | #else |
| 234 | smsr &= MSR_FE0 | MSR_FE1 | MSR_SF | MSR_SE | MSR_BE | MSR_LE; |
| 235 | #endif |
| 236 | /* Process MSR values */ |
| 237 | smsr |= MSR_ME | MSR_RI | MSR_IR | MSR_DR | MSR_PR | MSR_EE; |
| 238 | /* External providers the guest reserved */ |
| 239 | smsr |= (guest_msr & vcpu->arch.guest_owned_ext); |
| 240 | /* 64-bit Process MSR values */ |
| 241 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 242 | smsr |= MSR_ISF | MSR_HV; |
| 243 | #endif |
Simon Guo | 5706340 | 2018-05-23 15:02:01 +0800 | [diff] [blame] | 244 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 245 | /* |
| 246 | * in guest privileged state, we want to fail all TM transactions. |
| 247 | * So disable MSR TM bit so that all tbegin. will be able to be |
| 248 | * trapped into host. |
| 249 | */ |
| 250 | if (!(guest_msr & MSR_PR)) |
| 251 | smsr &= ~MSR_TM; |
| 252 | #endif |
Simon Guo | 95757bf | 2018-05-23 15:01:53 +0800 | [diff] [blame] | 253 | vcpu->arch.shadow_msr = smsr; |
| 254 | } |
| 255 | |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 256 | /* Copy data touched by real-mode code from shadow vcpu back to vcpu */ |
Alexander Graf | 07ae538 | 2018-01-31 22:24:58 +0100 | [diff] [blame] | 257 | void kvmppc_copy_from_svcpu(struct kvm_vcpu *vcpu) |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 258 | { |
Alexander Graf | 07ae538 | 2018-01-31 22:24:58 +0100 | [diff] [blame] | 259 | struct kvmppc_book3s_shadow_vcpu *svcpu = svcpu_get(vcpu); |
Simon Guo | 95757bf | 2018-05-23 15:01:53 +0800 | [diff] [blame] | 260 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 261 | ulong old_msr; |
| 262 | #endif |
Alexander Graf | 40fdd8c | 2013-11-29 02:29:00 +0100 | [diff] [blame] | 263 | |
| 264 | /* |
| 265 | * Maybe we were already preempted and synced the svcpu from |
| 266 | * our preempt notifiers. Don't bother touching this svcpu then. |
| 267 | */ |
| 268 | if (!svcpu->in_use) |
| 269 | goto out; |
| 270 | |
Simon Guo | 1143a70 | 2018-05-07 14:20:07 +0800 | [diff] [blame] | 271 | vcpu->arch.regs.gpr[0] = svcpu->gpr[0]; |
| 272 | vcpu->arch.regs.gpr[1] = svcpu->gpr[1]; |
| 273 | vcpu->arch.regs.gpr[2] = svcpu->gpr[2]; |
| 274 | vcpu->arch.regs.gpr[3] = svcpu->gpr[3]; |
| 275 | vcpu->arch.regs.gpr[4] = svcpu->gpr[4]; |
| 276 | vcpu->arch.regs.gpr[5] = svcpu->gpr[5]; |
| 277 | vcpu->arch.regs.gpr[6] = svcpu->gpr[6]; |
| 278 | vcpu->arch.regs.gpr[7] = svcpu->gpr[7]; |
| 279 | vcpu->arch.regs.gpr[8] = svcpu->gpr[8]; |
| 280 | vcpu->arch.regs.gpr[9] = svcpu->gpr[9]; |
| 281 | vcpu->arch.regs.gpr[10] = svcpu->gpr[10]; |
| 282 | vcpu->arch.regs.gpr[11] = svcpu->gpr[11]; |
| 283 | vcpu->arch.regs.gpr[12] = svcpu->gpr[12]; |
| 284 | vcpu->arch.regs.gpr[13] = svcpu->gpr[13]; |
Paul Mackerras | fd0944b | 2018-10-08 16:30:58 +1100 | [diff] [blame] | 285 | vcpu->arch.regs.ccr = svcpu->cr; |
Simon Guo | 173c520 | 2018-05-07 14:20:08 +0800 | [diff] [blame] | 286 | vcpu->arch.regs.xer = svcpu->xer; |
| 287 | vcpu->arch.regs.ctr = svcpu->ctr; |
| 288 | vcpu->arch.regs.link = svcpu->lr; |
| 289 | vcpu->arch.regs.nip = svcpu->pc; |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 290 | vcpu->arch.shadow_srr1 = svcpu->shadow_srr1; |
| 291 | vcpu->arch.fault_dar = svcpu->fault_dar; |
| 292 | vcpu->arch.fault_dsisr = svcpu->fault_dsisr; |
| 293 | vcpu->arch.last_inst = svcpu->last_inst; |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 294 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 295 | vcpu->arch.shadow_fscr = svcpu->shadow_fscr; |
| 296 | #endif |
Aneesh Kumar K.V | 3cd60e3 | 2014-06-04 16:47:55 +0530 | [diff] [blame] | 297 | /* |
| 298 | * Update purr and spurr using time base on exit. |
| 299 | */ |
| 300 | vcpu->arch.purr += get_tb() - vcpu->arch.entry_tb; |
| 301 | vcpu->arch.spurr += get_tb() - vcpu->arch.entry_tb; |
Paul Mackerras | 88b02cf9 | 2016-09-15 13:42:52 +1000 | [diff] [blame] | 302 | to_book3s(vcpu)->vtb += get_vtb() - vcpu->arch.entry_vtb; |
Aneesh Kumar K.V | 06da28e | 2014-06-05 17:38:05 +0530 | [diff] [blame] | 303 | if (cpu_has_feature(CPU_FTR_ARCH_207S)) |
| 304 | vcpu->arch.ic += mfspr(SPRN_IC) - vcpu->arch.entry_ic; |
Simon Guo | 95757bf | 2018-05-23 15:01:53 +0800 | [diff] [blame] | 305 | |
| 306 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 307 | /* |
| 308 | * Unlike other MSR bits, MSR[TS]bits can be changed at guest without |
| 309 | * notifying host: |
| 310 | * modified by unprivileged instructions like "tbegin"/"tend"/ |
| 311 | * "tresume"/"tsuspend" in PR KVM guest. |
| 312 | * |
| 313 | * It is necessary to sync here to calculate a correct shadow_msr. |
| 314 | * |
| 315 | * privileged guest's tbegin will be failed at present. So we |
| 316 | * only take care of problem state guest. |
| 317 | */ |
| 318 | old_msr = kvmppc_get_msr(vcpu); |
| 319 | if (unlikely((old_msr & MSR_PR) && |
| 320 | (vcpu->arch.shadow_srr1 & (MSR_TS_MASK)) != |
| 321 | (old_msr & (MSR_TS_MASK)))) { |
| 322 | old_msr &= ~(MSR_TS_MASK); |
| 323 | old_msr |= (vcpu->arch.shadow_srr1 & (MSR_TS_MASK)); |
| 324 | kvmppc_set_msr_fast(vcpu, old_msr); |
| 325 | kvmppc_recalc_shadow_msr(vcpu); |
| 326 | } |
| 327 | #endif |
| 328 | |
Alexander Graf | 40fdd8c | 2013-11-29 02:29:00 +0100 | [diff] [blame] | 329 | svcpu->in_use = false; |
| 330 | |
| 331 | out: |
Alexander Graf | 07ae538 | 2018-01-31 22:24:58 +0100 | [diff] [blame] | 332 | svcpu_put(svcpu); |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 333 | } |
| 334 | |
Simon Guo | 66c33e7 | 2018-05-23 15:01:57 +0800 | [diff] [blame] | 335 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
Simon Guo | e32c53d | 2018-05-23 15:02:04 +0800 | [diff] [blame] | 336 | void kvmppc_save_tm_sprs(struct kvm_vcpu *vcpu) |
Simon Guo | 66c33e7 | 2018-05-23 15:01:57 +0800 | [diff] [blame] | 337 | { |
| 338 | tm_enable(); |
| 339 | vcpu->arch.tfhar = mfspr(SPRN_TFHAR); |
| 340 | vcpu->arch.texasr = mfspr(SPRN_TEXASR); |
| 341 | vcpu->arch.tfiar = mfspr(SPRN_TFIAR); |
| 342 | tm_disable(); |
| 343 | } |
| 344 | |
Simon Guo | 5706340 | 2018-05-23 15:02:01 +0800 | [diff] [blame] | 345 | void kvmppc_restore_tm_sprs(struct kvm_vcpu *vcpu) |
Simon Guo | 66c33e7 | 2018-05-23 15:01:57 +0800 | [diff] [blame] | 346 | { |
| 347 | tm_enable(); |
| 348 | mtspr(SPRN_TFHAR, vcpu->arch.tfhar); |
| 349 | mtspr(SPRN_TEXASR, vcpu->arch.texasr); |
| 350 | mtspr(SPRN_TFIAR, vcpu->arch.tfiar); |
| 351 | tm_disable(); |
| 352 | } |
| 353 | |
Simon Guo | 13989b6 | 2018-05-23 15:01:59 +0800 | [diff] [blame] | 354 | /* loadup math bits which is enabled at kvmppc_get_msr() but not enabled at |
| 355 | * hardware. |
| 356 | */ |
| 357 | static void kvmppc_handle_lost_math_exts(struct kvm_vcpu *vcpu) |
| 358 | { |
| 359 | ulong exit_nr; |
| 360 | ulong ext_diff = (kvmppc_get_msr(vcpu) & ~vcpu->arch.guest_owned_ext) & |
| 361 | (MSR_FP | MSR_VEC | MSR_VSX); |
| 362 | |
| 363 | if (!ext_diff) |
| 364 | return; |
| 365 | |
| 366 | if (ext_diff == MSR_FP) |
| 367 | exit_nr = BOOK3S_INTERRUPT_FP_UNAVAIL; |
| 368 | else if (ext_diff == MSR_VEC) |
| 369 | exit_nr = BOOK3S_INTERRUPT_ALTIVEC; |
| 370 | else |
| 371 | exit_nr = BOOK3S_INTERRUPT_VSX; |
| 372 | |
| 373 | kvmppc_handle_ext(vcpu, exit_nr, ext_diff); |
| 374 | } |
| 375 | |
Simon Guo | 8d2e2fc | 2018-05-23 15:01:58 +0800 | [diff] [blame] | 376 | void kvmppc_save_tm_pr(struct kvm_vcpu *vcpu) |
| 377 | { |
| 378 | if (!(MSR_TM_ACTIVE(kvmppc_get_msr(vcpu)))) { |
| 379 | kvmppc_save_tm_sprs(vcpu); |
| 380 | return; |
| 381 | } |
| 382 | |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 383 | kvmppc_giveup_fac(vcpu, FSCR_TAR_LG); |
Simon Guo | 13989b6 | 2018-05-23 15:01:59 +0800 | [diff] [blame] | 384 | kvmppc_giveup_ext(vcpu, MSR_VSX); |
| 385 | |
Simon Guo | 8d2e2fc | 2018-05-23 15:01:58 +0800 | [diff] [blame] | 386 | preempt_disable(); |
| 387 | _kvmppc_save_tm_pr(vcpu, mfmsr()); |
| 388 | preempt_enable(); |
| 389 | } |
| 390 | |
| 391 | void kvmppc_restore_tm_pr(struct kvm_vcpu *vcpu) |
| 392 | { |
| 393 | if (!MSR_TM_ACTIVE(kvmppc_get_msr(vcpu))) { |
| 394 | kvmppc_restore_tm_sprs(vcpu); |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 395 | if (kvmppc_get_msr(vcpu) & MSR_TM) { |
Simon Guo | 13989b6 | 2018-05-23 15:01:59 +0800 | [diff] [blame] | 396 | kvmppc_handle_lost_math_exts(vcpu); |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 397 | if (vcpu->arch.fscr & FSCR_TAR) |
| 398 | kvmppc_handle_fac(vcpu, FSCR_TAR_LG); |
| 399 | } |
Simon Guo | 8d2e2fc | 2018-05-23 15:01:58 +0800 | [diff] [blame] | 400 | return; |
| 401 | } |
| 402 | |
| 403 | preempt_disable(); |
| 404 | _kvmppc_restore_tm_pr(vcpu, kvmppc_get_msr(vcpu)); |
| 405 | preempt_enable(); |
Simon Guo | 13989b6 | 2018-05-23 15:01:59 +0800 | [diff] [blame] | 406 | |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 407 | if (kvmppc_get_msr(vcpu) & MSR_TM) { |
Simon Guo | 13989b6 | 2018-05-23 15:01:59 +0800 | [diff] [blame] | 408 | kvmppc_handle_lost_math_exts(vcpu); |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 409 | if (vcpu->arch.fscr & FSCR_TAR) |
| 410 | kvmppc_handle_fac(vcpu, FSCR_TAR_LG); |
| 411 | } |
Simon Guo | 8d2e2fc | 2018-05-23 15:01:58 +0800 | [diff] [blame] | 412 | } |
Simon Guo | 66c33e7 | 2018-05-23 15:01:57 +0800 | [diff] [blame] | 413 | #endif |
| 414 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 415 | static int kvmppc_core_check_requests_pr(struct kvm_vcpu *vcpu) |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 416 | { |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 417 | int r = 1; /* Indicate we want to get back into the guest */ |
| 418 | |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 419 | /* We misuse TLB_FLUSH to indicate that we want to clear |
| 420 | all shadow cache entries */ |
| 421 | if (kvm_check_request(KVM_REQ_TLB_FLUSH, vcpu)) |
| 422 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
Alexander Graf | 7c973a2 | 2012-08-13 12:50:35 +0200 | [diff] [blame] | 423 | |
| 424 | return r; |
Alexander Graf | 03d25c5 | 2012-08-10 12:28:50 +0200 | [diff] [blame] | 425 | } |
| 426 | |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 427 | /************* MMU Notifiers *************/ |
Paul Mackerras | 491d6ec | 2013-09-20 14:52:54 +1000 | [diff] [blame] | 428 | static void do_kvm_unmap_hva(struct kvm *kvm, unsigned long start, |
| 429 | unsigned long end) |
| 430 | { |
| 431 | long i; |
| 432 | struct kvm_vcpu *vcpu; |
| 433 | struct kvm_memslots *slots; |
| 434 | struct kvm_memory_slot *memslot; |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 435 | |
Paul Mackerras | 491d6ec | 2013-09-20 14:52:54 +1000 | [diff] [blame] | 436 | slots = kvm_memslots(kvm); |
| 437 | kvm_for_each_memslot(memslot, slots) { |
| 438 | unsigned long hva_start, hva_end; |
| 439 | gfn_t gfn, gfn_end; |
| 440 | |
| 441 | hva_start = max(start, memslot->userspace_addr); |
| 442 | hva_end = min(end, memslot->userspace_addr + |
| 443 | (memslot->npages << PAGE_SHIFT)); |
| 444 | if (hva_start >= hva_end) |
| 445 | continue; |
| 446 | /* |
| 447 | * {gfn(page) | page intersects with [hva_start, hva_end)} = |
| 448 | * {gfn, gfn+1, ..., gfn_end-1}. |
| 449 | */ |
| 450 | gfn = hva_to_gfn_memslot(hva_start, memslot); |
| 451 | gfn_end = hva_to_gfn_memslot(hva_end + PAGE_SIZE - 1, memslot); |
| 452 | kvm_for_each_vcpu(i, vcpu, kvm) |
| 453 | kvmppc_mmu_pte_pflush(vcpu, gfn << PAGE_SHIFT, |
| 454 | gfn_end << PAGE_SHIFT); |
| 455 | } |
| 456 | } |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 457 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 458 | static int kvm_unmap_hva_range_pr(struct kvm *kvm, unsigned long start, |
| 459 | unsigned long end) |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 460 | { |
Paul Mackerras | 491d6ec | 2013-09-20 14:52:54 +1000 | [diff] [blame] | 461 | do_kvm_unmap_hva(kvm, start, end); |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
Andres Lagar-Cavilla | 5712846 | 2014-09-22 14:54:42 -0700 | [diff] [blame] | 466 | static int kvm_age_hva_pr(struct kvm *kvm, unsigned long start, |
| 467 | unsigned long end) |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 468 | { |
| 469 | /* XXX could be more clever ;) */ |
| 470 | return 0; |
| 471 | } |
| 472 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 473 | static int kvm_test_age_hva_pr(struct kvm *kvm, unsigned long hva) |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 474 | { |
| 475 | /* XXX could be more clever ;) */ |
| 476 | return 0; |
| 477 | } |
| 478 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 479 | static void kvm_set_spte_hva_pr(struct kvm *kvm, unsigned long hva, pte_t pte) |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 480 | { |
| 481 | /* The page will get remapped properly on its next fault */ |
Paul Mackerras | 491d6ec | 2013-09-20 14:52:54 +1000 | [diff] [blame] | 482 | do_kvm_unmap_hva(kvm, hva, hva + PAGE_SIZE); |
Alexander Graf | 9b0cb3c | 2012-08-10 13:23:55 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | /*****************************************/ |
| 486 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 487 | static void kvmppc_set_msr_pr(struct kvm_vcpu *vcpu, u64 msr) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 488 | { |
Simon Guo | 68ab07b | 2018-05-23 15:02:06 +0800 | [diff] [blame] | 489 | ulong old_msr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 490 | |
Paul Mackerras | 4f169d2 | 2018-06-07 18:07:06 +1000 | [diff] [blame] | 491 | /* For PAPR guest, make sure MSR reflects guest mode */ |
| 492 | if (vcpu->arch.papr_enabled) |
| 493 | msr = (msr & ~MSR_HV) | MSR_ME; |
| 494 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 495 | #ifdef EXIT_DEBUG |
| 496 | printk(KERN_INFO "KVM: Set MSR to 0x%llx\n", msr); |
| 497 | #endif |
| 498 | |
Simon Guo | 68ab07b | 2018-05-23 15:02:06 +0800 | [diff] [blame] | 499 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 500 | /* We should never target guest MSR to TS=10 && PR=0, |
| 501 | * since we always fail transaction for guest privilege |
| 502 | * state. |
| 503 | */ |
| 504 | if (!(msr & MSR_PR) && MSR_TM_TRANSACTIONAL(msr)) |
| 505 | kvmppc_emulate_tabort(vcpu, |
| 506 | TM_CAUSE_KVM_FAC_UNAV | TM_CAUSE_PERSISTENT); |
| 507 | #endif |
| 508 | |
| 509 | old_msr = kvmppc_get_msr(vcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 510 | msr &= to_book3s(vcpu)->msr_mask; |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 511 | kvmppc_set_msr_fast(vcpu, msr); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 512 | kvmppc_recalc_shadow_msr(vcpu); |
| 513 | |
| 514 | if (msr & MSR_POW) { |
| 515 | if (!vcpu->arch.pending_exceptions) { |
| 516 | kvm_vcpu_block(vcpu); |
Radim Krčmář | 72875d8 | 2017-04-26 22:32:19 +0200 | [diff] [blame] | 517 | kvm_clear_request(KVM_REQ_UNHALT, vcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 518 | vcpu->stat.halt_wakeup++; |
| 519 | |
| 520 | /* Unset POW bit after we woke up */ |
| 521 | msr &= ~MSR_POW; |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 522 | kvmppc_set_msr_fast(vcpu, msr); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 523 | } |
| 524 | } |
| 525 | |
Alexander Graf | c01e3f6 | 2014-07-11 02:58:58 +0200 | [diff] [blame] | 526 | if (kvmppc_is_split_real(vcpu)) |
| 527 | kvmppc_fixup_split_real(vcpu); |
| 528 | else |
| 529 | kvmppc_unfixup_split_real(vcpu); |
| 530 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 531 | if ((kvmppc_get_msr(vcpu) & (MSR_PR|MSR_IR|MSR_DR)) != |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 532 | (old_msr & (MSR_PR|MSR_IR|MSR_DR))) { |
| 533 | kvmppc_mmu_flush_segments(vcpu); |
| 534 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); |
| 535 | |
| 536 | /* Preload magic page segment when in kernel mode */ |
| 537 | if (!(msr & MSR_PR) && vcpu->arch.magic_page_pa) { |
| 538 | struct kvm_vcpu_arch *a = &vcpu->arch; |
| 539 | |
| 540 | if (msr & MSR_DR) |
| 541 | kvmppc_mmu_map_segment(vcpu, a->magic_page_ea); |
| 542 | else |
| 543 | kvmppc_mmu_map_segment(vcpu, a->magic_page_pa); |
| 544 | } |
| 545 | } |
| 546 | |
Benjamin Herrenschmidt | bbcc9c0 | 2012-03-13 21:52:44 +0000 | [diff] [blame] | 547 | /* |
| 548 | * When switching from 32 to 64-bit, we may have a stale 32-bit |
| 549 | * magic page around, we need to flush it. Typically 32-bit magic |
Finn Thain | 3cc97be | 2018-08-23 17:00:52 -0700 | [diff] [blame] | 550 | * page will be instantiated when calling into RTAS. Note: We |
Benjamin Herrenschmidt | bbcc9c0 | 2012-03-13 21:52:44 +0000 | [diff] [blame] | 551 | * assume that such transition only happens while in kernel mode, |
| 552 | * ie, we never transition from user 32-bit to kernel 64-bit with |
| 553 | * a 32-bit magic page around. |
| 554 | */ |
| 555 | if (vcpu->arch.magic_page_pa && |
| 556 | !(old_msr & MSR_PR) && !(old_msr & MSR_SF) && (msr & MSR_SF)) { |
| 557 | /* going from RTAS to normal kernel code */ |
| 558 | kvmppc_mmu_pte_flush(vcpu, (uint32_t)vcpu->arch.magic_page_pa, |
| 559 | ~0xFFFUL); |
| 560 | } |
| 561 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 562 | /* Preload FPU if it's enabled */ |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 563 | if (kvmppc_get_msr(vcpu) & MSR_FP) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 564 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); |
Simon Guo | 13989b6 | 2018-05-23 15:01:59 +0800 | [diff] [blame] | 565 | |
| 566 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 567 | if (kvmppc_get_msr(vcpu) & MSR_TM) |
| 568 | kvmppc_handle_lost_math_exts(vcpu); |
| 569 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 570 | } |
| 571 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 572 | void kvmppc_set_pvr_pr(struct kvm_vcpu *vcpu, u32 pvr) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 573 | { |
| 574 | u32 host_pvr; |
| 575 | |
| 576 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_SLB; |
| 577 | vcpu->arch.pvr = pvr; |
| 578 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 579 | if ((pvr >= 0x330000) && (pvr < 0x70330000)) { |
| 580 | kvmppc_mmu_book3s_64_init(vcpu); |
Alexander Graf | 1022fc3 | 2011-09-14 21:45:23 +0200 | [diff] [blame] | 581 | if (!to_book3s(vcpu)->hior_explicit) |
| 582 | to_book3s(vcpu)->hior = 0xfff00000; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 583 | to_book3s(vcpu)->msr_mask = 0xffffffffffffffffULL; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 584 | vcpu->arch.cpu_type = KVM_CPU_3S_64; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 585 | } else |
| 586 | #endif |
| 587 | { |
| 588 | kvmppc_mmu_book3s_32_init(vcpu); |
Alexander Graf | 1022fc3 | 2011-09-14 21:45:23 +0200 | [diff] [blame] | 589 | if (!to_book3s(vcpu)->hior_explicit) |
| 590 | to_book3s(vcpu)->hior = 0; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 591 | to_book3s(vcpu)->msr_mask = 0xffffffffULL; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 592 | vcpu->arch.cpu_type = KVM_CPU_3S_32; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 593 | } |
| 594 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 595 | kvmppc_sanity_check(vcpu); |
| 596 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 597 | /* If we are in hypervisor level on 970, we can tell the CPU to |
| 598 | * treat DCBZ as 32 bytes store */ |
| 599 | vcpu->arch.hflags &= ~BOOK3S_HFLAG_DCBZ32; |
| 600 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && (mfmsr() & MSR_HV) && |
| 601 | !strcmp(cur_cpu_spec->platform, "ppc970")) |
| 602 | vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32; |
| 603 | |
| 604 | /* Cell performs badly if MSR_FEx are set. So let's hope nobody |
| 605 | really needs them in a VM on Cell and force disable them. */ |
| 606 | if (!strcmp(cur_cpu_spec->platform, "ppc-cell-be")) |
| 607 | to_book3s(vcpu)->msr_mask &= ~(MSR_FE0 | MSR_FE1); |
| 608 | |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 609 | /* |
| 610 | * If they're asking for POWER6 or later, set the flag |
| 611 | * indicating that we can do multiple large page sizes |
| 612 | * and 1TB segments. |
| 613 | * Also set the flag that indicates that tlbie has the large |
| 614 | * page bit in the RB operand instead of the instruction. |
| 615 | */ |
| 616 | switch (PVR_VER(pvr)) { |
| 617 | case PVR_POWER6: |
| 618 | case PVR_POWER7: |
| 619 | case PVR_POWER7p: |
| 620 | case PVR_POWER8: |
Thomas Huth | 2365f6b | 2016-09-21 13:53:46 +0200 | [diff] [blame] | 621 | case PVR_POWER8E: |
| 622 | case PVR_POWER8NVL: |
Suraj Jitindar Singh | 6142236 | 2018-12-07 12:17:03 +1100 | [diff] [blame] | 623 | case PVR_POWER9: |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 624 | vcpu->arch.hflags |= BOOK3S_HFLAG_MULTI_PGSIZE | |
| 625 | BOOK3S_HFLAG_NEW_TLBIE; |
| 626 | break; |
| 627 | } |
| 628 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 629 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 630 | /* 32 bit Book3S always has 32 byte dcbz */ |
| 631 | vcpu->arch.hflags |= BOOK3S_HFLAG_DCBZ32; |
| 632 | #endif |
| 633 | |
| 634 | /* On some CPUs we can execute paired single operations natively */ |
| 635 | asm ( "mfpvr %0" : "=r"(host_pvr)); |
| 636 | switch (host_pvr) { |
| 637 | case 0x00080200: /* lonestar 2.0 */ |
| 638 | case 0x00088202: /* lonestar 2.2 */ |
| 639 | case 0x70000100: /* gekko 1.0 */ |
| 640 | case 0x00080100: /* gekko 2.0 */ |
| 641 | case 0x00083203: /* gekko 2.3a */ |
| 642 | case 0x00083213: /* gekko 2.3b */ |
| 643 | case 0x00083204: /* gekko 2.4 */ |
| 644 | case 0x00083214: /* gekko 2.4e (8SE) - retail HW2 */ |
| 645 | case 0x00087200: /* broadway */ |
| 646 | vcpu->arch.hflags |= BOOK3S_HFLAG_NATIVE_PS; |
| 647 | /* Enable HID2.PSE - in case we need it later */ |
| 648 | mtspr(SPRN_HID2_GEKKO, mfspr(SPRN_HID2_GEKKO) | (1 << 29)); |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | /* Book3s_32 CPUs always have 32 bytes cache line size, which Linux assumes. To |
| 653 | * make Book3s_32 Linux work on Book3s_64, we have to make sure we trap dcbz to |
| 654 | * emulate 32 bytes dcbz length. |
| 655 | * |
| 656 | * The Book3s_64 inventors also realized this case and implemented a special bit |
| 657 | * in the HID5 register, which is a hypervisor ressource. Thus we can't use it. |
| 658 | * |
| 659 | * My approach here is to patch the dcbz instruction on executing pages. |
| 660 | */ |
| 661 | static void kvmppc_patch_dcbz(struct kvm_vcpu *vcpu, struct kvmppc_pte *pte) |
| 662 | { |
| 663 | struct page *hpage; |
| 664 | u64 hpage_offset; |
| 665 | u32 *page; |
| 666 | int i; |
| 667 | |
| 668 | hpage = gfn_to_page(vcpu->kvm, pte->raddr >> PAGE_SHIFT); |
Xiao Guangrong | 32cad84 | 2012-08-03 15:42:52 +0800 | [diff] [blame] | 669 | if (is_error_page(hpage)) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 670 | return; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 671 | |
| 672 | hpage_offset = pte->raddr & ~PAGE_MASK; |
| 673 | hpage_offset &= ~0xFFFULL; |
| 674 | hpage_offset /= 4; |
| 675 | |
| 676 | get_page(hpage); |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 677 | page = kmap_atomic(hpage); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 678 | |
| 679 | /* patch dcbz into reserved instruction, so we trap */ |
| 680 | for (i=hpage_offset; i < hpage_offset + (HW_PAGE_SIZE / 4); i++) |
Alexander Graf | cd087ee | 2014-04-24 13:52:01 +0200 | [diff] [blame] | 681 | if ((be32_to_cpu(page[i]) & 0xff0007ff) == INS_DCBZ) |
| 682 | page[i] &= cpu_to_be32(0xfffffff7); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 683 | |
Cong Wang | 2480b20 | 2011-11-25 23:14:16 +0800 | [diff] [blame] | 684 | kunmap_atomic(page); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 685 | put_page(hpage); |
| 686 | } |
| 687 | |
Yaowei Bai | 378b417 | 2015-11-16 11:10:24 +0800 | [diff] [blame] | 688 | static bool kvmppc_visible_gpa(struct kvm_vcpu *vcpu, gpa_t gpa) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 689 | { |
| 690 | ulong mp_pa = vcpu->arch.magic_page_pa; |
| 691 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 692 | if (!(kvmppc_get_msr(vcpu) & MSR_SF)) |
Benjamin Herrenschmidt | bbcc9c0 | 2012-03-13 21:52:44 +0000 | [diff] [blame] | 693 | mp_pa = (uint32_t)mp_pa; |
| 694 | |
Alexander Graf | 89b68c9 | 2014-07-13 16:37:12 +0200 | [diff] [blame] | 695 | gpa &= ~0xFFFULL; |
| 696 | if (unlikely(mp_pa) && unlikely((mp_pa & KVM_PAM) == (gpa & KVM_PAM))) { |
Yaowei Bai | 378b417 | 2015-11-16 11:10:24 +0800 | [diff] [blame] | 697 | return true; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Alexander Graf | 89b68c9 | 2014-07-13 16:37:12 +0200 | [diff] [blame] | 700 | return kvm_is_visible_gfn(vcpu->kvm, gpa >> PAGE_SHIFT); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 701 | } |
| 702 | |
| 703 | int kvmppc_handle_pagefault(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 704 | ulong eaddr, int vec) |
| 705 | { |
| 706 | bool data = (vec == BOOK3S_INTERRUPT_DATA_STORAGE); |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 707 | bool iswrite = false; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 708 | int r = RESUME_GUEST; |
| 709 | int relocated; |
| 710 | int page_found = 0; |
Alexey Kardashevskiy | 96df226 | 2017-03-24 17:49:22 +1100 | [diff] [blame] | 711 | struct kvmppc_pte pte = { 0 }; |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 712 | bool dr = (kvmppc_get_msr(vcpu) & MSR_DR) ? true : false; |
| 713 | bool ir = (kvmppc_get_msr(vcpu) & MSR_IR) ? true : false; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 714 | u64 vsid; |
| 715 | |
| 716 | relocated = data ? dr : ir; |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 717 | if (data && (vcpu->arch.fault_dsisr & DSISR_ISSTORE)) |
| 718 | iswrite = true; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 719 | |
| 720 | /* Resolve real address if translation turned on */ |
| 721 | if (relocated) { |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 722 | page_found = vcpu->arch.mmu.xlate(vcpu, eaddr, &pte, data, iswrite); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 723 | } else { |
| 724 | pte.may_execute = true; |
| 725 | pte.may_read = true; |
| 726 | pte.may_write = true; |
| 727 | pte.raddr = eaddr & KVM_PAM; |
| 728 | pte.eaddr = eaddr; |
| 729 | pte.vpage = eaddr >> 12; |
Paul Mackerras | c9029c3 | 2013-09-20 14:52:45 +1000 | [diff] [blame] | 730 | pte.page_size = MMU_PAGE_64K; |
Alexey Kardashevskiy | 6c7d47c | 2017-11-22 14:42:21 +1100 | [diff] [blame] | 731 | pte.wimg = HPTE_R_M; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 732 | } |
| 733 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 734 | switch (kvmppc_get_msr(vcpu) & (MSR_DR|MSR_IR)) { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 735 | case 0: |
| 736 | pte.vpage |= ((u64)VSID_REAL << (SID_SHIFT - 12)); |
| 737 | break; |
| 738 | case MSR_DR: |
Alexander Graf | c01e3f6 | 2014-07-11 02:58:58 +0200 | [diff] [blame] | 739 | if (!data && |
| 740 | (vcpu->arch.hflags & BOOK3S_HFLAG_SPLIT_HACK) && |
| 741 | ((pte.raddr & SPLIT_HACK_MASK) == SPLIT_HACK_OFFS)) |
| 742 | pte.raddr &= ~SPLIT_HACK_MASK; |
| 743 | /* fall through */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 744 | case MSR_IR: |
| 745 | vcpu->arch.mmu.esid_to_vsid(vcpu, eaddr >> SID_SHIFT, &vsid); |
| 746 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 747 | if ((kvmppc_get_msr(vcpu) & (MSR_DR|MSR_IR)) == MSR_DR) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 748 | pte.vpage |= ((u64)VSID_REAL_DR << (SID_SHIFT - 12)); |
| 749 | else |
| 750 | pte.vpage |= ((u64)VSID_REAL_IR << (SID_SHIFT - 12)); |
| 751 | pte.vpage |= vsid; |
| 752 | |
| 753 | if (vsid == -1) |
| 754 | page_found = -EINVAL; |
| 755 | break; |
| 756 | } |
| 757 | |
| 758 | if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 759 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { |
| 760 | /* |
| 761 | * If we do the dcbz hack, we have to NX on every execution, |
| 762 | * so we can patch the executing code. This renders our guest |
| 763 | * NX-less. |
| 764 | */ |
| 765 | pte.may_execute = !data; |
| 766 | } |
| 767 | |
Paul Mackerras | 916ccad | 2018-06-07 18:04:37 +1000 | [diff] [blame] | 768 | if (page_found == -ENOENT || page_found == -EPERM) { |
| 769 | /* Page not found in guest PTE entries, or protection fault */ |
| 770 | u64 flags; |
| 771 | |
| 772 | if (page_found == -EPERM) |
| 773 | flags = DSISR_PROTFAULT; |
| 774 | else |
| 775 | flags = DSISR_NOHPTE; |
| 776 | if (data) { |
| 777 | flags |= vcpu->arch.fault_dsisr & DSISR_ISSTORE; |
| 778 | kvmppc_core_queue_data_storage(vcpu, eaddr, flags); |
| 779 | } else { |
| 780 | kvmppc_core_queue_inst_storage(vcpu, flags); |
| 781 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 782 | } else if (page_found == -EINVAL) { |
| 783 | /* Page not found in guest SLB */ |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 784 | kvmppc_set_dar(vcpu, kvmppc_get_fault_dar(vcpu)); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 785 | kvmppc_book3s_queue_irqprio(vcpu, vec + 0x80); |
Alexey Kardashevskiy | 9eecec1 | 2017-03-24 17:47:13 +1100 | [diff] [blame] | 786 | } else if (kvmppc_visible_gpa(vcpu, pte.raddr)) { |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 787 | if (data && !(vcpu->arch.fault_dsisr & DSISR_NOHPTE)) { |
| 788 | /* |
| 789 | * There is already a host HPTE there, presumably |
| 790 | * a read-only one for a page the guest thinks |
| 791 | * is writable, so get rid of it first. |
| 792 | */ |
| 793 | kvmppc_mmu_unmap_page(vcpu, &pte); |
| 794 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 795 | /* The guest's PTE is not mapped yet. Map on the host */ |
Alexey Kardashevskiy | bd9166f | 2017-03-24 17:48:10 +1100 | [diff] [blame] | 796 | if (kvmppc_mmu_map_page(vcpu, &pte, iswrite) == -EIO) { |
| 797 | /* Exit KVM if mapping failed */ |
| 798 | run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
| 799 | return RESUME_HOST; |
| 800 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 801 | if (data) |
| 802 | vcpu->stat.sp_storage++; |
| 803 | else if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 804 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 805 | kvmppc_patch_dcbz(vcpu, &pte); |
| 806 | } else { |
| 807 | /* MMIO */ |
| 808 | vcpu->stat.mmio_exits++; |
| 809 | vcpu->arch.paddr_accessed = pte.raddr; |
Alexander Graf | 6020c0f | 2012-03-12 02:26:30 +0100 | [diff] [blame] | 810 | vcpu->arch.vaddr_accessed = pte.eaddr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 811 | r = kvmppc_emulate_mmio(run, vcpu); |
| 812 | if ( r == RESUME_HOST_NV ) |
| 813 | r = RESUME_HOST; |
| 814 | } |
| 815 | |
| 816 | return r; |
| 817 | } |
| 818 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 819 | /* Give up external provider (FPU, Altivec, VSX) */ |
| 820 | void kvmppc_giveup_ext(struct kvm_vcpu *vcpu, ulong msr) |
| 821 | { |
| 822 | struct thread_struct *t = ¤t->thread; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 823 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 824 | /* |
| 825 | * VSX instructions can access FP and vector registers, so if |
| 826 | * we are giving up VSX, make sure we give up FP and VMX as well. |
| 827 | */ |
| 828 | if (msr & MSR_VSX) |
| 829 | msr |= MSR_FP | MSR_VEC; |
| 830 | |
| 831 | msr &= vcpu->arch.guest_owned_ext; |
| 832 | if (!msr) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 833 | return; |
| 834 | |
| 835 | #ifdef DEBUG_EXT |
| 836 | printk(KERN_INFO "Giving up ext 0x%lx\n", msr); |
| 837 | #endif |
| 838 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 839 | if (msr & MSR_FP) { |
| 840 | /* |
| 841 | * Note that on CPUs with VSX, giveup_fpu stores |
| 842 | * both the traditional FP registers and the added VSX |
Paul Mackerras | de79f7b | 2013-09-10 20:20:42 +1000 | [diff] [blame] | 843 | * registers into thread.fp_state.fpr[]. |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 844 | */ |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 845 | if (t->regs->msr & MSR_FP) |
Paul Mackerras | 9d1ffdd | 2013-08-06 14:14:33 +1000 | [diff] [blame] | 846 | giveup_fpu(current); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 847 | t->fp_save_area = NULL; |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 848 | } |
| 849 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 850 | #ifdef CONFIG_ALTIVEC |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 851 | if (msr & MSR_VEC) { |
Paul Mackerras | 9d1ffdd | 2013-08-06 14:14:33 +1000 | [diff] [blame] | 852 | if (current->thread.regs->msr & MSR_VEC) |
| 853 | giveup_altivec(current); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 854 | t->vr_save_area = NULL; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 855 | } |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 856 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 857 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 858 | vcpu->arch.guest_owned_ext &= ~(msr | MSR_VSX); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 859 | kvmppc_recalc_shadow_msr(vcpu); |
| 860 | } |
| 861 | |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 862 | /* Give up facility (TAR / EBB / DSCR) */ |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 863 | void kvmppc_giveup_fac(struct kvm_vcpu *vcpu, ulong fac) |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 864 | { |
| 865 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 866 | if (!(vcpu->arch.shadow_fscr & (1ULL << fac))) { |
| 867 | /* Facility not available to the guest, ignore giveup request*/ |
| 868 | return; |
| 869 | } |
Alexander Graf | e14e7a1 | 2014-04-22 12:26:58 +0200 | [diff] [blame] | 870 | |
| 871 | switch (fac) { |
| 872 | case FSCR_TAR_LG: |
| 873 | vcpu->arch.tar = mfspr(SPRN_TAR); |
| 874 | mtspr(SPRN_TAR, current->thread.tar); |
| 875 | vcpu->arch.shadow_fscr &= ~FSCR_TAR; |
| 876 | break; |
| 877 | } |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 878 | #endif |
| 879 | } |
| 880 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 881 | /* Handle external providers (FPU, Altivec, VSX) */ |
| 882 | static int kvmppc_handle_ext(struct kvm_vcpu *vcpu, unsigned int exit_nr, |
| 883 | ulong msr) |
| 884 | { |
| 885 | struct thread_struct *t = ¤t->thread; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 886 | |
| 887 | /* When we have paired singles, we emulate in software */ |
| 888 | if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE) |
| 889 | return RESUME_GUEST; |
| 890 | |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 891 | if (!(kvmppc_get_msr(vcpu) & msr)) { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 892 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 893 | return RESUME_GUEST; |
| 894 | } |
| 895 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 896 | if (msr == MSR_VSX) { |
| 897 | /* No VSX? Give an illegal instruction interrupt */ |
| 898 | #ifdef CONFIG_VSX |
| 899 | if (!cpu_has_feature(CPU_FTR_VSX)) |
| 900 | #endif |
| 901 | { |
| 902 | kvmppc_core_queue_program(vcpu, SRR1_PROGILL); |
| 903 | return RESUME_GUEST; |
| 904 | } |
| 905 | |
| 906 | /* |
| 907 | * We have to load up all the FP and VMX registers before |
| 908 | * we can let the guest use VSX instructions. |
| 909 | */ |
| 910 | msr = MSR_FP | MSR_VEC | MSR_VSX; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 911 | } |
| 912 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 913 | /* See if we already own all the ext(s) needed */ |
| 914 | msr &= ~vcpu->arch.guest_owned_ext; |
| 915 | if (!msr) |
| 916 | return RESUME_GUEST; |
| 917 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 918 | #ifdef DEBUG_EXT |
| 919 | printk(KERN_INFO "Loading up ext 0x%lx\n", msr); |
| 920 | #endif |
| 921 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 922 | if (msr & MSR_FP) { |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 923 | preempt_disable(); |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 924 | enable_kernel_fp(); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 925 | load_fp_state(&vcpu->arch.fp); |
Anton Blanchard | dc4fbba | 2015-10-29 11:44:05 +1100 | [diff] [blame] | 926 | disable_kernel_fp(); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 927 | t->fp_save_area = &vcpu->arch.fp; |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 928 | preempt_enable(); |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | if (msr & MSR_VEC) { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 932 | #ifdef CONFIG_ALTIVEC |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 933 | preempt_disable(); |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 934 | enable_kernel_altivec(); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 935 | load_vr_state(&vcpu->arch.vr); |
Anton Blanchard | dc4fbba | 2015-10-29 11:44:05 +1100 | [diff] [blame] | 936 | disable_kernel_altivec(); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 937 | t->vr_save_area = &vcpu->arch.vr; |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 938 | preempt_enable(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 939 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 942 | t->regs->msr |= msr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 943 | vcpu->arch.guest_owned_ext |= msr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 944 | kvmppc_recalc_shadow_msr(vcpu); |
| 945 | |
| 946 | return RESUME_GUEST; |
| 947 | } |
| 948 | |
Paul Mackerras | 9d1ffdd | 2013-08-06 14:14:33 +1000 | [diff] [blame] | 949 | /* |
| 950 | * Kernel code using FP or VMX could have flushed guest state to |
| 951 | * the thread_struct; if so, get it back now. |
| 952 | */ |
| 953 | static void kvmppc_handle_lost_ext(struct kvm_vcpu *vcpu) |
| 954 | { |
| 955 | unsigned long lost_ext; |
| 956 | |
| 957 | lost_ext = vcpu->arch.guest_owned_ext & ~current->thread.regs->msr; |
| 958 | if (!lost_ext) |
| 959 | return; |
| 960 | |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 961 | if (lost_ext & MSR_FP) { |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 962 | preempt_disable(); |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 963 | enable_kernel_fp(); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 964 | load_fp_state(&vcpu->arch.fp); |
Anton Blanchard | dc4fbba | 2015-10-29 11:44:05 +1100 | [diff] [blame] | 965 | disable_kernel_fp(); |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 966 | preempt_enable(); |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 967 | } |
Paul Mackerras | f248177 | 2013-09-20 14:52:42 +1000 | [diff] [blame] | 968 | #ifdef CONFIG_ALTIVEC |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 969 | if (lost_ext & MSR_VEC) { |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 970 | preempt_disable(); |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 971 | enable_kernel_altivec(); |
Paul Mackerras | 99dae3b | 2013-10-15 20:43:03 +1100 | [diff] [blame] | 972 | load_vr_state(&vcpu->arch.vr); |
Anton Blanchard | dc4fbba | 2015-10-29 11:44:05 +1100 | [diff] [blame] | 973 | disable_kernel_altivec(); |
Aneesh Kumar K.V | 7562c4f | 2014-05-04 22:56:08 +0530 | [diff] [blame] | 974 | preempt_enable(); |
Paul Mackerras | 09548fd | 2013-10-15 20:43:01 +1100 | [diff] [blame] | 975 | } |
Paul Mackerras | f248177 | 2013-09-20 14:52:42 +1000 | [diff] [blame] | 976 | #endif |
Paul Mackerras | 9d1ffdd | 2013-08-06 14:14:33 +1000 | [diff] [blame] | 977 | current->thread.regs->msr |= lost_ext; |
| 978 | } |
| 979 | |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 980 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 981 | |
Simon Guo | 533082a | 2018-05-23 15:02:00 +0800 | [diff] [blame] | 982 | void kvmppc_trigger_fac_interrupt(struct kvm_vcpu *vcpu, ulong fac) |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 983 | { |
| 984 | /* Inject the Interrupt Cause field and trigger a guest interrupt */ |
| 985 | vcpu->arch.fscr &= ~(0xffULL << 56); |
| 986 | vcpu->arch.fscr |= (fac << 56); |
| 987 | kvmppc_book3s_queue_irqprio(vcpu, BOOK3S_INTERRUPT_FAC_UNAVAIL); |
| 988 | } |
| 989 | |
| 990 | static void kvmppc_emulate_fac(struct kvm_vcpu *vcpu, ulong fac) |
| 991 | { |
| 992 | enum emulation_result er = EMULATE_FAIL; |
| 993 | |
| 994 | if (!(kvmppc_get_msr(vcpu) & MSR_PR)) |
| 995 | er = kvmppc_emulate_instruction(vcpu->run, vcpu); |
| 996 | |
| 997 | if ((er != EMULATE_DONE) && (er != EMULATE_AGAIN)) { |
| 998 | /* Couldn't emulate, trigger interrupt in guest */ |
| 999 | kvmppc_trigger_fac_interrupt(vcpu, fac); |
| 1000 | } |
| 1001 | } |
| 1002 | |
| 1003 | /* Enable facilities (TAR, EBB, DSCR) for the guest */ |
| 1004 | static int kvmppc_handle_fac(struct kvm_vcpu *vcpu, ulong fac) |
| 1005 | { |
Alexander Graf | 9916d57 | 2014-04-29 17:54:40 +0200 | [diff] [blame] | 1006 | bool guest_fac_enabled; |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 1007 | BUG_ON(!cpu_has_feature(CPU_FTR_ARCH_207S)); |
| 1008 | |
Alexander Graf | 9916d57 | 2014-04-29 17:54:40 +0200 | [diff] [blame] | 1009 | /* |
| 1010 | * Not every facility is enabled by FSCR bits, check whether the |
| 1011 | * guest has this facility enabled at all. |
| 1012 | */ |
| 1013 | switch (fac) { |
| 1014 | case FSCR_TAR_LG: |
| 1015 | case FSCR_EBB_LG: |
| 1016 | guest_fac_enabled = (vcpu->arch.fscr & (1ULL << fac)); |
| 1017 | break; |
| 1018 | case FSCR_TM_LG: |
| 1019 | guest_fac_enabled = kvmppc_get_msr(vcpu) & MSR_TM; |
| 1020 | break; |
| 1021 | default: |
| 1022 | guest_fac_enabled = false; |
| 1023 | break; |
| 1024 | } |
| 1025 | |
| 1026 | if (!guest_fac_enabled) { |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 1027 | /* Facility not enabled by the guest */ |
| 1028 | kvmppc_trigger_fac_interrupt(vcpu, fac); |
| 1029 | return RESUME_GUEST; |
| 1030 | } |
| 1031 | |
| 1032 | switch (fac) { |
Alexander Graf | e14e7a1 | 2014-04-22 12:26:58 +0200 | [diff] [blame] | 1033 | case FSCR_TAR_LG: |
| 1034 | /* TAR switching isn't lazy in Linux yet */ |
| 1035 | current->thread.tar = mfspr(SPRN_TAR); |
| 1036 | mtspr(SPRN_TAR, vcpu->arch.tar); |
| 1037 | vcpu->arch.shadow_fscr |= FSCR_TAR; |
| 1038 | break; |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 1039 | default: |
| 1040 | kvmppc_emulate_fac(vcpu, fac); |
| 1041 | break; |
| 1042 | } |
| 1043 | |
Simon Guo | 19c585e | 2018-05-23 15:02:02 +0800 | [diff] [blame] | 1044 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1045 | /* Since we disabled MSR_TM at privilege state, the mfspr instruction |
| 1046 | * for TM spr can trigger TM fac unavailable. In this case, the |
| 1047 | * emulation is handled by kvmppc_emulate_fac(), which invokes |
| 1048 | * kvmppc_emulate_mfspr() finally. But note the mfspr can include |
| 1049 | * RT for NV registers. So it need to restore those NV reg to reflect |
| 1050 | * the update. |
| 1051 | */ |
| 1052 | if ((fac == FSCR_TM_LG) && !(kvmppc_get_msr(vcpu) & MSR_PR)) |
| 1053 | return RESUME_GUEST_NV; |
| 1054 | #endif |
| 1055 | |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 1056 | return RESUME_GUEST; |
| 1057 | } |
Alexander Graf | 8e6afa3 | 2014-07-31 10:21:59 +0200 | [diff] [blame] | 1058 | |
| 1059 | void kvmppc_set_fscr(struct kvm_vcpu *vcpu, u64 fscr) |
| 1060 | { |
| 1061 | if ((vcpu->arch.fscr & FSCR_TAR) && !(fscr & FSCR_TAR)) { |
| 1062 | /* TAR got dropped, drop it in shadow too */ |
| 1063 | kvmppc_giveup_fac(vcpu, FSCR_TAR_LG); |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 1064 | } else if (!(vcpu->arch.fscr & FSCR_TAR) && (fscr & FSCR_TAR)) { |
| 1065 | vcpu->arch.fscr = fscr; |
| 1066 | kvmppc_handle_fac(vcpu, FSCR_TAR_LG); |
| 1067 | return; |
Alexander Graf | 8e6afa3 | 2014-07-31 10:21:59 +0200 | [diff] [blame] | 1068 | } |
Simon Guo | 7284ca8 | 2018-05-23 15:02:07 +0800 | [diff] [blame] | 1069 | |
Alexander Graf | 8e6afa3 | 2014-07-31 10:21:59 +0200 | [diff] [blame] | 1070 | vcpu->arch.fscr = fscr; |
| 1071 | } |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 1072 | #endif |
| 1073 | |
Laurent Vivier | 11dd6ac | 2016-04-08 18:05:00 +0200 | [diff] [blame] | 1074 | static void kvmppc_setup_debug(struct kvm_vcpu *vcpu) |
| 1075 | { |
| 1076 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
| 1077 | u64 msr = kvmppc_get_msr(vcpu); |
| 1078 | |
| 1079 | kvmppc_set_msr(vcpu, msr | MSR_SE); |
| 1080 | } |
| 1081 | } |
| 1082 | |
| 1083 | static void kvmppc_clear_debug(struct kvm_vcpu *vcpu) |
| 1084 | { |
| 1085 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
| 1086 | u64 msr = kvmppc_get_msr(vcpu); |
| 1087 | |
| 1088 | kvmppc_set_msr(vcpu, msr & ~MSR_SE); |
| 1089 | } |
| 1090 | } |
| 1091 | |
Thomas Huth | fcd4f3c | 2017-01-25 13:27:22 +0100 | [diff] [blame] | 1092 | static int kvmppc_exit_pr_progint(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1093 | unsigned int exit_nr) |
| 1094 | { |
| 1095 | enum emulation_result er; |
| 1096 | ulong flags; |
| 1097 | u32 last_inst; |
| 1098 | int emul, r; |
| 1099 | |
| 1100 | /* |
| 1101 | * shadow_srr1 only contains valid flags if we came here via a program |
| 1102 | * exception. The other exceptions (emulation assist, FP unavailable, |
| 1103 | * etc.) do not provide flags in SRR1, so use an illegal-instruction |
| 1104 | * exception when injecting a program interrupt into the guest. |
| 1105 | */ |
| 1106 | if (exit_nr == BOOK3S_INTERRUPT_PROGRAM) |
| 1107 | flags = vcpu->arch.shadow_srr1 & 0x1f0000ull; |
| 1108 | else |
| 1109 | flags = SRR1_PROGILL; |
| 1110 | |
| 1111 | emul = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst); |
| 1112 | if (emul != EMULATE_DONE) |
| 1113 | return RESUME_GUEST; |
| 1114 | |
| 1115 | if (kvmppc_get_msr(vcpu) & MSR_PR) { |
| 1116 | #ifdef EXIT_DEBUG |
| 1117 | pr_info("Userspace triggered 0x700 exception at\n 0x%lx (0x%x)\n", |
| 1118 | kvmppc_get_pc(vcpu), last_inst); |
| 1119 | #endif |
| 1120 | if ((last_inst & 0xff0007ff) != (INS_DCBZ & 0xfffffff7)) { |
| 1121 | kvmppc_core_queue_program(vcpu, flags); |
| 1122 | return RESUME_GUEST; |
| 1123 | } |
| 1124 | } |
| 1125 | |
| 1126 | vcpu->stat.emulated_inst_exits++; |
| 1127 | er = kvmppc_emulate_instruction(run, vcpu); |
| 1128 | switch (er) { |
| 1129 | case EMULATE_DONE: |
| 1130 | r = RESUME_GUEST_NV; |
| 1131 | break; |
| 1132 | case EMULATE_AGAIN: |
| 1133 | r = RESUME_GUEST; |
| 1134 | break; |
| 1135 | case EMULATE_FAIL: |
| 1136 | pr_crit("%s: emulation at %lx failed (%08x)\n", |
| 1137 | __func__, kvmppc_get_pc(vcpu), last_inst); |
| 1138 | kvmppc_core_queue_program(vcpu, flags); |
| 1139 | r = RESUME_GUEST; |
| 1140 | break; |
| 1141 | case EMULATE_DO_MMIO: |
| 1142 | run->exit_reason = KVM_EXIT_MMIO; |
| 1143 | r = RESUME_HOST_NV; |
| 1144 | break; |
| 1145 | case EMULATE_EXIT_USER: |
| 1146 | r = RESUME_HOST_NV; |
| 1147 | break; |
| 1148 | default: |
| 1149 | BUG(); |
| 1150 | } |
| 1151 | |
| 1152 | return r; |
| 1153 | } |
| 1154 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1155 | int kvmppc_handle_exit_pr(struct kvm_run *run, struct kvm_vcpu *vcpu, |
| 1156 | unsigned int exit_nr) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1157 | { |
| 1158 | int r = RESUME_HOST; |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 1159 | int s; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1160 | |
| 1161 | vcpu->stat.sum_exits++; |
| 1162 | |
| 1163 | run->exit_reason = KVM_EXIT_UNKNOWN; |
| 1164 | run->ready_for_interrupt_injection = 1; |
| 1165 | |
Alexander Graf | bd2be68 | 2012-08-13 01:04:19 +0200 | [diff] [blame] | 1166 | /* We get here with MSR.EE=1 */ |
Alexander Graf | 3b1d9d7 | 2012-04-30 10:56:12 +0200 | [diff] [blame] | 1167 | |
Alexander Graf | 97c9505 | 2012-08-02 15:10:00 +0200 | [diff] [blame] | 1168 | trace_kvm_exit(exit_nr, vcpu); |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 1169 | guest_exit(); |
Alexander Graf | c63ddcb | 2012-08-12 11:27:49 +0200 | [diff] [blame] | 1170 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1171 | switch (exit_nr) { |
| 1172 | case BOOK3S_INTERRUPT_INST_STORAGE: |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1173 | { |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1174 | ulong shadow_srr1 = vcpu->arch.shadow_srr1; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1175 | vcpu->stat.pf_instruc++; |
| 1176 | |
Alexander Graf | c01e3f6 | 2014-07-11 02:58:58 +0200 | [diff] [blame] | 1177 | if (kvmppc_is_split_real(vcpu)) |
| 1178 | kvmppc_fixup_split_real(vcpu); |
| 1179 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1180 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 1181 | /* We set segments as unused segments when invalidating them. So |
| 1182 | * treat the respective fault as segment fault. */ |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1183 | { |
| 1184 | struct kvmppc_book3s_shadow_vcpu *svcpu; |
| 1185 | u32 sr; |
| 1186 | |
| 1187 | svcpu = svcpu_get(vcpu); |
| 1188 | sr = svcpu->sr[kvmppc_get_pc(vcpu) >> SID_SHIFT]; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1189 | svcpu_put(svcpu); |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1190 | if (sr == SR_INVALID) { |
| 1191 | kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)); |
| 1192 | r = RESUME_GUEST; |
| 1193 | break; |
| 1194 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1195 | } |
| 1196 | #endif |
| 1197 | |
| 1198 | /* only care about PTEG not found errors, but leave NX alone */ |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1199 | if (shadow_srr1 & 0x40000000) { |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 1200 | int idx = srcu_read_lock(&vcpu->kvm->srcu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1201 | r = kvmppc_handle_pagefault(run, vcpu, kvmppc_get_pc(vcpu), exit_nr); |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 1202 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1203 | vcpu->stat.sp_instruc++; |
| 1204 | } else if (vcpu->arch.mmu.is_dcbz32(vcpu) && |
| 1205 | (!(vcpu->arch.hflags & BOOK3S_HFLAG_DCBZ32))) { |
| 1206 | /* |
| 1207 | * XXX If we do the dcbz hack we use the NX bit to flush&patch the page, |
| 1208 | * so we can't use the NX bit inside the guest. Let's cross our fingers, |
| 1209 | * that no guest that needs the dcbz hack does NX. |
| 1210 | */ |
| 1211 | kvmppc_mmu_pte_flush(vcpu, kvmppc_get_pc(vcpu), ~0xFFFUL); |
| 1212 | r = RESUME_GUEST; |
| 1213 | } else { |
Paul Mackerras | 916ccad | 2018-06-07 18:04:37 +1000 | [diff] [blame] | 1214 | kvmppc_core_queue_inst_storage(vcpu, |
| 1215 | shadow_srr1 & 0x58000000); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1216 | r = RESUME_GUEST; |
| 1217 | } |
| 1218 | break; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1219 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1220 | case BOOK3S_INTERRUPT_DATA_STORAGE: |
| 1221 | { |
| 1222 | ulong dar = kvmppc_get_fault_dar(vcpu); |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1223 | u32 fault_dsisr = vcpu->arch.fault_dsisr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1224 | vcpu->stat.pf_storage++; |
| 1225 | |
| 1226 | #ifdef CONFIG_PPC_BOOK3S_32 |
| 1227 | /* We set segments as unused segments when invalidating them. So |
| 1228 | * treat the respective fault as segment fault. */ |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1229 | { |
| 1230 | struct kvmppc_book3s_shadow_vcpu *svcpu; |
| 1231 | u32 sr; |
| 1232 | |
| 1233 | svcpu = svcpu_get(vcpu); |
| 1234 | sr = svcpu->sr[dar >> SID_SHIFT]; |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1235 | svcpu_put(svcpu); |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1236 | if (sr == SR_INVALID) { |
| 1237 | kvmppc_mmu_map_segment(vcpu, dar); |
| 1238 | r = RESUME_GUEST; |
| 1239 | break; |
| 1240 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1241 | } |
| 1242 | #endif |
| 1243 | |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 1244 | /* |
| 1245 | * We need to handle missing shadow PTEs, and |
| 1246 | * protection faults due to us mapping a page read-only |
| 1247 | * when the guest thinks it is writable. |
| 1248 | */ |
| 1249 | if (fault_dsisr & (DSISR_NOHPTE | DSISR_PROTFAULT)) { |
| 1250 | int idx = srcu_read_lock(&vcpu->kvm->srcu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1251 | r = kvmppc_handle_pagefault(run, vcpu, dar, exit_nr); |
Paul Mackerras | 93b159b | 2013-09-20 14:52:51 +1000 | [diff] [blame] | 1252 | srcu_read_unlock(&vcpu->kvm->srcu, idx); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1253 | } else { |
Paul Mackerras | 916ccad | 2018-06-07 18:04:37 +1000 | [diff] [blame] | 1254 | kvmppc_core_queue_data_storage(vcpu, dar, fault_dsisr); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1255 | r = RESUME_GUEST; |
| 1256 | } |
| 1257 | break; |
| 1258 | } |
| 1259 | case BOOK3S_INTERRUPT_DATA_SEGMENT: |
| 1260 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_fault_dar(vcpu)) < 0) { |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1261 | kvmppc_set_dar(vcpu, kvmppc_get_fault_dar(vcpu)); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1262 | kvmppc_book3s_queue_irqprio(vcpu, |
| 1263 | BOOK3S_INTERRUPT_DATA_SEGMENT); |
| 1264 | } |
| 1265 | r = RESUME_GUEST; |
| 1266 | break; |
| 1267 | case BOOK3S_INTERRUPT_INST_SEGMENT: |
| 1268 | if (kvmppc_mmu_map_segment(vcpu, kvmppc_get_pc(vcpu)) < 0) { |
| 1269 | kvmppc_book3s_queue_irqprio(vcpu, |
| 1270 | BOOK3S_INTERRUPT_INST_SEGMENT); |
| 1271 | } |
| 1272 | r = RESUME_GUEST; |
| 1273 | break; |
| 1274 | /* We're good on these - the host merely wanted to get our attention */ |
| 1275 | case BOOK3S_INTERRUPT_DECREMENTER: |
Alexander Graf | 4f225ae | 2012-03-13 23:05:16 +0100 | [diff] [blame] | 1276 | case BOOK3S_INTERRUPT_HV_DECREMENTER: |
Paul Mackerras | 4068890 | 2014-01-08 21:25:36 +1100 | [diff] [blame] | 1277 | case BOOK3S_INTERRUPT_DOORBELL: |
Alexander Graf | 568fccc | 2014-06-16 16:37:38 +0200 | [diff] [blame] | 1278 | case BOOK3S_INTERRUPT_H_DOORBELL: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1279 | vcpu->stat.dec_exits++; |
| 1280 | r = RESUME_GUEST; |
| 1281 | break; |
| 1282 | case BOOK3S_INTERRUPT_EXTERNAL: |
Alexander Graf | 4f225ae | 2012-03-13 23:05:16 +0100 | [diff] [blame] | 1283 | case BOOK3S_INTERRUPT_EXTERNAL_HV: |
Cameron Kaiser | b71dc51 | 2018-06-05 07:48:55 -0700 | [diff] [blame] | 1284 | case BOOK3S_INTERRUPT_H_VIRT: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1285 | vcpu->stat.ext_intr_exits++; |
| 1286 | r = RESUME_GUEST; |
| 1287 | break; |
Cameron Kaiser | b71dc51 | 2018-06-05 07:48:55 -0700 | [diff] [blame] | 1288 | case BOOK3S_INTERRUPT_HMI: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1289 | case BOOK3S_INTERRUPT_PERFMON: |
Cameron Kaiser | b71dc51 | 2018-06-05 07:48:55 -0700 | [diff] [blame] | 1290 | case BOOK3S_INTERRUPT_SYSTEM_RESET: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1291 | r = RESUME_GUEST; |
| 1292 | break; |
| 1293 | case BOOK3S_INTERRUPT_PROGRAM: |
Alexander Graf | 4f225ae | 2012-03-13 23:05:16 +0100 | [diff] [blame] | 1294 | case BOOK3S_INTERRUPT_H_EMUL_ASSIST: |
Thomas Huth | fcd4f3c | 2017-01-25 13:27:22 +0100 | [diff] [blame] | 1295 | r = kvmppc_exit_pr_progint(run, vcpu, exit_nr); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1296 | break; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1297 | case BOOK3S_INTERRUPT_SYSCALL: |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 1298 | { |
| 1299 | u32 last_sc; |
| 1300 | int emul; |
| 1301 | |
| 1302 | /* Get last sc for papr */ |
| 1303 | if (vcpu->arch.papr_enabled) { |
| 1304 | /* The sc instuction points SRR0 to the next inst */ |
| 1305 | emul = kvmppc_get_last_inst(vcpu, INST_SC, &last_sc); |
| 1306 | if (emul != EMULATE_DONE) { |
| 1307 | kvmppc_set_pc(vcpu, kvmppc_get_pc(vcpu) - 4); |
| 1308 | r = RESUME_GUEST; |
| 1309 | break; |
| 1310 | } |
| 1311 | } |
| 1312 | |
Alexander Graf | a668f2b | 2011-08-08 17:26:24 +0200 | [diff] [blame] | 1313 | if (vcpu->arch.papr_enabled && |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 1314 | (last_sc == 0x44000022) && |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1315 | !(kvmppc_get_msr(vcpu) & MSR_PR)) { |
Alexander Graf | a668f2b | 2011-08-08 17:26:24 +0200 | [diff] [blame] | 1316 | /* SC 1 papr hypercalls */ |
| 1317 | ulong cmd = kvmppc_get_gpr(vcpu, 3); |
| 1318 | int i; |
| 1319 | |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 1320 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexander Graf | a668f2b | 2011-08-08 17:26:24 +0200 | [diff] [blame] | 1321 | if (kvmppc_h_pr(vcpu, cmd) == EMULATE_DONE) { |
| 1322 | r = RESUME_GUEST; |
| 1323 | break; |
| 1324 | } |
Andreas Schwab | 96f38d7 | 2011-11-08 07:17:39 +0000 | [diff] [blame] | 1325 | #endif |
Alexander Graf | a668f2b | 2011-08-08 17:26:24 +0200 | [diff] [blame] | 1326 | |
| 1327 | run->papr_hcall.nr = cmd; |
| 1328 | for (i = 0; i < 9; ++i) { |
| 1329 | ulong gpr = kvmppc_get_gpr(vcpu, 4 + i); |
| 1330 | run->papr_hcall.args[i] = gpr; |
| 1331 | } |
| 1332 | run->exit_reason = KVM_EXIT_PAPR_HCALL; |
| 1333 | vcpu->arch.hcall_needed = 1; |
| 1334 | r = RESUME_HOST; |
| 1335 | } else if (vcpu->arch.osi_enabled && |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1336 | (((u32)kvmppc_get_gpr(vcpu, 3)) == OSI_SC_MAGIC_R3) && |
| 1337 | (((u32)kvmppc_get_gpr(vcpu, 4)) == OSI_SC_MAGIC_R4)) { |
| 1338 | /* MOL hypercalls */ |
| 1339 | u64 *gprs = run->osi.gprs; |
| 1340 | int i; |
| 1341 | |
| 1342 | run->exit_reason = KVM_EXIT_OSI; |
| 1343 | for (i = 0; i < 32; i++) |
| 1344 | gprs[i] = kvmppc_get_gpr(vcpu, i); |
| 1345 | vcpu->arch.osi_needed = 1; |
| 1346 | r = RESUME_HOST_NV; |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1347 | } else if (!(kvmppc_get_msr(vcpu) & MSR_PR) && |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1348 | (((u32)kvmppc_get_gpr(vcpu, 0)) == KVM_SC_MAGIC_R0)) { |
| 1349 | /* KVM PV hypercalls */ |
| 1350 | kvmppc_set_gpr(vcpu, 3, kvmppc_kvm_pv(vcpu)); |
| 1351 | r = RESUME_GUEST; |
| 1352 | } else { |
| 1353 | /* Guest syscalls */ |
| 1354 | vcpu->stat.syscall_exits++; |
| 1355 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 1356 | r = RESUME_GUEST; |
| 1357 | } |
| 1358 | break; |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 1359 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1360 | case BOOK3S_INTERRUPT_FP_UNAVAIL: |
| 1361 | case BOOK3S_INTERRUPT_ALTIVEC: |
| 1362 | case BOOK3S_INTERRUPT_VSX: |
| 1363 | { |
| 1364 | int ext_msr = 0; |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1365 | int emul; |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1366 | u32 last_inst; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1367 | |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1368 | if (vcpu->arch.hflags & BOOK3S_HFLAG_PAIRED_SINGLE) { |
| 1369 | /* Do paired single instruction emulation */ |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 1370 | emul = kvmppc_get_last_inst(vcpu, INST_GENERIC, |
| 1371 | &last_inst); |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1372 | if (emul == EMULATE_DONE) |
Thomas Huth | fcd4f3c | 2017-01-25 13:27:22 +0100 | [diff] [blame] | 1373 | r = kvmppc_exit_pr_progint(run, vcpu, exit_nr); |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1374 | else |
| 1375 | r = RESUME_GUEST; |
| 1376 | |
| 1377 | break; |
| 1378 | } |
| 1379 | |
| 1380 | /* Enable external provider */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1381 | switch (exit_nr) { |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1382 | case BOOK3S_INTERRUPT_FP_UNAVAIL: |
| 1383 | ext_msr = MSR_FP; |
| 1384 | break; |
| 1385 | |
| 1386 | case BOOK3S_INTERRUPT_ALTIVEC: |
| 1387 | ext_msr = MSR_VEC; |
| 1388 | break; |
| 1389 | |
| 1390 | case BOOK3S_INTERRUPT_VSX: |
| 1391 | ext_msr = MSR_VSX; |
| 1392 | break; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1395 | r = kvmppc_handle_ext(vcpu, exit_nr, ext_msr); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1396 | break; |
| 1397 | } |
| 1398 | case BOOK3S_INTERRUPT_ALIGNMENT: |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1399 | { |
Mihai Caraman | 51f0472 | 2014-07-23 19:06:21 +0300 | [diff] [blame] | 1400 | u32 last_inst; |
| 1401 | int emul = kvmppc_get_last_inst(vcpu, INST_GENERIC, &last_inst); |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1402 | |
| 1403 | if (emul == EMULATE_DONE) { |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1404 | u32 dsisr; |
| 1405 | u64 dar; |
| 1406 | |
| 1407 | dsisr = kvmppc_alignment_dsisr(vcpu, last_inst); |
| 1408 | dar = kvmppc_alignment_dar(vcpu, last_inst); |
| 1409 | |
| 1410 | kvmppc_set_dsisr(vcpu, dsisr); |
| 1411 | kvmppc_set_dar(vcpu, dar); |
| 1412 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1413 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 1414 | } |
| 1415 | r = RESUME_GUEST; |
| 1416 | break; |
Mihai Caraman | 9a26af6 | 2014-07-23 19:06:20 +0300 | [diff] [blame] | 1417 | } |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 1418 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 1419 | case BOOK3S_INTERRUPT_FAC_UNAVAIL: |
Simon Guo | 19c585e | 2018-05-23 15:02:02 +0800 | [diff] [blame] | 1420 | r = kvmppc_handle_fac(vcpu, vcpu->arch.shadow_fscr >> 56); |
Alexander Graf | 616dff8 | 2014-04-29 16:48:44 +0200 | [diff] [blame] | 1421 | break; |
| 1422 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1423 | case BOOK3S_INTERRUPT_MACHINE_CHECK: |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1424 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 1425 | r = RESUME_GUEST; |
| 1426 | break; |
Laurent Vivier | 11dd6ac | 2016-04-08 18:05:00 +0200 | [diff] [blame] | 1427 | case BOOK3S_INTERRUPT_TRACE: |
| 1428 | if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) { |
| 1429 | run->exit_reason = KVM_EXIT_DEBUG; |
| 1430 | r = RESUME_HOST; |
| 1431 | } else { |
| 1432 | kvmppc_book3s_queue_irqprio(vcpu, exit_nr); |
| 1433 | r = RESUME_GUEST; |
| 1434 | } |
| 1435 | break; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1436 | default: |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1437 | { |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1438 | ulong shadow_srr1 = vcpu->arch.shadow_srr1; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1439 | /* Ugh - bork here! What did we get? */ |
| 1440 | printk(KERN_EMERG "exit_nr=0x%x | pc=0x%lx | msr=0x%lx\n", |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1441 | exit_nr, kvmppc_get_pc(vcpu), shadow_srr1); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1442 | r = RESUME_HOST; |
| 1443 | BUG(); |
| 1444 | break; |
| 1445 | } |
Alexander Graf | 468a12c | 2011-12-09 14:44:13 +0100 | [diff] [blame] | 1446 | } |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1447 | |
| 1448 | if (!(r & RESUME_HOST)) { |
| 1449 | /* To avoid clobbering exit_reason, only check for signals if |
| 1450 | * we aren't already exiting to userspace for some other |
| 1451 | * reason. */ |
Alexander Graf | e371f71 | 2011-12-19 13:36:55 +0100 | [diff] [blame] | 1452 | |
| 1453 | /* |
| 1454 | * Interrupts could be timers for the guest which we have to |
| 1455 | * inject again, so let's postpone them until we're in the guest |
| 1456 | * and if we really did time things so badly, then we just exit |
| 1457 | * again due to a host external interrupt. |
| 1458 | */ |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 1459 | s = kvmppc_prepare_to_enter(vcpu); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 1460 | if (s <= 0) |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 1461 | r = s; |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 1462 | else { |
| 1463 | /* interrupts now hard-disabled */ |
Scott Wood | 5f1c248 | 2013-07-10 17:47:39 -0500 | [diff] [blame] | 1464 | kvmppc_fix_ee_before_entry(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1465 | } |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 1466 | |
Paul Mackerras | 9d1ffdd | 2013-08-06 14:14:33 +1000 | [diff] [blame] | 1467 | kvmppc_handle_lost_ext(vcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
| 1470 | trace_kvm_book3s_reenter(r, vcpu); |
| 1471 | |
| 1472 | return r; |
| 1473 | } |
| 1474 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1475 | static int kvm_arch_vcpu_ioctl_get_sregs_pr(struct kvm_vcpu *vcpu, |
| 1476 | struct kvm_sregs *sregs) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1477 | { |
| 1478 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 1479 | int i; |
| 1480 | |
| 1481 | sregs->pvr = vcpu->arch.pvr; |
| 1482 | |
| 1483 | sregs->u.s.sdr1 = to_book3s(vcpu)->sdr1; |
| 1484 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { |
| 1485 | for (i = 0; i < 64; i++) { |
| 1486 | sregs->u.s.ppc64.slb[i].slbe = vcpu->arch.slb[i].orige | i; |
| 1487 | sregs->u.s.ppc64.slb[i].slbv = vcpu->arch.slb[i].origv; |
| 1488 | } |
| 1489 | } else { |
| 1490 | for (i = 0; i < 16; i++) |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1491 | sregs->u.s.ppc32.sr[i] = kvmppc_get_sr(vcpu, i); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1492 | |
| 1493 | for (i = 0; i < 8; i++) { |
| 1494 | sregs->u.s.ppc32.ibat[i] = vcpu3s->ibat[i].raw; |
| 1495 | sregs->u.s.ppc32.dbat[i] = vcpu3s->dbat[i].raw; |
| 1496 | } |
| 1497 | } |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1502 | static int kvm_arch_vcpu_ioctl_set_sregs_pr(struct kvm_vcpu *vcpu, |
| 1503 | struct kvm_sregs *sregs) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1504 | { |
| 1505 | struct kvmppc_vcpu_book3s *vcpu3s = to_book3s(vcpu); |
| 1506 | int i; |
| 1507 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1508 | kvmppc_set_pvr_pr(vcpu, sregs->pvr); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1509 | |
| 1510 | vcpu3s->sdr1 = sregs->u.s.sdr1; |
Greg Kurz | f4093ee | 2017-10-16 12:29:44 +0200 | [diff] [blame] | 1511 | #ifdef CONFIG_PPC_BOOK3S_64 |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1512 | if (vcpu->arch.hflags & BOOK3S_HFLAG_SLB) { |
Greg Kurz | f4093ee | 2017-10-16 12:29:44 +0200 | [diff] [blame] | 1513 | /* Flush all SLB entries */ |
| 1514 | vcpu->arch.mmu.slbmte(vcpu, 0, 0); |
| 1515 | vcpu->arch.mmu.slbia(vcpu); |
| 1516 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1517 | for (i = 0; i < 64; i++) { |
Greg Kurz | f4093ee | 2017-10-16 12:29:44 +0200 | [diff] [blame] | 1518 | u64 rb = sregs->u.s.ppc64.slb[i].slbe; |
| 1519 | u64 rs = sregs->u.s.ppc64.slb[i].slbv; |
| 1520 | |
| 1521 | if (rb & SLB_ESID_V) |
| 1522 | vcpu->arch.mmu.slbmte(vcpu, rs, rb); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1523 | } |
Greg Kurz | f4093ee | 2017-10-16 12:29:44 +0200 | [diff] [blame] | 1524 | } else |
| 1525 | #endif |
| 1526 | { |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1527 | for (i = 0; i < 16; i++) { |
| 1528 | vcpu->arch.mmu.mtsrin(vcpu, i, sregs->u.s.ppc32.sr[i]); |
| 1529 | } |
| 1530 | for (i = 0; i < 8; i++) { |
| 1531 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), false, |
| 1532 | (u32)sregs->u.s.ppc32.ibat[i]); |
| 1533 | kvmppc_set_bat(vcpu, &(vcpu3s->ibat[i]), true, |
| 1534 | (u32)(sregs->u.s.ppc32.ibat[i] >> 32)); |
| 1535 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), false, |
| 1536 | (u32)sregs->u.s.ppc32.dbat[i]); |
| 1537 | kvmppc_set_bat(vcpu, &(vcpu3s->dbat[i]), true, |
| 1538 | (u32)(sregs->u.s.ppc32.dbat[i] >> 32)); |
| 1539 | } |
| 1540 | } |
| 1541 | |
| 1542 | /* Flush the MMU after messing with the segments */ |
| 1543 | kvmppc_mmu_pte_flush(vcpu, 0, 0); |
| 1544 | |
| 1545 | return 0; |
| 1546 | } |
| 1547 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1548 | static int kvmppc_get_one_reg_pr(struct kvm_vcpu *vcpu, u64 id, |
| 1549 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1550 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1551 | int r = 0; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1552 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1553 | switch (id) { |
Madhavan Srinivasan | a59c1d9 | 2014-09-09 22:37:35 +0530 | [diff] [blame] | 1554 | case KVM_REG_PPC_DEBUG_INST: |
| 1555 | *val = get_reg_val(id, KVMPPC_INST_SW_BREAKPOINT); |
| 1556 | break; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1557 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1558 | *val = get_reg_val(id, to_book3s(vcpu)->hior); |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1559 | break; |
Paul Mackerras | 88b02cf9 | 2016-09-15 13:42:52 +1000 | [diff] [blame] | 1560 | case KVM_REG_PPC_VTB: |
| 1561 | *val = get_reg_val(id, to_book3s(vcpu)->vtb); |
| 1562 | break; |
Aneesh Kumar K.V | e5ee542 | 2014-05-05 08:39:44 +0530 | [diff] [blame] | 1563 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1564 | case KVM_REG_PPC_LPCR_64: |
Aneesh Kumar K.V | e5ee542 | 2014-05-05 08:39:44 +0530 | [diff] [blame] | 1565 | /* |
| 1566 | * We are only interested in the LPCR_ILE bit |
| 1567 | */ |
| 1568 | if (vcpu->arch.intr_msr & MSR_LE) |
| 1569 | *val = get_reg_val(id, LPCR_ILE); |
| 1570 | else |
| 1571 | *val = get_reg_val(id, 0); |
| 1572 | break; |
Simon Guo | deeb879 | 2018-05-23 15:02:12 +0800 | [diff] [blame] | 1573 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1574 | case KVM_REG_PPC_TFHAR: |
| 1575 | *val = get_reg_val(id, vcpu->arch.tfhar); |
| 1576 | break; |
| 1577 | case KVM_REG_PPC_TFIAR: |
| 1578 | *val = get_reg_val(id, vcpu->arch.tfiar); |
| 1579 | break; |
| 1580 | case KVM_REG_PPC_TEXASR: |
| 1581 | *val = get_reg_val(id, vcpu->arch.texasr); |
| 1582 | break; |
| 1583 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 1584 | *val = get_reg_val(id, |
| 1585 | vcpu->arch.gpr_tm[id-KVM_REG_PPC_TM_GPR0]); |
| 1586 | break; |
| 1587 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 1588 | { |
| 1589 | int i, j; |
| 1590 | |
| 1591 | i = id - KVM_REG_PPC_TM_VSR0; |
| 1592 | if (i < 32) |
| 1593 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 1594 | val->vsxval[j] = vcpu->arch.fp_tm.fpr[i][j]; |
| 1595 | else { |
| 1596 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1597 | val->vval = vcpu->arch.vr_tm.vr[i-32]; |
| 1598 | else |
| 1599 | r = -ENXIO; |
| 1600 | } |
| 1601 | break; |
| 1602 | } |
| 1603 | case KVM_REG_PPC_TM_CR: |
| 1604 | *val = get_reg_val(id, vcpu->arch.cr_tm); |
| 1605 | break; |
| 1606 | case KVM_REG_PPC_TM_XER: |
| 1607 | *val = get_reg_val(id, vcpu->arch.xer_tm); |
| 1608 | break; |
| 1609 | case KVM_REG_PPC_TM_LR: |
| 1610 | *val = get_reg_val(id, vcpu->arch.lr_tm); |
| 1611 | break; |
| 1612 | case KVM_REG_PPC_TM_CTR: |
| 1613 | *val = get_reg_val(id, vcpu->arch.ctr_tm); |
| 1614 | break; |
| 1615 | case KVM_REG_PPC_TM_FPSCR: |
| 1616 | *val = get_reg_val(id, vcpu->arch.fp_tm.fpscr); |
| 1617 | break; |
| 1618 | case KVM_REG_PPC_TM_AMR: |
| 1619 | *val = get_reg_val(id, vcpu->arch.amr_tm); |
| 1620 | break; |
| 1621 | case KVM_REG_PPC_TM_PPR: |
| 1622 | *val = get_reg_val(id, vcpu->arch.ppr_tm); |
| 1623 | break; |
| 1624 | case KVM_REG_PPC_TM_VRSAVE: |
| 1625 | *val = get_reg_val(id, vcpu->arch.vrsave_tm); |
| 1626 | break; |
| 1627 | case KVM_REG_PPC_TM_VSCR: |
| 1628 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1629 | *val = get_reg_val(id, vcpu->arch.vr_tm.vscr.u[3]); |
| 1630 | else |
| 1631 | r = -ENXIO; |
| 1632 | break; |
| 1633 | case KVM_REG_PPC_TM_DSCR: |
| 1634 | *val = get_reg_val(id, vcpu->arch.dscr_tm); |
| 1635 | break; |
| 1636 | case KVM_REG_PPC_TM_TAR: |
| 1637 | *val = get_reg_val(id, vcpu->arch.tar_tm); |
| 1638 | break; |
| 1639 | #endif |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1640 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1641 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1642 | break; |
| 1643 | } |
| 1644 | |
| 1645 | return r; |
| 1646 | } |
| 1647 | |
Aneesh Kumar K.V | e5ee542 | 2014-05-05 08:39:44 +0530 | [diff] [blame] | 1648 | static void kvmppc_set_lpcr_pr(struct kvm_vcpu *vcpu, u64 new_lpcr) |
| 1649 | { |
| 1650 | if (new_lpcr & LPCR_ILE) |
| 1651 | vcpu->arch.intr_msr |= MSR_LE; |
| 1652 | else |
| 1653 | vcpu->arch.intr_msr &= ~MSR_LE; |
| 1654 | } |
| 1655 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1656 | static int kvmppc_set_one_reg_pr(struct kvm_vcpu *vcpu, u64 id, |
| 1657 | union kvmppc_one_reg *val) |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1658 | { |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1659 | int r = 0; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1660 | |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1661 | switch (id) { |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1662 | case KVM_REG_PPC_HIOR: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1663 | to_book3s(vcpu)->hior = set_reg_val(id, *val); |
| 1664 | to_book3s(vcpu)->hior_explicit = true; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1665 | break; |
Paul Mackerras | 88b02cf9 | 2016-09-15 13:42:52 +1000 | [diff] [blame] | 1666 | case KVM_REG_PPC_VTB: |
| 1667 | to_book3s(vcpu)->vtb = set_reg_val(id, *val); |
| 1668 | break; |
Aneesh Kumar K.V | e5ee542 | 2014-05-05 08:39:44 +0530 | [diff] [blame] | 1669 | case KVM_REG_PPC_LPCR: |
Alexey Kardashevskiy | a084024 | 2014-07-19 17:59:34 +1000 | [diff] [blame] | 1670 | case KVM_REG_PPC_LPCR_64: |
Aneesh Kumar K.V | e5ee542 | 2014-05-05 08:39:44 +0530 | [diff] [blame] | 1671 | kvmppc_set_lpcr_pr(vcpu, set_reg_val(id, *val)); |
| 1672 | break; |
Simon Guo | deeb879 | 2018-05-23 15:02:12 +0800 | [diff] [blame] | 1673 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1674 | case KVM_REG_PPC_TFHAR: |
| 1675 | vcpu->arch.tfhar = set_reg_val(id, *val); |
| 1676 | break; |
| 1677 | case KVM_REG_PPC_TFIAR: |
| 1678 | vcpu->arch.tfiar = set_reg_val(id, *val); |
| 1679 | break; |
| 1680 | case KVM_REG_PPC_TEXASR: |
| 1681 | vcpu->arch.texasr = set_reg_val(id, *val); |
| 1682 | break; |
| 1683 | case KVM_REG_PPC_TM_GPR0 ... KVM_REG_PPC_TM_GPR31: |
| 1684 | vcpu->arch.gpr_tm[id - KVM_REG_PPC_TM_GPR0] = |
| 1685 | set_reg_val(id, *val); |
| 1686 | break; |
| 1687 | case KVM_REG_PPC_TM_VSR0 ... KVM_REG_PPC_TM_VSR63: |
| 1688 | { |
| 1689 | int i, j; |
| 1690 | |
| 1691 | i = id - KVM_REG_PPC_TM_VSR0; |
| 1692 | if (i < 32) |
| 1693 | for (j = 0; j < TS_FPRWIDTH; j++) |
| 1694 | vcpu->arch.fp_tm.fpr[i][j] = val->vsxval[j]; |
| 1695 | else |
| 1696 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1697 | vcpu->arch.vr_tm.vr[i-32] = val->vval; |
| 1698 | else |
| 1699 | r = -ENXIO; |
| 1700 | break; |
| 1701 | } |
| 1702 | case KVM_REG_PPC_TM_CR: |
| 1703 | vcpu->arch.cr_tm = set_reg_val(id, *val); |
| 1704 | break; |
| 1705 | case KVM_REG_PPC_TM_XER: |
| 1706 | vcpu->arch.xer_tm = set_reg_val(id, *val); |
| 1707 | break; |
| 1708 | case KVM_REG_PPC_TM_LR: |
| 1709 | vcpu->arch.lr_tm = set_reg_val(id, *val); |
| 1710 | break; |
| 1711 | case KVM_REG_PPC_TM_CTR: |
| 1712 | vcpu->arch.ctr_tm = set_reg_val(id, *val); |
| 1713 | break; |
| 1714 | case KVM_REG_PPC_TM_FPSCR: |
| 1715 | vcpu->arch.fp_tm.fpscr = set_reg_val(id, *val); |
| 1716 | break; |
| 1717 | case KVM_REG_PPC_TM_AMR: |
| 1718 | vcpu->arch.amr_tm = set_reg_val(id, *val); |
| 1719 | break; |
| 1720 | case KVM_REG_PPC_TM_PPR: |
| 1721 | vcpu->arch.ppr_tm = set_reg_val(id, *val); |
| 1722 | break; |
| 1723 | case KVM_REG_PPC_TM_VRSAVE: |
| 1724 | vcpu->arch.vrsave_tm = set_reg_val(id, *val); |
| 1725 | break; |
| 1726 | case KVM_REG_PPC_TM_VSCR: |
| 1727 | if (cpu_has_feature(CPU_FTR_ALTIVEC)) |
| 1728 | vcpu->arch.vr.vscr.u[3] = set_reg_val(id, *val); |
| 1729 | else |
| 1730 | r = -ENXIO; |
| 1731 | break; |
| 1732 | case KVM_REG_PPC_TM_DSCR: |
| 1733 | vcpu->arch.dscr_tm = set_reg_val(id, *val); |
| 1734 | break; |
| 1735 | case KVM_REG_PPC_TM_TAR: |
| 1736 | vcpu->arch.tar_tm = set_reg_val(id, *val); |
| 1737 | break; |
| 1738 | #endif |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1739 | default: |
Paul Mackerras | a136a8b | 2012-09-25 20:31:56 +0000 | [diff] [blame] | 1740 | r = -EINVAL; |
Paul Mackerras | 31f3438 | 2011-12-12 12:26:50 +0000 | [diff] [blame] | 1741 | break; |
| 1742 | } |
| 1743 | |
| 1744 | return r; |
| 1745 | } |
| 1746 | |
Sean Christopherson | ff030fd | 2019-12-18 13:55:00 -0800 | [diff] [blame] | 1747 | static int kvmppc_core_vcpu_create_pr(struct kvm_vcpu *vcpu) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1748 | { |
| 1749 | struct kvmppc_vcpu_book3s *vcpu_book3s; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1750 | unsigned long p; |
Sean Christopherson | d307695 | 2019-12-18 13:54:58 -0800 | [diff] [blame] | 1751 | int err; |
| 1752 | |
Sean Christopherson | d307695 | 2019-12-18 13:54:58 -0800 | [diff] [blame] | 1753 | err = -ENOMEM; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1754 | |
Paul Mackerras | 3ff9550 | 2013-09-20 14:52:49 +1000 | [diff] [blame] | 1755 | vcpu_book3s = vzalloc(sizeof(struct kvmppc_vcpu_book3s)); |
| 1756 | if (!vcpu_book3s) |
Sean Christopherson | ff030fd | 2019-12-18 13:55:00 -0800 | [diff] [blame] | 1757 | goto out; |
Paul Mackerras | 3ff9550 | 2013-09-20 14:52:49 +1000 | [diff] [blame] | 1758 | vcpu->arch.book3s = vcpu_book3s; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1759 | |
Alexander Graf | ab78475 | 2014-04-06 23:31:48 +0200 | [diff] [blame] | 1760 | #ifdef CONFIG_KVM_BOOK3S_32_HANDLER |
Paul Mackerras | 3ff9550 | 2013-09-20 14:52:49 +1000 | [diff] [blame] | 1761 | vcpu->arch.shadow_vcpu = |
| 1762 | kzalloc(sizeof(*vcpu->arch.shadow_vcpu), GFP_KERNEL); |
| 1763 | if (!vcpu->arch.shadow_vcpu) |
| 1764 | goto free_vcpu3s; |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1765 | #endif |
Paul Mackerras | 3ff9550 | 2013-09-20 14:52:49 +1000 | [diff] [blame] | 1766 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1767 | p = __get_free_page(GFP_KERNEL|__GFP_ZERO); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1768 | if (!p) |
Sean Christopherson | d307695 | 2019-12-18 13:54:58 -0800 | [diff] [blame] | 1769 | goto free_shadow_vcpu; |
Alexander Graf | 89b68c9 | 2014-07-13 16:37:12 +0200 | [diff] [blame] | 1770 | vcpu->arch.shared = (void *)p; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1771 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1772 | /* Always start the shared struct in native endian mode */ |
| 1773 | #ifdef __BIG_ENDIAN__ |
| 1774 | vcpu->arch.shared_big_endian = true; |
| 1775 | #else |
| 1776 | vcpu->arch.shared_big_endian = false; |
| 1777 | #endif |
| 1778 | |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 1779 | /* |
| 1780 | * Default to the same as the host if we're on sufficiently |
| 1781 | * recent machine that we have 1TB segments; |
| 1782 | * otherwise default to PPC970FX. |
| 1783 | */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1784 | vcpu->arch.pvr = 0x3C0301; |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 1785 | if (mmu_has_feature(MMU_FTR_1T_SEGMENT)) |
| 1786 | vcpu->arch.pvr = mfspr(SPRN_PVR); |
Aneesh Kumar K.V | e5ee542 | 2014-05-05 08:39:44 +0530 | [diff] [blame] | 1787 | vcpu->arch.intr_msr = MSR_SF; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1788 | #else |
| 1789 | /* default to book3s_32 (750) */ |
| 1790 | vcpu->arch.pvr = 0x84202; |
Nicholas Piggin | 87a45e0 | 2019-10-02 16:00:22 +1000 | [diff] [blame] | 1791 | vcpu->arch.intr_msr = 0; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1792 | #endif |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1793 | kvmppc_set_pvr_pr(vcpu, vcpu->arch.pvr); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1794 | vcpu->arch.slb_nr = 64; |
| 1795 | |
Alexander Graf | 94810ba | 2014-04-24 13:04:01 +0200 | [diff] [blame] | 1796 | vcpu->arch.shadow_msr = MSR_USER64 & ~MSR_LE; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1797 | |
| 1798 | err = kvmppc_mmu_init(vcpu); |
| 1799 | if (err < 0) |
Sean Christopherson | cb10bf9 | 2019-12-18 13:54:47 -0800 | [diff] [blame] | 1800 | goto free_shared_page; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1801 | |
Sean Christopherson | c50bfbd | 2019-12-18 13:54:57 -0800 | [diff] [blame] | 1802 | return 0; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1803 | |
Sean Christopherson | cb10bf9 | 2019-12-18 13:54:47 -0800 | [diff] [blame] | 1804 | free_shared_page: |
| 1805 | free_page((unsigned long)vcpu->arch.shared); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1806 | free_shadow_vcpu: |
Alexander Graf | ab78475 | 2014-04-06 23:31:48 +0200 | [diff] [blame] | 1807 | #ifdef CONFIG_KVM_BOOK3S_32_HANDLER |
Paul Mackerras | 3ff9550 | 2013-09-20 14:52:49 +1000 | [diff] [blame] | 1808 | kfree(vcpu->arch.shadow_vcpu); |
| 1809 | free_vcpu3s: |
Paul Mackerras | a2d5602 | 2013-09-20 14:52:43 +1000 | [diff] [blame] | 1810 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1811 | vfree(vcpu_book3s); |
Sean Christopherson | ff030fd | 2019-12-18 13:55:00 -0800 | [diff] [blame] | 1812 | out: |
Sean Christopherson | c50bfbd | 2019-12-18 13:54:57 -0800 | [diff] [blame] | 1813 | return err; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1816 | static void kvmppc_core_vcpu_free_pr(struct kvm_vcpu *vcpu) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1817 | { |
| 1818 | struct kvmppc_vcpu_book3s *vcpu_book3s = to_book3s(vcpu); |
| 1819 | |
Greg Kurz | 1d0c32e | 2020-03-18 18:43:30 +0100 | [diff] [blame] | 1820 | kvmppc_mmu_destroy_pr(vcpu); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1821 | free_page((unsigned long)vcpu->arch.shared & PAGE_MASK); |
Alexander Graf | ab78475 | 2014-04-06 23:31:48 +0200 | [diff] [blame] | 1822 | #ifdef CONFIG_KVM_BOOK3S_32_HANDLER |
Paul Mackerras | 3ff9550 | 2013-09-20 14:52:49 +1000 | [diff] [blame] | 1823 | kfree(vcpu->arch.shadow_vcpu); |
| 1824 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1825 | vfree(vcpu_book3s); |
| 1826 | } |
| 1827 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1828 | static int kvmppc_vcpu_run_pr(struct kvm_run *kvm_run, struct kvm_vcpu *vcpu) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1829 | { |
| 1830 | int ret; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1831 | #ifdef CONFIG_ALTIVEC |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1832 | unsigned long uninitialized_var(vrsave); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1833 | #endif |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1834 | |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1835 | /* Check if we can run the vcpu at all */ |
| 1836 | if (!vcpu->arch.sane) { |
| 1837 | kvm_run->exit_reason = KVM_EXIT_INTERNAL_ERROR; |
Alexander Graf | 7d82714 | 2011-12-09 15:46:21 +0100 | [diff] [blame] | 1838 | ret = -EINVAL; |
| 1839 | goto out; |
Alexander Graf | af8f38b | 2011-08-10 13:57:08 +0200 | [diff] [blame] | 1840 | } |
| 1841 | |
Laurent Vivier | 11dd6ac | 2016-04-08 18:05:00 +0200 | [diff] [blame] | 1842 | kvmppc_setup_debug(vcpu); |
| 1843 | |
Alexander Graf | e371f71 | 2011-12-19 13:36:55 +0100 | [diff] [blame] | 1844 | /* |
| 1845 | * Interrupts could be timers for the guest which we have to inject |
| 1846 | * again, so let's postpone them until we're in the guest and if we |
| 1847 | * really did time things so badly, then we just exit again due to |
| 1848 | * a host external interrupt. |
| 1849 | */ |
Alexander Graf | 7ee7885 | 2012-08-13 12:44:41 +0200 | [diff] [blame] | 1850 | ret = kvmppc_prepare_to_enter(vcpu); |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 1851 | if (ret <= 0) |
Alexander Graf | 7d82714 | 2011-12-09 15:46:21 +0100 | [diff] [blame] | 1852 | goto out; |
Scott Wood | 6c85f52 | 2014-01-09 19:18:40 -0600 | [diff] [blame] | 1853 | /* interrupts now hard-disabled */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1854 | |
Anton Blanchard | c208505 | 2015-10-29 11:44:08 +1100 | [diff] [blame] | 1855 | /* Save FPU, Altivec and VSX state */ |
| 1856 | giveup_all(current); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1857 | |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1858 | /* Preload FPU if it's enabled */ |
Alexander Graf | 5deb8e7 | 2014-04-24 13:46:24 +0200 | [diff] [blame] | 1859 | if (kvmppc_get_msr(vcpu) & MSR_FP) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1860 | kvmppc_handle_ext(vcpu, BOOK3S_INTERRUPT_FP_UNAVAIL, MSR_FP); |
| 1861 | |
Scott Wood | 5f1c248 | 2013-07-10 17:47:39 -0500 | [diff] [blame] | 1862 | kvmppc_fix_ee_before_entry(); |
Paul Mackerras | df6909e5 | 2011-06-29 00:19:50 +0000 | [diff] [blame] | 1863 | |
| 1864 | ret = __kvmppc_vcpu_run(kvm_run, vcpu); |
| 1865 | |
Laurent Vivier | 11dd6ac | 2016-04-08 18:05:00 +0200 | [diff] [blame] | 1866 | kvmppc_clear_debug(vcpu); |
| 1867 | |
Paolo Bonzini | 6edaa53 | 2016-06-15 15:18:26 +0200 | [diff] [blame] | 1868 | /* No need for guest_exit. It's done in handle_exit. |
Alexander Graf | 24afa37 | 2012-08-12 12:42:30 +0200 | [diff] [blame] | 1869 | We also get here with interrupts enabled. */ |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1870 | |
Paul Mackerras | 28c483b | 2012-11-04 18:16:46 +0000 | [diff] [blame] | 1871 | /* Make sure we save the guest FPU/Altivec/VSX state */ |
| 1872 | kvmppc_giveup_ext(vcpu, MSR_FP | MSR_VEC | MSR_VSX); |
| 1873 | |
Alexander Graf | e14e7a1 | 2014-04-22 12:26:58 +0200 | [diff] [blame] | 1874 | /* Make sure we save the guest TAR/EBB/DSCR state */ |
| 1875 | kvmppc_giveup_fac(vcpu, FSCR_TAR_LG); |
| 1876 | |
Alexander Graf | 7d82714 | 2011-12-09 15:46:21 +0100 | [diff] [blame] | 1877 | out: |
Alexander Graf | 0652eaa | 2012-08-12 11:34:21 +0200 | [diff] [blame] | 1878 | vcpu->mode = OUTSIDE_GUEST_MODE; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 1879 | return ret; |
| 1880 | } |
| 1881 | |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1882 | /* |
| 1883 | * Get (and clear) the dirty memory log for a memory slot. |
| 1884 | */ |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1885 | static int kvm_vm_ioctl_get_dirty_log_pr(struct kvm *kvm, |
| 1886 | struct kvm_dirty_log *log) |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1887 | { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 1888 | struct kvm_memslots *slots; |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1889 | struct kvm_memory_slot *memslot; |
| 1890 | struct kvm_vcpu *vcpu; |
| 1891 | ulong ga, ga_end; |
| 1892 | int is_dirty = 0; |
| 1893 | int r; |
| 1894 | unsigned long n; |
| 1895 | |
| 1896 | mutex_lock(&kvm->slots_lock); |
| 1897 | |
| 1898 | r = kvm_get_dirty_log(kvm, log, &is_dirty); |
| 1899 | if (r) |
| 1900 | goto out; |
| 1901 | |
| 1902 | /* If nothing is dirty, don't bother messing with page tables. */ |
| 1903 | if (is_dirty) { |
Paolo Bonzini | 9f6b802 | 2015-05-17 16:20:07 +0200 | [diff] [blame] | 1904 | slots = kvm_memslots(kvm); |
| 1905 | memslot = id_to_memslot(slots, log->slot); |
Paul Mackerras | 82ed361 | 2011-12-15 02:03:22 +0000 | [diff] [blame] | 1906 | |
| 1907 | ga = memslot->base_gfn << PAGE_SHIFT; |
| 1908 | ga_end = ga + (memslot->npages << PAGE_SHIFT); |
| 1909 | |
| 1910 | kvm_for_each_vcpu(n, vcpu, kvm) |
| 1911 | kvmppc_mmu_pte_pflush(vcpu, ga, ga_end); |
| 1912 | |
| 1913 | n = kvm_dirty_bitmap_bytes(memslot); |
| 1914 | memset(memslot->dirty_bitmap, 0, n); |
| 1915 | } |
| 1916 | |
| 1917 | r = 0; |
| 1918 | out: |
| 1919 | mutex_unlock(&kvm->slots_lock); |
| 1920 | return r; |
| 1921 | } |
| 1922 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1923 | static void kvmppc_core_flush_memslot_pr(struct kvm *kvm, |
| 1924 | struct kvm_memory_slot *memslot) |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1925 | { |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1926 | return; |
| 1927 | } |
| 1928 | |
| 1929 | static int kvmppc_core_prepare_memory_region_pr(struct kvm *kvm, |
| 1930 | struct kvm_memory_slot *memslot, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1931 | const struct kvm_userspace_memory_region *mem) |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1932 | { |
| 1933 | return 0; |
| 1934 | } |
| 1935 | |
| 1936 | static void kvmppc_core_commit_memory_region_pr(struct kvm *kvm, |
Paolo Bonzini | 09170a4 | 2015-05-18 13:59:39 +0200 | [diff] [blame] | 1937 | const struct kvm_userspace_memory_region *mem, |
Paolo Bonzini | f36f3f2 | 2015-05-18 13:20:23 +0200 | [diff] [blame] | 1938 | const struct kvm_memory_slot *old, |
Bharata B Rao | f032b73 | 2018-12-12 15:15:30 +1100 | [diff] [blame] | 1939 | const struct kvm_memory_slot *new, |
| 1940 | enum kvm_mr_change change) |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1941 | { |
| 1942 | return; |
| 1943 | } |
| 1944 | |
| 1945 | static void kvmppc_core_free_memslot_pr(struct kvm_memory_slot *free, |
| 1946 | struct kvm_memory_slot *dont) |
| 1947 | { |
| 1948 | return; |
| 1949 | } |
| 1950 | |
| 1951 | static int kvmppc_core_create_memslot_pr(struct kvm_memory_slot *slot, |
| 1952 | unsigned long npages) |
| 1953 | { |
| 1954 | return 0; |
| 1955 | } |
| 1956 | |
| 1957 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1958 | #ifdef CONFIG_PPC64 |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 1959 | static int kvm_vm_ioctl_get_smmu_info_pr(struct kvm *kvm, |
| 1960 | struct kvm_ppc_smmu_info *info) |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1961 | { |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 1962 | long int i; |
| 1963 | struct kvm_vcpu *vcpu; |
| 1964 | |
| 1965 | info->flags = 0; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1966 | |
| 1967 | /* SLB is always 64 entries */ |
| 1968 | info->slb_size = 64; |
| 1969 | |
| 1970 | /* Standard 4k base page size segment */ |
| 1971 | info->sps[0].page_shift = 12; |
| 1972 | info->sps[0].slb_enc = 0; |
| 1973 | info->sps[0].enc[0].page_shift = 12; |
| 1974 | info->sps[0].enc[0].pte_enc = 0; |
| 1975 | |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 1976 | /* |
| 1977 | * 64k large page size. |
| 1978 | * We only want to put this in if the CPUs we're emulating |
| 1979 | * support it, but unfortunately we don't have a vcpu easily |
| 1980 | * to hand here to test. Just pick the first vcpu, and if |
| 1981 | * that doesn't exist yet, report the minimum capability, |
| 1982 | * i.e., no 64k pages. |
| 1983 | * 1T segment support goes along with 64k pages. |
| 1984 | */ |
| 1985 | i = 1; |
| 1986 | vcpu = kvm_get_vcpu(kvm, 0); |
| 1987 | if (vcpu && (vcpu->arch.hflags & BOOK3S_HFLAG_MULTI_PGSIZE)) { |
| 1988 | info->flags = KVM_PPC_1T_SEGMENTS; |
| 1989 | info->sps[i].page_shift = 16; |
| 1990 | info->sps[i].slb_enc = SLB_VSID_L | SLB_VSID_LP_01; |
| 1991 | info->sps[i].enc[0].page_shift = 16; |
| 1992 | info->sps[i].enc[0].pte_enc = 1; |
| 1993 | ++i; |
| 1994 | } |
| 1995 | |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 1996 | /* Standard 16M large page size segment */ |
Paul Mackerras | a4a0f25 | 2013-09-20 14:52:44 +1000 | [diff] [blame] | 1997 | info->sps[i].page_shift = 24; |
| 1998 | info->sps[i].slb_enc = SLB_VSID_L; |
| 1999 | info->sps[i].enc[0].page_shift = 24; |
| 2000 | info->sps[i].enc[0].pte_enc = 0; |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2001 | |
| 2002 | return 0; |
| 2003 | } |
Paul Mackerras | 9617a0b | 2018-05-30 15:47:17 +1000 | [diff] [blame] | 2004 | |
| 2005 | static int kvm_configure_mmu_pr(struct kvm *kvm, struct kvm_ppc_mmuv3_cfg *cfg) |
| 2006 | { |
| 2007 | if (!cpu_has_feature(CPU_FTR_ARCH_300)) |
| 2008 | return -ENODEV; |
| 2009 | /* Require flags and process table base and size to all be zero. */ |
| 2010 | if (cfg->flags || cfg->process_table) |
| 2011 | return -EINVAL; |
| 2012 | return 0; |
| 2013 | } |
| 2014 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2015 | #else |
| 2016 | static int kvm_vm_ioctl_get_smmu_info_pr(struct kvm *kvm, |
| 2017 | struct kvm_ppc_smmu_info *info) |
| 2018 | { |
| 2019 | /* We should not get called */ |
| 2020 | BUG(); |
David Michael | fd24a86 | 2020-01-26 17:31:58 -0500 | [diff] [blame] | 2021 | return 0; |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2022 | } |
Benjamin Herrenschmidt | 5b74716 | 2012-04-26 19:43:42 +0000 | [diff] [blame] | 2023 | #endif /* CONFIG_PPC64 */ |
| 2024 | |
Ian Munsie | a413f47 | 2012-12-03 18:36:13 +0000 | [diff] [blame] | 2025 | static unsigned int kvm_global_user_count = 0; |
| 2026 | static DEFINE_SPINLOCK(kvm_global_user_count_lock); |
| 2027 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2028 | static int kvmppc_core_init_vm_pr(struct kvm *kvm) |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 2029 | { |
Paul Mackerras | 9308ab8 | 2013-09-20 14:52:48 +1000 | [diff] [blame] | 2030 | mutex_init(&kvm->arch.hpt_mutex); |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 2031 | |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 2032 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 2033 | /* Start out with the default set of hcalls enabled */ |
| 2034 | kvmppc_pr_init_default_hcalls(kvm); |
| 2035 | #endif |
| 2036 | |
Ian Munsie | a413f47 | 2012-12-03 18:36:13 +0000 | [diff] [blame] | 2037 | if (firmware_has_feature(FW_FEATURE_SET_MODE)) { |
| 2038 | spin_lock(&kvm_global_user_count_lock); |
| 2039 | if (++kvm_global_user_count == 1) |
Benjamin Herrenschmidt | d3cbff1 | 2016-07-05 15:03:49 +1000 | [diff] [blame] | 2040 | pseries_disable_reloc_on_exc(); |
Ian Munsie | a413f47 | 2012-12-03 18:36:13 +0000 | [diff] [blame] | 2041 | spin_unlock(&kvm_global_user_count_lock); |
| 2042 | } |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 2043 | return 0; |
| 2044 | } |
| 2045 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2046 | static void kvmppc_core_destroy_vm_pr(struct kvm *kvm) |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 2047 | { |
Benjamin Herrenschmidt | f31e65e | 2012-03-15 21:58:34 +0000 | [diff] [blame] | 2048 | #ifdef CONFIG_PPC64 |
| 2049 | WARN_ON(!list_empty(&kvm->arch.spapr_tce_tables)); |
| 2050 | #endif |
Ian Munsie | a413f47 | 2012-12-03 18:36:13 +0000 | [diff] [blame] | 2051 | |
| 2052 | if (firmware_has_feature(FW_FEATURE_SET_MODE)) { |
| 2053 | spin_lock(&kvm_global_user_count_lock); |
| 2054 | BUG_ON(kvm_global_user_count == 0); |
| 2055 | if (--kvm_global_user_count == 0) |
Benjamin Herrenschmidt | d3cbff1 | 2016-07-05 15:03:49 +1000 | [diff] [blame] | 2056 | pseries_enable_reloc_on_exc(); |
Ian Munsie | a413f47 | 2012-12-03 18:36:13 +0000 | [diff] [blame] | 2057 | spin_unlock(&kvm_global_user_count_lock); |
| 2058 | } |
Paul Mackerras | f9e0554 | 2011-06-29 00:19:22 +0000 | [diff] [blame] | 2059 | } |
| 2060 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2061 | static int kvmppc_core_check_processor_compat_pr(void) |
| 2062 | { |
Aneesh Kumar K.V | 50de596 | 2016-04-29 23:25:43 +1000 | [diff] [blame] | 2063 | /* |
Paul Mackerras | ec531d0 | 2018-05-18 21:49:28 +1000 | [diff] [blame] | 2064 | * PR KVM can work on POWER9 inside a guest partition |
| 2065 | * running in HPT mode. It can't work if we are using |
| 2066 | * radix translation (because radix provides no way for |
Paul Mackerras | db96a04 | 2018-06-07 18:08:02 +1000 | [diff] [blame] | 2067 | * a process to have unique translations in quadrant 3). |
Aneesh Kumar K.V | 50de596 | 2016-04-29 23:25:43 +1000 | [diff] [blame] | 2068 | */ |
Paul Mackerras | db96a04 | 2018-06-07 18:08:02 +1000 | [diff] [blame] | 2069 | if (cpu_has_feature(CPU_FTR_ARCH_300) && radix_enabled()) |
Aneesh Kumar K.V | 50de596 | 2016-04-29 23:25:43 +1000 | [diff] [blame] | 2070 | return -EIO; |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2071 | return 0; |
| 2072 | } |
| 2073 | |
| 2074 | static long kvm_arch_vm_ioctl_pr(struct file *filp, |
| 2075 | unsigned int ioctl, unsigned long arg) |
| 2076 | { |
| 2077 | return -ENOTTY; |
| 2078 | } |
| 2079 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2080 | static struct kvmppc_ops kvm_ops_pr = { |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2081 | .get_sregs = kvm_arch_vcpu_ioctl_get_sregs_pr, |
| 2082 | .set_sregs = kvm_arch_vcpu_ioctl_set_sregs_pr, |
| 2083 | .get_one_reg = kvmppc_get_one_reg_pr, |
| 2084 | .set_one_reg = kvmppc_set_one_reg_pr, |
| 2085 | .vcpu_load = kvmppc_core_vcpu_load_pr, |
| 2086 | .vcpu_put = kvmppc_core_vcpu_put_pr, |
Nicholas Piggin | 87a45e0 | 2019-10-02 16:00:22 +1000 | [diff] [blame] | 2087 | .inject_interrupt = kvmppc_inject_interrupt_pr, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2088 | .set_msr = kvmppc_set_msr_pr, |
| 2089 | .vcpu_run = kvmppc_vcpu_run_pr, |
| 2090 | .vcpu_create = kvmppc_core_vcpu_create_pr, |
| 2091 | .vcpu_free = kvmppc_core_vcpu_free_pr, |
| 2092 | .check_requests = kvmppc_core_check_requests_pr, |
| 2093 | .get_dirty_log = kvm_vm_ioctl_get_dirty_log_pr, |
| 2094 | .flush_memslot = kvmppc_core_flush_memslot_pr, |
| 2095 | .prepare_memory_region = kvmppc_core_prepare_memory_region_pr, |
| 2096 | .commit_memory_region = kvmppc_core_commit_memory_region_pr, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2097 | .unmap_hva_range = kvm_unmap_hva_range_pr, |
| 2098 | .age_hva = kvm_age_hva_pr, |
| 2099 | .test_age_hva = kvm_test_age_hva_pr, |
| 2100 | .set_spte_hva = kvm_set_spte_hva_pr, |
| 2101 | .mmu_destroy = kvmppc_mmu_destroy_pr, |
| 2102 | .free_memslot = kvmppc_core_free_memslot_pr, |
| 2103 | .create_memslot = kvmppc_core_create_memslot_pr, |
| 2104 | .init_vm = kvmppc_core_init_vm_pr, |
| 2105 | .destroy_vm = kvmppc_core_destroy_vm_pr, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2106 | .get_smmu_info = kvm_vm_ioctl_get_smmu_info_pr, |
| 2107 | .emulate_op = kvmppc_core_emulate_op_pr, |
| 2108 | .emulate_mtspr = kvmppc_core_emulate_mtspr_pr, |
| 2109 | .emulate_mfspr = kvmppc_core_emulate_mfspr_pr, |
| 2110 | .fast_vcpu_kick = kvm_vcpu_kick, |
| 2111 | .arch_vm_ioctl = kvm_arch_vm_ioctl_pr, |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 2112 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 2113 | .hcall_implemented = kvmppc_hcall_impl_pr, |
Paul Mackerras | 9617a0b | 2018-05-30 15:47:17 +1000 | [diff] [blame] | 2114 | .configure_mmu = kvm_configure_mmu_pr, |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 2115 | #endif |
Simon Guo | 2e6baa4 | 2018-05-21 13:24:22 +0800 | [diff] [blame] | 2116 | .giveup_ext = kvmppc_giveup_ext, |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2117 | }; |
| 2118 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2119 | |
| 2120 | int kvmppc_book3s_init_pr(void) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2121 | { |
| 2122 | int r; |
| 2123 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2124 | r = kvmppc_core_check_processor_compat_pr(); |
| 2125 | if (r < 0) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2126 | return r; |
| 2127 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2128 | kvm_ops_pr.owner = THIS_MODULE; |
| 2129 | kvmppc_pr_ops = &kvm_ops_pr; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2130 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2131 | r = kvmppc_mmu_hpte_sysinit(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2132 | return r; |
| 2133 | } |
| 2134 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2135 | void kvmppc_book3s_exit_pr(void) |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2136 | { |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2137 | kvmppc_pr_ops = NULL; |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2138 | kvmppc_mmu_hpte_sysexit(); |
Paul Mackerras | f05ed4d | 2011-06-29 00:17:58 +0000 | [diff] [blame] | 2139 | } |
| 2140 | |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2141 | /* |
| 2142 | * We only support separate modules for book3s 64 |
| 2143 | */ |
| 2144 | #ifdef CONFIG_PPC_BOOK3S_64 |
| 2145 | |
Aneesh Kumar K.V | 3a167bea | 2013-10-07 22:17:53 +0530 | [diff] [blame] | 2146 | module_init(kvmppc_book3s_init_pr); |
| 2147 | module_exit(kvmppc_book3s_exit_pr); |
Aneesh Kumar K.V | 2ba9f0d | 2013-10-07 22:17:59 +0530 | [diff] [blame] | 2148 | |
| 2149 | MODULE_LICENSE("GPL"); |
Alexander Graf | 398a76c | 2013-12-09 13:53:42 +0100 | [diff] [blame] | 2150 | MODULE_ALIAS_MISCDEV(KVM_MINOR); |
| 2151 | MODULE_ALIAS("devname:kvm"); |
Aneesh Kumar K.V | cbbc58d | 2013-10-07 22:18:01 +0530 | [diff] [blame] | 2152 | #endif |