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