book3s_64_slb.S 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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 2009
  16. *
  17. * Authors: Alexander Graf <agraf@suse.de>
  18. */
  19. #define SHADOW_SLB_ESID(num) (SLBSHADOW_SAVEAREA + (num * 0x10))
  20. #define SHADOW_SLB_VSID(num) (SLBSHADOW_SAVEAREA + (num * 0x10) + 0x8)
  21. #define UNBOLT_SLB_ENTRY(num) \
  22. ld r9, SHADOW_SLB_ESID(num)(r12); \
  23. /* Invalid? Skip. */; \
  24. rldicl. r0, r9, 37, 63; \
  25. beq slb_entry_skip_ ## num; \
  26. xoris r9, r9, SLB_ESID_V@h; \
  27. std r9, SHADOW_SLB_ESID(num)(r12); \
  28. slb_entry_skip_ ## num:
  29. #define REBOLT_SLB_ENTRY(num) \
  30. ld r10, SHADOW_SLB_ESID(num)(r11); \
  31. cmpdi r10, 0; \
  32. beq slb_exit_skip_ ## num; \
  33. oris r10, r10, SLB_ESID_V@h; \
  34. ld r9, SHADOW_SLB_VSID(num)(r11); \
  35. slbmte r9, r10; \
  36. std r10, SHADOW_SLB_ESID(num)(r11); \
  37. slb_exit_skip_ ## num:
  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. * R13 = PACA
  49. * R1 = host R1
  50. * R2 = host R2
  51. * R9 = guest IP
  52. * R10 = guest MSR
  53. * all other GPRS = free
  54. * PACA[KVM_CR] = guest CR
  55. * PACA[KVM_XER] = guest XER
  56. */
  57. mtsrr0 r9
  58. mtsrr1 r10
  59. /* Activate guest mode, so faults get handled by KVM */
  60. li r11, KVM_GUEST_MODE_GUEST
  61. stb r11, PACA_KVM_IN_GUEST(r13)
  62. /* Remove LPAR shadow entries */
  63. #if SLB_NUM_BOLTED == 3
  64. ld r12, PACA_SLBSHADOWPTR(r13)
  65. /* Save off the first entry so we can slbie it later */
  66. ld r10, SHADOW_SLB_ESID(0)(r12)
  67. ld r11, SHADOW_SLB_VSID(0)(r12)
  68. /* Remove bolted entries */
  69. UNBOLT_SLB_ENTRY(0)
  70. UNBOLT_SLB_ENTRY(1)
  71. UNBOLT_SLB_ENTRY(2)
  72. #else
  73. #error unknown number of bolted entries
  74. #endif
  75. /* Flush SLB */
  76. slbia
  77. /* r0 = esid & ESID_MASK */
  78. rldicr r10, r10, 0, 35
  79. /* r0 |= CLASS_BIT(VSID) */
  80. rldic r12, r11, 56 - 36, 36
  81. or r10, r10, r12
  82. slbie r10
  83. isync
  84. /* Fill SLB with our shadow */
  85. lbz r12, PACA_KVM_SLB_MAX(r13)
  86. mulli r12, r12, 16
  87. addi r12, r12, PACA_KVM_SLB
  88. add r12, r12, r13
  89. /* for (r11 = kvm_slb; r11 < kvm_slb + kvm_slb_size; r11+=slb_entry) */
  90. li r11, PACA_KVM_SLB
  91. add r11, r11, r13
  92. slb_loop_enter:
  93. ld r10, 0(r11)
  94. rldicl. r0, r10, 37, 63
  95. beq slb_loop_enter_skip
  96. ld r9, 8(r11)
  97. slbmte r9, r10
  98. slb_loop_enter_skip:
  99. addi r11, r11, 16
  100. cmpd cr0, r11, r12
  101. blt slb_loop_enter
  102. slb_do_enter:
  103. /* Enter guest */
  104. ld r0, (PACA_KVM_R0)(r13)
  105. ld r1, (PACA_KVM_R1)(r13)
  106. ld r2, (PACA_KVM_R2)(r13)
  107. ld r3, (PACA_KVM_R3)(r13)
  108. ld r4, (PACA_KVM_R4)(r13)
  109. ld r5, (PACA_KVM_R5)(r13)
  110. ld r6, (PACA_KVM_R6)(r13)
  111. ld r7, (PACA_KVM_R7)(r13)
  112. ld r8, (PACA_KVM_R8)(r13)
  113. ld r9, (PACA_KVM_R9)(r13)
  114. ld r10, (PACA_KVM_R10)(r13)
  115. ld r12, (PACA_KVM_R12)(r13)
  116. lwz r11, (PACA_KVM_CR)(r13)
  117. mtcr r11
  118. ld r11, (PACA_KVM_XER)(r13)
  119. mtxer r11
  120. ld r11, (PACA_KVM_R11)(r13)
  121. ld r13, (PACA_KVM_R13)(r13)
  122. RFI
  123. kvmppc_handler_trampoline_enter_end:
  124. /******************************************************************************
  125. * *
  126. * Exit code *
  127. * *
  128. *****************************************************************************/
  129. .global kvmppc_handler_trampoline_exit
  130. kvmppc_handler_trampoline_exit:
  131. /* Register usage at this point:
  132. *
  133. * SPRG_SCRATCH0 = guest R13
  134. * R12 = exit handler id
  135. * R13 = PACA
  136. * PACA.KVM.SCRATCH0 = guest R12
  137. * PACA.KVM.SCRATCH1 = guest CR
  138. *
  139. */
  140. /* Save registers */
  141. std r0, PACA_KVM_R0(r13)
  142. std r1, PACA_KVM_R1(r13)
  143. std r2, PACA_KVM_R2(r13)
  144. std r3, PACA_KVM_R3(r13)
  145. std r4, PACA_KVM_R4(r13)
  146. std r5, PACA_KVM_R5(r13)
  147. std r6, PACA_KVM_R6(r13)
  148. std r7, PACA_KVM_R7(r13)
  149. std r8, PACA_KVM_R8(r13)
  150. std r9, PACA_KVM_R9(r13)
  151. std r10, PACA_KVM_R10(r13)
  152. std r11, PACA_KVM_R11(r13)
  153. /* Restore R1/R2 so we can handle faults */
  154. ld r1, PACA_KVM_HOST_R1(r13)
  155. ld r2, PACA_KVM_HOST_R2(r13)
  156. /* Save guest PC and MSR in GPRs */
  157. mfsrr0 r3
  158. mfsrr1 r4
  159. /* Get scratch'ed off registers */
  160. mfspr r9, SPRN_SPRG_SCRATCH0
  161. std r9, PACA_KVM_R13(r13)
  162. ld r8, PACA_KVM_SCRATCH0(r13)
  163. std r8, PACA_KVM_R12(r13)
  164. lwz r7, PACA_KVM_SCRATCH1(r13)
  165. stw r7, PACA_KVM_CR(r13)
  166. /* Save more register state */
  167. mfxer r6
  168. stw r6, PACA_KVM_XER(r13)
  169. mfdar r5
  170. mfdsisr r6
  171. /*
  172. * In order for us to easily get the last instruction,
  173. * we got the #vmexit at, we exploit the fact that the
  174. * virtual layout is still the same here, so we can just
  175. * ld from the guest's PC address
  176. */
  177. /* We only load the last instruction when it's safe */
  178. cmpwi r12, BOOK3S_INTERRUPT_DATA_STORAGE
  179. beq ld_last_inst
  180. cmpwi r12, BOOK3S_INTERRUPT_PROGRAM
  181. beq ld_last_inst
  182. b no_ld_last_inst
  183. ld_last_inst:
  184. /* Save off the guest instruction we're at */
  185. /* Set guest mode to 'jump over instruction' so if lwz faults
  186. * we'll just continue at the next IP. */
  187. li r9, KVM_GUEST_MODE_SKIP
  188. stb r9, PACA_KVM_IN_GUEST(r13)
  189. /* 1) enable paging for data */
  190. mfmsr r9
  191. ori r11, r9, MSR_DR /* Enable paging for data */
  192. mtmsr r11
  193. /* 2) fetch the instruction */
  194. li r0, KVM_INST_FETCH_FAILED /* In case lwz faults */
  195. lwz r0, 0(r3)
  196. /* 3) disable paging again */
  197. mtmsr r9
  198. no_ld_last_inst:
  199. /* Unset guest mode */
  200. li r9, KVM_GUEST_MODE_NONE
  201. stb r9, PACA_KVM_IN_GUEST(r13)
  202. /* Restore bolted entries from the shadow and fix it along the way */
  203. /* We don't store anything in entry 0, so we don't need to take care of it */
  204. slbia
  205. isync
  206. #if SLB_NUM_BOLTED == 3
  207. ld r11, PACA_SLBSHADOWPTR(r13)
  208. REBOLT_SLB_ENTRY(0)
  209. REBOLT_SLB_ENTRY(1)
  210. REBOLT_SLB_ENTRY(2)
  211. #else
  212. #error unknown number of bolted entries
  213. #endif
  214. slb_do_exit:
  215. /* Register usage at this point:
  216. *
  217. * R0 = guest last inst
  218. * R1 = host R1
  219. * R2 = host R2
  220. * R3 = guest PC
  221. * R4 = guest MSR
  222. * R5 = guest DAR
  223. * R6 = guest DSISR
  224. * R12 = exit handler id
  225. * R13 = PACA
  226. * PACA.KVM.* = guest *
  227. *
  228. */
  229. /* RFI into the highmem handler */
  230. mfmsr r7
  231. ori r7, r7, MSR_IR|MSR_DR|MSR_RI /* Enable paging */
  232. mtsrr1 r7
  233. ld r8, PACA_KVM_VMHANDLER(r13) /* Highmem handler address */
  234. mtsrr0 r8
  235. RFI
  236. kvmppc_handler_trampoline_exit_end: