book3s_segment.S 11 KB

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