book3s_segment.S 8.9 KB

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