book3s_64_interrupts.S 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. ld r3, VCPU_TRAMPOLINE_ENTER(r4)
  84. std r3, PACA_KVM_RMHANDLER(r13)
  85. kvm_start_lightweight:
  86. ld r9, VCPU_PC(r4) /* r9 = vcpu->arch.pc */
  87. ld r10, VCPU_SHADOW_MSR(r4) /* r10 = vcpu->arch.shadow_msr */
  88. /* Load some guest state in the respective registers */
  89. ld r3, VCPU_CTR(r4) /* r3 = vcpu->arch.ctr */
  90. mtctr r3 /* CTR = r3 */
  91. ld r3, VCPU_LR(r4) /* r3 = vcpu->arch.lr */
  92. mtlr r3 /* LR = r3 */
  93. DISABLE_INTERRUPTS
  94. /* Some guests may need to have dcbz set to 32 byte length.
  95. *
  96. * Usually we ensure that by patching the guest's instructions
  97. * to trap on dcbz and emulate it in the hypervisor.
  98. *
  99. * If we can, we should tell the CPU to use 32 byte dcbz though,
  100. * because that's a lot faster.
  101. */
  102. ld r3, VCPU_HFLAGS(r4)
  103. rldicl. r3, r3, 0, 63 /* CR = ((r3 & 1) == 0) */
  104. beq no_dcbz32_on
  105. mfspr r3,SPRN_HID5
  106. ori r3, r3, 0x80 /* XXX HID5_dcbz32 = 0x80 */
  107. mtspr SPRN_HID5,r3
  108. no_dcbz32_on:
  109. /* This sets the Magic value for the trampoline */
  110. /* XXX this needs to move into a safe function, so we can
  111. be sure we don't get any interrupts */
  112. li r11, 1
  113. stb r11, PACA_KVM_IN_GUEST(r13)
  114. ld r3, PACA_KVM_RMHANDLER(r13)
  115. mtsrr0 r3
  116. LOAD_REG_IMMEDIATE(r3, MSR_KERNEL & ~(MSR_IR | MSR_DR))
  117. mtsrr1 r3
  118. /* Jump to SLB patching handlder and into our guest */
  119. RFI
  120. /*
  121. * This is the handler in module memory. It gets jumped at from the
  122. * lowmem trampoline code, so it's basically the guest exit code.
  123. *
  124. */
  125. .global kvmppc_handler_highmem
  126. kvmppc_handler_highmem:
  127. /*
  128. * Register usage at this point:
  129. *
  130. * R0 = guest last inst
  131. * R1 = host R1
  132. * R2 = host R2
  133. * R3 = guest PC
  134. * R4 = guest MSR
  135. * R5 = guest DAR
  136. * R6 = guest DSISR
  137. * R13 = PACA
  138. * PACA.KVM.* = guest *
  139. *
  140. */
  141. /* R7 = vcpu */
  142. ld r7, GPR4(r1)
  143. /* Now save the guest state */
  144. stw r0, VCPU_LAST_INST(r7)
  145. std r3, VCPU_PC(r7)
  146. std r4, VCPU_SHADOW_MSR(r7)
  147. std r5, VCPU_FAULT_DEAR(r7)
  148. std r6, VCPU_FAULT_DSISR(r7)
  149. ld r5, VCPU_HFLAGS(r7)
  150. rldicl. r5, r5, 0, 63 /* CR = ((r5 & 1) == 0) */
  151. beq no_dcbz32_off
  152. mfspr r5,SPRN_HID5
  153. rldimi r5,r5,6,56
  154. mtspr SPRN_HID5,r5
  155. no_dcbz32_off:
  156. std r14, VCPU_GPR(r14)(r7)
  157. std r15, VCPU_GPR(r15)(r7)
  158. std r16, VCPU_GPR(r16)(r7)
  159. std r17, VCPU_GPR(r17)(r7)
  160. std r18, VCPU_GPR(r18)(r7)
  161. std r19, VCPU_GPR(r19)(r7)
  162. std r20, VCPU_GPR(r20)(r7)
  163. std r21, VCPU_GPR(r21)(r7)
  164. std r22, VCPU_GPR(r22)(r7)
  165. std r23, VCPU_GPR(r23)(r7)
  166. std r24, VCPU_GPR(r24)(r7)
  167. std r25, VCPU_GPR(r25)(r7)
  168. std r26, VCPU_GPR(r26)(r7)
  169. std r27, VCPU_GPR(r27)(r7)
  170. std r28, VCPU_GPR(r28)(r7)
  171. std r29, VCPU_GPR(r29)(r7)
  172. std r30, VCPU_GPR(r30)(r7)
  173. std r31, VCPU_GPR(r31)(r7)
  174. /* Save guest CTR */
  175. mfctr r5
  176. std r5, VCPU_CTR(r7)
  177. /* Save guest LR */
  178. mflr r5
  179. std r5, VCPU_LR(r7)
  180. /* Restore host msr -> SRR1 */
  181. ld r6, VCPU_HOST_MSR(r7)
  182. /*
  183. * For some interrupts, we need to call the real Linux
  184. * handler, so it can do work for us. This has to happen
  185. * as if the interrupt arrived from the kernel though,
  186. * so let's fake it here where most state is restored.
  187. *
  188. * Call Linux for hardware interrupts/decrementer
  189. * r3 = address of interrupt handler (exit reason)
  190. */
  191. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  192. beq call_linux_handler
  193. cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
  194. beq call_linux_handler
  195. /* Back to EE=1 */
  196. mtmsr r6
  197. b kvm_return_point
  198. call_linux_handler:
  199. /*
  200. * If we land here we need to jump back to the handler we
  201. * came from.
  202. *
  203. * We have a page that we can access from real mode, so let's
  204. * jump back to that and use it as a trampoline to get back into the
  205. * interrupt handler!
  206. *
  207. * R3 still contains the exit code,
  208. * R5 VCPU_HOST_RETIP and
  209. * R6 VCPU_HOST_MSR
  210. */
  211. /* Restore host IP -> SRR0 */
  212. ld r5, VCPU_HOST_RETIP(r7)
  213. /* XXX Better move to a safe function?
  214. * What if we get an HTAB flush in between mtsrr0 and mtsrr1? */
  215. mtlr r12
  216. ld r4, VCPU_TRAMPOLINE_LOWMEM(r7)
  217. mtsrr0 r4
  218. LOAD_REG_IMMEDIATE(r3, MSR_KERNEL & ~(MSR_IR | MSR_DR))
  219. mtsrr1 r3
  220. RFI
  221. .global kvm_return_point
  222. kvm_return_point:
  223. /* Jump back to lightweight entry if we're supposed to */
  224. /* go back into the guest */
  225. /* Pass the exit number as 3rd argument to kvmppc_handle_exit */
  226. mr r5, r12
  227. /* Restore r3 (kvm_run) and r4 (vcpu) */
  228. REST_2GPRS(3, r1)
  229. bl KVMPPC_HANDLE_EXIT
  230. /* If RESUME_GUEST, get back in the loop */
  231. cmpwi r3, RESUME_GUEST
  232. beq kvm_loop_lightweight
  233. cmpwi r3, RESUME_GUEST_NV
  234. beq kvm_loop_heavyweight
  235. kvm_exit_loop:
  236. ld r4, _LINK(r1)
  237. mtlr r4
  238. /* Restore non-volatile host registers (r14 - r31) */
  239. REST_NVGPRS(r1)
  240. addi r1, r1, SWITCH_FRAME_SIZE
  241. blr
  242. kvm_loop_heavyweight:
  243. ld r4, _LINK(r1)
  244. std r4, (16 + SWITCH_FRAME_SIZE)(r1)
  245. /* Load vcpu and cpu_run */
  246. REST_2GPRS(3, r1)
  247. /* Load non-volatile guest state from the vcpu */
  248. VCPU_LOAD_NVGPRS(r4)
  249. /* Jump back into the beginning of this function */
  250. b kvm_start_lightweight
  251. kvm_loop_lightweight:
  252. /* We'll need the vcpu pointer */
  253. REST_GPR(4, r1)
  254. /* Jump back into the beginning of this function */
  255. b kvm_start_lightweight