book3s_64_interrupts.S 7.5 KB

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