book3s_rmhandlers.S 6.7 KB

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