book3s_rmhandlers.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. #include <asm/ppc_asm.h>
  20. #include <asm/kvm_asm.h>
  21. #include <asm/reg.h>
  22. #include <asm/page.h>
  23. #include <asm/asm-offsets.h>
  24. #ifdef CONFIG_PPC_BOOK3S_64
  25. #include <asm/exception-64s.h>
  26. #endif
  27. /*****************************************************************************
  28. * *
  29. * Real Mode handlers that need to be in low physical memory *
  30. * *
  31. ****************************************************************************/
  32. #if defined(CONFIG_PPC_BOOK3S_64)
  33. #define LOAD_SHADOW_VCPU(reg) GET_PACA(reg)
  34. #define SHADOW_VCPU_OFF PACA_KVM_SVCPU
  35. #define MSR_NOIRQ MSR_KERNEL & ~(MSR_IR | MSR_DR)
  36. #define FUNC(name) GLUE(.,name)
  37. #elif defined(CONFIG_PPC_BOOK3S_32)
  38. #define LOAD_SHADOW_VCPU(reg) \
  39. mfspr reg, SPRN_SPRG_THREAD; \
  40. lwz reg, THREAD_KVM_SVCPU(reg); \
  41. /* PPC32 can have a NULL pointer - let's check for that */ \
  42. mtspr SPRN_SPRG_SCRATCH1, r12; /* Save r12 */ \
  43. mfcr r12; \
  44. cmpwi reg, 0; \
  45. bne 1f; \
  46. mfspr reg, SPRN_SPRG_SCRATCH0; \
  47. mtcr r12; \
  48. mfspr r12, SPRN_SPRG_SCRATCH1; \
  49. b kvmppc_resume_\intno; \
  50. 1:; \
  51. mtcr r12; \
  52. mfspr r12, SPRN_SPRG_SCRATCH1; \
  53. tophys(reg, reg)
  54. #define SHADOW_VCPU_OFF 0
  55. #define MSR_NOIRQ MSR_KERNEL
  56. #define FUNC(name) name
  57. #endif
  58. .macro INTERRUPT_TRAMPOLINE intno
  59. .global kvmppc_trampoline_\intno
  60. kvmppc_trampoline_\intno:
  61. SET_SCRATCH0(r13) /* Save r13 */
  62. /*
  63. * First thing to do is to find out if we're coming
  64. * from a KVM guest or a Linux process.
  65. *
  66. * To distinguish, we check a magic byte in the PACA/current
  67. */
  68. LOAD_SHADOW_VCPU(r13)
  69. PPC_STL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
  70. mfcr r12
  71. stw r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
  72. lbz r12, (SHADOW_VCPU_OFF + SVCPU_IN_GUEST)(r13)
  73. cmpwi r12, KVM_GUEST_MODE_NONE
  74. bne ..kvmppc_handler_hasmagic_\intno
  75. /* No KVM guest? Then jump back to the Linux handler! */
  76. lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
  77. mtcr r12
  78. PPC_LL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
  79. GET_SCRATCH0(r13) /* r13 = original r13 */
  80. b kvmppc_resume_\intno /* Get back original handler */
  81. /* Now we know we're handling a KVM guest */
  82. ..kvmppc_handler_hasmagic_\intno:
  83. /* Should we just skip the faulting instruction? */
  84. cmpwi r12, KVM_GUEST_MODE_SKIP
  85. beq kvmppc_handler_skip_ins
  86. /* Let's store which interrupt we're handling */
  87. li r12, \intno
  88. /* Jump into the SLB exit code that goes to the highmem handler */
  89. b kvmppc_handler_trampoline_exit
  90. .endm
  91. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSTEM_RESET
  92. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_MACHINE_CHECK
  93. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_STORAGE
  94. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_STORAGE
  95. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_EXTERNAL
  96. #ifdef CONFIG_PPC_BOOK3S_64
  97. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_EXTERNAL_HV
  98. #endif
  99. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALIGNMENT
  100. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PROGRAM
  101. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_FP_UNAVAIL
  102. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DECREMENTER
  103. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_SYSCALL
  104. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_TRACE
  105. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_PERFMON
  106. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_ALTIVEC
  107. /* Those are only available on 64 bit machines */
  108. #ifdef CONFIG_PPC_BOOK3S_64
  109. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_DATA_SEGMENT
  110. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_INST_SEGMENT
  111. INTERRUPT_TRAMPOLINE BOOK3S_INTERRUPT_VSX
  112. #endif
  113. /*
  114. * Bring us back to the faulting code, but skip the
  115. * faulting instruction.
  116. *
  117. * This is a generic exit path from the interrupt
  118. * trampolines above.
  119. *
  120. * Input Registers:
  121. *
  122. * R12 = free
  123. * R13 = Shadow VCPU (PACA)
  124. * SVCPU.SCRATCH0 = guest R12
  125. * SVCPU.SCRATCH1 = guest CR
  126. * SPRG_SCRATCH0 = guest R13
  127. *
  128. */
  129. kvmppc_handler_skip_ins:
  130. /* Patch the IP to the next instruction */
  131. mfsrr0 r12
  132. addi r12, r12, 4
  133. mtsrr0 r12
  134. /* Clean up all state */
  135. lwz r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH1)(r13)
  136. mtcr r12
  137. PPC_LL r12, (SHADOW_VCPU_OFF + SVCPU_SCRATCH0)(r13)
  138. GET_SCRATCH0(r13)
  139. /* And get back into the code */
  140. RFI
  141. /*
  142. * This trampoline brings us back to a real mode handler
  143. *
  144. * Input Registers:
  145. *
  146. * R5 = SRR0
  147. * R6 = SRR1
  148. * LR = real-mode IP
  149. *
  150. */
  151. .global kvmppc_handler_lowmem_trampoline
  152. kvmppc_handler_lowmem_trampoline:
  153. mtsrr0 r5
  154. mtsrr1 r6
  155. blr
  156. kvmppc_handler_lowmem_trampoline_end:
  157. /*
  158. * Call a function in real mode
  159. *
  160. * Input Registers:
  161. *
  162. * R3 = function
  163. * R4 = MSR
  164. * R5 = scratch register
  165. *
  166. */
  167. _GLOBAL(kvmppc_rmcall)
  168. LOAD_REG_IMMEDIATE(r5, MSR_NOIRQ)
  169. mtmsr r5 /* Disable relocation and interrupts, so mtsrr
  170. doesn't get interrupted */
  171. sync
  172. mtsrr0 r3
  173. mtsrr1 r4
  174. RFI
  175. #if defined(CONFIG_PPC_BOOK3S_32)
  176. #define STACK_LR INT_FRAME_SIZE+4
  177. /* load_up_xxx have to run with MSR_DR=0 on Book3S_32 */
  178. #define MSR_EXT_START \
  179. PPC_STL r20, _NIP(r1); \
  180. mfmsr r20; \
  181. LOAD_REG_IMMEDIATE(r3, MSR_DR|MSR_EE); \
  182. andc r3,r20,r3; /* Disable DR,EE */ \
  183. mtmsr r3; \
  184. sync
  185. #define MSR_EXT_END \
  186. mtmsr r20; /* Enable DR,EE */ \
  187. sync; \
  188. PPC_LL r20, _NIP(r1)
  189. #elif defined(CONFIG_PPC_BOOK3S_64)
  190. #define STACK_LR _LINK
  191. #define MSR_EXT_START
  192. #define MSR_EXT_END
  193. #endif
  194. /*
  195. * Activate current's external feature (FPU/Altivec/VSX)
  196. */
  197. #define define_load_up(what) \
  198. \
  199. _GLOBAL(kvmppc_load_up_ ## what); \
  200. PPC_STLU r1, -INT_FRAME_SIZE(r1); \
  201. mflr r3; \
  202. PPC_STL r3, STACK_LR(r1); \
  203. MSR_EXT_START; \
  204. \
  205. bl FUNC(load_up_ ## what); \
  206. \
  207. MSR_EXT_END; \
  208. PPC_LL r3, STACK_LR(r1); \
  209. mtlr r3; \
  210. addi r1, r1, INT_FRAME_SIZE; \
  211. blr
  212. define_load_up(fpu)
  213. #ifdef CONFIG_ALTIVEC
  214. define_load_up(altivec)
  215. #endif
  216. #ifdef CONFIG_VSX
  217. define_load_up(vsx)
  218. #endif
  219. .global kvmppc_trampoline_lowmem
  220. kvmppc_trampoline_lowmem:
  221. PPC_LONG kvmppc_handler_lowmem_trampoline - CONFIG_KERNEL_START
  222. .global kvmppc_trampoline_enter
  223. kvmppc_trampoline_enter:
  224. PPC_LONG kvmppc_handler_trampoline_enter - CONFIG_KERNEL_START
  225. #include "book3s_segment.S"