book3s_segment.S 11 KB

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