book3s_segment.S 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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 2010
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. /* Real mode helpers */
  20. #if defined(CONFIG_PPC_BOOK3S_64)
  21. #define GET_SHADOW_VCPU(reg) \
  22. mr reg, r13
  23. #elif defined(CONFIG_PPC_BOOK3S_32)
  24. #define GET_SHADOW_VCPU(reg) \
  25. tophys(reg, r2); \
  26. lwz reg, (THREAD + THREAD_KVM_SVCPU)(reg); \
  27. tophys(reg, reg)
  28. #endif
  29. /* Disable for nested KVM */
  30. #define USE_QUICK_LAST_INST
  31. /* Get helper functions for subarch specific functionality */
  32. #if defined(CONFIG_PPC_BOOK3S_64)
  33. #include "book3s_64_slb.S"
  34. #elif defined(CONFIG_PPC_BOOK3S_32)
  35. #include "book3s_32_sr.S"
  36. #endif
  37. /******************************************************************************
  38. * *
  39. * Entry code *
  40. * *
  41. *****************************************************************************/
  42. .global kvmppc_handler_trampoline_enter
  43. kvmppc_handler_trampoline_enter:
  44. /* Required state:
  45. *
  46. * MSR = ~IR|DR
  47. * R1 = host R1
  48. * R2 = host R2
  49. * R4 = guest shadow MSR
  50. * R5 = normal host MSR
  51. * R6 = current host MSR (EE, IR, DR off)
  52. * LR = highmem guest exit code
  53. * all other volatile GPRS = free
  54. * SVCPU[CR] = guest CR
  55. * SVCPU[XER] = guest XER
  56. * SVCPU[CTR] = guest CTR
  57. * SVCPU[LR] = guest LR
  58. */
  59. /* r3 = shadow vcpu */
  60. GET_SHADOW_VCPU(r3)
  61. /* Save guest exit handler address and MSR */
  62. mflr r0
  63. PPC_STL r0, HSTATE_VMHANDLER(r3)
  64. PPC_STL r5, HSTATE_HOST_MSR(r3)
  65. /* Save R1/R2 in the PACA (64-bit) or shadow_vcpu (32-bit) */
  66. PPC_STL r1, HSTATE_HOST_R1(r3)
  67. PPC_STL r2, HSTATE_HOST_R2(r3)
  68. /* Activate guest mode, so faults get handled by KVM */
  69. li r11, KVM_GUEST_MODE_GUEST
  70. stb r11, HSTATE_IN_GUEST(r3)
  71. /* Switch to guest segment. This is subarch specific. */
  72. LOAD_GUEST_SEGMENTS
  73. #ifdef CONFIG_PPC_BOOK3S_64
  74. /* Some guests may need to have dcbz set to 32 byte length.
  75. *
  76. * Usually we ensure that by patching the guest's instructions
  77. * to trap on dcbz and emulate it in the hypervisor.
  78. *
  79. * If we can, we should tell the CPU to use 32 byte dcbz though,
  80. * because that's a lot faster.
  81. */
  82. lbz r0, HSTATE_RESTORE_HID5(r3)
  83. cmpwi r0, 0
  84. beq no_dcbz32_on
  85. mfspr r0,SPRN_HID5
  86. ori r0, r0, 0x80 /* XXX HID5_dcbz32 = 0x80 */
  87. mtspr SPRN_HID5,r0
  88. no_dcbz32_on:
  89. #endif /* CONFIG_PPC_BOOK3S_64 */
  90. /* Enter guest */
  91. PPC_LL r8, SVCPU_CTR(r3)
  92. PPC_LL r9, SVCPU_LR(r3)
  93. lwz r10, SVCPU_CR(r3)
  94. lwz r11, SVCPU_XER(r3)
  95. mtctr r8
  96. mtlr r9
  97. mtcr r10
  98. mtxer r11
  99. /* Move SRR0 and SRR1 into the respective regs */
  100. PPC_LL r9, SVCPU_PC(r3)
  101. /* First clear RI in our current MSR value */
  102. li r0, MSR_RI
  103. andc r6, r6, r0
  104. PPC_LL r0, SVCPU_R0(r3)
  105. PPC_LL r1, SVCPU_R1(r3)
  106. PPC_LL r2, SVCPU_R2(r3)
  107. PPC_LL r5, SVCPU_R5(r3)
  108. PPC_LL r7, SVCPU_R7(r3)
  109. PPC_LL r8, SVCPU_R8(r3)
  110. PPC_LL r10, SVCPU_R10(r3)
  111. PPC_LL r11, SVCPU_R11(r3)
  112. PPC_LL r12, SVCPU_R12(r3)
  113. PPC_LL r13, SVCPU_R13(r3)
  114. MTMSR_EERI(r6)
  115. mtsrr0 r9
  116. mtsrr1 r4
  117. PPC_LL r4, SVCPU_R4(r3)
  118. PPC_LL r6, SVCPU_R6(r3)
  119. PPC_LL r9, SVCPU_R9(r3)
  120. PPC_LL r3, (SVCPU_R3)(r3)
  121. RFI
  122. kvmppc_handler_trampoline_enter_end:
  123. /******************************************************************************
  124. * *
  125. * Exit code *
  126. * *
  127. *****************************************************************************/
  128. .global kvmppc_handler_trampoline_exit
  129. kvmppc_handler_trampoline_exit:
  130. .global kvmppc_interrupt
  131. kvmppc_interrupt:
  132. /* Register usage at this point:
  133. *
  134. * SPRG_SCRATCH0 = guest R13
  135. * R12 = exit handler id
  136. * R13 = shadow vcpu (32-bit) or PACA (64-bit)
  137. * HSTATE.SCRATCH0 = guest R12
  138. * HSTATE.SCRATCH1 = guest CR
  139. *
  140. */
  141. /* Save registers */
  142. PPC_STL r0, SVCPU_R0(r13)
  143. PPC_STL r1, SVCPU_R1(r13)
  144. PPC_STL r2, SVCPU_R2(r13)
  145. PPC_STL r3, SVCPU_R3(r13)
  146. PPC_STL r4, SVCPU_R4(r13)
  147. PPC_STL r5, SVCPU_R5(r13)
  148. PPC_STL r6, SVCPU_R6(r13)
  149. PPC_STL r7, SVCPU_R7(r13)
  150. PPC_STL r8, SVCPU_R8(r13)
  151. PPC_STL r9, SVCPU_R9(r13)
  152. PPC_STL r10, SVCPU_R10(r13)
  153. PPC_STL r11, SVCPU_R11(r13)
  154. /* Restore R1/R2 so we can handle faults */
  155. PPC_LL r1, HSTATE_HOST_R1(r13)
  156. PPC_LL r2, HSTATE_HOST_R2(r13)
  157. /* Save guest PC and MSR */
  158. #ifdef CONFIG_PPC64
  159. BEGIN_FTR_SECTION
  160. andi. r0, r12, 0x2
  161. cmpwi cr1, r0, 0
  162. beq 1f
  163. mfspr r3,SPRN_HSRR0
  164. mfspr r4,SPRN_HSRR1
  165. andi. r12,r12,0x3ffd
  166. b 2f
  167. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  168. #endif
  169. 1: mfsrr0 r3
  170. mfsrr1 r4
  171. 2:
  172. PPC_STL r3, SVCPU_PC(r13)
  173. PPC_STL r4, SVCPU_SHADOW_SRR1(r13)
  174. /* Get scratch'ed off registers */
  175. GET_SCRATCH0(r9)
  176. PPC_LL r8, HSTATE_SCRATCH0(r13)
  177. lwz r7, HSTATE_SCRATCH1(r13)
  178. PPC_STL r9, SVCPU_R13(r13)
  179. PPC_STL r8, SVCPU_R12(r13)
  180. stw r7, SVCPU_CR(r13)
  181. /* Save more register state */
  182. mfxer r5
  183. mfdar r6
  184. mfdsisr r7
  185. mfctr r8
  186. mflr r9
  187. stw r5, SVCPU_XER(r13)
  188. PPC_STL r6, SVCPU_FAULT_DAR(r13)
  189. stw r7, SVCPU_FAULT_DSISR(r13)
  190. PPC_STL r8, SVCPU_CTR(r13)
  191. PPC_STL r9, SVCPU_LR(r13)
  192. /*
  193. * In order for us to easily get the last instruction,
  194. * we got the #vmexit at, we exploit the fact that the
  195. * virtual layout is still the same here, so we can just
  196. * ld from the guest's PC address
  197. */
  198. /* We only load the last instruction when it's safe */
  199. cmpwi r12, BOOK3S_INTERRUPT_DATA_STORAGE
  200. beq ld_last_inst
  201. cmpwi r12, BOOK3S_INTERRUPT_PROGRAM
  202. beq ld_last_inst
  203. cmpwi r12, BOOK3S_INTERRUPT_SYSCALL
  204. beq ld_last_prev_inst
  205. cmpwi r12, BOOK3S_INTERRUPT_ALIGNMENT
  206. beq- ld_last_inst
  207. #ifdef CONFIG_PPC64
  208. BEGIN_FTR_SECTION
  209. cmpwi r12, BOOK3S_INTERRUPT_H_EMUL_ASSIST
  210. beq- ld_last_inst
  211. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  212. #endif
  213. b no_ld_last_inst
  214. ld_last_prev_inst:
  215. addi r3, r3, -4
  216. ld_last_inst:
  217. /* Save off the guest instruction we're at */
  218. /* In case lwz faults */
  219. li r0, KVM_INST_FETCH_FAILED
  220. #ifdef USE_QUICK_LAST_INST
  221. /* Set guest mode to 'jump over instruction' so if lwz faults
  222. * we'll just continue at the next IP. */
  223. li r9, KVM_GUEST_MODE_SKIP
  224. stb r9, HSTATE_IN_GUEST(r13)
  225. /* 1) enable paging for data */
  226. mfmsr r9
  227. ori r11, r9, MSR_DR /* Enable paging for data */
  228. mtmsr r11
  229. sync
  230. /* 2) fetch the instruction */
  231. lwz r0, 0(r3)
  232. /* 3) disable paging again */
  233. mtmsr r9
  234. sync
  235. #endif
  236. stw r0, SVCPU_LAST_INST(r13)
  237. no_ld_last_inst:
  238. /* Unset guest mode */
  239. li r9, KVM_GUEST_MODE_NONE
  240. stb r9, HSTATE_IN_GUEST(r13)
  241. /* Switch back to host MMU */
  242. LOAD_HOST_SEGMENTS
  243. #ifdef CONFIG_PPC_BOOK3S_64
  244. lbz r5, HSTATE_RESTORE_HID5(r13)
  245. cmpwi r5, 0
  246. beq no_dcbz32_off
  247. li r4, 0
  248. mfspr r5,SPRN_HID5
  249. rldimi r5,r4,6,56
  250. mtspr SPRN_HID5,r5
  251. no_dcbz32_off:
  252. #endif /* CONFIG_PPC_BOOK3S_64 */
  253. /*
  254. * For some interrupts, we need to call the real Linux
  255. * handler, so it can do work for us. This has to happen
  256. * as if the interrupt arrived from the kernel though,
  257. * so let's fake it here where most state is restored.
  258. *
  259. * Having set up SRR0/1 with the address where we want
  260. * to continue with relocation on (potentially in module
  261. * space), we either just go straight there with rfi[d],
  262. * or we jump to an interrupt handler if there is an
  263. * interrupt to be handled first. In the latter case,
  264. * the rfi[d] at the end of the interrupt handler will
  265. * get us back to where we want to continue.
  266. */
  267. /* Register usage at this point:
  268. *
  269. * R1 = host R1
  270. * R2 = host R2
  271. * R10 = raw exit handler id
  272. * R12 = exit handler id
  273. * R13 = shadow vcpu (32-bit) or PACA (64-bit)
  274. * SVCPU.* = guest *
  275. *
  276. */
  277. PPC_LL r6, HSTATE_HOST_MSR(r13)
  278. PPC_LL r8, HSTATE_VMHANDLER(r13)
  279. #ifdef CONFIG_PPC64
  280. BEGIN_FTR_SECTION
  281. beq cr1, 1f
  282. mtspr SPRN_HSRR1, r6
  283. mtspr SPRN_HSRR0, r8
  284. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  285. #endif
  286. 1: /* Restore host msr -> SRR1 */
  287. mtsrr1 r6
  288. /* Load highmem handler address */
  289. mtsrr0 r8
  290. /* RFI into the highmem handler, or jump to interrupt handler */
  291. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  292. beqa BOOK3S_INTERRUPT_EXTERNAL
  293. cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
  294. beqa BOOK3S_INTERRUPT_DECREMENTER
  295. cmpwi r12, BOOK3S_INTERRUPT_PERFMON
  296. beqa BOOK3S_INTERRUPT_PERFMON
  297. RFI
  298. kvmppc_handler_trampoline_exit_end: