Alexander Graf | c862125 | 2009-10-30 05:47:09 +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 | * You should have received a copy of the GNU General Public License |
| 12 | * along with this program; if not, write to the Free Software |
| 13 | * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 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> |
| 23 | #include <asm/page.h> |
| 24 | #include <asm/asm-offsets.h> |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 25 | |
| 26 | #ifdef CONFIG_PPC_BOOK3S_64 |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 27 | #include <asm/exception-64s.h> |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 28 | #endif |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 29 | |
| 30 | /***************************************************************************** |
| 31 | * * |
| 32 | * Real Mode handlers that need to be in low physical memory * |
| 33 | * * |
| 34 | ****************************************************************************/ |
| 35 | |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 36 | #if defined(CONFIG_PPC_BOOK3S_64) |
| 37 | |
Benjamin Herrenschmidt | 2dd60d7 | 2011-01-20 17:50:21 +1100 | [diff] [blame] | 38 | #define LOAD_SHADOW_VCPU(reg) GET_PACA(reg) |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 39 | #define SHADOW_VCPU_OFF PACA_KVM_SVCPU |
| 40 | #define MSR_NOIRQ MSR_KERNEL & ~(MSR_IR | MSR_DR) |
| 41 | #define FUNC(name) GLUE(.,name) |
| 42 | |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 43 | kvmppc_skip_interrupt: |
| 44 | /* |
| 45 | * Here all GPRs are unchanged from when the interrupt happened |
| 46 | * except for r13, which is saved in SPRG_SCRATCH0. |
| 47 | */ |
| 48 | mfspr r13, SPRN_SRR0 |
| 49 | addi r13, r13, 4 |
| 50 | mtspr SPRN_SRR0, r13 |
| 51 | GET_SCRATCH0(r13) |
| 52 | rfid |
| 53 | b . |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 54 | |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 55 | kvmppc_skip_Hinterrupt: |
| 56 | /* |
| 57 | * Here all GPRs are unchanged from when the interrupt happened |
| 58 | * except for r13, which is saved in SPRG_SCRATCH0. |
| 59 | */ |
| 60 | mfspr r13, SPRN_HSRR0 |
| 61 | addi r13, r13, 4 |
| 62 | mtspr SPRN_HSRR0, r13 |
| 63 | GET_SCRATCH0(r13) |
| 64 | hrfid |
| 65 | b . |
| 66 | |
| 67 | #elif defined(CONFIG_PPC_BOOK3S_32) |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 68 | |
| 69 | #define SHADOW_VCPU_OFF 0 |
| 70 | #define MSR_NOIRQ MSR_KERNEL |
| 71 | #define FUNC(name) name |
| 72 | |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 73 | .macro INTERRUPT_TRAMPOLINE intno |
| 74 | |
| 75 | .global kvmppc_trampoline_\intno |
| 76 | kvmppc_trampoline_\intno: |
| 77 | |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 78 | mtspr SPRN_SPRG_SCRATCH0, r13 /* Save r13 */ |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * First thing to do is to find out if we're coming |
| 82 | * from a KVM guest or a Linux process. |
| 83 | * |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 84 | * To distinguish, we check a magic byte in the PACA/current |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 85 | */ |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 86 | mfspr r13, SPRN_SPRG_THREAD |
| 87 | lwz r13, THREAD_KVM_SVCPU(r13) |
| 88 | /* PPC32 can have a NULL pointer - let's check for that */ |
| 89 | mtspr SPRN_SPRG_SCRATCH1, r12 /* Save r12 */ |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 90 | mfcr r12 |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 91 | cmpwi r13, 0 |
| 92 | bne 1f |
| 93 | 2: mtcr r12 |
| 94 | mfspr r12, SPRN_SPRG_SCRATCH1 |
| 95 | mfspr r13, SPRN_SPRG_SCRATCH0 /* r13 = original r13 */ |
| 96 | b kvmppc_resume_\intno /* Get back original handler */ |
| 97 | |
| 98 | 1: tophys(r13, r13) |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 99 | stw r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13) |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 100 | mfspr r12, SPRN_SPRG_SCRATCH1 |
| 101 | stw r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13) |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 102 | lbz r12, (SHADOW_VCPU_OFF + SVCPU_IN_GUEST)(r13) |
Alexander Graf | b4433a7 | 2010-01-08 02:58:04 +0100 | [diff] [blame] | 103 | cmpwi r12, KVM_GUEST_MODE_NONE |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 104 | bne ..kvmppc_handler_hasmagic_\intno |
| 105 | /* No KVM guest? Then jump back to the Linux handler! */ |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 106 | lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13) |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 107 | b 2b |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 108 | |
| 109 | /* Now we know we're handling a KVM guest */ |
| 110 | ..kvmppc_handler_hasmagic_\intno: |
Alexander Graf | b4433a7 | 2010-01-08 02:58:04 +0100 | [diff] [blame] | 111 | |
| 112 | /* Should we just skip the faulting instruction? */ |
| 113 | cmpwi r12, KVM_GUEST_MODE_SKIP |
| 114 | beq kvmppc_handler_skip_ins |
| 115 | |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 116 | /* Let's store which interrupt we're handling */ |
| 117 | li r12, \intno |
| 118 | |
| 119 | /* Jump into the SLB exit code that goes to the highmem handler */ |
| 120 | b kvmppc_handler_trampoline_exit |
| 121 | |
| 122 | .endm |
| 123 | |
| 124 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSTEM_RESET |
| 125 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_MACHINE_CHECK |
| 126 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_STORAGE |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 127 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_STORAGE |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 128 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_EXTERNAL |
| 129 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALIGNMENT |
| 130 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PROGRAM |
| 131 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_FP_UNAVAIL |
| 132 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DECREMENTER |
| 133 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSCALL |
| 134 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_TRACE |
| 135 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PERFMON |
| 136 | INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALTIVEC |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 137 | |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 138 | /* |
Alexander Graf | b4433a7 | 2010-01-08 02:58:04 +0100 | [diff] [blame] | 139 | * Bring us back to the faulting code, but skip the |
| 140 | * faulting instruction. |
| 141 | * |
| 142 | * This is a generic exit path from the interrupt |
| 143 | * trampolines above. |
| 144 | * |
| 145 | * Input Registers: |
| 146 | * |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 147 | * R12 = free |
| 148 | * R13 = Shadow VCPU (PACA) |
| 149 | * SVCPU.SCRATCH0 = guest R12 |
| 150 | * SVCPU.SCRATCH1 = guest CR |
| 151 | * SPRG_SCRATCH0 = guest R13 |
Alexander Graf | b4433a7 | 2010-01-08 02:58:04 +0100 | [diff] [blame] | 152 | * |
| 153 | */ |
| 154 | kvmppc_handler_skip_ins: |
| 155 | |
| 156 | /* Patch the IP to the next instruction */ |
| 157 | mfsrr0 r12 |
| 158 | addi r12, r12, 4 |
| 159 | mtsrr0 r12 |
| 160 | |
| 161 | /* Clean up all state */ |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 162 | lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13) |
Alexander Graf | b4433a7 | 2010-01-08 02:58:04 +0100 | [diff] [blame] | 163 | mtcr r12 |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 164 | PPC_LL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13) |
Paul Mackerras | 673b189 | 2011-04-05 13:59:58 +1000 | [diff] [blame] | 165 | GET_SCRATCH0(r13) |
Alexander Graf | b4433a7 | 2010-01-08 02:58:04 +0100 | [diff] [blame] | 166 | |
| 167 | /* And get back into the code */ |
| 168 | RFI |
Paul Mackerras | b01c8b5 | 2011-06-29 00:18:26 +0000 | [diff] [blame^] | 169 | #endif |
Alexander Graf | b4433a7 | 2010-01-08 02:58:04 +0100 | [diff] [blame] | 170 | |
| 171 | /* |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 172 | * This trampoline brings us back to a real mode handler |
| 173 | * |
| 174 | * Input Registers: |
| 175 | * |
Alexander Graf | 7e57cba | 2010-01-08 02:58:03 +0100 | [diff] [blame] | 176 | * R5 = SRR0 |
| 177 | * R6 = SRR1 |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 178 | * LR = real-mode IP |
| 179 | * |
| 180 | */ |
| 181 | .global kvmppc_handler_lowmem_trampoline |
| 182 | kvmppc_handler_lowmem_trampoline: |
| 183 | |
Alexander Graf | 7e57cba | 2010-01-08 02:58:03 +0100 | [diff] [blame] | 184 | mtsrr0 r5 |
| 185 | mtsrr1 r6 |
Alexander Graf | c862125 | 2009-10-30 05:47:09 +0000 | [diff] [blame] | 186 | blr |
| 187 | kvmppc_handler_lowmem_trampoline_end: |
| 188 | |
Alexander Graf | 021ec9c | 2010-01-08 02:58:06 +0100 | [diff] [blame] | 189 | /* |
| 190 | * Call a function in real mode |
| 191 | * |
| 192 | * Input Registers: |
| 193 | * |
| 194 | * R3 = function |
| 195 | * R4 = MSR |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 196 | * R5 = scratch register |
Alexander Graf | 021ec9c | 2010-01-08 02:58:06 +0100 | [diff] [blame] | 197 | * |
| 198 | */ |
| 199 | _GLOBAL(kvmppc_rmcall) |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 200 | LOAD_REG_IMMEDIATE(r5, MSR_NOIRQ) |
| 201 | mtmsr r5 /* Disable relocation and interrupts, so mtsrr |
Alexander Graf | 021ec9c | 2010-01-08 02:58:06 +0100 | [diff] [blame] | 202 | doesn't get interrupted */ |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 203 | sync |
Alexander Graf | 021ec9c | 2010-01-08 02:58:06 +0100 | [diff] [blame] | 204 | mtsrr0 r3 |
| 205 | mtsrr1 r4 |
| 206 | RFI |
| 207 | |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 208 | #if defined(CONFIG_PPC_BOOK3S_32) |
| 209 | #define STACK_LR INT_FRAME_SIZE+4 |
Alexander Graf | 0e67790 | 2010-07-29 15:04:20 +0200 | [diff] [blame] | 210 | |
| 211 | /* load_up_xxx have to run with MSR_DR=0 on Book3S_32 */ |
| 212 | #define MSR_EXT_START \ |
| 213 | PPC_STL r20, _NIP(r1); \ |
| 214 | mfmsr r20; \ |
| 215 | LOAD_REG_IMMEDIATE(r3, MSR_DR|MSR_EE); \ |
| 216 | andc r3,r20,r3; /* Disable DR,EE */ \ |
| 217 | mtmsr r3; \ |
| 218 | sync |
| 219 | |
| 220 | #define MSR_EXT_END \ |
| 221 | mtmsr r20; /* Enable DR,EE */ \ |
| 222 | sync; \ |
| 223 | PPC_LL r20, _NIP(r1) |
| 224 | |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 225 | #elif defined(CONFIG_PPC_BOOK3S_64) |
| 226 | #define STACK_LR _LINK |
Alexander Graf | 0e67790 | 2010-07-29 15:04:20 +0200 | [diff] [blame] | 227 | #define MSR_EXT_START |
| 228 | #define MSR_EXT_END |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 229 | #endif |
| 230 | |
Alexander Graf | d5e5281 | 2010-01-15 14:49:10 +0100 | [diff] [blame] | 231 | /* |
| 232 | * Activate current's external feature (FPU/Altivec/VSX) |
| 233 | */ |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 234 | #define define_load_up(what) \ |
| 235 | \ |
| 236 | _GLOBAL(kvmppc_load_up_ ## what); \ |
| 237 | PPC_STLU r1, -INT_FRAME_SIZE(r1); \ |
| 238 | mflr r3; \ |
| 239 | PPC_STL r3, STACK_LR(r1); \ |
Alexander Graf | 0e67790 | 2010-07-29 15:04:20 +0200 | [diff] [blame] | 240 | MSR_EXT_START; \ |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 241 | \ |
| 242 | bl FUNC(load_up_ ## what); \ |
| 243 | \ |
Alexander Graf | 0e67790 | 2010-07-29 15:04:20 +0200 | [diff] [blame] | 244 | MSR_EXT_END; \ |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 245 | PPC_LL r3, STACK_LR(r1); \ |
Alexander Graf | 8c3a4e0 | 2010-04-16 00:11:46 +0200 | [diff] [blame] | 246 | mtlr r3; \ |
| 247 | addi r1, r1, INT_FRAME_SIZE; \ |
Alexander Graf | d5e5281 | 2010-01-15 14:49:10 +0100 | [diff] [blame] | 248 | blr |
| 249 | |
| 250 | define_load_up(fpu) |
| 251 | #ifdef CONFIG_ALTIVEC |
| 252 | define_load_up(altivec) |
| 253 | #endif |
| 254 | #ifdef CONFIG_VSX |
| 255 | define_load_up(vsx) |
| 256 | #endif |
| 257 | |
Alexander Graf | 53e5b8b | 2010-04-16 00:11:48 +0200 | [diff] [blame] | 258 | #include "book3s_segment.S" |