book3s_interrupts.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. #include <asm/exception-64s.h>
  25. #if defined(CONFIG_PPC_BOOK3S_64)
  26. #define ULONG_SIZE 8
  27. #define FUNC(name) GLUE(.,name)
  28. #define GET_SHADOW_VCPU(reg) \
  29. addi reg, r13, PACA_KVM_SVCPU
  30. #define DISABLE_INTERRUPTS \
  31. mfmsr r0; \
  32. rldicl r0,r0,48,1; \
  33. rotldi r0,r0,16; \
  34. mtmsrd r0,1; \
  35. #elif defined(CONFIG_PPC_BOOK3S_32)
  36. #define ULONG_SIZE 4
  37. #define FUNC(name) name
  38. #define GET_SHADOW_VCPU(reg) \
  39. lwz reg, (THREAD + THREAD_KVM_SVCPU)(r2)
  40. #define DISABLE_INTERRUPTS \
  41. mfmsr r0; \
  42. rlwinm r0,r0,0,17,15; \
  43. mtmsr r0; \
  44. #endif /* CONFIG_PPC_BOOK3S_XX */
  45. #define VCPU_GPR(n) (VCPU_GPRS + (n * ULONG_SIZE))
  46. #define VCPU_LOAD_NVGPRS(vcpu) \
  47. PPC_LL r14, VCPU_GPR(r14)(vcpu); \
  48. PPC_LL r15, VCPU_GPR(r15)(vcpu); \
  49. PPC_LL r16, VCPU_GPR(r16)(vcpu); \
  50. PPC_LL r17, VCPU_GPR(r17)(vcpu); \
  51. PPC_LL r18, VCPU_GPR(r18)(vcpu); \
  52. PPC_LL r19, VCPU_GPR(r19)(vcpu); \
  53. PPC_LL r20, VCPU_GPR(r20)(vcpu); \
  54. PPC_LL r21, VCPU_GPR(r21)(vcpu); \
  55. PPC_LL r22, VCPU_GPR(r22)(vcpu); \
  56. PPC_LL r23, VCPU_GPR(r23)(vcpu); \
  57. PPC_LL r24, VCPU_GPR(r24)(vcpu); \
  58. PPC_LL r25, VCPU_GPR(r25)(vcpu); \
  59. PPC_LL r26, VCPU_GPR(r26)(vcpu); \
  60. PPC_LL r27, VCPU_GPR(r27)(vcpu); \
  61. PPC_LL r28, VCPU_GPR(r28)(vcpu); \
  62. PPC_LL r29, VCPU_GPR(r29)(vcpu); \
  63. PPC_LL r30, VCPU_GPR(r30)(vcpu); \
  64. PPC_LL r31, VCPU_GPR(r31)(vcpu); \
  65. /*****************************************************************************
  66. * *
  67. * Guest entry / exit code that is in kernel module memory (highmem) *
  68. * *
  69. ****************************************************************************/
  70. /* Registers:
  71. * r3: kvm_run pointer
  72. * r4: vcpu pointer
  73. */
  74. _GLOBAL(__kvmppc_vcpu_entry)
  75. kvm_start_entry:
  76. /* Write correct stack frame */
  77. mflr r0
  78. PPC_STL r0,PPC_LR_STKOFF(r1)
  79. /* Save host state to the stack */
  80. PPC_STLU r1, -SWITCH_FRAME_SIZE(r1)
  81. /* Save r3 (kvm_run) and r4 (vcpu) */
  82. SAVE_2GPRS(3, r1)
  83. /* Save non-volatile registers (r14 - r31) */
  84. SAVE_NVGPRS(r1)
  85. /* Save LR */
  86. PPC_STL r0, _LINK(r1)
  87. /* Load non-volatile guest state from the vcpu */
  88. VCPU_LOAD_NVGPRS(r4)
  89. GET_SHADOW_VCPU(r5)
  90. /* Save R1/R2 in the PACA */
  91. PPC_STL r1, SVCPU_HOST_R1(r5)
  92. PPC_STL r2, SVCPU_HOST_R2(r5)
  93. /* XXX swap in/out on load? */
  94. PPC_LL r3, VCPU_HIGHMEM_HANDLER(r4)
  95. PPC_STL r3, SVCPU_VMHANDLER(r5)
  96. kvm_start_lightweight:
  97. PPC_LL r10, VCPU_SHADOW_MSR(r4) /* r10 = vcpu->arch.shadow_msr */
  98. DISABLE_INTERRUPTS
  99. #ifdef CONFIG_PPC_BOOK3S_64
  100. /* Some guests may need to have dcbz set to 32 byte length.
  101. *
  102. * Usually we ensure that by patching the guest's instructions
  103. * to trap on dcbz and emulate it in the hypervisor.
  104. *
  105. * If we can, we should tell the CPU to use 32 byte dcbz though,
  106. * because that's a lot faster.
  107. */
  108. PPC_LL r3, VCPU_HFLAGS(r4)
  109. rldicl. r3, r3, 0, 63 /* CR = ((r3 & 1) == 0) */
  110. beq no_dcbz32_on
  111. mfspr r3,SPRN_HID5
  112. ori r3, r3, 0x80 /* XXX HID5_dcbz32 = 0x80 */
  113. mtspr SPRN_HID5,r3
  114. no_dcbz32_on:
  115. #endif /* CONFIG_PPC_BOOK3S_64 */
  116. PPC_LL r6, VCPU_RMCALL(r4)
  117. mtctr r6
  118. PPC_LL r3, VCPU_TRAMPOLINE_ENTER(r4)
  119. LOAD_REG_IMMEDIATE(r4, MSR_KERNEL & ~(MSR_IR | MSR_DR))
  120. /* Jump to segment patching handler and into our guest */
  121. bctr
  122. /*
  123. * This is the handler in module memory. It gets jumped at from the
  124. * lowmem trampoline code, so it's basically the guest exit code.
  125. *
  126. */
  127. .global kvmppc_handler_highmem
  128. kvmppc_handler_highmem:
  129. /*
  130. * Register usage at this point:
  131. *
  132. * R1 = host R1
  133. * R2 = host R2
  134. * R12 = exit handler id
  135. * R13 = PACA
  136. * SVCPU.* = guest *
  137. *
  138. */
  139. /* R7 = vcpu */
  140. PPC_LL r7, GPR4(r1)
  141. #ifdef CONFIG_PPC_BOOK3S_64
  142. PPC_LL r5, VCPU_HFLAGS(r7)
  143. rldicl. r5, r5, 0, 63 /* CR = ((r5 & 1) == 0) */
  144. beq no_dcbz32_off
  145. li r4, 0
  146. mfspr r5,SPRN_HID5
  147. rldimi r5,r4,6,56
  148. mtspr SPRN_HID5,r5
  149. no_dcbz32_off:
  150. #endif /* CONFIG_PPC_BOOK3S_64 */
  151. PPC_STL r14, VCPU_GPR(r14)(r7)
  152. PPC_STL r15, VCPU_GPR(r15)(r7)
  153. PPC_STL r16, VCPU_GPR(r16)(r7)
  154. PPC_STL r17, VCPU_GPR(r17)(r7)
  155. PPC_STL r18, VCPU_GPR(r18)(r7)
  156. PPC_STL r19, VCPU_GPR(r19)(r7)
  157. PPC_STL r20, VCPU_GPR(r20)(r7)
  158. PPC_STL r21, VCPU_GPR(r21)(r7)
  159. PPC_STL r22, VCPU_GPR(r22)(r7)
  160. PPC_STL r23, VCPU_GPR(r23)(r7)
  161. PPC_STL r24, VCPU_GPR(r24)(r7)
  162. PPC_STL r25, VCPU_GPR(r25)(r7)
  163. PPC_STL r26, VCPU_GPR(r26)(r7)
  164. PPC_STL r27, VCPU_GPR(r27)(r7)
  165. PPC_STL r28, VCPU_GPR(r28)(r7)
  166. PPC_STL r29, VCPU_GPR(r29)(r7)
  167. PPC_STL r30, VCPU_GPR(r30)(r7)
  168. PPC_STL r31, VCPU_GPR(r31)(r7)
  169. /* Restore host msr -> SRR1 */
  170. PPC_LL r6, VCPU_HOST_MSR(r7)
  171. /*
  172. * For some interrupts, we need to call the real Linux
  173. * handler, so it can do work for us. This has to happen
  174. * as if the interrupt arrived from the kernel though,
  175. * so let's fake it here where most state is restored.
  176. *
  177. * Call Linux for hardware interrupts/decrementer
  178. * r3 = address of interrupt handler (exit reason)
  179. */
  180. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  181. beq call_linux_handler
  182. cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
  183. beq call_linux_handler
  184. cmpwi r12, BOOK3S_INTERRUPT_PERFMON
  185. beq call_linux_handler
  186. /* Back to EE=1 */
  187. mtmsr r6
  188. sync
  189. b kvm_return_point
  190. call_linux_handler:
  191. /*
  192. * If we land here we need to jump back to the handler we
  193. * came from.
  194. *
  195. * We have a page that we can access from real mode, so let's
  196. * jump back to that and use it as a trampoline to get back into the
  197. * interrupt handler!
  198. *
  199. * R3 still contains the exit code,
  200. * R5 VCPU_HOST_RETIP and
  201. * R6 VCPU_HOST_MSR
  202. */
  203. /* Restore host IP -> SRR0 */
  204. PPC_LL r5, VCPU_HOST_RETIP(r7)
  205. /* XXX Better move to a safe function?
  206. * What if we get an HTAB flush in between mtsrr0 and mtsrr1? */
  207. mtlr r12
  208. PPC_LL r4, VCPU_TRAMPOLINE_LOWMEM(r7)
  209. mtsrr0 r4
  210. LOAD_REG_IMMEDIATE(r3, MSR_KERNEL & ~(MSR_IR | MSR_DR))
  211. mtsrr1 r3
  212. RFI
  213. .global kvm_return_point
  214. kvm_return_point:
  215. /* Jump back to lightweight entry if we're supposed to */
  216. /* go back into the guest */
  217. /* Pass the exit number as 3rd argument to kvmppc_handle_exit */
  218. mr r5, r12
  219. /* Restore r3 (kvm_run) and r4 (vcpu) */
  220. REST_2GPRS(3, r1)
  221. bl FUNC(kvmppc_handle_exit)
  222. /* If RESUME_GUEST, get back in the loop */
  223. cmpwi r3, RESUME_GUEST
  224. beq kvm_loop_lightweight
  225. cmpwi r3, RESUME_GUEST_NV
  226. beq kvm_loop_heavyweight
  227. kvm_exit_loop:
  228. PPC_LL r4, _LINK(r1)
  229. mtlr r4
  230. /* Restore non-volatile host registers (r14 - r31) */
  231. REST_NVGPRS(r1)
  232. addi r1, r1, SWITCH_FRAME_SIZE
  233. blr
  234. kvm_loop_heavyweight:
  235. PPC_LL r4, _LINK(r1)
  236. PPC_STL r4, (PPC_LR_STKOFF + SWITCH_FRAME_SIZE)(r1)
  237. /* Load vcpu and cpu_run */
  238. REST_2GPRS(3, r1)
  239. /* Load non-volatile guest state from the vcpu */
  240. VCPU_LOAD_NVGPRS(r4)
  241. /* Jump back into the beginning of this function */
  242. b kvm_start_lightweight
  243. kvm_loop_lightweight:
  244. /* We'll need the vcpu pointer */
  245. REST_GPR(4, r1)
  246. /* Jump back into the beginning of this function */
  247. b kvm_start_lightweight