book3s_segment.S 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * R13 = PACA
  48. * R1 = host R1
  49. * R2 = host R2
  50. * R10 = guest MSR
  51. * all other volatile GPRS = free
  52. * SVCPU[CR] = guest CR
  53. * SVCPU[XER] = guest XER
  54. * SVCPU[CTR] = guest CTR
  55. * SVCPU[LR] = guest LR
  56. */
  57. /* r3 = shadow vcpu */
  58. GET_SHADOW_VCPU(r3)
  59. /* Save R1/R2 in the PACA (64-bit) or shadow_vcpu (32-bit) */
  60. PPC_STL r1, HSTATE_HOST_R1(r3)
  61. PPC_STL r2, HSTATE_HOST_R2(r3)
  62. /* Move SRR0 and SRR1 into the respective regs */
  63. PPC_LL r9, SVCPU_PC(r3)
  64. mtsrr0 r9
  65. mtsrr1 r10
  66. /* Activate guest mode, so faults get handled by KVM */
  67. li r11, KVM_GUEST_MODE_GUEST
  68. stb r11, HSTATE_IN_GUEST(r3)
  69. /* Switch to guest segment. This is subarch specific. */
  70. LOAD_GUEST_SEGMENTS
  71. /* Enter guest */
  72. PPC_LL r4, SVCPU_CTR(r3)
  73. PPC_LL r5, SVCPU_LR(r3)
  74. lwz r6, SVCPU_CR(r3)
  75. lwz r7, SVCPU_XER(r3)
  76. mtctr r4
  77. mtlr r5
  78. mtcr r6
  79. mtxer r7
  80. PPC_LL r0, SVCPU_R0(r3)
  81. PPC_LL r1, SVCPU_R1(r3)
  82. PPC_LL r2, SVCPU_R2(r3)
  83. PPC_LL r4, SVCPU_R4(r3)
  84. PPC_LL r5, SVCPU_R5(r3)
  85. PPC_LL r6, SVCPU_R6(r3)
  86. PPC_LL r7, SVCPU_R7(r3)
  87. PPC_LL r8, SVCPU_R8(r3)
  88. PPC_LL r9, SVCPU_R9(r3)
  89. PPC_LL r10, SVCPU_R10(r3)
  90. PPC_LL r11, SVCPU_R11(r3)
  91. PPC_LL r12, SVCPU_R12(r3)
  92. PPC_LL r13, SVCPU_R13(r3)
  93. PPC_LL r3, (SVCPU_R3)(r3)
  94. RFI
  95. kvmppc_handler_trampoline_enter_end:
  96. /******************************************************************************
  97. * *
  98. * Exit code *
  99. * *
  100. *****************************************************************************/
  101. .global kvmppc_handler_trampoline_exit
  102. kvmppc_handler_trampoline_exit:
  103. .global kvmppc_interrupt
  104. kvmppc_interrupt:
  105. /* Register usage at this point:
  106. *
  107. * SPRG_SCRATCH0 = guest R13
  108. * R12 = exit handler id
  109. * R13 = shadow vcpu (32-bit) or PACA (64-bit)
  110. * HSTATE.SCRATCH0 = guest R12
  111. * HSTATE.SCRATCH1 = guest CR
  112. *
  113. */
  114. /* Save registers */
  115. PPC_STL r0, SVCPU_R0(r13)
  116. PPC_STL r1, SVCPU_R1(r13)
  117. PPC_STL r2, SVCPU_R2(r13)
  118. PPC_STL r3, SVCPU_R3(r13)
  119. PPC_STL r4, SVCPU_R4(r13)
  120. PPC_STL r5, SVCPU_R5(r13)
  121. PPC_STL r6, SVCPU_R6(r13)
  122. PPC_STL r7, SVCPU_R7(r13)
  123. PPC_STL r8, SVCPU_R8(r13)
  124. PPC_STL r9, SVCPU_R9(r13)
  125. PPC_STL r10, SVCPU_R10(r13)
  126. PPC_STL r11, SVCPU_R11(r13)
  127. /* Restore R1/R2 so we can handle faults */
  128. PPC_LL r1, HSTATE_HOST_R1(r13)
  129. PPC_LL r2, HSTATE_HOST_R2(r13)
  130. /* Save guest PC and MSR */
  131. #ifdef CONFIG_PPC64
  132. BEGIN_FTR_SECTION
  133. andi. r0,r12,0x2
  134. beq 1f
  135. mfspr r3,SPRN_HSRR0
  136. mfspr r4,SPRN_HSRR1
  137. andi. r12,r12,0x3ffd
  138. b 2f
  139. END_FTR_SECTION_IFSET(CPU_FTR_HVMODE)
  140. #endif
  141. 1: mfsrr0 r3
  142. mfsrr1 r4
  143. 2:
  144. PPC_STL r3, SVCPU_PC(r13)
  145. PPC_STL r4, SVCPU_SHADOW_SRR1(r13)
  146. /* Get scratch'ed off registers */
  147. GET_SCRATCH0(r9)
  148. PPC_LL r8, HSTATE_SCRATCH0(r13)
  149. lwz r7, HSTATE_SCRATCH1(r13)
  150. PPC_STL r9, SVCPU_R13(r13)
  151. PPC_STL r8, SVCPU_R12(r13)
  152. stw r7, SVCPU_CR(r13)
  153. /* Save more register state */
  154. mfxer r5
  155. mfdar r6
  156. mfdsisr r7
  157. mfctr r8
  158. mflr r9
  159. stw r5, SVCPU_XER(r13)
  160. PPC_STL r6, SVCPU_FAULT_DAR(r13)
  161. stw r7, SVCPU_FAULT_DSISR(r13)
  162. PPC_STL r8, SVCPU_CTR(r13)
  163. PPC_STL r9, SVCPU_LR(r13)
  164. /*
  165. * In order for us to easily get the last instruction,
  166. * we got the #vmexit at, we exploit the fact that the
  167. * virtual layout is still the same here, so we can just
  168. * ld from the guest's PC address
  169. */
  170. /* We only load the last instruction when it's safe */
  171. cmpwi r12, BOOK3S_INTERRUPT_DATA_STORAGE
  172. beq ld_last_inst
  173. cmpwi r12, BOOK3S_INTERRUPT_PROGRAM
  174. beq ld_last_inst
  175. cmpwi r12, BOOK3S_INTERRUPT_ALIGNMENT
  176. beq- ld_last_inst
  177. b no_ld_last_inst
  178. ld_last_inst:
  179. /* Save off the guest instruction we're at */
  180. /* In case lwz faults */
  181. li r0, KVM_INST_FETCH_FAILED
  182. #ifdef USE_QUICK_LAST_INST
  183. /* Set guest mode to 'jump over instruction' so if lwz faults
  184. * we'll just continue at the next IP. */
  185. li r9, KVM_GUEST_MODE_SKIP
  186. stb r9, HSTATE_IN_GUEST(r13)
  187. /* 1) enable paging for data */
  188. mfmsr r9
  189. ori r11, r9, MSR_DR /* Enable paging for data */
  190. mtmsr r11
  191. sync
  192. /* 2) fetch the instruction */
  193. lwz r0, 0(r3)
  194. /* 3) disable paging again */
  195. mtmsr r9
  196. sync
  197. #endif
  198. stw r0, SVCPU_LAST_INST(r13)
  199. no_ld_last_inst:
  200. /* Unset guest mode */
  201. li r9, KVM_GUEST_MODE_NONE
  202. stb r9, HSTATE_IN_GUEST(r13)
  203. /* Switch back to host MMU */
  204. LOAD_HOST_SEGMENTS
  205. /* Register usage at this point:
  206. *
  207. * R1 = host R1
  208. * R2 = host R2
  209. * R12 = exit handler id
  210. * R13 = shadow vcpu (32-bit) or PACA (64-bit)
  211. * SVCPU.* = guest *
  212. *
  213. */
  214. /* RFI into the highmem handler */
  215. mfmsr r7
  216. ori r7, r7, MSR_IR|MSR_DR|MSR_RI|MSR_ME /* Enable paging */
  217. mtsrr1 r7
  218. /* Load highmem handler address */
  219. PPC_LL r8, HSTATE_VMHANDLER(r13)
  220. mtsrr0 r8
  221. RFI
  222. kvmppc_handler_trampoline_exit_end: