Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This program is free software; you can redistribute it and/or modify |
| 3 | * it under the terms of the GNU General Public License, version 2, as |
| 4 | * published by the Free Software Foundation. |
| 5 | * |
| 6 | * This program is distributed in the hope that it will be useful, |
| 7 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 8 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 9 | * GNU General Public License for more details. |
| 10 | * |
| 11 | * Copyright 2011 Paul Mackerras, IBM Corp. <paulus@au1.ibm.com> |
| 12 | * |
| 13 | * Derived from book3s_rmhandlers.S and other files, which are: |
| 14 | * |
| 15 | * Copyright SUSE Linux Products GmbH 2009 |
| 16 | * |
| 17 | * Authors: Alexander Graf <agraf@suse.de> |
| 18 | */ |
| 19 | |
| 20 | #include <asm/ppc_asm.h> |
| 21 | #include <asm/kvm_asm.h> |
| 22 | #include <asm/reg.h> |
Paul Mackerras | 177339d | 2011-07-23 17:41:11 +1000 | [diff] [blame] | 23 | #include <asm/mmu.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 24 | #include <asm/page.h> |
Paul Mackerras | 177339d | 2011-07-23 17:41:11 +1000 | [diff] [blame] | 25 | #include <asm/ptrace.h> |
| 26 | #include <asm/hvcall.h> |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 27 | #include <asm/asm-offsets.h> |
| 28 | #include <asm/exception-64s.h> |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 29 | #include <asm/kvm_book3s_asm.h> |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 30 | #include <asm/mmu-hash64.h> |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 31 | #include <asm/tm.h> |
| 32 | |
| 33 | #define VCPU_GPRS_TM(reg) (((reg) * ULONG_SIZE) + VCPU_GPR_TM) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 34 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 35 | /* Values in HSTATE_NAPPING(r13) */ |
| 36 | #define NAPPING_CEDE 1 |
| 37 | #define NAPPING_NOVCPU 2 |
| 38 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 39 | /* |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 40 | * Call kvmppc_hv_entry in real mode. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 41 | * Must be called with interrupts hard-disabled. |
| 42 | * |
| 43 | * Input Registers: |
| 44 | * |
| 45 | * LR = return address to continue at after eventually re-enabling MMU |
| 46 | */ |
Anton Blanchard | 6ed179b | 2014-06-12 18:16:53 +1000 | [diff] [blame] | 47 | _GLOBAL_TOC(kvmppc_hv_entry_trampoline) |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 48 | mflr r0 |
| 49 | std r0, PPC_LR_STKOFF(r1) |
| 50 | stdu r1, -112(r1) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 51 | mfmsr r10 |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 52 | LOAD_REG_ADDR(r5, kvmppc_call_hv_entry) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 53 | li r0,MSR_RI |
| 54 | andc r0,r10,r0 |
| 55 | li r6,MSR_IR | MSR_DR |
| 56 | andc r6,r10,r6 |
| 57 | mtmsrd r0,1 /* clear RI in MSR */ |
| 58 | mtsrr0 r5 |
| 59 | mtsrr1 r6 |
| 60 | RFI |
| 61 | |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 62 | kvmppc_call_hv_entry: |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 63 | ld r4, HSTATE_KVM_VCPU(r13) |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 64 | bl kvmppc_hv_entry |
| 65 | |
| 66 | /* Back from guest - restore host state and return to caller */ |
| 67 | |
Michael Neuling | eee7ff9 | 2014-01-08 21:25:19 +1100 | [diff] [blame] | 68 | BEGIN_FTR_SECTION |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 69 | /* Restore host DABR and DABRX */ |
| 70 | ld r5,HSTATE_DABR(r13) |
| 71 | li r6,7 |
| 72 | mtspr SPRN_DABR,r5 |
| 73 | mtspr SPRN_DABRX,r6 |
Michael Neuling | eee7ff9 | 2014-01-08 21:25:19 +1100 | [diff] [blame] | 74 | END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 75 | |
| 76 | /* Restore SPRG3 */ |
Scott Wood | 9d378df | 2014-03-10 17:29:38 -0500 | [diff] [blame] | 77 | ld r3,PACA_SPRG_VDSO(r13) |
| 78 | mtspr SPRN_SPRG_VDSO_WRITE,r3 |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 79 | |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 80 | /* Reload the host's PMU registers */ |
| 81 | ld r3, PACALPPACAPTR(r13) /* is the host using the PMU? */ |
| 82 | lbz r4, LPPACA_PMCINUSE(r3) |
| 83 | cmpwi r4, 0 |
| 84 | beq 23f /* skip if not */ |
Paul Mackerras | 9bc01a9 | 2014-05-26 19:48:40 +1000 | [diff] [blame] | 85 | BEGIN_FTR_SECTION |
Michael Ellerman | 9a4fc4e | 2014-07-10 19:34:31 +1000 | [diff] [blame] | 86 | ld r3, HSTATE_MMCR0(r13) |
Paul Mackerras | 9bc01a9 | 2014-05-26 19:48:40 +1000 | [diff] [blame] | 87 | andi. r4, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO |
| 88 | cmpwi r4, MMCR0_PMAO |
| 89 | beql kvmppc_fix_pmao |
| 90 | END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG) |
Michael Ellerman | 9a4fc4e | 2014-07-10 19:34:31 +1000 | [diff] [blame] | 91 | lwz r3, HSTATE_PMC1(r13) |
| 92 | lwz r4, HSTATE_PMC2(r13) |
| 93 | lwz r5, HSTATE_PMC3(r13) |
| 94 | lwz r6, HSTATE_PMC4(r13) |
| 95 | lwz r8, HSTATE_PMC5(r13) |
| 96 | lwz r9, HSTATE_PMC6(r13) |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 97 | mtspr SPRN_PMC1, r3 |
| 98 | mtspr SPRN_PMC2, r4 |
| 99 | mtspr SPRN_PMC3, r5 |
| 100 | mtspr SPRN_PMC4, r6 |
| 101 | mtspr SPRN_PMC5, r8 |
| 102 | mtspr SPRN_PMC6, r9 |
Michael Ellerman | 9a4fc4e | 2014-07-10 19:34:31 +1000 | [diff] [blame] | 103 | ld r3, HSTATE_MMCR0(r13) |
| 104 | ld r4, HSTATE_MMCR1(r13) |
| 105 | ld r5, HSTATE_MMCRA(r13) |
| 106 | ld r6, HSTATE_SIAR(r13) |
| 107 | ld r7, HSTATE_SDAR(r13) |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 108 | mtspr SPRN_MMCR1, r4 |
| 109 | mtspr SPRN_MMCRA, r5 |
Paul Mackerras | 72cde5a | 2014-03-25 10:47:08 +1100 | [diff] [blame] | 110 | mtspr SPRN_SIAR, r6 |
| 111 | mtspr SPRN_SDAR, r7 |
| 112 | BEGIN_FTR_SECTION |
Michael Ellerman | 9a4fc4e | 2014-07-10 19:34:31 +1000 | [diff] [blame] | 113 | ld r8, HSTATE_MMCR2(r13) |
| 114 | ld r9, HSTATE_SIER(r13) |
Paul Mackerras | 72cde5a | 2014-03-25 10:47:08 +1100 | [diff] [blame] | 115 | mtspr SPRN_MMCR2, r8 |
| 116 | mtspr SPRN_SIER, r9 |
| 117 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 118 | mtspr SPRN_MMCR0, r3 |
| 119 | isync |
| 120 | 23: |
| 121 | |
| 122 | /* |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 123 | * Reload DEC. HDEC interrupts were disabled when |
| 124 | * we reloaded the host's LPCR value. |
| 125 | */ |
| 126 | ld r3, HSTATE_DECEXP(r13) |
| 127 | mftb r4 |
| 128 | subf r4, r4, r3 |
| 129 | mtspr SPRN_DEC, r4 |
| 130 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 131 | /* hwthread_req may have got set by cede or no vcpu, so clear it */ |
| 132 | li r0, 0 |
| 133 | stb r0, HSTATE_HWTHREAD_REQ(r13) |
| 134 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 135 | /* |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 136 | * For external and machine check interrupts, we need |
| 137 | * to call the Linux handler to process the interrupt. |
| 138 | * We do that by jumping to absolute address 0x500 for |
| 139 | * external interrupts, or the machine_check_fwnmi label |
| 140 | * for machine checks (since firmware might have patched |
| 141 | * the vector area at 0x200). The [h]rfid at the end of the |
| 142 | * handler will return to the book3s_hv_interrupts.S code. |
| 143 | * For other interrupts we do the rfid to get back |
| 144 | * to the book3s_hv_interrupts.S code here. |
| 145 | */ |
| 146 | ld r8, 112+PPC_LR_STKOFF(r1) |
| 147 | addi r1, r1, 112 |
| 148 | ld r7, HSTATE_HOST_MSR(r13) |
| 149 | |
| 150 | cmpwi cr1, r12, BOOK3S_INTERRUPT_MACHINE_CHECK |
| 151 | cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 152 | beq 11f |
Mahesh Salgaonkar | 0869b6f | 2014-07-29 18:40:01 +0530 | [diff] [blame] | 153 | cmpwi cr2, r12, BOOK3S_INTERRUPT_HMI |
| 154 | beq cr2, 14f /* HMI check */ |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 155 | |
| 156 | /* RFI into the highmem handler, or branch to interrupt handler */ |
| 157 | mfmsr r6 |
| 158 | li r0, MSR_RI |
| 159 | andc r6, r6, r0 |
| 160 | mtmsrd r6, 1 /* Clear RI in MSR */ |
| 161 | mtsrr0 r8 |
| 162 | mtsrr1 r7 |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 163 | beq cr1, 13f /* machine check */ |
| 164 | RFI |
| 165 | |
| 166 | /* On POWER7, we have external interrupts set to use HSRR0/1 */ |
| 167 | 11: mtspr SPRN_HSRR0, r8 |
| 168 | mtspr SPRN_HSRR1, r7 |
| 169 | ba 0x500 |
| 170 | |
| 171 | 13: b machine_check_fwnmi |
| 172 | |
Mahesh Salgaonkar | 0869b6f | 2014-07-29 18:40:01 +0530 | [diff] [blame] | 173 | 14: mtspr SPRN_HSRR0, r8 |
| 174 | mtspr SPRN_HSRR1, r7 |
| 175 | b hmi_exception_after_realmode |
| 176 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 177 | kvmppc_primary_no_guest: |
| 178 | /* We handle this much like a ceded vcpu */ |
Paul Mackerras | fd6d53b | 2015-03-28 14:21:08 +1100 | [diff] [blame] | 179 | /* put the HDEC into the DEC, since HDEC interrupts don't wake us */ |
| 180 | mfspr r3, SPRN_HDEC |
| 181 | mtspr SPRN_DEC, r3 |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 182 | /* |
| 183 | * Make sure the primary has finished the MMU switch. |
| 184 | * We should never get here on a secondary thread, but |
| 185 | * check it for robustness' sake. |
| 186 | */ |
| 187 | ld r5, HSTATE_KVM_VCORE(r13) |
| 188 | 65: lbz r0, VCORE_IN_GUEST(r5) |
| 189 | cmpwi r0, 0 |
| 190 | beq 65b |
| 191 | /* Set LPCR. */ |
| 192 | ld r8,VCORE_LPCR(r5) |
| 193 | mtspr SPRN_LPCR,r8 |
| 194 | isync |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 195 | /* set our bit in napping_threads */ |
| 196 | ld r5, HSTATE_KVM_VCORE(r13) |
| 197 | lbz r7, HSTATE_PTID(r13) |
| 198 | li r0, 1 |
| 199 | sld r0, r0, r7 |
| 200 | addi r6, r5, VCORE_NAPPING_THREADS |
| 201 | 1: lwarx r3, 0, r6 |
| 202 | or r3, r3, r0 |
| 203 | stwcx. r3, 0, r6 |
| 204 | bne 1b |
Paul Mackerras | 7d6c40d | 2015-03-28 14:21:09 +1100 | [diff] [blame] | 205 | /* order napping_threads update vs testing entry_exit_map */ |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 206 | isync |
| 207 | li r12, 0 |
| 208 | lwz r7, VCORE_ENTRY_EXIT(r5) |
| 209 | cmpwi r7, 0x100 |
| 210 | bge kvm_novcpu_exit /* another thread already exiting */ |
| 211 | li r3, NAPPING_NOVCPU |
| 212 | stb r3, HSTATE_NAPPING(r13) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 213 | |
Paul Mackerras | ccc0777 | 2015-03-28 14:21:07 +1100 | [diff] [blame] | 214 | li r3, 0 /* Don't wake on privileged (OS) doorbell */ |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 215 | b kvm_do_nap |
| 216 | |
| 217 | kvm_novcpu_wakeup: |
| 218 | ld r1, HSTATE_HOST_R1(r13) |
| 219 | ld r5, HSTATE_KVM_VCORE(r13) |
| 220 | li r0, 0 |
| 221 | stb r0, HSTATE_NAPPING(r13) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 222 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 223 | /* check the wake reason */ |
| 224 | bl kvmppc_check_wake_reason |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 225 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 226 | /* see if any other thread is already exiting */ |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 227 | lwz r0, VCORE_ENTRY_EXIT(r5) |
| 228 | cmpwi r0, 0x100 |
| 229 | bge kvm_novcpu_exit |
| 230 | |
| 231 | /* clear our bit in napping_threads */ |
| 232 | lbz r7, HSTATE_PTID(r13) |
| 233 | li r0, 1 |
| 234 | sld r0, r0, r7 |
| 235 | addi r6, r5, VCORE_NAPPING_THREADS |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 236 | 4: lwarx r7, 0, r6 |
| 237 | andc r7, r7, r0 |
| 238 | stwcx. r7, 0, r6 |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 239 | bne 4b |
| 240 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 241 | /* See if the wake reason means we need to exit */ |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 242 | cmpdi r3, 0 |
| 243 | bge kvm_novcpu_exit |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 244 | |
Paul Mackerras | fd6d53b | 2015-03-28 14:21:08 +1100 | [diff] [blame] | 245 | /* See if our timeslice has expired (HDEC is negative) */ |
| 246 | mfspr r0, SPRN_HDEC |
| 247 | li r12, BOOK3S_INTERRUPT_HV_DECREMENTER |
| 248 | cmpwi r0, 0 |
| 249 | blt kvm_novcpu_exit |
| 250 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 251 | /* Got an IPI but other vcpus aren't yet exiting, must be a latecomer */ |
| 252 | ld r4, HSTATE_KVM_VCPU(r13) |
| 253 | cmpdi r4, 0 |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 254 | beq kvmppc_primary_no_guest |
| 255 | |
| 256 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 257 | addi r3, r4, VCPU_TB_RMENTRY |
| 258 | bl kvmhv_start_timing |
| 259 | #endif |
| 260 | b kvmppc_got_guest |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 261 | |
| 262 | kvm_novcpu_exit: |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 263 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 264 | ld r4, HSTATE_KVM_VCPU(r13) |
| 265 | cmpdi r4, 0 |
| 266 | beq 13f |
| 267 | addi r3, r4, VCPU_TB_RMEXIT |
| 268 | bl kvmhv_accumulate_time |
| 269 | #endif |
Paul Mackerras | eddb60f | 2015-03-28 14:21:11 +1100 | [diff] [blame] | 270 | 13: mr r3, r12 |
| 271 | stw r12, 112-4(r1) |
| 272 | bl kvmhv_commence_exit |
| 273 | nop |
| 274 | lwz r12, 112-4(r1) |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 275 | b kvmhv_switch_to_host |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 276 | |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 277 | /* |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 278 | * We come in here when wakened from nap mode. |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 279 | * Relocation is off and most register values are lost. |
| 280 | * r13 points to the PACA. |
| 281 | */ |
| 282 | .globl kvm_start_guest |
| 283 | kvm_start_guest: |
Preeti U Murthy | fd17dc7 | 2014-04-11 16:01:58 +0530 | [diff] [blame] | 284 | |
| 285 | /* Set runlatch bit the minute you wake up from nap */ |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 286 | mfspr r0, SPRN_CTRLF |
| 287 | ori r0, r0, 1 |
| 288 | mtspr SPRN_CTRLT, r0 |
Preeti U Murthy | fd17dc7 | 2014-04-11 16:01:58 +0530 | [diff] [blame] | 289 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 290 | ld r2,PACATOC(r13) |
| 291 | |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 292 | li r0,KVM_HWTHREAD_IN_KVM |
| 293 | stb r0,HSTATE_HWTHREAD_STATE(r13) |
| 294 | |
| 295 | /* NV GPR values from power7_idle() will no longer be valid */ |
| 296 | li r0,1 |
| 297 | stb r0,PACA_NAPSTATELOST(r13) |
| 298 | |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 299 | /* were we napping due to cede? */ |
| 300 | lbz r0,HSTATE_NAPPING(r13) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 301 | cmpwi r0,NAPPING_CEDE |
| 302 | beq kvm_end_cede |
| 303 | cmpwi r0,NAPPING_NOVCPU |
| 304 | beq kvm_novcpu_wakeup |
| 305 | |
| 306 | ld r1,PACAEMERGSP(r13) |
| 307 | subi r1,r1,STACK_FRAME_OVERHEAD |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 308 | |
| 309 | /* |
| 310 | * We weren't napping due to cede, so this must be a secondary |
| 311 | * thread being woken up to run a guest, or being woken up due |
| 312 | * to a stray IPI. (Or due to some machine check or hypervisor |
| 313 | * maintenance interrupt while the core is in KVM.) |
| 314 | */ |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 315 | |
| 316 | /* Check the wake reason in SRR1 to see why we got here */ |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 317 | bl kvmppc_check_wake_reason |
| 318 | cmpdi r3, 0 |
| 319 | bge kvm_no_guest |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 320 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 321 | /* get vcore pointer, NULL if we have nothing to run */ |
| 322 | ld r5,HSTATE_KVM_VCORE(r13) |
| 323 | cmpdi r5,0 |
| 324 | /* if we have no vcore to run, go back to sleep */ |
Paul Mackerras | 7b444c6 | 2012-10-15 01:16:14 +0000 | [diff] [blame] | 325 | beq kvm_no_guest |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 326 | |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 327 | kvm_secondary_got_guest: |
| 328 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 329 | /* Set HSTATE_DSCR(r13) to something sensible */ |
Anshuman Khandual | 1db3652 | 2015-05-21 12:13:03 +0530 | [diff] [blame] | 330 | ld r6, PACA_DSCR_DEFAULT(r13) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 331 | std r6, HSTATE_DSCR(r13) |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 332 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 333 | /* On thread 0 of a subcore, set HDEC to max */ |
| 334 | lbz r4, HSTATE_PTID(r13) |
| 335 | cmpwi r4, 0 |
| 336 | bne 63f |
| 337 | lis r6, 0x7fff |
| 338 | ori r6, r6, 0xffff |
| 339 | mtspr SPRN_HDEC, r6 |
| 340 | /* and set per-LPAR registers, if doing dynamic micro-threading */ |
| 341 | ld r6, HSTATE_SPLIT_MODE(r13) |
| 342 | cmpdi r6, 0 |
| 343 | beq 63f |
| 344 | ld r0, KVM_SPLIT_RPR(r6) |
| 345 | mtspr SPRN_RPR, r0 |
| 346 | ld r0, KVM_SPLIT_PMMAR(r6) |
| 347 | mtspr SPRN_PMMAR, r0 |
| 348 | ld r0, KVM_SPLIT_LDBAR(r6) |
| 349 | mtspr SPRN_LDBAR, r0 |
| 350 | isync |
| 351 | 63: |
| 352 | /* Order load of vcpu after load of vcore */ |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 353 | lwsync |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 354 | ld r4, HSTATE_KVM_VCPU(r13) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 355 | bl kvmppc_hv_entry |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 356 | |
| 357 | /* Back from the guest, go back to nap */ |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 358 | /* Clear our vcpu and vcore pointers so we don't come back in early */ |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 359 | li r0, 0 |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 360 | std r0, HSTATE_KVM_VCPU(r13) |
Paul Mackerras | f019b7a | 2013-11-16 17:46:03 +1100 | [diff] [blame] | 361 | /* |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 362 | * Once we clear HSTATE_KVM_VCORE(r13), the code in |
Paul Mackerras | 5d5b99c | 2015-03-28 14:21:06 +1100 | [diff] [blame] | 363 | * kvmppc_run_core() is going to assume that all our vcpu |
| 364 | * state is visible in memory. This lwsync makes sure |
| 365 | * that that is true. |
Paul Mackerras | f019b7a | 2013-11-16 17:46:03 +1100 | [diff] [blame] | 366 | */ |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 367 | lwsync |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 368 | std r0, HSTATE_KVM_VCORE(r13) |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 369 | |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 370 | /* |
| 371 | * At this point we have finished executing in the guest. |
| 372 | * We need to wait for hwthread_req to become zero, since |
| 373 | * we may not turn on the MMU while hwthread_req is non-zero. |
| 374 | * While waiting we also need to check if we get given a vcpu to run. |
| 375 | */ |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 376 | kvm_no_guest: |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 377 | lbz r3, HSTATE_HWTHREAD_REQ(r13) |
| 378 | cmpwi r3, 0 |
| 379 | bne 53f |
| 380 | HMT_MEDIUM |
| 381 | li r0, KVM_HWTHREAD_IN_KERNEL |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 382 | stb r0, HSTATE_HWTHREAD_STATE(r13) |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 383 | /* need to recheck hwthread_req after a barrier, to avoid race */ |
| 384 | sync |
| 385 | lbz r3, HSTATE_HWTHREAD_REQ(r13) |
| 386 | cmpwi r3, 0 |
| 387 | bne 54f |
| 388 | /* |
| 389 | * We jump to power7_wakeup_loss, which will return to the caller |
| 390 | * of power7_nap in the powernv cpu offline loop. The value we |
| 391 | * put in r3 becomes the return value for power7_nap. |
| 392 | */ |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 393 | li r3, LPCR_PECE0 |
| 394 | mfspr r4, SPRN_LPCR |
| 395 | rlwimi r4, r3, 0, LPCR_PECE0 | LPCR_PECE1 |
| 396 | mtspr SPRN_LPCR, r4 |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 397 | li r3, 0 |
| 398 | b power7_wakeup_loss |
| 399 | |
| 400 | 53: HMT_LOW |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 401 | ld r5, HSTATE_KVM_VCORE(r13) |
| 402 | cmpdi r5, 0 |
| 403 | bne 60f |
| 404 | ld r3, HSTATE_SPLIT_MODE(r13) |
| 405 | cmpdi r3, 0 |
| 406 | beq kvm_no_guest |
| 407 | lbz r0, KVM_SPLIT_DO_NAP(r3) |
| 408 | cmpwi r0, 0 |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 409 | beq kvm_no_guest |
| 410 | HMT_MEDIUM |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 411 | b kvm_unsplit_nap |
| 412 | 60: HMT_MEDIUM |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 413 | b kvm_secondary_got_guest |
| 414 | |
| 415 | 54: li r0, KVM_HWTHREAD_IN_KVM |
| 416 | stb r0, HSTATE_HWTHREAD_STATE(r13) |
| 417 | b kvm_no_guest |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 418 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 419 | /* |
| 420 | * Here the primary thread is trying to return the core to |
| 421 | * whole-core mode, so we need to nap. |
| 422 | */ |
| 423 | kvm_unsplit_nap: |
Gautham R. Shenoy | 7f23532 | 2015-09-02 21:48:58 +0530 | [diff] [blame^] | 424 | /* |
| 425 | * Ensure that secondary doesn't nap when it has |
| 426 | * its vcore pointer set. |
| 427 | */ |
| 428 | sync /* matches smp_mb() before setting split_info.do_nap */ |
| 429 | ld r0, HSTATE_KVM_VCORE(r13) |
| 430 | cmpdi r0, 0 |
| 431 | bne kvm_no_guest |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 432 | /* clear any pending message */ |
| 433 | BEGIN_FTR_SECTION |
| 434 | lis r6, (PPC_DBELL_SERVER << (63-36))@h |
| 435 | PPC_MSGCLR(6) |
| 436 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
| 437 | /* Set kvm_split_mode.napped[tid] = 1 */ |
| 438 | ld r3, HSTATE_SPLIT_MODE(r13) |
| 439 | li r0, 1 |
| 440 | lhz r4, PACAPACAINDEX(r13) |
| 441 | clrldi r4, r4, 61 /* micro-threading => P8 => 8 threads/core */ |
| 442 | addi r4, r4, KVM_SPLIT_NAPPED |
| 443 | stbx r0, r3, r4 |
| 444 | /* Check the do_nap flag again after setting napped[] */ |
| 445 | sync |
| 446 | lbz r0, KVM_SPLIT_DO_NAP(r3) |
| 447 | cmpwi r0, 0 |
| 448 | beq 57f |
| 449 | li r3, (LPCR_PECEDH | LPCR_PECE0) >> 4 |
| 450 | mfspr r4, SPRN_LPCR |
| 451 | rlwimi r4, r3, 4, (LPCR_PECEDP | LPCR_PECEDH | LPCR_PECE0 | LPCR_PECE1) |
| 452 | mtspr SPRN_LPCR, r4 |
| 453 | isync |
| 454 | std r0, HSTATE_SCRATCH0(r13) |
| 455 | ptesync |
| 456 | ld r0, HSTATE_SCRATCH0(r13) |
| 457 | 1: cmpd r0, r0 |
| 458 | bne 1b |
| 459 | nap |
| 460 | b . |
| 461 | |
| 462 | 57: li r0, 0 |
| 463 | stbx r0, r3, r4 |
| 464 | b kvm_no_guest |
| 465 | |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 466 | /****************************************************************************** |
| 467 | * * |
| 468 | * Entry code * |
| 469 | * * |
| 470 | *****************************************************************************/ |
| 471 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 472 | .global kvmppc_hv_entry |
| 473 | kvmppc_hv_entry: |
| 474 | |
| 475 | /* Required state: |
| 476 | * |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 477 | * R4 = vcpu pointer (or NULL) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 478 | * MSR = ~IR|DR |
| 479 | * R13 = PACA |
| 480 | * R1 = host R1 |
Michael Neuling | 06a29e4 | 2014-08-19 14:59:30 +1000 | [diff] [blame] | 481 | * R2 = TOC |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 482 | * all other volatile GPRS = free |
| 483 | */ |
| 484 | mflr r0 |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 485 | std r0, PPC_LR_STKOFF(r1) |
| 486 | stdu r1, -112(r1) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 487 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 488 | /* Save R1 in the PACA */ |
| 489 | std r1, HSTATE_HOST_R1(r13) |
| 490 | |
Paul Mackerras | 44a3add | 2013-10-04 21:45:04 +1000 | [diff] [blame] | 491 | li r6, KVM_GUEST_MODE_HOST_HV |
| 492 | stb r6, HSTATE_IN_GUEST(r13) |
| 493 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 494 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 495 | /* Store initial timestamp */ |
| 496 | cmpdi r4, 0 |
| 497 | beq 1f |
| 498 | addi r3, r4, VCPU_TB_RMENTRY |
| 499 | bl kvmhv_start_timing |
| 500 | 1: |
| 501 | #endif |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 502 | /* Clear out SLB */ |
| 503 | li r6,0 |
| 504 | slbmte r6,r6 |
| 505 | slbia |
| 506 | ptesync |
| 507 | |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 508 | /* |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 509 | * POWER7/POWER8 host -> guest partition switch code. |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 510 | * We don't have to lock against concurrent tlbies, |
| 511 | * but we do have to coordinate across hardware threads. |
| 512 | */ |
Paul Mackerras | 7d6c40d | 2015-03-28 14:21:09 +1100 | [diff] [blame] | 513 | /* Set bit in entry map iff exit map is zero. */ |
| 514 | ld r5, HSTATE_KVM_VCORE(r13) |
| 515 | li r7, 1 |
| 516 | lbz r6, HSTATE_PTID(r13) |
| 517 | sld r7, r7, r6 |
| 518 | addi r9, r5, VCORE_ENTRY_EXIT |
| 519 | 21: lwarx r3, 0, r9 |
| 520 | cmpwi r3, 0x100 /* any threads starting to exit? */ |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 521 | bge secondary_too_late /* if so we're too late to the party */ |
Paul Mackerras | 7d6c40d | 2015-03-28 14:21:09 +1100 | [diff] [blame] | 522 | or r3, r3, r7 |
| 523 | stwcx. r3, 0, r9 |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 524 | bne 21b |
| 525 | |
| 526 | /* Primary thread switches to guest partition. */ |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 527 | ld r9,VCORE_KVM(r5) /* pointer to struct kvm */ |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 528 | cmpwi r6,0 |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 529 | bne 10f |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 530 | ld r6,KVM_SDR1(r9) |
| 531 | lwz r7,KVM_LPID(r9) |
| 532 | li r0,LPID_RSVD /* switch to reserved LPID */ |
| 533 | mtspr SPRN_LPID,r0 |
| 534 | ptesync |
| 535 | mtspr SPRN_SDR1,r6 /* switch to partition page table */ |
| 536 | mtspr SPRN_LPID,r7 |
| 537 | isync |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 538 | |
| 539 | /* See if we need to flush the TLB */ |
| 540 | lhz r6,PACAPACAINDEX(r13) /* test_bit(cpu, need_tlb_flush) */ |
| 541 | clrldi r7,r6,64-6 /* extract bit number (6 bits) */ |
| 542 | srdi r6,r6,6 /* doubleword number */ |
| 543 | sldi r6,r6,3 /* address offset */ |
| 544 | add r6,r6,r9 |
| 545 | addi r6,r6,KVM_NEED_FLUSH /* dword in kvm->arch.need_tlb_flush */ |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 546 | li r0,1 |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 547 | sld r0,r0,r7 |
| 548 | ld r7,0(r6) |
| 549 | and. r7,r7,r0 |
| 550 | beq 22f |
| 551 | 23: ldarx r7,0,r6 /* if set, clear the bit */ |
| 552 | andc r7,r7,r0 |
| 553 | stdcx. r7,0,r6 |
| 554 | bne 23b |
Paul Mackerras | ca25205 | 2014-01-08 21:25:22 +1100 | [diff] [blame] | 555 | /* Flush the TLB of any entries for this LPID */ |
| 556 | /* use arch 2.07S as a proxy for POWER8 */ |
| 557 | BEGIN_FTR_SECTION |
| 558 | li r6,512 /* POWER8 has 512 sets */ |
| 559 | FTR_SECTION_ELSE |
| 560 | li r6,128 /* POWER7 has 128 sets */ |
| 561 | ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | 1b400ba | 2012-11-21 23:28:08 +0000 | [diff] [blame] | 562 | mtctr r6 |
| 563 | li r7,0x800 /* IS field = 0b10 */ |
| 564 | ptesync |
| 565 | 28: tlbiel r7 |
| 566 | addi r7,r7,0x1000 |
| 567 | bdnz 28b |
| 568 | ptesync |
| 569 | |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 570 | /* Add timebase offset onto timebase */ |
| 571 | 22: ld r8,VCORE_TB_OFFSET(r5) |
| 572 | cmpdi r8,0 |
| 573 | beq 37f |
| 574 | mftb r6 /* current host timebase */ |
| 575 | add r8,r8,r6 |
| 576 | mtspr SPRN_TBU40,r8 /* update upper 40 bits */ |
| 577 | mftb r7 /* check if lower 24 bits overflowed */ |
| 578 | clrldi r6,r6,40 |
| 579 | clrldi r7,r7,40 |
| 580 | cmpld r7,r6 |
| 581 | bge 37f |
| 582 | addis r8,r8,0x100 /* if so, increment upper 40 bits */ |
| 583 | mtspr SPRN_TBU40,r8 |
| 584 | |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 585 | /* Load guest PCR value to select appropriate compat mode */ |
| 586 | 37: ld r7, VCORE_PCR(r5) |
| 587 | cmpdi r7, 0 |
| 588 | beq 38f |
| 589 | mtspr SPRN_PCR, r7 |
| 590 | 38: |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 591 | |
| 592 | BEGIN_FTR_SECTION |
| 593 | /* DPDES is shared between threads */ |
| 594 | ld r8, VCORE_DPDES(r5) |
| 595 | mtspr SPRN_DPDES, r8 |
| 596 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
| 597 | |
Paul Mackerras | 388cc6e | 2013-09-21 14:35:02 +1000 | [diff] [blame] | 598 | li r0,1 |
Paul Mackerras | 371fefd | 2011-06-29 00:23:08 +0000 | [diff] [blame] | 599 | stb r0,VCORE_IN_GUEST(r5) /* signal secondaries to continue */ |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 600 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 601 | /* Do we have a guest vcpu to run? */ |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 602 | 10: cmpdi r4, 0 |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 603 | beq kvmppc_primary_no_guest |
| 604 | kvmppc_got_guest: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 605 | |
| 606 | /* Load up guest SLB entries */ |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 607 | lwz r5,VCPU_SLB_MAX(r4) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 608 | cmpwi r5,0 |
| 609 | beq 9f |
| 610 | mtctr r5 |
| 611 | addi r6,r4,VCPU_SLB |
| 612 | 1: ld r8,VCPU_SLB_E(r6) |
| 613 | ld r9,VCPU_SLB_V(r6) |
| 614 | slbmte r9,r8 |
| 615 | addi r6,r6,VCPU_SLB_SIZE |
| 616 | bdnz 1b |
| 617 | 9: |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 618 | /* Increment yield count if they have a VPA */ |
| 619 | ld r3, VCPU_VPA(r4) |
| 620 | cmpdi r3, 0 |
| 621 | beq 25f |
Alexander Graf | 0865a58 | 2014-06-11 10:36:17 +0200 | [diff] [blame] | 622 | li r6, LPPACA_YIELDCOUNT |
| 623 | LWZX_BE r5, r3, r6 |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 624 | addi r5, r5, 1 |
Alexander Graf | 0865a58 | 2014-06-11 10:36:17 +0200 | [diff] [blame] | 625 | STWX_BE r5, r3, r6 |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 626 | li r6, 1 |
| 627 | stb r6, VCPU_VPA_DIRTY(r4) |
| 628 | 25: |
| 629 | |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 630 | /* Save purr/spurr */ |
| 631 | mfspr r5,SPRN_PURR |
| 632 | mfspr r6,SPRN_SPURR |
| 633 | std r5,HSTATE_PURR(r13) |
| 634 | std r6,HSTATE_SPURR(r13) |
| 635 | ld r7,VCPU_PURR(r4) |
| 636 | ld r8,VCPU_SPURR(r4) |
| 637 | mtspr SPRN_PURR,r7 |
| 638 | mtspr SPRN_SPURR,r8 |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 639 | |
Michael Neuling | eee7ff9 | 2014-01-08 21:25:19 +1100 | [diff] [blame] | 640 | BEGIN_FTR_SECTION |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 641 | /* Set partition DABR */ |
| 642 | /* Do this before re-enabling PMU to avoid P7 DABR corruption bug */ |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 643 | lwz r5,VCPU_DABRX(r4) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 644 | ld r6,VCPU_DABR(r4) |
| 645 | mtspr SPRN_DABRX,r5 |
| 646 | mtspr SPRN_DABR,r6 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 647 | isync |
Michael Neuling | eee7ff9 | 2014-01-08 21:25:19 +1100 | [diff] [blame] | 648 | END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 649 | |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 650 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 651 | BEGIN_FTR_SECTION |
| 652 | b skip_tm |
| 653 | END_FTR_SECTION_IFCLR(CPU_FTR_TM) |
| 654 | |
| 655 | /* Turn on TM/FP/VSX/VMX so we can restore them. */ |
| 656 | mfmsr r5 |
| 657 | li r6, MSR_TM >> 32 |
| 658 | sldi r6, r6, 32 |
| 659 | or r5, r5, r6 |
| 660 | ori r5, r5, MSR_FP |
| 661 | oris r5, r5, (MSR_VEC | MSR_VSX)@h |
| 662 | mtmsrd r5 |
| 663 | |
| 664 | /* |
| 665 | * The user may change these outside of a transaction, so they must |
| 666 | * always be context switched. |
| 667 | */ |
| 668 | ld r5, VCPU_TFHAR(r4) |
| 669 | ld r6, VCPU_TFIAR(r4) |
| 670 | ld r7, VCPU_TEXASR(r4) |
| 671 | mtspr SPRN_TFHAR, r5 |
| 672 | mtspr SPRN_TFIAR, r6 |
| 673 | mtspr SPRN_TEXASR, r7 |
| 674 | |
| 675 | ld r5, VCPU_MSR(r4) |
| 676 | rldicl. r5, r5, 64 - MSR_TS_S_LG, 62 |
| 677 | beq skip_tm /* TM not active in guest */ |
| 678 | |
| 679 | /* Make sure the failure summary is set, otherwise we'll program check |
| 680 | * when we trechkpt. It's possible that this might have been not set |
| 681 | * on a kvmppc_set_one_reg() call but we shouldn't let this crash the |
| 682 | * host. |
| 683 | */ |
| 684 | oris r7, r7, (TEXASR_FS)@h |
| 685 | mtspr SPRN_TEXASR, r7 |
| 686 | |
| 687 | /* |
| 688 | * We need to load up the checkpointed state for the guest. |
| 689 | * We need to do this early as it will blow away any GPRs, VSRs and |
| 690 | * some SPRs. |
| 691 | */ |
| 692 | |
| 693 | mr r31, r4 |
| 694 | addi r3, r31, VCPU_FPRS_TM |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 695 | bl load_fp_state |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 696 | addi r3, r31, VCPU_VRS_TM |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 697 | bl load_vr_state |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 698 | mr r4, r31 |
| 699 | lwz r7, VCPU_VRSAVE_TM(r4) |
| 700 | mtspr SPRN_VRSAVE, r7 |
| 701 | |
| 702 | ld r5, VCPU_LR_TM(r4) |
| 703 | lwz r6, VCPU_CR_TM(r4) |
| 704 | ld r7, VCPU_CTR_TM(r4) |
| 705 | ld r8, VCPU_AMR_TM(r4) |
| 706 | ld r9, VCPU_TAR_TM(r4) |
| 707 | mtlr r5 |
| 708 | mtcr r6 |
| 709 | mtctr r7 |
| 710 | mtspr SPRN_AMR, r8 |
| 711 | mtspr SPRN_TAR, r9 |
| 712 | |
| 713 | /* |
| 714 | * Load up PPR and DSCR values but don't put them in the actual SPRs |
| 715 | * till the last moment to avoid running with userspace PPR and DSCR for |
| 716 | * too long. |
| 717 | */ |
| 718 | ld r29, VCPU_DSCR_TM(r4) |
| 719 | ld r30, VCPU_PPR_TM(r4) |
| 720 | |
| 721 | std r2, PACATMSCRATCH(r13) /* Save TOC */ |
| 722 | |
| 723 | /* Clear the MSR RI since r1, r13 are all going to be foobar. */ |
| 724 | li r5, 0 |
| 725 | mtmsrd r5, 1 |
| 726 | |
| 727 | /* Load GPRs r0-r28 */ |
| 728 | reg = 0 |
| 729 | .rept 29 |
| 730 | ld reg, VCPU_GPRS_TM(reg)(r31) |
| 731 | reg = reg + 1 |
| 732 | .endr |
| 733 | |
| 734 | mtspr SPRN_DSCR, r29 |
| 735 | mtspr SPRN_PPR, r30 |
| 736 | |
| 737 | /* Load final GPRs */ |
| 738 | ld 29, VCPU_GPRS_TM(29)(r31) |
| 739 | ld 30, VCPU_GPRS_TM(30)(r31) |
| 740 | ld 31, VCPU_GPRS_TM(31)(r31) |
| 741 | |
| 742 | /* TM checkpointed state is now setup. All GPRs are now volatile. */ |
| 743 | TRECHKPT |
| 744 | |
| 745 | /* Now let's get back the state we need. */ |
| 746 | HMT_MEDIUM |
| 747 | GET_PACA(r13) |
| 748 | ld r29, HSTATE_DSCR(r13) |
| 749 | mtspr SPRN_DSCR, r29 |
| 750 | ld r4, HSTATE_KVM_VCPU(r13) |
| 751 | ld r1, HSTATE_HOST_R1(r13) |
| 752 | ld r2, PACATMSCRATCH(r13) |
| 753 | |
| 754 | /* Set the MSR RI since we have our registers back. */ |
| 755 | li r5, MSR_RI |
| 756 | mtmsrd r5, 1 |
| 757 | skip_tm: |
| 758 | #endif |
| 759 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 760 | /* Load guest PMU registers */ |
| 761 | /* R4 is live here (vcpu pointer) */ |
| 762 | li r3, 1 |
| 763 | sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */ |
| 764 | mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */ |
| 765 | isync |
Paul Mackerras | 9bc01a9 | 2014-05-26 19:48:40 +1000 | [diff] [blame] | 766 | BEGIN_FTR_SECTION |
| 767 | ld r3, VCPU_MMCR(r4) |
| 768 | andi. r5, r3, MMCR0_PMAO_SYNC | MMCR0_PMAO |
| 769 | cmpwi r5, MMCR0_PMAO |
| 770 | beql kvmppc_fix_pmao |
| 771 | END_FTR_SECTION_IFSET(CPU_FTR_PMAO_BUG) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 772 | lwz r3, VCPU_PMC(r4) /* always load up guest PMU registers */ |
| 773 | lwz r5, VCPU_PMC + 4(r4) /* to prevent information leak */ |
| 774 | lwz r6, VCPU_PMC + 8(r4) |
| 775 | lwz r7, VCPU_PMC + 12(r4) |
| 776 | lwz r8, VCPU_PMC + 16(r4) |
| 777 | lwz r9, VCPU_PMC + 20(r4) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 778 | mtspr SPRN_PMC1, r3 |
| 779 | mtspr SPRN_PMC2, r5 |
| 780 | mtspr SPRN_PMC3, r6 |
| 781 | mtspr SPRN_PMC4, r7 |
| 782 | mtspr SPRN_PMC5, r8 |
| 783 | mtspr SPRN_PMC6, r9 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 784 | ld r3, VCPU_MMCR(r4) |
| 785 | ld r5, VCPU_MMCR + 8(r4) |
| 786 | ld r6, VCPU_MMCR + 16(r4) |
| 787 | ld r7, VCPU_SIAR(r4) |
| 788 | ld r8, VCPU_SDAR(r4) |
| 789 | mtspr SPRN_MMCR1, r5 |
| 790 | mtspr SPRN_MMCRA, r6 |
| 791 | mtspr SPRN_SIAR, r7 |
| 792 | mtspr SPRN_SDAR, r8 |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 793 | BEGIN_FTR_SECTION |
| 794 | ld r5, VCPU_MMCR + 24(r4) |
| 795 | ld r6, VCPU_SIER(r4) |
| 796 | lwz r7, VCPU_PMC + 24(r4) |
| 797 | lwz r8, VCPU_PMC + 28(r4) |
| 798 | ld r9, VCPU_MMCR + 32(r4) |
| 799 | mtspr SPRN_MMCR2, r5 |
| 800 | mtspr SPRN_SIER, r6 |
| 801 | mtspr SPRN_SPMC1, r7 |
| 802 | mtspr SPRN_SPMC2, r8 |
| 803 | mtspr SPRN_MMCRS, r9 |
| 804 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 805 | mtspr SPRN_MMCR0, r3 |
| 806 | isync |
| 807 | |
| 808 | /* Load up FP, VMX and VSX registers */ |
| 809 | bl kvmppc_load_fp |
| 810 | |
| 811 | ld r14, VCPU_GPR(R14)(r4) |
| 812 | ld r15, VCPU_GPR(R15)(r4) |
| 813 | ld r16, VCPU_GPR(R16)(r4) |
| 814 | ld r17, VCPU_GPR(R17)(r4) |
| 815 | ld r18, VCPU_GPR(R18)(r4) |
| 816 | ld r19, VCPU_GPR(R19)(r4) |
| 817 | ld r20, VCPU_GPR(R20)(r4) |
| 818 | ld r21, VCPU_GPR(R21)(r4) |
| 819 | ld r22, VCPU_GPR(R22)(r4) |
| 820 | ld r23, VCPU_GPR(R23)(r4) |
| 821 | ld r24, VCPU_GPR(R24)(r4) |
| 822 | ld r25, VCPU_GPR(R25)(r4) |
| 823 | ld r26, VCPU_GPR(R26)(r4) |
| 824 | ld r27, VCPU_GPR(R27)(r4) |
| 825 | ld r28, VCPU_GPR(R28)(r4) |
| 826 | ld r29, VCPU_GPR(R29)(r4) |
| 827 | ld r30, VCPU_GPR(R30)(r4) |
| 828 | ld r31, VCPU_GPR(R31)(r4) |
| 829 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 830 | /* Switch DSCR to guest value */ |
| 831 | ld r5, VCPU_DSCR(r4) |
| 832 | mtspr SPRN_DSCR, r5 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 833 | |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 834 | BEGIN_FTR_SECTION |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 835 | /* Skip next section on POWER7 */ |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 836 | b 8f |
| 837 | END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) |
| 838 | /* Turn on TM so we can access TFHAR/TFIAR/TEXASR */ |
| 839 | mfmsr r8 |
| 840 | li r0, 1 |
| 841 | rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG |
| 842 | mtmsrd r8 |
| 843 | |
| 844 | /* Load up POWER8-specific registers */ |
| 845 | ld r5, VCPU_IAMR(r4) |
| 846 | lwz r6, VCPU_PSPB(r4) |
| 847 | ld r7, VCPU_FSCR(r4) |
| 848 | mtspr SPRN_IAMR, r5 |
| 849 | mtspr SPRN_PSPB, r6 |
| 850 | mtspr SPRN_FSCR, r7 |
| 851 | ld r5, VCPU_DAWR(r4) |
| 852 | ld r6, VCPU_DAWRX(r4) |
| 853 | ld r7, VCPU_CIABR(r4) |
| 854 | ld r8, VCPU_TAR(r4) |
| 855 | mtspr SPRN_DAWR, r5 |
| 856 | mtspr SPRN_DAWRX, r6 |
| 857 | mtspr SPRN_CIABR, r7 |
| 858 | mtspr SPRN_TAR, r8 |
| 859 | ld r5, VCPU_IC(r4) |
| 860 | ld r6, VCPU_VTB(r4) |
| 861 | mtspr SPRN_IC, r5 |
| 862 | mtspr SPRN_VTB, r6 |
Michael Neuling | 7b49041 | 2014-01-08 21:25:32 +1100 | [diff] [blame] | 863 | ld r8, VCPU_EBBHR(r4) |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 864 | mtspr SPRN_EBBHR, r8 |
| 865 | ld r5, VCPU_EBBRR(r4) |
| 866 | ld r6, VCPU_BESCR(r4) |
| 867 | ld r7, VCPU_CSIGR(r4) |
| 868 | ld r8, VCPU_TACR(r4) |
| 869 | mtspr SPRN_EBBRR, r5 |
| 870 | mtspr SPRN_BESCR, r6 |
| 871 | mtspr SPRN_CSIGR, r7 |
| 872 | mtspr SPRN_TACR, r8 |
| 873 | ld r5, VCPU_TCSCR(r4) |
| 874 | ld r6, VCPU_ACOP(r4) |
| 875 | lwz r7, VCPU_GUEST_PID(r4) |
| 876 | ld r8, VCPU_WORT(r4) |
| 877 | mtspr SPRN_TCSCR, r5 |
| 878 | mtspr SPRN_ACOP, r6 |
| 879 | mtspr SPRN_PID, r7 |
| 880 | mtspr SPRN_WORT, r8 |
| 881 | 8: |
| 882 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 883 | /* |
| 884 | * Set the decrementer to the guest decrementer. |
| 885 | */ |
| 886 | ld r8,VCPU_DEC_EXPIRES(r4) |
Paul Mackerras | c5fb80d | 2014-03-25 10:47:07 +1100 | [diff] [blame] | 887 | /* r8 is a host timebase value here, convert to guest TB */ |
| 888 | ld r5,HSTATE_KVM_VCORE(r13) |
| 889 | ld r6,VCORE_TB_OFFSET(r5) |
| 890 | add r8,r8,r6 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 891 | mftb r7 |
| 892 | subf r3,r7,r8 |
| 893 | mtspr SPRN_DEC,r3 |
| 894 | stw r3,VCPU_DEC(r4) |
| 895 | |
| 896 | ld r5, VCPU_SPRG0(r4) |
| 897 | ld r6, VCPU_SPRG1(r4) |
| 898 | ld r7, VCPU_SPRG2(r4) |
| 899 | ld r8, VCPU_SPRG3(r4) |
| 900 | mtspr SPRN_SPRG0, r5 |
| 901 | mtspr SPRN_SPRG1, r6 |
| 902 | mtspr SPRN_SPRG2, r7 |
| 903 | mtspr SPRN_SPRG3, r8 |
| 904 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 905 | /* Load up DAR and DSISR */ |
| 906 | ld r5, VCPU_DAR(r4) |
| 907 | lwz r6, VCPU_DSISR(r4) |
| 908 | mtspr SPRN_DAR, r5 |
| 909 | mtspr SPRN_DSISR, r6 |
| 910 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 911 | /* Restore AMR and UAMOR, set AMOR to all 1s */ |
| 912 | ld r5,VCPU_AMR(r4) |
| 913 | ld r6,VCPU_UAMOR(r4) |
| 914 | li r7,-1 |
| 915 | mtspr SPRN_AMR,r5 |
| 916 | mtspr SPRN_UAMOR,r6 |
| 917 | mtspr SPRN_AMOR,r7 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 918 | |
| 919 | /* Restore state of CTRL run bit; assume 1 on entry */ |
| 920 | lwz r5,VCPU_CTRL(r4) |
| 921 | andi. r5,r5,1 |
| 922 | bne 4f |
| 923 | mfspr r6,SPRN_CTRLF |
| 924 | clrrdi r6,r6,1 |
| 925 | mtspr SPRN_CTRLT,r6 |
| 926 | 4: |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 927 | /* Secondary threads wait for primary to have done partition switch */ |
| 928 | ld r5, HSTATE_KVM_VCORE(r13) |
| 929 | lbz r6, HSTATE_PTID(r13) |
| 930 | cmpwi r6, 0 |
| 931 | beq 21f |
| 932 | lbz r0, VCORE_IN_GUEST(r5) |
| 933 | cmpwi r0, 0 |
| 934 | bne 21f |
| 935 | HMT_LOW |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 936 | 20: lwz r3, VCORE_ENTRY_EXIT(r5) |
| 937 | cmpwi r3, 0x100 |
| 938 | bge no_switch_exit |
| 939 | lbz r0, VCORE_IN_GUEST(r5) |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 940 | cmpwi r0, 0 |
| 941 | beq 20b |
| 942 | HMT_MEDIUM |
| 943 | 21: |
| 944 | /* Set LPCR. */ |
| 945 | ld r8,VCORE_LPCR(r5) |
| 946 | mtspr SPRN_LPCR,r8 |
| 947 | isync |
| 948 | |
| 949 | /* Check if HDEC expires soon */ |
| 950 | mfspr r3, SPRN_HDEC |
| 951 | cmpwi r3, 512 /* 1 microsecond */ |
| 952 | blt hdec_soon |
| 953 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 954 | ld r6, VCPU_CTR(r4) |
Sam bobroff | c63517c | 2015-05-27 09:56:57 +1000 | [diff] [blame] | 955 | ld r7, VCPU_XER(r4) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 956 | |
| 957 | mtctr r6 |
| 958 | mtxer r7 |
| 959 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 960 | kvmppc_cede_reentry: /* r4 = vcpu, r13 = paca */ |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 961 | ld r10, VCPU_PC(r4) |
| 962 | ld r11, VCPU_MSR(r4) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 963 | ld r6, VCPU_SRR0(r4) |
| 964 | ld r7, VCPU_SRR1(r4) |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 965 | mtspr SPRN_SRR0, r6 |
| 966 | mtspr SPRN_SRR1, r7 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 967 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 968 | deliver_guest_interrupt: |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 969 | /* r11 = vcpu->arch.msr & ~MSR_HV */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 970 | rldicl r11, r11, 63 - MSR_HV_LG, 1 |
| 971 | rotldi r11, r11, 1 + MSR_HV_LG |
| 972 | ori r11, r11, MSR_ME |
| 973 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 974 | /* Check if we can deliver an external or decrementer interrupt now */ |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 975 | ld r0, VCPU_PENDING_EXC(r4) |
| 976 | rldicl r0, r0, 64 - BOOK3S_IRQPRIO_EXTERNAL_LEVEL, 63 |
| 977 | cmpdi cr1, r0, 0 |
| 978 | andi. r8, r11, MSR_EE |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 979 | mfspr r8, SPRN_LPCR |
| 980 | /* Insert EXTERNAL_LEVEL bit into LPCR at the MER bit position */ |
| 981 | rldimi r8, r0, LPCR_MER_SH, 63 - LPCR_MER_SH |
| 982 | mtspr SPRN_LPCR, r8 |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 983 | isync |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 984 | beq 5f |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 985 | li r0, BOOK3S_INTERRUPT_EXTERNAL |
| 986 | bne cr1, 12f |
| 987 | mfspr r0, SPRN_DEC |
| 988 | cmpwi r0, 0 |
| 989 | li r0, BOOK3S_INTERRUPT_DECREMENTER |
| 990 | bge 5f |
| 991 | |
| 992 | 12: mtspr SPRN_SRR0, r10 |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 993 | mr r10,r0 |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 994 | mtspr SPRN_SRR1, r11 |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 995 | mr r9, r4 |
| 996 | bl kvmppc_msr_interrupt |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 997 | 5: |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 998 | |
Liu Ping Fan | 27025a6 | 2013-11-19 14:12:48 +0800 | [diff] [blame] | 999 | /* |
| 1000 | * Required state: |
| 1001 | * R4 = vcpu |
| 1002 | * R10: value for HSRR0 |
| 1003 | * R11: value for HSRR1 |
| 1004 | * R13 = PACA |
| 1005 | */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1006 | fast_guest_return: |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 1007 | li r0,0 |
| 1008 | stb r0,VCPU_CEDED(r4) /* cancel cede */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1009 | mtspr SPRN_HSRR0,r10 |
| 1010 | mtspr SPRN_HSRR1,r11 |
| 1011 | |
| 1012 | /* Activate guest mode, so faults get handled by KVM */ |
Paul Mackerras | 44a3add | 2013-10-04 21:45:04 +1000 | [diff] [blame] | 1013 | li r9, KVM_GUEST_MODE_GUEST_HV |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1014 | stb r9, HSTATE_IN_GUEST(r13) |
| 1015 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1016 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 1017 | /* Accumulate timing */ |
| 1018 | addi r3, r4, VCPU_TB_GUEST |
| 1019 | bl kvmhv_accumulate_time |
| 1020 | #endif |
| 1021 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1022 | /* Enter guest */ |
| 1023 | |
Paul Mackerras | 0acb911 | 2013-02-04 18:10:51 +0000 | [diff] [blame] | 1024 | BEGIN_FTR_SECTION |
| 1025 | ld r5, VCPU_CFAR(r4) |
| 1026 | mtspr SPRN_CFAR, r5 |
| 1027 | END_FTR_SECTION_IFSET(CPU_FTR_CFAR) |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1028 | BEGIN_FTR_SECTION |
| 1029 | ld r0, VCPU_PPR(r4) |
| 1030 | END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) |
Paul Mackerras | 0acb911 | 2013-02-04 18:10:51 +0000 | [diff] [blame] | 1031 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1032 | ld r5, VCPU_LR(r4) |
| 1033 | lwz r6, VCPU_CR(r4) |
| 1034 | mtlr r5 |
| 1035 | mtcr r6 |
| 1036 | |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1037 | ld r1, VCPU_GPR(R1)(r4) |
| 1038 | ld r2, VCPU_GPR(R2)(r4) |
| 1039 | ld r3, VCPU_GPR(R3)(r4) |
| 1040 | ld r5, VCPU_GPR(R5)(r4) |
| 1041 | ld r6, VCPU_GPR(R6)(r4) |
| 1042 | ld r7, VCPU_GPR(R7)(r4) |
| 1043 | ld r8, VCPU_GPR(R8)(r4) |
| 1044 | ld r9, VCPU_GPR(R9)(r4) |
| 1045 | ld r10, VCPU_GPR(R10)(r4) |
| 1046 | ld r11, VCPU_GPR(R11)(r4) |
| 1047 | ld r12, VCPU_GPR(R12)(r4) |
| 1048 | ld r13, VCPU_GPR(R13)(r4) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1049 | |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1050 | BEGIN_FTR_SECTION |
| 1051 | mtspr SPRN_PPR, r0 |
| 1052 | END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) |
| 1053 | ld r0, VCPU_GPR(R0)(r4) |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1054 | ld r4, VCPU_GPR(R4)(r4) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1055 | |
| 1056 | hrfid |
| 1057 | b . |
| 1058 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1059 | secondary_too_late: |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1060 | li r12, 0 |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1061 | cmpdi r4, 0 |
| 1062 | beq 11f |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1063 | stw r12, VCPU_TRAP(r4) |
| 1064 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1065 | addi r3, r4, VCPU_TB_RMEXIT |
| 1066 | bl kvmhv_accumulate_time |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1067 | #endif |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1068 | 11: b kvmhv_switch_to_host |
| 1069 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1070 | no_switch_exit: |
| 1071 | HMT_MEDIUM |
| 1072 | li r12, 0 |
| 1073 | b 12f |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1074 | hdec_soon: |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1075 | li r12, BOOK3S_INTERRUPT_HV_DECREMENTER |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1076 | 12: stw r12, VCPU_TRAP(r4) |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1077 | mr r9, r4 |
| 1078 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1079 | addi r3, r4, VCPU_TB_RMEXIT |
| 1080 | bl kvmhv_accumulate_time |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1081 | #endif |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1082 | b guest_exit_cont |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1083 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1084 | /****************************************************************************** |
| 1085 | * * |
| 1086 | * Exit code * |
| 1087 | * * |
| 1088 | *****************************************************************************/ |
| 1089 | |
| 1090 | /* |
| 1091 | * We come here from the first-level interrupt handlers. |
| 1092 | */ |
Aneesh Kumar K.V | dd96b2c | 2013-10-07 22:17:55 +0530 | [diff] [blame] | 1093 | .globl kvmppc_interrupt_hv |
| 1094 | kvmppc_interrupt_hv: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1095 | /* |
| 1096 | * Register contents: |
| 1097 | * R12 = interrupt vector |
| 1098 | * R13 = PACA |
| 1099 | * guest CR, R12 saved in shadow VCPU SCRATCH1/0 |
| 1100 | * guest R13 saved in SPRN_SCRATCH0 |
| 1101 | */ |
Aneesh Kumar K.V | 36e7bb3 | 2013-11-11 19:29:47 +0530 | [diff] [blame] | 1102 | std r9, HSTATE_SCRATCH2(r13) |
Paul Mackerras | 44a3add | 2013-10-04 21:45:04 +1000 | [diff] [blame] | 1103 | |
| 1104 | lbz r9, HSTATE_IN_GUEST(r13) |
| 1105 | cmpwi r9, KVM_GUEST_MODE_HOST_HV |
| 1106 | beq kvmppc_bad_host_intr |
Aneesh Kumar K.V | dd96b2c | 2013-10-07 22:17:55 +0530 | [diff] [blame] | 1107 | #ifdef CONFIG_KVM_BOOK3S_PR_POSSIBLE |
| 1108 | cmpwi r9, KVM_GUEST_MODE_GUEST |
Aneesh Kumar K.V | 36e7bb3 | 2013-11-11 19:29:47 +0530 | [diff] [blame] | 1109 | ld r9, HSTATE_SCRATCH2(r13) |
Aneesh Kumar K.V | dd96b2c | 2013-10-07 22:17:55 +0530 | [diff] [blame] | 1110 | beq kvmppc_interrupt_pr |
| 1111 | #endif |
Paul Mackerras | 44a3add | 2013-10-04 21:45:04 +1000 | [diff] [blame] | 1112 | /* We're now back in the host but in guest MMU context */ |
| 1113 | li r9, KVM_GUEST_MODE_HOST_HV |
| 1114 | stb r9, HSTATE_IN_GUEST(r13) |
| 1115 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1116 | ld r9, HSTATE_KVM_VCPU(r13) |
| 1117 | |
| 1118 | /* Save registers */ |
| 1119 | |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1120 | std r0, VCPU_GPR(R0)(r9) |
| 1121 | std r1, VCPU_GPR(R1)(r9) |
| 1122 | std r2, VCPU_GPR(R2)(r9) |
| 1123 | std r3, VCPU_GPR(R3)(r9) |
| 1124 | std r4, VCPU_GPR(R4)(r9) |
| 1125 | std r5, VCPU_GPR(R5)(r9) |
| 1126 | std r6, VCPU_GPR(R6)(r9) |
| 1127 | std r7, VCPU_GPR(R7)(r9) |
| 1128 | std r8, VCPU_GPR(R8)(r9) |
Aneesh Kumar K.V | 36e7bb3 | 2013-11-11 19:29:47 +0530 | [diff] [blame] | 1129 | ld r0, HSTATE_SCRATCH2(r13) |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1130 | std r0, VCPU_GPR(R9)(r9) |
| 1131 | std r10, VCPU_GPR(R10)(r9) |
| 1132 | std r11, VCPU_GPR(R11)(r9) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1133 | ld r3, HSTATE_SCRATCH0(r13) |
| 1134 | lwz r4, HSTATE_SCRATCH1(r13) |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1135 | std r3, VCPU_GPR(R12)(r9) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1136 | stw r4, VCPU_CR(r9) |
Paul Mackerras | 0acb911 | 2013-02-04 18:10:51 +0000 | [diff] [blame] | 1137 | BEGIN_FTR_SECTION |
| 1138 | ld r3, HSTATE_CFAR(r13) |
| 1139 | std r3, VCPU_CFAR(r9) |
| 1140 | END_FTR_SECTION_IFSET(CPU_FTR_CFAR) |
Paul Mackerras | 4b8473c | 2013-09-20 14:52:39 +1000 | [diff] [blame] | 1141 | BEGIN_FTR_SECTION |
| 1142 | ld r4, HSTATE_PPR(r13) |
| 1143 | std r4, VCPU_PPR(r9) |
| 1144 | END_FTR_SECTION_IFSET(CPU_FTR_HAS_PPR) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1145 | |
| 1146 | /* Restore R1/R2 so we can handle faults */ |
| 1147 | ld r1, HSTATE_HOST_R1(r13) |
| 1148 | ld r2, PACATOC(r13) |
| 1149 | |
| 1150 | mfspr r10, SPRN_SRR0 |
| 1151 | mfspr r11, SPRN_SRR1 |
| 1152 | std r10, VCPU_SRR0(r9) |
| 1153 | std r11, VCPU_SRR1(r9) |
| 1154 | andi. r0, r12, 2 /* need to read HSRR0/1? */ |
| 1155 | beq 1f |
| 1156 | mfspr r10, SPRN_HSRR0 |
| 1157 | mfspr r11, SPRN_HSRR1 |
| 1158 | clrrdi r12, r12, 2 |
| 1159 | 1: std r10, VCPU_PC(r9) |
| 1160 | std r11, VCPU_MSR(r9) |
| 1161 | |
| 1162 | GET_SCRATCH0(r3) |
| 1163 | mflr r4 |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1164 | std r3, VCPU_GPR(R13)(r9) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1165 | std r4, VCPU_LR(r9) |
| 1166 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1167 | stw r12,VCPU_TRAP(r9) |
| 1168 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1169 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 1170 | addi r3, r9, VCPU_TB_RMINTR |
| 1171 | mr r4, r9 |
| 1172 | bl kvmhv_accumulate_time |
| 1173 | ld r5, VCPU_GPR(R5)(r9) |
| 1174 | ld r6, VCPU_GPR(R6)(r9) |
| 1175 | ld r7, VCPU_GPR(R7)(r9) |
| 1176 | ld r8, VCPU_GPR(R8)(r9) |
| 1177 | #endif |
| 1178 | |
Paul Mackerras | 4a157d6 | 2014-12-03 13:30:39 +1100 | [diff] [blame] | 1179 | /* Save HEIR (HV emulation assist reg) in emul_inst |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1180 | if this is an HEI (HV emulation interrupt, e40) */ |
| 1181 | li r3,KVM_INST_FETCH_FAILED |
Paul Mackerras | 2bf2760 | 2015-03-20 20:39:40 +1100 | [diff] [blame] | 1182 | stw r3,VCPU_LAST_INST(r9) |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1183 | cmpwi r12,BOOK3S_INTERRUPT_H_EMUL_ASSIST |
| 1184 | bne 11f |
| 1185 | mfspr r3,SPRN_HEIR |
Paul Mackerras | 4a157d6 | 2014-12-03 13:30:39 +1100 | [diff] [blame] | 1186 | 11: stw r3,VCPU_HEIR(r9) |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1187 | |
| 1188 | /* these are volatile across C function calls */ |
| 1189 | mfctr r3 |
| 1190 | mfxer r4 |
| 1191 | std r3, VCPU_CTR(r9) |
Sam bobroff | c63517c | 2015-05-27 09:56:57 +1000 | [diff] [blame] | 1192 | std r4, VCPU_XER(r9) |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1193 | |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1194 | /* If this is a page table miss then see if it's theirs or ours */ |
| 1195 | cmpwi r12, BOOK3S_INTERRUPT_H_DATA_STORAGE |
| 1196 | beq kvmppc_hdsi |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1197 | cmpwi r12, BOOK3S_INTERRUPT_H_INST_STORAGE |
| 1198 | beq kvmppc_hisi |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1199 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1200 | /* See if this is a leftover HDEC interrupt */ |
| 1201 | cmpwi r12,BOOK3S_INTERRUPT_HV_DECREMENTER |
| 1202 | bne 2f |
| 1203 | mfspr r3,SPRN_HDEC |
| 1204 | cmpwi r3,0 |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 1205 | mr r4,r9 |
| 1206 | bge fast_guest_return |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1207 | 2: |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1208 | /* See if this is an hcall we can handle in real mode */ |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1209 | cmpwi r12,BOOK3S_INTERRUPT_SYSCALL |
| 1210 | beq hcall_try_real_mode |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1211 | |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 1212 | /* Hypervisor doorbell - exit only if host IPI flag set */ |
| 1213 | cmpwi r12, BOOK3S_INTERRUPT_H_DOORBELL |
| 1214 | bne 3f |
| 1215 | lbz r0, HSTATE_HOST_IPI(r13) |
| 1216 | beq 4f |
| 1217 | b guest_exit_cont |
| 1218 | 3: |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 1219 | /* External interrupt ? */ |
| 1220 | cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 1221 | bne+ guest_exit_cont |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 1222 | |
| 1223 | /* External interrupt, first check for host_ipi. If this is |
| 1224 | * set, we know the host wants us out so let's do it now |
| 1225 | */ |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 1226 | bl kvmppc_read_intr |
| 1227 | cmpdi r3, 0 |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 1228 | bgt guest_exit_cont |
Benjamin Herrenschmidt | 54695c3 | 2013-04-17 20:30:50 +0000 | [diff] [blame] | 1229 | |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 1230 | /* Check if any CPU is heading out to the host, if so head out too */ |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 1231 | 4: ld r5, HSTATE_KVM_VCORE(r13) |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 1232 | lwz r0, VCORE_ENTRY_EXIT(r5) |
| 1233 | cmpwi r0, 0x100 |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 1234 | mr r4, r9 |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 1235 | blt deliver_guest_interrupt |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1236 | |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1237 | guest_exit_cont: /* r9 = vcpu, r12 = trap, r13 = paca */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1238 | /* Save more register state */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1239 | mfdar r6 |
| 1240 | mfdsisr r7 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1241 | std r6, VCPU_DAR(r9) |
| 1242 | stw r7, VCPU_DSISR(r9) |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1243 | /* don't overwrite fault_dar/fault_dsisr if HDSI */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1244 | cmpwi r12,BOOK3S_INTERRUPT_H_DATA_STORAGE |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1245 | beq mc_cont |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1246 | std r6, VCPU_FAULT_DAR(r9) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1247 | stw r7, VCPU_FAULT_DSISR(r9) |
| 1248 | |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1249 | /* See if it is a machine check */ |
| 1250 | cmpwi r12, BOOK3S_INTERRUPT_MACHINE_CHECK |
| 1251 | beq machine_check_realmode |
| 1252 | mc_cont: |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1253 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 1254 | addi r3, r9, VCPU_TB_RMEXIT |
| 1255 | mr r4, r9 |
| 1256 | bl kvmhv_accumulate_time |
| 1257 | #endif |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1258 | |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1259 | /* Increment exit count, poke other threads to exit */ |
| 1260 | bl kvmhv_commence_exit |
Paul Mackerras | eddb60f | 2015-03-28 14:21:11 +1100 | [diff] [blame] | 1261 | nop |
| 1262 | ld r9, HSTATE_KVM_VCPU(r13) |
| 1263 | lwz r12, VCPU_TRAP(r9) |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1264 | |
Paul Mackerras | ec25716 | 2015-06-24 21:18:03 +1000 | [diff] [blame] | 1265 | /* Stop others sending VCPU interrupts to this physical CPU */ |
| 1266 | li r0, -1 |
| 1267 | stw r0, VCPU_CPU(r9) |
| 1268 | stw r0, VCPU_THREAD_CPU(r9) |
| 1269 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1270 | /* Save guest CTRL register, set runlatch to 1 */ |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1271 | mfspr r6,SPRN_CTRLF |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1272 | stw r6,VCPU_CTRL(r9) |
| 1273 | andi. r0,r6,1 |
| 1274 | bne 4f |
| 1275 | ori r6,r6,1 |
| 1276 | mtspr SPRN_CTRLT,r6 |
| 1277 | 4: |
| 1278 | /* Read the guest SLB and save it away */ |
| 1279 | lwz r0,VCPU_SLB_NR(r9) /* number of entries in SLB */ |
| 1280 | mtctr r0 |
| 1281 | li r6,0 |
| 1282 | addi r7,r9,VCPU_SLB |
| 1283 | li r5,0 |
| 1284 | 1: slbmfee r8,r6 |
| 1285 | andis. r0,r8,SLB_ESID_V@h |
| 1286 | beq 2f |
| 1287 | add r8,r8,r6 /* put index in */ |
| 1288 | slbmfev r3,r6 |
| 1289 | std r8,VCPU_SLB_E(r7) |
| 1290 | std r3,VCPU_SLB_V(r7) |
| 1291 | addi r7,r7,VCPU_SLB_SIZE |
| 1292 | addi r5,r5,1 |
| 1293 | 2: addi r6,r6,1 |
| 1294 | bdnz 1b |
| 1295 | stw r5,VCPU_SLB_MAX(r9) |
| 1296 | |
| 1297 | /* |
| 1298 | * Save the guest PURR/SPURR |
| 1299 | */ |
| 1300 | mfspr r5,SPRN_PURR |
| 1301 | mfspr r6,SPRN_SPURR |
| 1302 | ld r7,VCPU_PURR(r9) |
| 1303 | ld r8,VCPU_SPURR(r9) |
| 1304 | std r5,VCPU_PURR(r9) |
| 1305 | std r6,VCPU_SPURR(r9) |
| 1306 | subf r5,r7,r5 |
| 1307 | subf r6,r8,r6 |
| 1308 | |
| 1309 | /* |
| 1310 | * Restore host PURR/SPURR and add guest times |
| 1311 | * so that the time in the guest gets accounted. |
| 1312 | */ |
| 1313 | ld r3,HSTATE_PURR(r13) |
| 1314 | ld r4,HSTATE_SPURR(r13) |
| 1315 | add r3,r3,r5 |
| 1316 | add r4,r4,r6 |
| 1317 | mtspr SPRN_PURR,r3 |
| 1318 | mtspr SPRN_SPURR,r4 |
| 1319 | |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 1320 | /* Save DEC */ |
| 1321 | mfspr r5,SPRN_DEC |
| 1322 | mftb r6 |
| 1323 | extsw r5,r5 |
| 1324 | add r5,r5,r6 |
Paul Mackerras | c5fb80d | 2014-03-25 10:47:07 +1100 | [diff] [blame] | 1325 | /* r5 is a guest timebase value here, convert to host TB */ |
| 1326 | ld r3,HSTATE_KVM_VCORE(r13) |
| 1327 | ld r4,VCORE_TB_OFFSET(r3) |
| 1328 | subf r5,r4,r5 |
Paul Mackerras | 93b0f4d | 2013-09-06 13:17:46 +1000 | [diff] [blame] | 1329 | std r5,VCPU_DEC_EXPIRES(r9) |
| 1330 | |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1331 | BEGIN_FTR_SECTION |
| 1332 | b 8f |
| 1333 | END_FTR_SECTION_IFCLR(CPU_FTR_ARCH_207S) |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1334 | /* Save POWER8-specific registers */ |
| 1335 | mfspr r5, SPRN_IAMR |
| 1336 | mfspr r6, SPRN_PSPB |
| 1337 | mfspr r7, SPRN_FSCR |
| 1338 | std r5, VCPU_IAMR(r9) |
| 1339 | stw r6, VCPU_PSPB(r9) |
| 1340 | std r7, VCPU_FSCR(r9) |
| 1341 | mfspr r5, SPRN_IC |
| 1342 | mfspr r6, SPRN_VTB |
| 1343 | mfspr r7, SPRN_TAR |
| 1344 | std r5, VCPU_IC(r9) |
| 1345 | std r6, VCPU_VTB(r9) |
| 1346 | std r7, VCPU_TAR(r9) |
Michael Neuling | 7b49041 | 2014-01-08 21:25:32 +1100 | [diff] [blame] | 1347 | mfspr r8, SPRN_EBBHR |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1348 | std r8, VCPU_EBBHR(r9) |
| 1349 | mfspr r5, SPRN_EBBRR |
| 1350 | mfspr r6, SPRN_BESCR |
| 1351 | mfspr r7, SPRN_CSIGR |
| 1352 | mfspr r8, SPRN_TACR |
| 1353 | std r5, VCPU_EBBRR(r9) |
| 1354 | std r6, VCPU_BESCR(r9) |
| 1355 | std r7, VCPU_CSIGR(r9) |
| 1356 | std r8, VCPU_TACR(r9) |
| 1357 | mfspr r5, SPRN_TCSCR |
| 1358 | mfspr r6, SPRN_ACOP |
| 1359 | mfspr r7, SPRN_PID |
| 1360 | mfspr r8, SPRN_WORT |
| 1361 | std r5, VCPU_TCSCR(r9) |
| 1362 | std r6, VCPU_ACOP(r9) |
| 1363 | stw r7, VCPU_GUEST_PID(r9) |
| 1364 | std r8, VCPU_WORT(r9) |
| 1365 | 8: |
| 1366 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1367 | /* Save and reset AMR and UAMOR before turning on the MMU */ |
| 1368 | mfspr r5,SPRN_AMR |
| 1369 | mfspr r6,SPRN_UAMOR |
| 1370 | std r5,VCPU_AMR(r9) |
| 1371 | std r6,VCPU_UAMOR(r9) |
| 1372 | li r6,0 |
| 1373 | mtspr SPRN_AMR,r6 |
| 1374 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1375 | /* Switch DSCR back to host value */ |
| 1376 | mfspr r8, SPRN_DSCR |
| 1377 | ld r7, HSTATE_DSCR(r13) |
Paul Mackerras | cfc8602 | 2013-09-21 09:53:28 +1000 | [diff] [blame] | 1378 | std r8, VCPU_DSCR(r9) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1379 | mtspr SPRN_DSCR, r7 |
| 1380 | |
| 1381 | /* Save non-volatile GPRs */ |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1382 | std r14, VCPU_GPR(R14)(r9) |
| 1383 | std r15, VCPU_GPR(R15)(r9) |
| 1384 | std r16, VCPU_GPR(R16)(r9) |
| 1385 | std r17, VCPU_GPR(R17)(r9) |
| 1386 | std r18, VCPU_GPR(R18)(r9) |
| 1387 | std r19, VCPU_GPR(R19)(r9) |
| 1388 | std r20, VCPU_GPR(R20)(r9) |
| 1389 | std r21, VCPU_GPR(R21)(r9) |
| 1390 | std r22, VCPU_GPR(R22)(r9) |
| 1391 | std r23, VCPU_GPR(R23)(r9) |
| 1392 | std r24, VCPU_GPR(R24)(r9) |
| 1393 | std r25, VCPU_GPR(R25)(r9) |
| 1394 | std r26, VCPU_GPR(R26)(r9) |
| 1395 | std r27, VCPU_GPR(R27)(r9) |
| 1396 | std r28, VCPU_GPR(R28)(r9) |
| 1397 | std r29, VCPU_GPR(R29)(r9) |
| 1398 | std r30, VCPU_GPR(R30)(r9) |
| 1399 | std r31, VCPU_GPR(R31)(r9) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1400 | |
| 1401 | /* Save SPRGs */ |
| 1402 | mfspr r3, SPRN_SPRG0 |
| 1403 | mfspr r4, SPRN_SPRG1 |
| 1404 | mfspr r5, SPRN_SPRG2 |
| 1405 | mfspr r6, SPRN_SPRG3 |
| 1406 | std r3, VCPU_SPRG0(r9) |
| 1407 | std r4, VCPU_SPRG1(r9) |
| 1408 | std r5, VCPU_SPRG2(r9) |
| 1409 | std r6, VCPU_SPRG3(r9) |
| 1410 | |
Paul Mackerras | 8943633 | 2012-03-02 01:38:23 +0000 | [diff] [blame] | 1411 | /* save FP state */ |
| 1412 | mr r3, r9 |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 1413 | bl kvmppc_save_fp |
Paul Mackerras | 8943633 | 2012-03-02 01:38:23 +0000 | [diff] [blame] | 1414 | |
Paul Mackerras | 0a8ecce | 2014-04-14 08:56:26 +1000 | [diff] [blame] | 1415 | #ifdef CONFIG_PPC_TRANSACTIONAL_MEM |
| 1416 | BEGIN_FTR_SECTION |
| 1417 | b 2f |
| 1418 | END_FTR_SECTION_IFCLR(CPU_FTR_TM) |
| 1419 | /* Turn on TM. */ |
| 1420 | mfmsr r8 |
| 1421 | li r0, 1 |
| 1422 | rldimi r8, r0, MSR_TM_LG, 63-MSR_TM_LG |
| 1423 | mtmsrd r8 |
| 1424 | |
| 1425 | ld r5, VCPU_MSR(r9) |
| 1426 | rldicl. r5, r5, 64 - MSR_TS_S_LG, 62 |
| 1427 | beq 1f /* TM not active in guest. */ |
| 1428 | |
| 1429 | li r3, TM_CAUSE_KVM_RESCHED |
| 1430 | |
| 1431 | /* Clear the MSR RI since r1, r13 are all going to be foobar. */ |
| 1432 | li r5, 0 |
| 1433 | mtmsrd r5, 1 |
| 1434 | |
| 1435 | /* All GPRs are volatile at this point. */ |
| 1436 | TRECLAIM(R3) |
| 1437 | |
| 1438 | /* Temporarily store r13 and r9 so we have some regs to play with */ |
| 1439 | SET_SCRATCH0(r13) |
| 1440 | GET_PACA(r13) |
| 1441 | std r9, PACATMSCRATCH(r13) |
| 1442 | ld r9, HSTATE_KVM_VCPU(r13) |
| 1443 | |
| 1444 | /* Get a few more GPRs free. */ |
| 1445 | std r29, VCPU_GPRS_TM(29)(r9) |
| 1446 | std r30, VCPU_GPRS_TM(30)(r9) |
| 1447 | std r31, VCPU_GPRS_TM(31)(r9) |
| 1448 | |
| 1449 | /* Save away PPR and DSCR soon so don't run with user values. */ |
| 1450 | mfspr r31, SPRN_PPR |
| 1451 | HMT_MEDIUM |
| 1452 | mfspr r30, SPRN_DSCR |
| 1453 | ld r29, HSTATE_DSCR(r13) |
| 1454 | mtspr SPRN_DSCR, r29 |
| 1455 | |
| 1456 | /* Save all but r9, r13 & r29-r31 */ |
| 1457 | reg = 0 |
| 1458 | .rept 29 |
| 1459 | .if (reg != 9) && (reg != 13) |
| 1460 | std reg, VCPU_GPRS_TM(reg)(r9) |
| 1461 | .endif |
| 1462 | reg = reg + 1 |
| 1463 | .endr |
| 1464 | /* ... now save r13 */ |
| 1465 | GET_SCRATCH0(r4) |
| 1466 | std r4, VCPU_GPRS_TM(13)(r9) |
| 1467 | /* ... and save r9 */ |
| 1468 | ld r4, PACATMSCRATCH(r13) |
| 1469 | std r4, VCPU_GPRS_TM(9)(r9) |
| 1470 | |
| 1471 | /* Reload stack pointer and TOC. */ |
| 1472 | ld r1, HSTATE_HOST_R1(r13) |
| 1473 | ld r2, PACATOC(r13) |
| 1474 | |
| 1475 | /* Set MSR RI now we have r1 and r13 back. */ |
| 1476 | li r5, MSR_RI |
| 1477 | mtmsrd r5, 1 |
| 1478 | |
| 1479 | /* Save away checkpinted SPRs. */ |
| 1480 | std r31, VCPU_PPR_TM(r9) |
| 1481 | std r30, VCPU_DSCR_TM(r9) |
| 1482 | mflr r5 |
| 1483 | mfcr r6 |
| 1484 | mfctr r7 |
| 1485 | mfspr r8, SPRN_AMR |
| 1486 | mfspr r10, SPRN_TAR |
| 1487 | std r5, VCPU_LR_TM(r9) |
| 1488 | stw r6, VCPU_CR_TM(r9) |
| 1489 | std r7, VCPU_CTR_TM(r9) |
| 1490 | std r8, VCPU_AMR_TM(r9) |
| 1491 | std r10, VCPU_TAR_TM(r9) |
| 1492 | |
| 1493 | /* Restore r12 as trap number. */ |
| 1494 | lwz r12, VCPU_TRAP(r9) |
| 1495 | |
| 1496 | /* Save FP/VSX. */ |
| 1497 | addi r3, r9, VCPU_FPRS_TM |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 1498 | bl store_fp_state |
Paul Mackerras | 0a8ecce | 2014-04-14 08:56:26 +1000 | [diff] [blame] | 1499 | addi r3, r9, VCPU_VRS_TM |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 1500 | bl store_vr_state |
Paul Mackerras | 0a8ecce | 2014-04-14 08:56:26 +1000 | [diff] [blame] | 1501 | mfspr r6, SPRN_VRSAVE |
| 1502 | stw r6, VCPU_VRSAVE_TM(r9) |
| 1503 | 1: |
| 1504 | /* |
| 1505 | * We need to save these SPRs after the treclaim so that the software |
| 1506 | * error code is recorded correctly in the TEXASR. Also the user may |
| 1507 | * change these outside of a transaction, so they must always be |
| 1508 | * context switched. |
| 1509 | */ |
| 1510 | mfspr r5, SPRN_TFHAR |
| 1511 | mfspr r6, SPRN_TFIAR |
| 1512 | mfspr r7, SPRN_TEXASR |
| 1513 | std r5, VCPU_TFHAR(r9) |
| 1514 | std r6, VCPU_TFIAR(r9) |
| 1515 | std r7, VCPU_TEXASR(r9) |
| 1516 | 2: |
| 1517 | #endif |
| 1518 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1519 | /* Increment yield count if they have a VPA */ |
| 1520 | ld r8, VCPU_VPA(r9) /* do they have a VPA? */ |
| 1521 | cmpdi r8, 0 |
| 1522 | beq 25f |
Alexander Graf | 0865a58 | 2014-06-11 10:36:17 +0200 | [diff] [blame] | 1523 | li r4, LPPACA_YIELDCOUNT |
| 1524 | LWZX_BE r3, r8, r4 |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1525 | addi r3, r3, 1 |
Alexander Graf | 0865a58 | 2014-06-11 10:36:17 +0200 | [diff] [blame] | 1526 | STWX_BE r3, r8, r4 |
Paul Mackerras | c35635e | 2013-04-18 19:51:04 +0000 | [diff] [blame] | 1527 | li r3, 1 |
| 1528 | stb r3, VCPU_VPA_DIRTY(r9) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1529 | 25: |
| 1530 | /* Save PMU registers if requested */ |
| 1531 | /* r8 and cr0.eq are live here */ |
Paul Mackerras | 9bc01a9 | 2014-05-26 19:48:40 +1000 | [diff] [blame] | 1532 | BEGIN_FTR_SECTION |
| 1533 | /* |
| 1534 | * POWER8 seems to have a hardware bug where setting |
| 1535 | * MMCR0[PMAE] along with MMCR0[PMC1CE] and/or MMCR0[PMCjCE] |
| 1536 | * when some counters are already negative doesn't seem |
| 1537 | * to cause a performance monitor alert (and hence interrupt). |
| 1538 | * The effect of this is that when saving the PMU state, |
| 1539 | * if there is no PMU alert pending when we read MMCR0 |
| 1540 | * before freezing the counters, but one becomes pending |
| 1541 | * before we read the counters, we lose it. |
| 1542 | * To work around this, we need a way to freeze the counters |
| 1543 | * before reading MMCR0. Normally, freezing the counters |
| 1544 | * is done by writing MMCR0 (to set MMCR0[FC]) which |
| 1545 | * unavoidably writes MMCR0[PMA0] as well. On POWER8, |
| 1546 | * we can also freeze the counters using MMCR2, by writing |
| 1547 | * 1s to all the counter freeze condition bits (there are |
| 1548 | * 9 bits each for 6 counters). |
| 1549 | */ |
| 1550 | li r3, -1 /* set all freeze bits */ |
| 1551 | clrrdi r3, r3, 10 |
| 1552 | mfspr r10, SPRN_MMCR2 |
| 1553 | mtspr SPRN_MMCR2, r3 |
| 1554 | isync |
| 1555 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1556 | li r3, 1 |
| 1557 | sldi r3, r3, 31 /* MMCR0_FC (freeze counters) bit */ |
| 1558 | mfspr r4, SPRN_MMCR0 /* save MMCR0 */ |
| 1559 | mtspr SPRN_MMCR0, r3 /* freeze all counters, disable ints */ |
Paul Mackerras | 8943633 | 2012-03-02 01:38:23 +0000 | [diff] [blame] | 1560 | mfspr r6, SPRN_MMCRA |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 1561 | /* Clear MMCRA in order to disable SDAR updates */ |
Paul Mackerras | 8943633 | 2012-03-02 01:38:23 +0000 | [diff] [blame] | 1562 | li r7, 0 |
| 1563 | mtspr SPRN_MMCRA, r7 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1564 | isync |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1565 | beq 21f /* if no VPA, save PMU stuff anyway */ |
| 1566 | lbz r7, LPPACA_PMCINUSE(r8) |
| 1567 | cmpwi r7, 0 /* did they ask for PMU stuff to be saved? */ |
| 1568 | bne 21f |
| 1569 | std r3, VCPU_MMCR(r9) /* if not, set saved MMCR0 to FC */ |
| 1570 | b 22f |
| 1571 | 21: mfspr r5, SPRN_MMCR1 |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 1572 | mfspr r7, SPRN_SIAR |
| 1573 | mfspr r8, SPRN_SDAR |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1574 | std r4, VCPU_MMCR(r9) |
| 1575 | std r5, VCPU_MMCR + 8(r9) |
| 1576 | std r6, VCPU_MMCR + 16(r9) |
Paul Mackerras | 9bc01a9 | 2014-05-26 19:48:40 +1000 | [diff] [blame] | 1577 | BEGIN_FTR_SECTION |
| 1578 | std r10, VCPU_MMCR + 24(r9) |
| 1579 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | 1494178 | 2013-09-06 13:11:18 +1000 | [diff] [blame] | 1580 | std r7, VCPU_SIAR(r9) |
| 1581 | std r8, VCPU_SDAR(r9) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1582 | mfspr r3, SPRN_PMC1 |
| 1583 | mfspr r4, SPRN_PMC2 |
| 1584 | mfspr r5, SPRN_PMC3 |
| 1585 | mfspr r6, SPRN_PMC4 |
| 1586 | mfspr r7, SPRN_PMC5 |
| 1587 | mfspr r8, SPRN_PMC6 |
| 1588 | stw r3, VCPU_PMC(r9) |
| 1589 | stw r4, VCPU_PMC + 4(r9) |
| 1590 | stw r5, VCPU_PMC + 8(r9) |
| 1591 | stw r6, VCPU_PMC + 12(r9) |
| 1592 | stw r7, VCPU_PMC + 16(r9) |
| 1593 | stw r8, VCPU_PMC + 20(r9) |
Paul Mackerras | 9e368f2 | 2011-06-29 00:40:08 +0000 | [diff] [blame] | 1594 | BEGIN_FTR_SECTION |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1595 | mfspr r5, SPRN_SIER |
| 1596 | mfspr r6, SPRN_SPMC1 |
| 1597 | mfspr r7, SPRN_SPMC2 |
| 1598 | mfspr r8, SPRN_MMCRS |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1599 | std r5, VCPU_SIER(r9) |
| 1600 | stw r6, VCPU_PMC + 24(r9) |
| 1601 | stw r7, VCPU_PMC + 28(r9) |
| 1602 | std r8, VCPU_MMCR + 32(r9) |
| 1603 | lis r4, 0x8000 |
| 1604 | mtspr SPRN_MMCRS, r4 |
| 1605 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1606 | 22: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1607 | /* Clear out SLB */ |
| 1608 | li r5,0 |
| 1609 | slbmte r5,r5 |
| 1610 | slbia |
| 1611 | ptesync |
| 1612 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1613 | /* |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 1614 | * POWER7/POWER8 guest -> host partition switch code. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1615 | * We don't have to lock against tlbies but we do |
| 1616 | * have to coordinate the hardware threads. |
| 1617 | */ |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1618 | kvmhv_switch_to_host: |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1619 | /* Secondary threads wait for primary to do partition switch */ |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 1620 | ld r5,HSTATE_KVM_VCORE(r13) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 1621 | ld r4,VCORE_KVM(r5) /* pointer to struct kvm */ |
| 1622 | lbz r3,HSTATE_PTID(r13) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1623 | cmpwi r3,0 |
| 1624 | beq 15f |
| 1625 | HMT_LOW |
| 1626 | 13: lbz r3,VCORE_IN_GUEST(r5) |
| 1627 | cmpwi r3,0 |
| 1628 | bne 13b |
| 1629 | HMT_MEDIUM |
| 1630 | b 16f |
| 1631 | |
| 1632 | /* Primary thread waits for all the secondaries to exit guest */ |
| 1633 | 15: lwz r3,VCORE_ENTRY_EXIT(r5) |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1634 | rlwinm r0,r3,32-8,0xff |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1635 | clrldi r3,r3,56 |
| 1636 | cmpw r3,r0 |
| 1637 | bne 15b |
| 1638 | isync |
| 1639 | |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1640 | /* Did we actually switch to the guest at all? */ |
| 1641 | lbz r6, VCORE_IN_GUEST(r5) |
| 1642 | cmpwi r6, 0 |
| 1643 | beq 19f |
| 1644 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1645 | /* Primary thread switches back to host partition */ |
| 1646 | ld r6,KVM_HOST_SDR1(r4) |
| 1647 | lwz r7,KVM_HOST_LPID(r4) |
| 1648 | li r8,LPID_RSVD /* switch to reserved LPID */ |
| 1649 | mtspr SPRN_LPID,r8 |
| 1650 | ptesync |
| 1651 | mtspr SPRN_SDR1,r6 /* switch to partition page table */ |
| 1652 | mtspr SPRN_LPID,r7 |
| 1653 | isync |
| 1654 | |
Michael Neuling | b005255e | 2014-01-08 21:25:21 +1100 | [diff] [blame] | 1655 | BEGIN_FTR_SECTION |
| 1656 | /* DPDES is shared between threads */ |
| 1657 | mfspr r7, SPRN_DPDES |
| 1658 | std r7, VCORE_DPDES(r5) |
| 1659 | /* clear DPDES so we don't get guest doorbells in the host */ |
| 1660 | li r8, 0 |
| 1661 | mtspr SPRN_DPDES, r8 |
| 1662 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
| 1663 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1664 | /* Subtract timebase offset from timebase */ |
| 1665 | ld r8,VCORE_TB_OFFSET(r5) |
| 1666 | cmpdi r8,0 |
| 1667 | beq 17f |
Paul Mackerras | c5fb80d | 2014-03-25 10:47:07 +1100 | [diff] [blame] | 1668 | mftb r6 /* current guest timebase */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1669 | subf r8,r8,r6 |
| 1670 | mtspr SPRN_TBU40,r8 /* update upper 40 bits */ |
| 1671 | mftb r7 /* check if lower 24 bits overflowed */ |
| 1672 | clrldi r6,r6,40 |
| 1673 | clrldi r7,r7,40 |
| 1674 | cmpld r7,r6 |
| 1675 | bge 17f |
| 1676 | addis r8,r8,0x100 /* if so, increment upper 40 bits */ |
| 1677 | mtspr SPRN_TBU40,r8 |
| 1678 | |
| 1679 | /* Reset PCR */ |
| 1680 | 17: ld r0, VCORE_PCR(r5) |
| 1681 | cmpdi r0, 0 |
| 1682 | beq 18f |
| 1683 | li r0, 0 |
| 1684 | mtspr SPRN_PCR, r0 |
| 1685 | 18: |
| 1686 | /* Signal secondary CPUs to continue */ |
| 1687 | stb r0,VCORE_IN_GUEST(r5) |
Paul Mackerras | b4deba5 | 2015-07-02 20:38:16 +1000 | [diff] [blame] | 1688 | 19: lis r8,0x7fff /* MAX_INT@h */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1689 | mtspr SPRN_HDEC,r8 |
| 1690 | |
| 1691 | 16: ld r8,KVM_HOST_LPCR(r4) |
| 1692 | mtspr SPRN_LPCR,r8 |
| 1693 | isync |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1694 | |
| 1695 | /* load host SLB entries */ |
Paul Mackerras | c17b98c | 2014-12-03 13:30:38 +1100 | [diff] [blame] | 1696 | ld r8,PACA_SLBSHADOWPTR(r13) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1697 | |
| 1698 | .rept SLB_NUM_BOLTED |
Alexander Graf | 0865a58 | 2014-06-11 10:36:17 +0200 | [diff] [blame] | 1699 | li r3, SLBSHADOW_SAVEAREA |
| 1700 | LDX_BE r5, r8, r3 |
| 1701 | addi r3, r3, 8 |
| 1702 | LDX_BE r6, r8, r3 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1703 | andis. r7,r5,SLB_ESID_V@h |
| 1704 | beq 1f |
| 1705 | slbmte r6,r5 |
| 1706 | 1: addi r8,r8,16 |
| 1707 | .endr |
| 1708 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 1709 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 1710 | /* Finish timing, if we have a vcpu */ |
| 1711 | ld r4, HSTATE_KVM_VCPU(r13) |
| 1712 | cmpdi r4, 0 |
| 1713 | li r3, 0 |
| 1714 | beq 2f |
| 1715 | bl kvmhv_accumulate_time |
| 1716 | 2: |
| 1717 | #endif |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1718 | /* Unset guest mode */ |
| 1719 | li r0, KVM_GUEST_MODE_NONE |
| 1720 | stb r0, HSTATE_IN_GUEST(r13) |
| 1721 | |
Paul Mackerras | 218309b | 2013-09-06 13:23:44 +1000 | [diff] [blame] | 1722 | ld r0, 112+PPC_LR_STKOFF(r1) |
| 1723 | addi r1, r1, 112 |
| 1724 | mtlr r0 |
| 1725 | blr |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1726 | |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1727 | /* |
| 1728 | * Check whether an HDSI is an HPTE not found fault or something else. |
| 1729 | * If it is an HPTE not found fault that is due to the guest accessing |
| 1730 | * a page that they have mapped but which we have paged out, then |
| 1731 | * we continue on with the guest exit path. In all other cases, |
| 1732 | * reflect the HDSI to the guest as a DSI. |
| 1733 | */ |
| 1734 | kvmppc_hdsi: |
| 1735 | mfspr r4, SPRN_HDAR |
| 1736 | mfspr r6, SPRN_HDSISR |
Paul Mackerras | 4cf302b | 2011-12-12 12:38:51 +0000 | [diff] [blame] | 1737 | /* HPTE not found fault or protection fault? */ |
| 1738 | andis. r0, r6, (DSISR_NOHPTE | DSISR_PROTFAULT)@h |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1739 | beq 1f /* if not, send it to the guest */ |
| 1740 | andi. r0, r11, MSR_DR /* data relocation enabled? */ |
| 1741 | beq 3f |
| 1742 | clrrdi r0, r4, 28 |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1743 | PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */ |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1744 | bne 1f /* if no SLB entry found */ |
| 1745 | 4: std r4, VCPU_FAULT_DAR(r9) |
| 1746 | stw r6, VCPU_FAULT_DSISR(r9) |
| 1747 | |
| 1748 | /* Search the hash table. */ |
| 1749 | mr r3, r9 /* vcpu pointer */ |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1750 | li r7, 1 /* data fault */ |
Anton Blanchard | b1576fe | 2014-02-04 16:04:35 +1100 | [diff] [blame] | 1751 | bl kvmppc_hpte_hv_fault |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1752 | ld r9, HSTATE_KVM_VCPU(r13) |
| 1753 | ld r10, VCPU_PC(r9) |
| 1754 | ld r11, VCPU_MSR(r9) |
| 1755 | li r12, BOOK3S_INTERRUPT_H_DATA_STORAGE |
| 1756 | cmpdi r3, 0 /* retry the instruction */ |
| 1757 | beq 6f |
| 1758 | cmpdi r3, -1 /* handle in kernel mode */ |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1759 | beq guest_exit_cont |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1760 | cmpdi r3, -2 /* MMIO emulation; need instr word */ |
| 1761 | beq 2f |
| 1762 | |
| 1763 | /* Synthesize a DSI for the guest */ |
| 1764 | ld r4, VCPU_FAULT_DAR(r9) |
| 1765 | mr r6, r3 |
| 1766 | 1: mtspr SPRN_DAR, r4 |
| 1767 | mtspr SPRN_DSISR, r6 |
| 1768 | mtspr SPRN_SRR0, r10 |
| 1769 | mtspr SPRN_SRR1, r11 |
| 1770 | li r10, BOOK3S_INTERRUPT_DATA_STORAGE |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 1771 | bl kvmppc_msr_interrupt |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1772 | fast_interrupt_c_return: |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1773 | 6: ld r7, VCPU_CTR(r9) |
Sam bobroff | c63517c | 2015-05-27 09:56:57 +1000 | [diff] [blame] | 1774 | ld r8, VCPU_XER(r9) |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1775 | mtctr r7 |
| 1776 | mtxer r8 |
| 1777 | mr r4, r9 |
| 1778 | b fast_guest_return |
| 1779 | |
| 1780 | 3: ld r5, VCPU_KVM(r9) /* not relocated, use VRMA */ |
| 1781 | ld r5, KVM_VRMA_SLB_V(r5) |
| 1782 | b 4b |
| 1783 | |
| 1784 | /* If this is for emulated MMIO, load the instruction word */ |
| 1785 | 2: li r8, KVM_INST_FETCH_FAILED /* In case lwz faults */ |
| 1786 | |
| 1787 | /* Set guest mode to 'jump over instruction' so if lwz faults |
| 1788 | * we'll just continue at the next IP. */ |
| 1789 | li r0, KVM_GUEST_MODE_SKIP |
| 1790 | stb r0, HSTATE_IN_GUEST(r13) |
| 1791 | |
| 1792 | /* Do the access with MSR:DR enabled */ |
| 1793 | mfmsr r3 |
| 1794 | ori r4, r3, MSR_DR /* Enable paging for data */ |
| 1795 | mtmsrd r4 |
| 1796 | lwz r8, 0(r10) |
| 1797 | mtmsrd r3 |
| 1798 | |
| 1799 | /* Store the result */ |
| 1800 | stw r8, VCPU_LAST_INST(r9) |
| 1801 | |
| 1802 | /* Unset guest mode. */ |
Paul Mackerras | 44a3add | 2013-10-04 21:45:04 +1000 | [diff] [blame] | 1803 | li r0, KVM_GUEST_MODE_HOST_HV |
Paul Mackerras | 697d389 | 2011-12-12 12:36:37 +0000 | [diff] [blame] | 1804 | stb r0, HSTATE_IN_GUEST(r13) |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1805 | b guest_exit_cont |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 1806 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1807 | /* |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1808 | * Similarly for an HISI, reflect it to the guest as an ISI unless |
| 1809 | * it is an HPTE not found fault for a page that we have paged out. |
| 1810 | */ |
| 1811 | kvmppc_hisi: |
| 1812 | andis. r0, r11, SRR1_ISI_NOPT@h |
| 1813 | beq 1f |
| 1814 | andi. r0, r11, MSR_IR /* instruction relocation enabled? */ |
| 1815 | beq 3f |
| 1816 | clrrdi r0, r10, 28 |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1817 | PPC_SLBFEE_DOT(R5, R0) /* if so, look up SLB */ |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1818 | bne 1f /* if no SLB entry found */ |
| 1819 | 4: |
| 1820 | /* Search the hash table. */ |
| 1821 | mr r3, r9 /* vcpu pointer */ |
| 1822 | mr r4, r10 |
| 1823 | mr r6, r11 |
| 1824 | li r7, 0 /* instruction fault */ |
Anton Blanchard | b1576fe | 2014-02-04 16:04:35 +1100 | [diff] [blame] | 1825 | bl kvmppc_hpte_hv_fault |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1826 | ld r9, HSTATE_KVM_VCPU(r13) |
| 1827 | ld r10, VCPU_PC(r9) |
| 1828 | ld r11, VCPU_MSR(r9) |
| 1829 | li r12, BOOK3S_INTERRUPT_H_INST_STORAGE |
| 1830 | cmpdi r3, 0 /* retry the instruction */ |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1831 | beq fast_interrupt_c_return |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1832 | cmpdi r3, -1 /* handle in kernel mode */ |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1833 | beq guest_exit_cont |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1834 | |
| 1835 | /* Synthesize an ISI for the guest */ |
| 1836 | mr r11, r3 |
| 1837 | 1: mtspr SPRN_SRR0, r10 |
| 1838 | mtspr SPRN_SRR1, r11 |
| 1839 | li r10, BOOK3S_INTERRUPT_INST_STORAGE |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 1840 | bl kvmppc_msr_interrupt |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1841 | b fast_interrupt_c_return |
Paul Mackerras | 342d3db | 2011-12-12 12:38:05 +0000 | [diff] [blame] | 1842 | |
| 1843 | 3: ld r6, VCPU_KVM(r9) /* not relocated, use VRMA */ |
| 1844 | ld r5, KVM_VRMA_SLB_V(r6) |
| 1845 | b 4b |
| 1846 | |
| 1847 | /* |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1848 | * Try to handle an hcall in real mode. |
| 1849 | * Returns to the guest if we handle it, or continues on up to |
| 1850 | * the kernel if we can't (i.e. if we don't have a handler for |
| 1851 | * it, or if the handler returns H_TOO_HARD). |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 1852 | * |
| 1853 | * r5 - r8 contain hcall args, |
| 1854 | * r9 = vcpu, r10 = pc, r11 = msr, r12 = trap, r13 = paca |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1855 | */ |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1856 | hcall_try_real_mode: |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1857 | ld r3,VCPU_GPR(R3)(r9) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1858 | andi. r0,r11,MSR_PR |
Liu Ping Fan | 27025a6 | 2013-11-19 14:12:48 +0800 | [diff] [blame] | 1859 | /* sc 1 from userspace - reflect to guest syscall */ |
| 1860 | bne sc_1_fast_return |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1861 | clrrdi r3,r3,2 |
| 1862 | cmpldi r3,hcall_real_table_end - hcall_real_table |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1863 | bge guest_exit_cont |
Paul Mackerras | 699a0ea | 2014-06-02 11:02:59 +1000 | [diff] [blame] | 1864 | /* See if this hcall is enabled for in-kernel handling */ |
| 1865 | ld r4, VCPU_KVM(r9) |
| 1866 | srdi r0, r3, 8 /* r0 = (r3 / 4) >> 6 */ |
| 1867 | sldi r0, r0, 3 /* index into kvm->arch.enabled_hcalls[] */ |
| 1868 | add r4, r4, r0 |
| 1869 | ld r0, KVM_ENABLED_HCALLS(r4) |
| 1870 | rlwinm r4, r3, 32-2, 0x3f /* r4 = (r3 / 4) & 0x3f */ |
| 1871 | srd r0, r0, r4 |
| 1872 | andi. r0, r0, 1 |
| 1873 | beq guest_exit_cont |
| 1874 | /* Get pointer to handler, if any, and call it */ |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1875 | LOAD_REG_ADDR(r4, hcall_real_table) |
Paul Mackerras | 4baa1d8 | 2013-07-08 20:09:53 +1000 | [diff] [blame] | 1876 | lwax r3,r3,r4 |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1877 | cmpwi r3,0 |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1878 | beq guest_exit_cont |
Anton Blanchard | 05a308c | 2014-06-12 18:16:10 +1000 | [diff] [blame] | 1879 | add r12,r3,r4 |
| 1880 | mtctr r12 |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1881 | mr r3,r9 /* get vcpu pointer */ |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1882 | ld r4,VCPU_GPR(R4)(r9) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1883 | bctrl |
| 1884 | cmpdi r3,H_TOO_HARD |
| 1885 | beq hcall_real_fallback |
| 1886 | ld r4,HSTATE_KVM_VCPU(r13) |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 1887 | std r3,VCPU_GPR(R3)(r4) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1888 | ld r10,VCPU_PC(r4) |
| 1889 | ld r11,VCPU_MSR(r4) |
| 1890 | b fast_guest_return |
| 1891 | |
Liu Ping Fan | 27025a6 | 2013-11-19 14:12:48 +0800 | [diff] [blame] | 1892 | sc_1_fast_return: |
| 1893 | mtspr SPRN_SRR0,r10 |
| 1894 | mtspr SPRN_SRR1,r11 |
| 1895 | li r10, BOOK3S_INTERRUPT_SYSCALL |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 1896 | bl kvmppc_msr_interrupt |
Liu Ping Fan | 27025a6 | 2013-11-19 14:12:48 +0800 | [diff] [blame] | 1897 | mr r4,r9 |
| 1898 | b fast_guest_return |
| 1899 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1900 | /* We've attempted a real mode hcall, but it's punted it back |
| 1901 | * to userspace. We need to restore some clobbered volatiles |
| 1902 | * before resuming the pass-it-to-qemu path */ |
| 1903 | hcall_real_fallback: |
| 1904 | li r12,BOOK3S_INTERRUPT_SYSCALL |
| 1905 | ld r9, HSTATE_KVM_VCPU(r13) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1906 | |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 1907 | b guest_exit_cont |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1908 | |
| 1909 | .globl hcall_real_table |
| 1910 | hcall_real_table: |
| 1911 | .long 0 /* 0 - unused */ |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1912 | .long DOTSYM(kvmppc_h_remove) - hcall_real_table |
| 1913 | .long DOTSYM(kvmppc_h_enter) - hcall_real_table |
| 1914 | .long DOTSYM(kvmppc_h_read) - hcall_real_table |
Paul Mackerras | cdeee51 | 2015-06-24 21:18:07 +1000 | [diff] [blame] | 1915 | .long DOTSYM(kvmppc_h_clear_mod) - hcall_real_table |
| 1916 | .long DOTSYM(kvmppc_h_clear_ref) - hcall_real_table |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1917 | .long DOTSYM(kvmppc_h_protect) - hcall_real_table |
| 1918 | .long DOTSYM(kvmppc_h_get_tce) - hcall_real_table |
| 1919 | .long DOTSYM(kvmppc_h_put_tce) - hcall_real_table |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1920 | .long 0 /* 0x24 - H_SET_SPRG0 */ |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1921 | .long DOTSYM(kvmppc_h_set_dabr) - hcall_real_table |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1922 | .long 0 /* 0x2c */ |
| 1923 | .long 0 /* 0x30 */ |
| 1924 | .long 0 /* 0x34 */ |
| 1925 | .long 0 /* 0x38 */ |
| 1926 | .long 0 /* 0x3c */ |
| 1927 | .long 0 /* 0x40 */ |
| 1928 | .long 0 /* 0x44 */ |
| 1929 | .long 0 /* 0x48 */ |
| 1930 | .long 0 /* 0x4c */ |
| 1931 | .long 0 /* 0x50 */ |
| 1932 | .long 0 /* 0x54 */ |
| 1933 | .long 0 /* 0x58 */ |
| 1934 | .long 0 /* 0x5c */ |
| 1935 | .long 0 /* 0x60 */ |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 1936 | #ifdef CONFIG_KVM_XICS |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1937 | .long DOTSYM(kvmppc_rm_h_eoi) - hcall_real_table |
| 1938 | .long DOTSYM(kvmppc_rm_h_cppr) - hcall_real_table |
| 1939 | .long DOTSYM(kvmppc_rm_h_ipi) - hcall_real_table |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 1940 | .long 0 /* 0x70 - H_IPOLL */ |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1941 | .long DOTSYM(kvmppc_rm_h_xirr) - hcall_real_table |
Benjamin Herrenschmidt | e7d26f2 | 2013-04-17 20:31:15 +0000 | [diff] [blame] | 1942 | #else |
| 1943 | .long 0 /* 0x64 - H_EOI */ |
| 1944 | .long 0 /* 0x68 - H_CPPR */ |
| 1945 | .long 0 /* 0x6c - H_IPI */ |
| 1946 | .long 0 /* 0x70 - H_IPOLL */ |
| 1947 | .long 0 /* 0x74 - H_XIRR */ |
| 1948 | #endif |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1949 | .long 0 /* 0x78 */ |
| 1950 | .long 0 /* 0x7c */ |
| 1951 | .long 0 /* 0x80 */ |
| 1952 | .long 0 /* 0x84 */ |
| 1953 | .long 0 /* 0x88 */ |
| 1954 | .long 0 /* 0x8c */ |
| 1955 | .long 0 /* 0x90 */ |
| 1956 | .long 0 /* 0x94 */ |
| 1957 | .long 0 /* 0x98 */ |
| 1958 | .long 0 /* 0x9c */ |
| 1959 | .long 0 /* 0xa0 */ |
| 1960 | .long 0 /* 0xa4 */ |
| 1961 | .long 0 /* 0xa8 */ |
| 1962 | .long 0 /* 0xac */ |
| 1963 | .long 0 /* 0xb0 */ |
| 1964 | .long 0 /* 0xb4 */ |
| 1965 | .long 0 /* 0xb8 */ |
| 1966 | .long 0 /* 0xbc */ |
| 1967 | .long 0 /* 0xc0 */ |
| 1968 | .long 0 /* 0xc4 */ |
| 1969 | .long 0 /* 0xc8 */ |
| 1970 | .long 0 /* 0xcc */ |
| 1971 | .long 0 /* 0xd0 */ |
| 1972 | .long 0 /* 0xd4 */ |
| 1973 | .long 0 /* 0xd8 */ |
| 1974 | .long 0 /* 0xdc */ |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1975 | .long DOTSYM(kvmppc_h_cede) - hcall_real_table |
Sam Bobroff | 90fd09f | 2014-12-03 13:30:40 +1100 | [diff] [blame] | 1976 | .long DOTSYM(kvmppc_rm_h_confer) - hcall_real_table |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 1977 | .long 0 /* 0xe8 */ |
| 1978 | .long 0 /* 0xec */ |
| 1979 | .long 0 /* 0xf0 */ |
| 1980 | .long 0 /* 0xf4 */ |
| 1981 | .long 0 /* 0xf8 */ |
| 1982 | .long 0 /* 0xfc */ |
| 1983 | .long 0 /* 0x100 */ |
| 1984 | .long 0 /* 0x104 */ |
| 1985 | .long 0 /* 0x108 */ |
| 1986 | .long 0 /* 0x10c */ |
| 1987 | .long 0 /* 0x110 */ |
| 1988 | .long 0 /* 0x114 */ |
| 1989 | .long 0 /* 0x118 */ |
| 1990 | .long 0 /* 0x11c */ |
| 1991 | .long 0 /* 0x120 */ |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1992 | .long DOTSYM(kvmppc_h_bulk_remove) - hcall_real_table |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 1993 | .long 0 /* 0x128 */ |
| 1994 | .long 0 /* 0x12c */ |
| 1995 | .long 0 /* 0x130 */ |
Anton Blanchard | c1fb019 | 2014-02-04 16:07:01 +1100 | [diff] [blame] | 1996 | .long DOTSYM(kvmppc_h_set_xdabr) - hcall_real_table |
Michael Ellerman | e928e9c | 2015-03-20 20:39:41 +1100 | [diff] [blame] | 1997 | .long 0 /* 0x138 */ |
| 1998 | .long 0 /* 0x13c */ |
| 1999 | .long 0 /* 0x140 */ |
| 2000 | .long 0 /* 0x144 */ |
| 2001 | .long 0 /* 0x148 */ |
| 2002 | .long 0 /* 0x14c */ |
| 2003 | .long 0 /* 0x150 */ |
| 2004 | .long 0 /* 0x154 */ |
| 2005 | .long 0 /* 0x158 */ |
| 2006 | .long 0 /* 0x15c */ |
| 2007 | .long 0 /* 0x160 */ |
| 2008 | .long 0 /* 0x164 */ |
| 2009 | .long 0 /* 0x168 */ |
| 2010 | .long 0 /* 0x16c */ |
| 2011 | .long 0 /* 0x170 */ |
| 2012 | .long 0 /* 0x174 */ |
| 2013 | .long 0 /* 0x178 */ |
| 2014 | .long 0 /* 0x17c */ |
| 2015 | .long 0 /* 0x180 */ |
| 2016 | .long 0 /* 0x184 */ |
| 2017 | .long 0 /* 0x188 */ |
| 2018 | .long 0 /* 0x18c */ |
| 2019 | .long 0 /* 0x190 */ |
| 2020 | .long 0 /* 0x194 */ |
| 2021 | .long 0 /* 0x198 */ |
| 2022 | .long 0 /* 0x19c */ |
| 2023 | .long 0 /* 0x1a0 */ |
| 2024 | .long 0 /* 0x1a4 */ |
| 2025 | .long 0 /* 0x1a8 */ |
| 2026 | .long 0 /* 0x1ac */ |
| 2027 | .long 0 /* 0x1b0 */ |
| 2028 | .long 0 /* 0x1b4 */ |
| 2029 | .long 0 /* 0x1b8 */ |
| 2030 | .long 0 /* 0x1bc */ |
| 2031 | .long 0 /* 0x1c0 */ |
| 2032 | .long 0 /* 0x1c4 */ |
| 2033 | .long 0 /* 0x1c8 */ |
| 2034 | .long 0 /* 0x1cc */ |
| 2035 | .long 0 /* 0x1d0 */ |
| 2036 | .long 0 /* 0x1d4 */ |
| 2037 | .long 0 /* 0x1d8 */ |
| 2038 | .long 0 /* 0x1dc */ |
| 2039 | .long 0 /* 0x1e0 */ |
| 2040 | .long 0 /* 0x1e4 */ |
| 2041 | .long 0 /* 0x1e8 */ |
| 2042 | .long 0 /* 0x1ec */ |
| 2043 | .long 0 /* 0x1f0 */ |
| 2044 | .long 0 /* 0x1f4 */ |
| 2045 | .long 0 /* 0x1f8 */ |
| 2046 | .long 0 /* 0x1fc */ |
| 2047 | .long 0 /* 0x200 */ |
| 2048 | .long 0 /* 0x204 */ |
| 2049 | .long 0 /* 0x208 */ |
| 2050 | .long 0 /* 0x20c */ |
| 2051 | .long 0 /* 0x210 */ |
| 2052 | .long 0 /* 0x214 */ |
| 2053 | .long 0 /* 0x218 */ |
| 2054 | .long 0 /* 0x21c */ |
| 2055 | .long 0 /* 0x220 */ |
| 2056 | .long 0 /* 0x224 */ |
| 2057 | .long 0 /* 0x228 */ |
| 2058 | .long 0 /* 0x22c */ |
| 2059 | .long 0 /* 0x230 */ |
| 2060 | .long 0 /* 0x234 */ |
| 2061 | .long 0 /* 0x238 */ |
| 2062 | .long 0 /* 0x23c */ |
| 2063 | .long 0 /* 0x240 */ |
| 2064 | .long 0 /* 0x244 */ |
| 2065 | .long 0 /* 0x248 */ |
| 2066 | .long 0 /* 0x24c */ |
| 2067 | .long 0 /* 0x250 */ |
| 2068 | .long 0 /* 0x254 */ |
| 2069 | .long 0 /* 0x258 */ |
| 2070 | .long 0 /* 0x25c */ |
| 2071 | .long 0 /* 0x260 */ |
| 2072 | .long 0 /* 0x264 */ |
| 2073 | .long 0 /* 0x268 */ |
| 2074 | .long 0 /* 0x26c */ |
| 2075 | .long 0 /* 0x270 */ |
| 2076 | .long 0 /* 0x274 */ |
| 2077 | .long 0 /* 0x278 */ |
| 2078 | .long 0 /* 0x27c */ |
| 2079 | .long 0 /* 0x280 */ |
| 2080 | .long 0 /* 0x284 */ |
| 2081 | .long 0 /* 0x288 */ |
| 2082 | .long 0 /* 0x28c */ |
| 2083 | .long 0 /* 0x290 */ |
| 2084 | .long 0 /* 0x294 */ |
| 2085 | .long 0 /* 0x298 */ |
| 2086 | .long 0 /* 0x29c */ |
| 2087 | .long 0 /* 0x2a0 */ |
| 2088 | .long 0 /* 0x2a4 */ |
| 2089 | .long 0 /* 0x2a8 */ |
| 2090 | .long 0 /* 0x2ac */ |
| 2091 | .long 0 /* 0x2b0 */ |
| 2092 | .long 0 /* 0x2b4 */ |
| 2093 | .long 0 /* 0x2b8 */ |
| 2094 | .long 0 /* 0x2bc */ |
| 2095 | .long 0 /* 0x2c0 */ |
| 2096 | .long 0 /* 0x2c4 */ |
| 2097 | .long 0 /* 0x2c8 */ |
| 2098 | .long 0 /* 0x2cc */ |
| 2099 | .long 0 /* 0x2d0 */ |
| 2100 | .long 0 /* 0x2d4 */ |
| 2101 | .long 0 /* 0x2d8 */ |
| 2102 | .long 0 /* 0x2dc */ |
| 2103 | .long 0 /* 0x2e0 */ |
| 2104 | .long 0 /* 0x2e4 */ |
| 2105 | .long 0 /* 0x2e8 */ |
| 2106 | .long 0 /* 0x2ec */ |
| 2107 | .long 0 /* 0x2f0 */ |
| 2108 | .long 0 /* 0x2f4 */ |
| 2109 | .long 0 /* 0x2f8 */ |
| 2110 | .long 0 /* 0x2fc */ |
| 2111 | .long DOTSYM(kvmppc_h_random) - hcall_real_table |
Paul Mackerras | ae2113a | 2014-06-02 11:03:00 +1000 | [diff] [blame] | 2112 | .globl hcall_real_table_end |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2113 | hcall_real_table_end: |
| 2114 | |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 2115 | _GLOBAL(kvmppc_h_set_xdabr) |
| 2116 | andi. r0, r5, DABRX_USER | DABRX_KERNEL |
| 2117 | beq 6f |
| 2118 | li r0, DABRX_USER | DABRX_KERNEL | DABRX_BTI |
| 2119 | andc. r0, r5, r0 |
| 2120 | beq 3f |
| 2121 | 6: li r3, H_PARAMETER |
| 2122 | blr |
| 2123 | |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2124 | _GLOBAL(kvmppc_h_set_dabr) |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 2125 | li r5, DABRX_USER | DABRX_KERNEL |
| 2126 | 3: |
Michael Neuling | eee7ff9 | 2014-01-08 21:25:19 +1100 | [diff] [blame] | 2127 | BEGIN_FTR_SECTION |
| 2128 | b 2f |
| 2129 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2130 | std r4,VCPU_DABR(r3) |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 2131 | stw r5, VCPU_DABRX(r3) |
| 2132 | mtspr SPRN_DABRX, r5 |
Paul Mackerras | 8943633 | 2012-03-02 01:38:23 +0000 | [diff] [blame] | 2133 | /* Work around P7 bug where DABR can get corrupted on mtspr */ |
| 2134 | 1: mtspr SPRN_DABR,r4 |
| 2135 | mfspr r5, SPRN_DABR |
| 2136 | cmpd r4, r5 |
| 2137 | bne 1b |
| 2138 | isync |
Paul Mackerras | a8606e2 | 2011-06-29 00:22:05 +0000 | [diff] [blame] | 2139 | li r3,0 |
| 2140 | blr |
| 2141 | |
Paul Mackerras | 8563bf5 | 2014-01-08 21:25:29 +1100 | [diff] [blame] | 2142 | /* Emulate H_SET_DABR/X on P8 for the sake of compat mode guests */ |
| 2143 | 2: rlwimi r5, r4, 5, DAWRX_DR | DAWRX_DW |
| 2144 | rlwimi r5, r4, 1, DAWRX_WT |
| 2145 | clrrdi r4, r4, 3 |
| 2146 | std r4, VCPU_DAWR(r3) |
| 2147 | std r5, VCPU_DAWRX(r3) |
| 2148 | mtspr SPRN_DAWR, r4 |
| 2149 | mtspr SPRN_DAWRX, r5 |
| 2150 | li r3, 0 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2151 | blr |
| 2152 | |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 2153 | _GLOBAL(kvmppc_h_cede) /* r3 = vcpu pointer, r11 = msr, r13 = paca */ |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2154 | ori r11,r11,MSR_EE |
| 2155 | std r11,VCPU_MSR(r3) |
| 2156 | li r0,1 |
| 2157 | stb r0,VCPU_CEDED(r3) |
| 2158 | sync /* order setting ceded vs. testing prodded */ |
| 2159 | lbz r5,VCPU_PRODDED(r3) |
| 2160 | cmpwi r5,0 |
Paul Mackerras | 04f995a | 2012-08-06 00:03:28 +0000 | [diff] [blame] | 2161 | bne kvm_cede_prodded |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 2162 | li r12,0 /* set trap to 0 to say hcall is handled */ |
| 2163 | stw r12,VCPU_TRAP(r3) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2164 | li r0,H_SUCCESS |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 2165 | std r0,VCPU_GPR(R3)(r3) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2166 | |
| 2167 | /* |
| 2168 | * Set our bit in the bitmask of napping threads unless all the |
| 2169 | * other threads are already napping, in which case we send this |
| 2170 | * up to the host. |
| 2171 | */ |
| 2172 | ld r5,HSTATE_KVM_VCORE(r13) |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 2173 | lbz r6,HSTATE_PTID(r13) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2174 | lwz r8,VCORE_ENTRY_EXIT(r5) |
| 2175 | clrldi r8,r8,56 |
| 2176 | li r0,1 |
| 2177 | sld r0,r0,r6 |
| 2178 | addi r6,r5,VCORE_NAPPING_THREADS |
| 2179 | 31: lwarx r4,0,r6 |
| 2180 | or r4,r4,r0 |
Paul Mackerras | 7d6c40d | 2015-03-28 14:21:09 +1100 | [diff] [blame] | 2181 | cmpw r4,r8 |
| 2182 | beq kvm_cede_exit |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2183 | stwcx. r4,0,r6 |
| 2184 | bne 31b |
Paul Mackerras | 7d6c40d | 2015-03-28 14:21:09 +1100 | [diff] [blame] | 2185 | /* order napping_threads update vs testing entry_exit_map */ |
Paul Mackerras | f019b7a | 2013-11-16 17:46:03 +1100 | [diff] [blame] | 2186 | isync |
Paul Mackerras | e0b7ec0 | 2014-01-08 21:25:20 +1100 | [diff] [blame] | 2187 | li r0,NAPPING_CEDE |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2188 | stb r0,HSTATE_NAPPING(r13) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2189 | lwz r7,VCORE_ENTRY_EXIT(r5) |
| 2190 | cmpwi r7,0x100 |
| 2191 | bge 33f /* another thread already exiting */ |
| 2192 | |
| 2193 | /* |
| 2194 | * Although not specifically required by the architecture, POWER7 |
| 2195 | * preserves the following registers in nap mode, even if an SMT mode |
| 2196 | * switch occurs: SLB entries, PURR, SPURR, AMOR, UAMOR, AMR, SPRG0-3, |
| 2197 | * DAR, DSISR, DABR, DABRX, DSCR, PMCx, MMCRx, SIAR, SDAR. |
| 2198 | */ |
| 2199 | /* Save non-volatile GPRs */ |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 2200 | std r14, VCPU_GPR(R14)(r3) |
| 2201 | std r15, VCPU_GPR(R15)(r3) |
| 2202 | std r16, VCPU_GPR(R16)(r3) |
| 2203 | std r17, VCPU_GPR(R17)(r3) |
| 2204 | std r18, VCPU_GPR(R18)(r3) |
| 2205 | std r19, VCPU_GPR(R19)(r3) |
| 2206 | std r20, VCPU_GPR(R20)(r3) |
| 2207 | std r21, VCPU_GPR(R21)(r3) |
| 2208 | std r22, VCPU_GPR(R22)(r3) |
| 2209 | std r23, VCPU_GPR(R23)(r3) |
| 2210 | std r24, VCPU_GPR(R24)(r3) |
| 2211 | std r25, VCPU_GPR(R25)(r3) |
| 2212 | std r26, VCPU_GPR(R26)(r3) |
| 2213 | std r27, VCPU_GPR(R27)(r3) |
| 2214 | std r28, VCPU_GPR(R28)(r3) |
| 2215 | std r29, VCPU_GPR(R29)(r3) |
| 2216 | std r30, VCPU_GPR(R30)(r3) |
| 2217 | std r31, VCPU_GPR(R31)(r3) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2218 | |
| 2219 | /* save FP state */ |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2220 | bl kvmppc_save_fp |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2221 | |
Paul Mackerras | fd6d53b | 2015-03-28 14:21:08 +1100 | [diff] [blame] | 2222 | /* |
| 2223 | * Set DEC to the smaller of DEC and HDEC, so that we wake |
| 2224 | * no later than the end of our timeslice (HDEC interrupts |
| 2225 | * don't wake us from nap). |
| 2226 | */ |
| 2227 | mfspr r3, SPRN_DEC |
| 2228 | mfspr r4, SPRN_HDEC |
| 2229 | mftb r5 |
| 2230 | cmpw r3, r4 |
| 2231 | ble 67f |
| 2232 | mtspr SPRN_DEC, r4 |
| 2233 | 67: |
| 2234 | /* save expiry time of guest decrementer */ |
| 2235 | extsw r3, r3 |
| 2236 | add r3, r3, r5 |
| 2237 | ld r4, HSTATE_KVM_VCPU(r13) |
| 2238 | ld r5, HSTATE_KVM_VCORE(r13) |
| 2239 | ld r6, VCORE_TB_OFFSET(r5) |
| 2240 | subf r3, r6, r3 /* convert to host TB value */ |
| 2241 | std r3, VCPU_DEC_EXPIRES(r4) |
| 2242 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2243 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 2244 | ld r4, HSTATE_KVM_VCPU(r13) |
| 2245 | addi r3, r4, VCPU_TB_CEDE |
| 2246 | bl kvmhv_accumulate_time |
| 2247 | #endif |
| 2248 | |
Paul Mackerras | ccc0777 | 2015-03-28 14:21:07 +1100 | [diff] [blame] | 2249 | lis r3, LPCR_PECEDP@h /* Do wake on privileged doorbell */ |
| 2250 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2251 | /* |
Paul Mackerras | aa31e84 | 2014-01-08 21:25:26 +1100 | [diff] [blame] | 2252 | * Take a nap until a decrementer or external or doobell interrupt |
Paul Mackerras | ccc0777 | 2015-03-28 14:21:07 +1100 | [diff] [blame] | 2253 | * occurs, with PECE1 and PECE0 set in LPCR. |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 2254 | * On POWER8, set PECEDH, and if we are ceding, also set PECEDP. |
Paul Mackerras | ccc0777 | 2015-03-28 14:21:07 +1100 | [diff] [blame] | 2255 | * Also clear the runlatch bit before napping. |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2256 | */ |
Paul Mackerras | 56548fc | 2014-12-03 14:48:40 +1100 | [diff] [blame] | 2257 | kvm_do_nap: |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 2258 | mfspr r0, SPRN_CTRLF |
| 2259 | clrrdi r0, r0, 1 |
| 2260 | mtspr SPRN_CTRLT, r0 |
Preeti U Murthy | 582b910 | 2014-04-11 16:02:08 +0530 | [diff] [blame] | 2261 | |
Paul Mackerras | f0888f7 | 2012-02-03 00:54:17 +0000 | [diff] [blame] | 2262 | li r0,1 |
| 2263 | stb r0,HSTATE_HWTHREAD_REQ(r13) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2264 | mfspr r5,SPRN_LPCR |
| 2265 | ori r5,r5,LPCR_PECE0 | LPCR_PECE1 |
Paul Mackerras | aa31e84 | 2014-01-08 21:25:26 +1100 | [diff] [blame] | 2266 | BEGIN_FTR_SECTION |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 2267 | ori r5, r5, LPCR_PECEDH |
Paul Mackerras | ccc0777 | 2015-03-28 14:21:07 +1100 | [diff] [blame] | 2268 | rlwimi r5, r3, 0, LPCR_PECEDP |
Paul Mackerras | aa31e84 | 2014-01-08 21:25:26 +1100 | [diff] [blame] | 2269 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2270 | mtspr SPRN_LPCR,r5 |
| 2271 | isync |
| 2272 | li r0, 0 |
| 2273 | std r0, HSTATE_SCRATCH0(r13) |
| 2274 | ptesync |
| 2275 | ld r0, HSTATE_SCRATCH0(r13) |
| 2276 | 1: cmpd r0, r0 |
| 2277 | bne 1b |
| 2278 | nap |
| 2279 | b . |
| 2280 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2281 | 33: mr r4, r3 |
| 2282 | li r3, 0 |
| 2283 | li r12, 0 |
| 2284 | b 34f |
| 2285 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2286 | kvm_end_cede: |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 2287 | /* get vcpu pointer */ |
| 2288 | ld r4, HSTATE_KVM_VCPU(r13) |
| 2289 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2290 | /* Woken by external or decrementer interrupt */ |
| 2291 | ld r1, HSTATE_HOST_R1(r13) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2292 | |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2293 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 2294 | addi r3, r4, VCPU_TB_RMINTR |
| 2295 | bl kvmhv_accumulate_time |
| 2296 | #endif |
| 2297 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2298 | /* load up FP state */ |
| 2299 | bl kvmppc_load_fp |
| 2300 | |
Paul Mackerras | fd6d53b | 2015-03-28 14:21:08 +1100 | [diff] [blame] | 2301 | /* Restore guest decrementer */ |
| 2302 | ld r3, VCPU_DEC_EXPIRES(r4) |
| 2303 | ld r5, HSTATE_KVM_VCORE(r13) |
| 2304 | ld r6, VCORE_TB_OFFSET(r5) |
| 2305 | add r3, r3, r6 /* convert host TB to guest TB value */ |
| 2306 | mftb r7 |
| 2307 | subf r3, r7, r3 |
| 2308 | mtspr SPRN_DEC, r3 |
| 2309 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2310 | /* Load NV GPRS */ |
Michael Neuling | c75df6f | 2012-06-25 13:33:10 +0000 | [diff] [blame] | 2311 | ld r14, VCPU_GPR(R14)(r4) |
| 2312 | ld r15, VCPU_GPR(R15)(r4) |
| 2313 | ld r16, VCPU_GPR(R16)(r4) |
| 2314 | ld r17, VCPU_GPR(R17)(r4) |
| 2315 | ld r18, VCPU_GPR(R18)(r4) |
| 2316 | ld r19, VCPU_GPR(R19)(r4) |
| 2317 | ld r20, VCPU_GPR(R20)(r4) |
| 2318 | ld r21, VCPU_GPR(R21)(r4) |
| 2319 | ld r22, VCPU_GPR(R22)(r4) |
| 2320 | ld r23, VCPU_GPR(R23)(r4) |
| 2321 | ld r24, VCPU_GPR(R24)(r4) |
| 2322 | ld r25, VCPU_GPR(R25)(r4) |
| 2323 | ld r26, VCPU_GPR(R26)(r4) |
| 2324 | ld r27, VCPU_GPR(R27)(r4) |
| 2325 | ld r28, VCPU_GPR(R28)(r4) |
| 2326 | ld r29, VCPU_GPR(R29)(r4) |
| 2327 | ld r30, VCPU_GPR(R30)(r4) |
| 2328 | ld r31, VCPU_GPR(R31)(r4) |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2329 | |
| 2330 | /* Check the wake reason in SRR1 to see why we got here */ |
| 2331 | bl kvmppc_check_wake_reason |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2332 | |
| 2333 | /* clear our bit in vcore->napping_threads */ |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2334 | 34: ld r5,HSTATE_KVM_VCORE(r13) |
| 2335 | lbz r7,HSTATE_PTID(r13) |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2336 | li r0,1 |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2337 | sld r0,r0,r7 |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2338 | addi r6,r5,VCORE_NAPPING_THREADS |
| 2339 | 32: lwarx r7,0,r6 |
| 2340 | andc r7,r7,r0 |
| 2341 | stwcx. r7,0,r6 |
| 2342 | bne 32b |
| 2343 | li r0,0 |
| 2344 | stb r0,HSTATE_NAPPING(r13) |
| 2345 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2346 | /* See if the wake reason means we need to exit */ |
| 2347 | stw r12, VCPU_TRAP(r4) |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 2348 | mr r9, r4 |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2349 | cmpdi r3, 0 |
| 2350 | bgt guest_exit_cont |
Paul Mackerras | 4619ac8 | 2013-04-17 20:31:41 +0000 | [diff] [blame] | 2351 | |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2352 | /* see if any other thread is already exiting */ |
| 2353 | lwz r0,VCORE_ENTRY_EXIT(r5) |
| 2354 | cmpwi r0,0x100 |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2355 | bge guest_exit_cont |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2356 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2357 | b kvmppc_cede_reentry /* if not go back to guest */ |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2358 | |
| 2359 | /* cede when already previously prodded case */ |
Paul Mackerras | 04f995a | 2012-08-06 00:03:28 +0000 | [diff] [blame] | 2360 | kvm_cede_prodded: |
| 2361 | li r0,0 |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2362 | stb r0,VCPU_PRODDED(r3) |
| 2363 | sync /* order testing prodded vs. clearing ceded */ |
| 2364 | stb r0,VCPU_CEDED(r3) |
| 2365 | li r3,H_SUCCESS |
| 2366 | blr |
| 2367 | |
| 2368 | /* we've ceded but we want to give control to the host */ |
Paul Mackerras | 04f995a | 2012-08-06 00:03:28 +0000 | [diff] [blame] | 2369 | kvm_cede_exit: |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 2370 | ld r9, HSTATE_KVM_VCPU(r13) |
| 2371 | b guest_exit_cont |
Paul Mackerras | 19ccb76 | 2011-07-23 17:42:46 +1000 | [diff] [blame] | 2372 | |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 2373 | /* Try to handle a machine check in real mode */ |
| 2374 | machine_check_realmode: |
| 2375 | mr r3, r9 /* get vcpu pointer */ |
Anton Blanchard | b1576fe | 2014-02-04 16:04:35 +1100 | [diff] [blame] | 2376 | bl kvmppc_realmode_machine_check |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 2377 | nop |
Mahesh Salgaonkar | 74845bc | 2014-06-11 14:18:21 +0530 | [diff] [blame] | 2378 | cmpdi r3, 0 /* Did we handle MCE ? */ |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 2379 | ld r9, HSTATE_KVM_VCPU(r13) |
| 2380 | li r12, BOOK3S_INTERRUPT_MACHINE_CHECK |
Mahesh Salgaonkar | 74845bc | 2014-06-11 14:18:21 +0530 | [diff] [blame] | 2381 | /* |
| 2382 | * Deliver unhandled/fatal (e.g. UE) MCE errors to guest through |
| 2383 | * machine check interrupt (set HSRR0 to 0x200). And for handled |
| 2384 | * errors (no-fatal), just go back to guest execution with current |
| 2385 | * HSRR0 instead of exiting guest. This new approach will inject |
| 2386 | * machine check to guest for fatal error causing guest to crash. |
| 2387 | * |
| 2388 | * The old code used to return to host for unhandled errors which |
| 2389 | * was causing guest to hang with soft lockups inside guest and |
| 2390 | * makes it difficult to recover guest instance. |
| 2391 | */ |
| 2392 | ld r10, VCPU_PC(r9) |
| 2393 | ld r11, VCPU_MSR(r9) |
| 2394 | bne 2f /* Continue guest execution. */ |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 2395 | /* If not, deliver a machine check. SRR0/1 are already set */ |
| 2396 | li r10, BOOK3S_INTERRUPT_MACHINE_CHECK |
Paul Mackerras | 000a25d | 2014-05-26 19:48:41 +1000 | [diff] [blame] | 2397 | ld r11, VCPU_MSR(r9) |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 2398 | bl kvmppc_msr_interrupt |
Mahesh Salgaonkar | 74845bc | 2014-06-11 14:18:21 +0530 | [diff] [blame] | 2399 | 2: b fast_interrupt_c_return |
Paul Mackerras | b4072df | 2012-11-23 22:37:50 +0000 | [diff] [blame] | 2400 | |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2401 | /* |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2402 | * Check the reason we woke from nap, and take appropriate action. |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 2403 | * Returns (in r3): |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2404 | * 0 if nothing needs to be done |
| 2405 | * 1 if something happened that needs to be handled by the host |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 2406 | * -1 if there was a guest wakeup (IPI or msgsnd) |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2407 | * |
| 2408 | * Also sets r12 to the interrupt vector for any interrupt that needs |
| 2409 | * to be handled now by the host (0x500 for external interrupt), or zero. |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 2410 | * Modifies r0, r6, r7, r8. |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2411 | */ |
| 2412 | kvmppc_check_wake_reason: |
| 2413 | mfspr r6, SPRN_SRR1 |
Paul Mackerras | aa31e84 | 2014-01-08 21:25:26 +1100 | [diff] [blame] | 2414 | BEGIN_FTR_SECTION |
| 2415 | rlwinm r6, r6, 45-31, 0xf /* extract wake reason field (P8) */ |
| 2416 | FTR_SECTION_ELSE |
| 2417 | rlwinm r6, r6, 45-31, 0xe /* P7 wake reason field is 3 bits */ |
| 2418 | ALT_FTR_SECTION_END_IFSET(CPU_FTR_ARCH_207S) |
| 2419 | cmpwi r6, 8 /* was it an external interrupt? */ |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2420 | li r12, BOOK3S_INTERRUPT_EXTERNAL |
| 2421 | beq kvmppc_read_intr /* if so, see what it was */ |
| 2422 | li r3, 0 |
| 2423 | li r12, 0 |
| 2424 | cmpwi r6, 6 /* was it the decrementer? */ |
| 2425 | beq 0f |
Paul Mackerras | aa31e84 | 2014-01-08 21:25:26 +1100 | [diff] [blame] | 2426 | BEGIN_FTR_SECTION |
| 2427 | cmpwi r6, 5 /* privileged doorbell? */ |
| 2428 | beq 0f |
Paul Mackerras | 5d00f66 | 2014-01-08 21:25:28 +1100 | [diff] [blame] | 2429 | cmpwi r6, 3 /* hypervisor doorbell? */ |
| 2430 | beq 3f |
Paul Mackerras | aa31e84 | 2014-01-08 21:25:26 +1100 | [diff] [blame] | 2431 | END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S) |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2432 | li r3, 1 /* anything else, return 1 */ |
| 2433 | 0: blr |
| 2434 | |
Paul Mackerras | 5d00f66 | 2014-01-08 21:25:28 +1100 | [diff] [blame] | 2435 | /* hypervisor doorbell */ |
| 2436 | 3: li r12, BOOK3S_INTERRUPT_H_DOORBELL |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 2437 | /* see if it's a host IPI */ |
Paul Mackerras | 5d00f66 | 2014-01-08 21:25:28 +1100 | [diff] [blame] | 2438 | li r3, 1 |
Paul Mackerras | 66feed6 | 2015-03-28 14:21:12 +1100 | [diff] [blame] | 2439 | lbz r0, HSTATE_HOST_IPI(r13) |
| 2440 | cmpwi r0, 0 |
| 2441 | bnelr |
| 2442 | /* if not, clear it and return -1 */ |
| 2443 | lis r6, (PPC_DBELL_SERVER << (63-36))@h |
| 2444 | PPC_MSGCLR(6) |
| 2445 | li r3, -1 |
Paul Mackerras | 5d00f66 | 2014-01-08 21:25:28 +1100 | [diff] [blame] | 2446 | blr |
| 2447 | |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2448 | /* |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2449 | * Determine what sort of external interrupt is pending (if any). |
| 2450 | * Returns: |
| 2451 | * 0 if no interrupt is pending |
| 2452 | * 1 if an interrupt is pending that needs to be handled by the host |
| 2453 | * -1 if there was a guest wakeup IPI (which has now been cleared) |
Paul Mackerras | 1f09c3e | 2015-03-28 14:21:04 +1100 | [diff] [blame] | 2454 | * Modifies r0, r6, r7, r8, returns value in r3. |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2455 | */ |
| 2456 | kvmppc_read_intr: |
| 2457 | /* see if a host IPI is pending */ |
| 2458 | li r3, 1 |
| 2459 | lbz r0, HSTATE_HOST_IPI(r13) |
| 2460 | cmpwi r0, 0 |
| 2461 | bne 1f |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2462 | |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2463 | /* Now read the interrupt from the ICP */ |
| 2464 | ld r6, HSTATE_XICS_PHYS(r13) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2465 | li r7, XICS_XIRR |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2466 | cmpdi r6, 0 |
| 2467 | beq- 1f |
| 2468 | lwzcix r0, r6, r7 |
Alexander Graf | 76d072f | 2014-06-11 10:37:52 +0200 | [diff] [blame] | 2469 | /* |
| 2470 | * Save XIRR for later. Since we get in in reverse endian on LE |
| 2471 | * systems, save it byte reversed and fetch it back in host endian. |
| 2472 | */ |
| 2473 | li r3, HSTATE_SAVED_XIRR |
| 2474 | STWX_BE r0, r3, r13 |
| 2475 | #ifdef __LITTLE_ENDIAN__ |
| 2476 | lwz r3, HSTATE_SAVED_XIRR(r13) |
| 2477 | #else |
| 2478 | mr r3, r0 |
| 2479 | #endif |
| 2480 | rlwinm. r3, r3, 0, 0xffffff |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2481 | sync |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2482 | beq 1f /* if nothing pending in the ICP */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2483 | |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2484 | /* We found something in the ICP... |
| 2485 | * |
| 2486 | * If it's not an IPI, stash it in the PACA and return to |
| 2487 | * the host, we don't (yet) handle directing real external |
| 2488 | * interrupts directly to the guest |
| 2489 | */ |
| 2490 | cmpwi r3, XICS_IPI /* if there is, is it an IPI? */ |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2491 | bne 42f |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2492 | |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2493 | /* It's an IPI, clear the MFRR and EOI it */ |
| 2494 | li r3, 0xff |
| 2495 | li r8, XICS_MFRR |
| 2496 | stbcix r3, r6, r8 /* clear the IPI */ |
| 2497 | stwcix r0, r6, r7 /* EOI it */ |
| 2498 | sync |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2499 | |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2500 | /* We need to re-check host IPI now in case it got set in the |
| 2501 | * meantime. If it's clear, we bounce the interrupt to the |
| 2502 | * guest |
| 2503 | */ |
| 2504 | lbz r0, HSTATE_HOST_IPI(r13) |
| 2505 | cmpwi r0, 0 |
| 2506 | bne- 43f |
| 2507 | |
| 2508 | /* OK, it's an IPI for us */ |
Paul Mackerras | 6af27c8 | 2015-03-28 14:21:10 +1100 | [diff] [blame] | 2509 | li r12, 0 |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2510 | li r3, -1 |
| 2511 | 1: blr |
| 2512 | |
Alexander Graf | 76d072f | 2014-06-11 10:37:52 +0200 | [diff] [blame] | 2513 | 42: /* It's not an IPI and it's for the host. We saved a copy of XIRR in |
| 2514 | * the PACA earlier, it will be picked up by the host ICP driver |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2515 | */ |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2516 | li r3, 1 |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2517 | b 1b |
| 2518 | |
| 2519 | 43: /* We raced with the host, we need to resend that IPI, bummer */ |
| 2520 | li r0, IPI_PRIORITY |
| 2521 | stbcix r0, r6, r8 /* set the IPI */ |
| 2522 | sync |
Paul Mackerras | e3bbbbf | 2014-01-08 21:25:25 +1100 | [diff] [blame] | 2523 | li r3, 1 |
Paul Mackerras | c934243 | 2013-09-06 13:24:13 +1000 | [diff] [blame] | 2524 | b 1b |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2525 | |
| 2526 | /* |
| 2527 | * Save away FP, VMX and VSX registers. |
| 2528 | * r3 = vcpu pointer |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2529 | * N.B. r30 and r31 are volatile across this function, |
| 2530 | * thus it is not callable from C. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2531 | */ |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2532 | kvmppc_save_fp: |
| 2533 | mflr r30 |
| 2534 | mr r31,r3 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2535 | mfmsr r5 |
| 2536 | ori r8,r5,MSR_FP |
| 2537 | #ifdef CONFIG_ALTIVEC |
| 2538 | BEGIN_FTR_SECTION |
| 2539 | oris r8,r8,MSR_VEC@h |
| 2540 | END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) |
| 2541 | #endif |
| 2542 | #ifdef CONFIG_VSX |
| 2543 | BEGIN_FTR_SECTION |
| 2544 | oris r8,r8,MSR_VSX@h |
| 2545 | END_FTR_SECTION_IFSET(CPU_FTR_VSX) |
| 2546 | #endif |
| 2547 | mtmsrd r8 |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2548 | addi r3,r3,VCPU_FPRS |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 2549 | bl store_fp_state |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2550 | #ifdef CONFIG_ALTIVEC |
| 2551 | BEGIN_FTR_SECTION |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2552 | addi r3,r31,VCPU_VRS |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 2553 | bl store_vr_state |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2554 | END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) |
| 2555 | #endif |
| 2556 | mfspr r6,SPRN_VRSAVE |
Paul Mackerras | e724f08 | 2014-03-13 20:02:48 +1100 | [diff] [blame] | 2557 | stw r6,VCPU_VRSAVE(r31) |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2558 | mtlr r30 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2559 | blr |
| 2560 | |
| 2561 | /* |
| 2562 | * Load up FP, VMX and VSX registers |
| 2563 | * r4 = vcpu pointer |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2564 | * N.B. r30 and r31 are volatile across this function, |
| 2565 | * thus it is not callable from C. |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2566 | */ |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2567 | kvmppc_load_fp: |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2568 | mflr r30 |
| 2569 | mr r31,r4 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2570 | mfmsr r9 |
| 2571 | ori r8,r9,MSR_FP |
| 2572 | #ifdef CONFIG_ALTIVEC |
| 2573 | BEGIN_FTR_SECTION |
| 2574 | oris r8,r8,MSR_VEC@h |
| 2575 | END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) |
| 2576 | #endif |
| 2577 | #ifdef CONFIG_VSX |
| 2578 | BEGIN_FTR_SECTION |
| 2579 | oris r8,r8,MSR_VSX@h |
| 2580 | END_FTR_SECTION_IFSET(CPU_FTR_VSX) |
| 2581 | #endif |
| 2582 | mtmsrd r8 |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2583 | addi r3,r4,VCPU_FPRS |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 2584 | bl load_fp_state |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2585 | #ifdef CONFIG_ALTIVEC |
| 2586 | BEGIN_FTR_SECTION |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2587 | addi r3,r31,VCPU_VRS |
Alexander Graf | 9bf163f | 2014-06-16 14:41:15 +0200 | [diff] [blame] | 2588 | bl load_vr_state |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2589 | END_FTR_SECTION_IFSET(CPU_FTR_ALTIVEC) |
| 2590 | #endif |
Paul Mackerras | e724f08 | 2014-03-13 20:02:48 +1100 | [diff] [blame] | 2591 | lwz r7,VCPU_VRSAVE(r31) |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2592 | mtspr SPRN_VRSAVE,r7 |
Paul Mackerras | 595e4f7 | 2013-10-15 20:43:04 +1100 | [diff] [blame] | 2593 | mtlr r30 |
| 2594 | mr r4,r31 |
Paul Mackerras | de56a94 | 2011-06-29 00:21:34 +0000 | [diff] [blame] | 2595 | blr |
Paul Mackerras | 44a3add | 2013-10-04 21:45:04 +1000 | [diff] [blame] | 2596 | |
| 2597 | /* |
| 2598 | * We come here if we get any exception or interrupt while we are |
| 2599 | * executing host real mode code while in guest MMU context. |
| 2600 | * For now just spin, but we should do something better. |
| 2601 | */ |
| 2602 | kvmppc_bad_host_intr: |
| 2603 | b . |
Michael Neuling | e4e3812 | 2014-03-25 10:47:02 +1100 | [diff] [blame] | 2604 | |
| 2605 | /* |
| 2606 | * This mimics the MSR transition on IRQ delivery. The new guest MSR is taken |
| 2607 | * from VCPU_INTR_MSR and is modified based on the required TM state changes. |
| 2608 | * r11 has the guest MSR value (in/out) |
| 2609 | * r9 has a vcpu pointer (in) |
| 2610 | * r0 is used as a scratch register |
| 2611 | */ |
| 2612 | kvmppc_msr_interrupt: |
| 2613 | rldicl r0, r11, 64 - MSR_TS_S_LG, 62 |
| 2614 | cmpwi r0, 2 /* Check if we are in transactional state.. */ |
| 2615 | ld r11, VCPU_INTR_MSR(r9) |
| 2616 | bne 1f |
| 2617 | /* ... if transactional, change to suspended */ |
| 2618 | li r0, 1 |
| 2619 | 1: rldimi r11, r0, MSR_TS_S_LG, 63 - MSR_TS_T_LG |
| 2620 | blr |
Paul Mackerras | 9bc01a9 | 2014-05-26 19:48:40 +1000 | [diff] [blame] | 2621 | |
| 2622 | /* |
| 2623 | * This works around a hardware bug on POWER8E processors, where |
| 2624 | * writing a 1 to the MMCR0[PMAO] bit doesn't generate a |
| 2625 | * performance monitor interrupt. Instead, when we need to have |
| 2626 | * an interrupt pending, we have to arrange for a counter to overflow. |
| 2627 | */ |
| 2628 | kvmppc_fix_pmao: |
| 2629 | li r3, 0 |
| 2630 | mtspr SPRN_MMCR2, r3 |
| 2631 | lis r3, (MMCR0_PMXE | MMCR0_FCECE)@h |
| 2632 | ori r3, r3, MMCR0_PMCjCE | MMCR0_C56RUN |
| 2633 | mtspr SPRN_MMCR0, r3 |
| 2634 | lis r3, 0x7fff |
| 2635 | ori r3, r3, 0xffff |
| 2636 | mtspr SPRN_PMC6, r3 |
| 2637 | isync |
| 2638 | blr |
Paul Mackerras | b6c295d | 2015-03-28 14:21:02 +1100 | [diff] [blame] | 2639 | |
| 2640 | #ifdef CONFIG_KVM_BOOK3S_HV_EXIT_TIMING |
| 2641 | /* |
| 2642 | * Start timing an activity |
| 2643 | * r3 = pointer to time accumulation struct, r4 = vcpu |
| 2644 | */ |
| 2645 | kvmhv_start_timing: |
| 2646 | ld r5, HSTATE_KVM_VCORE(r13) |
| 2647 | lbz r6, VCORE_IN_GUEST(r5) |
| 2648 | cmpwi r6, 0 |
| 2649 | beq 5f /* if in guest, need to */ |
| 2650 | ld r6, VCORE_TB_OFFSET(r5) /* subtract timebase offset */ |
| 2651 | 5: mftb r5 |
| 2652 | subf r5, r6, r5 |
| 2653 | std r3, VCPU_CUR_ACTIVITY(r4) |
| 2654 | std r5, VCPU_ACTIVITY_START(r4) |
| 2655 | blr |
| 2656 | |
| 2657 | /* |
| 2658 | * Accumulate time to one activity and start another. |
| 2659 | * r3 = pointer to new time accumulation struct, r4 = vcpu |
| 2660 | */ |
| 2661 | kvmhv_accumulate_time: |
| 2662 | ld r5, HSTATE_KVM_VCORE(r13) |
| 2663 | lbz r8, VCORE_IN_GUEST(r5) |
| 2664 | cmpwi r8, 0 |
| 2665 | beq 4f /* if in guest, need to */ |
| 2666 | ld r8, VCORE_TB_OFFSET(r5) /* subtract timebase offset */ |
| 2667 | 4: ld r5, VCPU_CUR_ACTIVITY(r4) |
| 2668 | ld r6, VCPU_ACTIVITY_START(r4) |
| 2669 | std r3, VCPU_CUR_ACTIVITY(r4) |
| 2670 | mftb r7 |
| 2671 | subf r7, r8, r7 |
| 2672 | std r7, VCPU_ACTIVITY_START(r4) |
| 2673 | cmpdi r5, 0 |
| 2674 | beqlr |
| 2675 | subf r3, r6, r7 |
| 2676 | ld r8, TAS_SEQCOUNT(r5) |
| 2677 | cmpdi r8, 0 |
| 2678 | addi r8, r8, 1 |
| 2679 | std r8, TAS_SEQCOUNT(r5) |
| 2680 | lwsync |
| 2681 | ld r7, TAS_TOTAL(r5) |
| 2682 | add r7, r7, r3 |
| 2683 | std r7, TAS_TOTAL(r5) |
| 2684 | ld r6, TAS_MIN(r5) |
| 2685 | ld r7, TAS_MAX(r5) |
| 2686 | beq 3f |
| 2687 | cmpd r3, r6 |
| 2688 | bge 1f |
| 2689 | 3: std r3, TAS_MIN(r5) |
| 2690 | 1: cmpd r3, r7 |
| 2691 | ble 2f |
| 2692 | std r3, TAS_MAX(r5) |
| 2693 | 2: lwsync |
| 2694 | addi r8, r8, 1 |
| 2695 | std r8, TAS_SEQCOUNT(r5) |
| 2696 | blr |
| 2697 | #endif |