book3s_segment.S 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. BEGIN_FTR_SECTION
  75. /* Save host FSCR */
  76. mfspr r8, SPRN_FSCR
  77. std r8, HSTATE_HOST_FSCR(r13)
  78. /* Set FSCR during guest execution */
  79. ld r9, SVCPU_SHADOW_FSCR(r13)
  80. mtspr SPRN_FSCR, r9
  81. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  82. /* Some guests may need to have dcbz set to 32 byte length.
  83. *
  84. * Usually we ensure that by patching the guest's instructions
  85. * to trap on dcbz and emulate it in the hypervisor.
  86. *
  87. * If we can, we should tell the CPU to use 32 byte dcbz though,
  88. * because that's a lot faster.
  89. */
  90. lbz r0, HSTATE_RESTORE_HID5(r3)
  91. cmpwi r0, 0
  92. beq no_dcbz32_on
  93. mfspr r0,SPRN_HID5
  94. ori r0, r0, 0x80 /* XXX HID5_dcbz32 = 0x80 */
  95. mtspr SPRN_HID5,r0
  96. no_dcbz32_on:
  97. #endif /* CONFIG_PPC_BOOK3S_64 */
  98. /* Enter guest */
  99. PPC_LL r8, SVCPU_CTR(r3)
  100. PPC_LL r9, SVCPU_LR(r3)
  101. lwz r10, SVCPU_CR(r3)
  102. PPC_LL r11, SVCPU_XER(r3)
  103. mtctr r8
  104. mtlr r9
  105. mtcr r10
  106. mtxer r11
  107. /* Move SRR0 and SRR1 into the respective regs */
  108. PPC_LL r9, SVCPU_PC(r3)
  109. /* First clear RI in our current MSR value */
  110. li r0, MSR_RI
  111. andc r6, r6, r0
  112. PPC_LL r0, SVCPU_R0(r3)
  113. PPC_LL r1, SVCPU_R1(r3)
  114. PPC_LL r2, SVCPU_R2(r3)
  115. PPC_LL r5, SVCPU_R5(r3)
  116. PPC_LL r7, SVCPU_R7(r3)
  117. PPC_LL r8, SVCPU_R8(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. MTMSR_EERI(r6)
  123. mtsrr0 r9
  124. mtsrr1 r4
  125. PPC_LL r4, SVCPU_R4(r3)
  126. PPC_LL r6, SVCPU_R6(r3)
  127. PPC_LL r9, SVCPU_R9(r3)
  128. PPC_LL r3, (SVCPU_R3)(r3)
  129. RFI_TO_GUEST
  130. kvmppc_handler_trampoline_enter_end:
  131. /******************************************************************************
  132. * *
  133. * Exit code *
  134. * *
  135. *****************************************************************************/
  136. .global kvmppc_handler_trampoline_exit
  137. kvmppc_handler_trampoline_exit:
  138. .global kvmppc_interrupt_pr
  139. kvmppc_interrupt_pr:
  140. /* Register usage at this point:
  141. *
  142. * SPRG_SCRATCH0 = guest R13
  143. * R12 = exit handler id
  144. * R13 = shadow vcpu (32-bit) or PACA (64-bit)
  145. * HSTATE.SCRATCH0 = guest R12
  146. * HSTATE.SCRATCH1 = guest CR
  147. *
  148. */
  149. /* Save registers */
  150. PPC_STL r0, SVCPU_R0(r13)
  151. PPC_STL r1, SVCPU_R1(r13)
  152. PPC_STL r2, SVCPU_R2(r13)
  153. PPC_STL r3, SVCPU_R3(r13)
  154. PPC_STL r4, SVCPU_R4(r13)
  155. PPC_STL r5, SVCPU_R5(r13)
  156. PPC_STL r6, SVCPU_R6(r13)
  157. PPC_STL r7, SVCPU_R7(r13)
  158. PPC_STL r8, SVCPU_R8(r13)
  159. PPC_STL r9, SVCPU_R9(r13)
  160. PPC_STL r10, SVCPU_R10(r13)
  161. PPC_STL r11, SVCPU_R11(r13)
  162. /* Restore R1/R2 so we can handle faults */
  163. PPC_LL r1, HSTATE_HOST_R1(r13)
  164. PPC_LL r2, HSTATE_HOST_R2(r13)
  165. /* Save guest PC and MSR */
  166. #ifdef CONFIG_PPC64
  167. BEGIN_FTR_SECTION
  168. andi. r0, r12, 0x2
  169. cmpwi cr1, r0, 0
  170. beq 1f
  171. mfspr r3,SPRN_HSRR0
  172. mfspr r4,SPRN_HSRR1
  173. andi. r12,r12,0x3ffd
  174. b 2f
  175. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  176. #endif
  177. 1: mfsrr0 r3
  178. mfsrr1 r4
  179. 2:
  180. PPC_STL r3, SVCPU_PC(r13)
  181. PPC_STL r4, SVCPU_SHADOW_SRR1(r13)
  182. /* Get scratch'ed off registers */
  183. GET_SCRATCH0(r9)
  184. PPC_LL r8, HSTATE_SCRATCH0(r13)
  185. lwz r7, HSTATE_SCRATCH1(r13)
  186. PPC_STL r9, SVCPU_R13(r13)
  187. PPC_STL r8, SVCPU_R12(r13)
  188. stw r7, SVCPU_CR(r13)
  189. /* Save more register state */
  190. mfxer r5
  191. mfdar r6
  192. mfdsisr r7
  193. mfctr r8
  194. mflr r9
  195. PPC_STL r5, SVCPU_XER(r13)
  196. PPC_STL r6, SVCPU_FAULT_DAR(r13)
  197. stw r7, SVCPU_FAULT_DSISR(r13)
  198. PPC_STL r8, SVCPU_CTR(r13)
  199. PPC_STL r9, SVCPU_LR(r13)
  200. /*
  201. * In order for us to easily get the last instruction,
  202. * we got the #vmexit at, we exploit the fact that the
  203. * virtual layout is still the same here, so we can just
  204. * ld from the guest's PC address
  205. */
  206. /* We only load the last instruction when it's safe */
  207. cmpwi r12, BOOK3S_INTERRUPT_DATA_STORAGE
  208. beq ld_last_inst
  209. cmpwi r12, BOOK3S_INTERRUPT_PROGRAM
  210. beq ld_last_inst
  211. cmpwi r12, BOOK3S_INTERRUPT_SYSCALL
  212. beq ld_last_prev_inst
  213. cmpwi r12, BOOK3S_INTERRUPT_ALIGNMENT
  214. beq- ld_last_inst
  215. #ifdef CONFIG_PPC64
  216. BEGIN_FTR_SECTION
  217. cmpwi r12, BOOK3S_INTERRUPT_H_EMUL_ASSIST
  218. beq- ld_last_inst
  219. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  220. BEGIN_FTR_SECTION
  221. cmpwi r12, BOOK3S_INTERRUPT_FAC_UNAVAIL
  222. beq- ld_last_inst
  223. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  224. #endif
  225. b no_ld_last_inst
  226. ld_last_prev_inst:
  227. addi r3, r3, -4
  228. ld_last_inst:
  229. /* Save off the guest instruction we're at */
  230. /* In case lwz faults */
  231. li r0, KVM_INST_FETCH_FAILED
  232. #ifdef USE_QUICK_LAST_INST
  233. /* Set guest mode to 'jump over instruction' so if lwz faults
  234. * we'll just continue at the next IP. */
  235. li r9, KVM_GUEST_MODE_SKIP
  236. stb r9, HSTATE_IN_GUEST(r13)
  237. /* 1) enable paging for data */
  238. mfmsr r9
  239. ori r11, r9, MSR_DR /* Enable paging for data */
  240. mtmsr r11
  241. sync
  242. /* 2) fetch the instruction */
  243. lwz r0, 0(r3)
  244. /* 3) disable paging again */
  245. mtmsr r9
  246. sync
  247. #endif
  248. stw r0, SVCPU_LAST_INST(r13)
  249. no_ld_last_inst:
  250. /* Unset guest mode */
  251. li r9, KVM_GUEST_MODE_NONE
  252. stb r9, HSTATE_IN_GUEST(r13)
  253. /* Switch back to host MMU */
  254. LOAD_HOST_SEGMENTS
  255. #ifdef CONFIG_PPC_BOOK3S_64
  256. lbz r5, HSTATE_RESTORE_HID5(r13)
  257. cmpwi r5, 0
  258. beq no_dcbz32_off
  259. li r4, 0
  260. mfspr r5,SPRN_HID5
  261. rldimi r5,r4,6,56
  262. mtspr SPRN_HID5,r5
  263. no_dcbz32_off:
  264. BEGIN_FTR_SECTION
  265. /* Save guest FSCR on a FAC_UNAVAIL interrupt */
  266. cmpwi r12, BOOK3S_INTERRUPT_FAC_UNAVAIL
  267. bne+ no_fscr_save
  268. mfspr r7, SPRN_FSCR
  269. std r7, SVCPU_SHADOW_FSCR(r13)
  270. no_fscr_save:
  271. /* Restore host FSCR */
  272. ld r8, HSTATE_HOST_FSCR(r13)
  273. mtspr SPRN_FSCR, r8
  274. END_FTR_SECTION_IFSET(CPU_FTR_ARCH_207S)
  275. #endif /* CONFIG_PPC_BOOK3S_64 */
  276. /*
  277. * For some interrupts, we need to call the real Linux
  278. * handler, so it can do work for us. This has to happen
  279. * as if the interrupt arrived from the kernel though,
  280. * so let's fake it here where most state is restored.
  281. *
  282. * Having set up SRR0/1 with the address where we want
  283. * to continue with relocation on (potentially in module
  284. * space), we either just go straight there with rfi[d],
  285. * or we jump to an interrupt handler if there is an
  286. * interrupt to be handled first. In the latter case,
  287. * the rfi[d] at the end of the interrupt handler will
  288. * get us back to where we want to continue.
  289. */
  290. /* Register usage at this point:
  291. *
  292. * R1 = host R1
  293. * R2 = host R2
  294. * R10 = raw exit handler id
  295. * R12 = exit handler id
  296. * R13 = shadow vcpu (32-bit) or PACA (64-bit)
  297. * SVCPU.* = guest *
  298. *
  299. */
  300. PPC_LL r6, HSTATE_HOST_MSR(r13)
  301. PPC_LL r8, HSTATE_VMHANDLER(r13)
  302. #ifdef CONFIG_PPC64
  303. BEGIN_FTR_SECTION
  304. beq cr1, 1f
  305. mtspr SPRN_HSRR1, r6
  306. mtspr SPRN_HSRR0, r8
  307. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  308. #endif
  309. 1: /* Restore host msr -> SRR1 */
  310. mtsrr1 r6
  311. /* Load highmem handler address */
  312. mtsrr0 r8
  313. /* RFI into the highmem handler, or jump to interrupt handler */
  314. cmpwi r12, BOOK3S_INTERRUPT_EXTERNAL
  315. beqa BOOK3S_INTERRUPT_EXTERNAL
  316. cmpwi r12, BOOK3S_INTERRUPT_DECREMENTER
  317. beqa BOOK3S_INTERRUPT_DECREMENTER
  318. cmpwi r12, BOOK3S_INTERRUPT_PERFMON
  319. beqa BOOK3S_INTERRUPT_PERFMON
  320. cmpwi r12, BOOK3S_INTERRUPT_DOORBELL
  321. beqa BOOK3S_INTERRUPT_DOORBELL
  322. RFI_TO_KERNEL
  323. kvmppc_handler_trampoline_exit_end: