book3s_64_interrupts.S 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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 mfpaca tmp_reg, src_reg, offset, vcpu_reg
  29. ld \tmp_reg, (PACA_EXMC+\offset)(r13)
  30. std \tmp_reg, VCPU_GPR(\src_reg)(\vcpu_reg)
  31. .endm
  32. .macro DISABLE_INTERRUPTS
  33. mfmsr r0
  34. rldicl r0,r0,48,1
  35. rotldi r0,r0,16
  36. mtmsrd r0,1
  37. .endm
  38. /*****************************************************************************
  39. * *
  40. * Guest entry / exit code that is in kernel module memory (highmem) *
  41. * *
  42. ****************************************************************************/
  43. /* Registers:
  44. * r3: kvm_run pointer
  45. * r4: vcpu pointer
  46. */
  47. _GLOBAL(__kvmppc_vcpu_entry)
  48. kvm_start_entry:
  49. /* Write correct stack frame */
  50. mflr r0
  51. std r0,16(r1)
  52. /* Save host state to the stack */
  53. stdu r1, -SWITCH_FRAME_SIZE(r1)
  54. /* Save r3 (kvm_run) and r4 (vcpu) */
  55. SAVE_2GPRS(3, r1)
  56. /* Save non-volatile registers (r14 - r31) */
  57. SAVE_NVGPRS(r1)
  58. /* Save LR */
  59. mflr r14
  60. std r14, _LINK(r1)
  61. /* XXX optimize non-volatile loading away */
  62. kvm_start_lightweight:
  63. DISABLE_INTERRUPTS
  64. /* Save R1/R2 in the PACA */
  65. std r1, PACAR1(r13)
  66. std r2, (PACA_EXMC+EX_SRR0)(r13)
  67. ld r3, VCPU_HIGHMEM_HANDLER(r4)
  68. std r3, PACASAVEDMSR(r13)
  69. /* Load non-volatile guest state from the vcpu */
  70. ld r14, VCPU_GPR(r14)(r4)
  71. ld r15, VCPU_GPR(r15)(r4)
  72. ld r16, VCPU_GPR(r16)(r4)
  73. ld r17, VCPU_GPR(r17)(r4)
  74. ld r18, VCPU_GPR(r18)(r4)
  75. ld r19, VCPU_GPR(r19)(r4)
  76. ld r20, VCPU_GPR(r20)(r4)
  77. ld r21, VCPU_GPR(r21)(r4)
  78. ld r22, VCPU_GPR(r22)(r4)
  79. ld r23, VCPU_GPR(r23)(r4)
  80. ld r24, VCPU_GPR(r24)(r4)
  81. ld r25, VCPU_GPR(r25)(r4)
  82. ld r26, VCPU_GPR(r26)(r4)
  83. ld r27, VCPU_GPR(r27)(r4)
  84. ld r28, VCPU_GPR(r28)(r4)
  85. ld r29, VCPU_GPR(r29)(r4)
  86. ld r30, VCPU_GPR(r30)(r4)
  87. ld r31, VCPU_GPR(r31)(r4)
  88. ld r9, VCPU_PC(r4) /* r9 = vcpu->arch.pc */
  89. ld r10, VCPU_SHADOW_MSR(r4) /* r10 = vcpu->arch.shadow_msr */
  90. ld r3, VCPU_TRAMPOLINE_ENTER(r4)
  91. mtsrr0 r3
  92. LOAD_REG_IMMEDIATE(r3, MSR_KERNEL & ~(MSR_IR | MSR_DR))
  93. mtsrr1 r3
  94. /* Load guest state in the respective registers */
  95. lwz r3, VCPU_CR(r4) /* r3 = vcpu->arch.cr */
  96. stw r3, (PACA_EXMC + EX_CCR)(r13)
  97. ld r3, VCPU_CTR(r4) /* r3 = vcpu->arch.ctr */
  98. mtctr r3 /* CTR = r3 */
  99. ld r3, VCPU_LR(r4) /* r3 = vcpu->arch.lr */
  100. mtlr r3 /* LR = r3 */
  101. ld r3, VCPU_XER(r4) /* r3 = vcpu->arch.xer */
  102. std r3, (PACA_EXMC + EX_R3)(r13)
  103. /* Some guests may need to have dcbz set to 32 byte length.
  104. *
  105. * Usually we ensure that by patching the guest's instructions
  106. * to trap on dcbz and emulate it in the hypervisor.
  107. *
  108. * If we can, we should tell the CPU to use 32 byte dcbz though,
  109. * because that's a lot faster.
  110. */
  111. ld r3, VCPU_HFLAGS(r4)
  112. rldicl. r3, r3, 0, 63 /* CR = ((r3 & 1) == 0) */
  113. beq no_dcbz32_on
  114. mfspr r3,SPRN_HID5
  115. ori r3, r3, 0x80 /* XXX HID5_dcbz32 = 0x80 */
  116. mtspr SPRN_HID5,r3
  117. no_dcbz32_on:
  118. /* Load guest GPRs */
  119. ld r3, VCPU_GPR(r9)(r4)
  120. std r3, (PACA_EXMC + EX_R9)(r13)
  121. ld r3, VCPU_GPR(r10)(r4)
  122. std r3, (PACA_EXMC + EX_R10)(r13)
  123. ld r3, VCPU_GPR(r11)(r4)
  124. std r3, (PACA_EXMC + EX_R11)(r13)
  125. ld r3, VCPU_GPR(r12)(r4)
  126. std r3, (PACA_EXMC + EX_R12)(r13)
  127. ld r3, VCPU_GPR(r13)(r4)
  128. std r3, (PACA_EXMC + EX_R13)(r13)
  129. ld r0, VCPU_GPR(r0)(r4)
  130. ld r1, VCPU_GPR(r1)(r4)
  131. ld r2, VCPU_GPR(r2)(r4)
  132. ld r3, VCPU_GPR(r3)(r4)
  133. ld r5, VCPU_GPR(r5)(r4)
  134. ld r6, VCPU_GPR(r6)(r4)
  135. ld r7, VCPU_GPR(r7)(r4)
  136. ld r8, VCPU_GPR(r8)(r4)
  137. ld r4, VCPU_GPR(r4)(r4)
  138. /* This sets the Magic value for the trampoline */
  139. li r11, 1
  140. stb r11, PACA_KVM_IN_GUEST(r13)
  141. /* Jump to SLB patching handlder and into our guest */
  142. RFI
  143. /*
  144. * This is the handler in module memory. It gets jumped at from the
  145. * lowmem trampoline code, so it's basically the guest exit code.
  146. *
  147. */
  148. .global kvmppc_handler_highmem
  149. kvmppc_handler_highmem:
  150. /*
  151. * Register usage at this point:
  152. *
  153. * R00 = guest R13
  154. * R01 = host R1
  155. * R02 = host R2
  156. * R10 = guest PC
  157. * R11 = guest MSR
  158. * R12 = exit handler id
  159. * R13 = PACA
  160. * PACA.exmc.R9 = guest R1
  161. * PACA.exmc.R10 = guest R10
  162. * PACA.exmc.R11 = guest R11
  163. * PACA.exmc.R12 = guest R12
  164. * PACA.exmc.R13 = guest R2
  165. * PACA.exmc.DAR = guest DAR
  166. * PACA.exmc.DSISR = guest DSISR
  167. * PACA.exmc.LR = guest instruction
  168. * PACA.exmc.CCR = guest CR
  169. * PACA.exmc.SRR0 = guest R0
  170. *
  171. */
  172. std r3, (PACA_EXMC+EX_R3)(r13)
  173. /* save the exit id in R3 */
  174. mr r3, r12
  175. /* R12 = vcpu */
  176. ld r12, GPR4(r1)
  177. /* Now save the guest state */
  178. std r0, VCPU_GPR(r13)(r12)
  179. std r4, VCPU_GPR(r4)(r12)
  180. std r5, VCPU_GPR(r5)(r12)
  181. std r6, VCPU_GPR(r6)(r12)
  182. std r7, VCPU_GPR(r7)(r12)
  183. std r8, VCPU_GPR(r8)(r12)
  184. std r9, VCPU_GPR(r9)(r12)
  185. /* get registers from PACA */
  186. mfpaca r5, r0, EX_SRR0, r12
  187. mfpaca r5, r3, EX_R3, r12
  188. mfpaca r5, r1, EX_R9, r12
  189. mfpaca r5, r10, EX_R10, r12
  190. mfpaca r5, r11, EX_R11, r12
  191. mfpaca r5, r12, EX_R12, r12
  192. mfpaca r5, r2, EX_R13, r12
  193. lwz r5, (PACA_EXMC+EX_LR)(r13)
  194. stw r5, VCPU_LAST_INST(r12)
  195. lwz r5, (PACA_EXMC+EX_CCR)(r13)
  196. stw r5, VCPU_CR(r12)
  197. ld r5, VCPU_HFLAGS(r12)
  198. rldicl. r5, r5, 0, 63 /* CR = ((r5 & 1) == 0) */
  199. beq no_dcbz32_off
  200. mfspr r5,SPRN_HID5
  201. rldimi r5,r5,6,56
  202. mtspr SPRN_HID5,r5
  203. no_dcbz32_off:
  204. /* XXX maybe skip on lightweight? */
  205. std r14, VCPU_GPR(r14)(r12)
  206. std r15, VCPU_GPR(r15)(r12)
  207. std r16, VCPU_GPR(r16)(r12)
  208. std r17, VCPU_GPR(r17)(r12)
  209. std r18, VCPU_GPR(r18)(r12)
  210. std r19, VCPU_GPR(r19)(r12)
  211. std r20, VCPU_GPR(r20)(r12)
  212. std r21, VCPU_GPR(r21)(r12)
  213. std r22, VCPU_GPR(r22)(r12)
  214. std r23, VCPU_GPR(r23)(r12)
  215. std r24, VCPU_GPR(r24)(r12)
  216. std r25, VCPU_GPR(r25)(r12)
  217. std r26, VCPU_GPR(r26)(r12)
  218. std r27, VCPU_GPR(r27)(r12)
  219. std r28, VCPU_GPR(r28)(r12)
  220. std r29, VCPU_GPR(r29)(r12)
  221. std r30, VCPU_GPR(r30)(r12)
  222. std r31, VCPU_GPR(r31)(r12)
  223. /* Restore non-volatile host registers (r14 - r31) */
  224. REST_NVGPRS(r1)
  225. /* Save guest PC (R10) */
  226. std r10, VCPU_PC(r12)
  227. /* Save guest msr (R11) */
  228. std r11, VCPU_SHADOW_MSR(r12)
  229. /* Save guest CTR (in R12) */
  230. mfctr r5
  231. std r5, VCPU_CTR(r12)
  232. /* Save guest LR */
  233. mflr r5
  234. std r5, VCPU_LR(r12)
  235. /* Save guest XER */
  236. mfxer r5
  237. std r5, VCPU_XER(r12)
  238. /* Save guest DAR */
  239. ld r5, (PACA_EXMC+EX_DAR)(r13)
  240. std r5, VCPU_FAULT_DEAR(r12)
  241. /* Save guest DSISR */
  242. lwz r5, (PACA_EXMC+EX_DSISR)(r13)
  243. std r5, VCPU_FAULT_DSISR(r12)
  244. /* Restore host msr -> SRR1 */
  245. ld r7, VCPU_HOST_MSR(r12)
  246. mtsrr1 r7
  247. /* Restore host IP -> SRR0 */
  248. ld r6, VCPU_HOST_RETIP(r12)
  249. mtsrr0 r6
  250. /*
  251. * For some interrupts, we need to call the real Linux
  252. * handler, so it can do work for us. This has to happen
  253. * as if the interrupt arrived from the kernel though,
  254. * so let's fake it here where most state is restored.
  255. *
  256. * Call Linux for hardware interrupts/decrementer
  257. * r3 = address of interrupt handler (exit reason)
  258. */
  259. cmpwi r3, BOOK3S_INTERRUPT_EXTERNAL
  260. beq call_linux_handler
  261. cmpwi r3, BOOK3S_INTERRUPT_DECREMENTER
  262. beq call_linux_handler
  263. /* Back to Interruptable Mode! (goto kvm_return_point) */
  264. RFI
  265. call_linux_handler:
  266. /*
  267. * If we land here we need to jump back to the handler we
  268. * came from.
  269. *
  270. * We have a page that we can access from real mode, so let's
  271. * jump back to that and use it as a trampoline to get back into the
  272. * interrupt handler!
  273. *
  274. * R3 still contains the exit code,
  275. * R6 VCPU_HOST_RETIP and
  276. * R7 VCPU_HOST_MSR
  277. */
  278. mtlr r3
  279. ld r5, VCPU_TRAMPOLINE_LOWMEM(r12)
  280. mtsrr0 r5
  281. LOAD_REG_IMMEDIATE(r5, MSR_KERNEL & ~(MSR_IR | MSR_DR))
  282. mtsrr1 r5
  283. RFI
  284. .global kvm_return_point
  285. kvm_return_point:
  286. /* Jump back to lightweight entry if we're supposed to */
  287. /* go back into the guest */
  288. mr r5, r3
  289. /* Restore r3 (kvm_run) and r4 (vcpu) */
  290. REST_2GPRS(3, r1)
  291. bl KVMPPC_HANDLE_EXIT
  292. #if 0 /* XXX get lightweight exits back */
  293. cmpwi r3, RESUME_GUEST
  294. bne kvm_exit_heavyweight
  295. /* put VCPU and KVM_RUN back into place and roll again! */
  296. REST_2GPRS(3, r1)
  297. b kvm_start_lightweight
  298. kvm_exit_heavyweight:
  299. /* Restore non-volatile host registers */
  300. ld r14, _LINK(r1)
  301. mtlr r14
  302. REST_NVGPRS(r1)
  303. addi r1, r1, SWITCH_FRAME_SIZE
  304. #else
  305. ld r4, _LINK(r1)
  306. mtlr r4
  307. cmpwi r3, RESUME_GUEST
  308. bne kvm_exit_heavyweight
  309. REST_2GPRS(3, r1)
  310. addi r1, r1, SWITCH_FRAME_SIZE
  311. b kvm_start_entry
  312. kvm_exit_heavyweight:
  313. addi r1, r1, SWITCH_FRAME_SIZE
  314. #endif
  315. blr