book3s_64_slb.S 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. /* Remove LPAR shadow entries */
  60. #if SLB_NUM_BOLTED == 3
  61. ld r12, PACA_SLBSHADOWPTR(r13)
  62. /* Save off the first entry so we can slbie it later */
  63. ld r10, SHADOW_SLB_ESID(0)(r12)
  64. ld r11, SHADOW_SLB_VSID(0)(r12)
  65. /* Remove bolted entries */
  66. UNBOLT_SLB_ENTRY(0)
  67. UNBOLT_SLB_ENTRY(1)
  68. UNBOLT_SLB_ENTRY(2)
  69. #else
  70. #error unknown number of bolted entries
  71. #endif
  72. /* Flush SLB */
  73. slbia
  74. /* r0 = esid & ESID_MASK */
  75. rldicr r10, r10, 0, 35
  76. /* r0 |= CLASS_BIT(VSID) */
  77. rldic r12, r11, 56 - 36, 36
  78. or r10, r10, r12
  79. slbie r10
  80. isync
  81. /* Fill SLB with our shadow */
  82. lbz r12, PACA_KVM_SLB_MAX(r13)
  83. mulli r12, r12, 16
  84. addi r12, r12, PACA_KVM_SLB
  85. add r12, r12, r13
  86. /* for (r11 = kvm_slb; r11 < kvm_slb + kvm_slb_size; r11+=slb_entry) */
  87. li r11, PACA_KVM_SLB
  88. add r11, r11, r13
  89. slb_loop_enter:
  90. ld r10, 0(r11)
  91. rldicl. r0, r10, 37, 63
  92. beq slb_loop_enter_skip
  93. ld r9, 8(r11)
  94. slbmte r9, r10
  95. slb_loop_enter_skip:
  96. addi r11, r11, 16
  97. cmpd cr0, r11, r12
  98. blt slb_loop_enter
  99. slb_do_enter:
  100. /* Enter guest */
  101. ld r0, (PACA_KVM_R0)(r13)
  102. ld r1, (PACA_KVM_R1)(r13)
  103. ld r2, (PACA_KVM_R2)(r13)
  104. ld r3, (PACA_KVM_R3)(r13)
  105. ld r4, (PACA_KVM_R4)(r13)
  106. ld r5, (PACA_KVM_R5)(r13)
  107. ld r6, (PACA_KVM_R6)(r13)
  108. ld r7, (PACA_KVM_R7)(r13)
  109. ld r8, (PACA_KVM_R8)(r13)
  110. ld r9, (PACA_KVM_R9)(r13)
  111. ld r10, (PACA_KVM_R10)(r13)
  112. ld r12, (PACA_KVM_R12)(r13)
  113. lwz r11, (PACA_KVM_CR)(r13)
  114. mtcr r11
  115. ld r11, (PACA_KVM_XER)(r13)
  116. mtxer r11
  117. ld r11, (PACA_KVM_R11)(r13)
  118. ld r13, (PACA_KVM_R13)(r13)
  119. RFI
  120. kvmppc_handler_trampoline_enter_end:
  121. /******************************************************************************
  122. * *
  123. * Exit code *
  124. * *
  125. *****************************************************************************/
  126. .global kvmppc_handler_trampoline_exit
  127. kvmppc_handler_trampoline_exit:
  128. /* Register usage at this point:
  129. *
  130. * SPRG_SCRATCH0 = guest R13
  131. * R12 = exit handler id
  132. * R13 = PACA
  133. * PACA.KVM.SCRATCH0 = guest R12
  134. * PACA.KVM.SCRATCH1 = guest CR
  135. *
  136. */
  137. /* Save registers */
  138. std r0, PACA_KVM_R0(r13)
  139. std r1, PACA_KVM_R1(r13)
  140. std r2, PACA_KVM_R2(r13)
  141. std r3, PACA_KVM_R3(r13)
  142. std r4, PACA_KVM_R4(r13)
  143. std r5, PACA_KVM_R5(r13)
  144. std r6, PACA_KVM_R6(r13)
  145. std r7, PACA_KVM_R7(r13)
  146. std r8, PACA_KVM_R8(r13)
  147. std r9, PACA_KVM_R9(r13)
  148. std r10, PACA_KVM_R10(r13)
  149. std r11, PACA_KVM_R11(r13)
  150. /* Restore R1/R2 so we can handle faults */
  151. ld r1, PACA_KVM_HOST_R1(r13)
  152. ld r2, PACA_KVM_HOST_R2(r13)
  153. /* Save guest PC and MSR in GPRs */
  154. mfsrr0 r3
  155. mfsrr1 r4
  156. /* Get scratch'ed off registers */
  157. mfspr r9, SPRN_SPRG_SCRATCH0
  158. std r9, PACA_KVM_R13(r13)
  159. ld r8, PACA_KVM_SCRATCH0(r13)
  160. std r8, PACA_KVM_R12(r13)
  161. lwz r7, PACA_KVM_SCRATCH1(r13)
  162. stw r7, PACA_KVM_CR(r13)
  163. /* Save more register state */
  164. mfxer r6
  165. stw r6, PACA_KVM_XER(r13)
  166. mfdar r5
  167. mfdsisr r6
  168. /* Unset guest state */
  169. li r9, 0
  170. stb r9, PACA_KVM_IN_GUEST(r13)
  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. /* 1) enable paging for data */
  186. mfmsr r9
  187. ori r11, r9, MSR_DR /* Enable paging for data */
  188. mtmsr r11
  189. /* 2) fetch the instruction */
  190. /* XXX implement PACA_KVM_IN_GUEST=2 path to safely jump over this */
  191. lwz r0, 0(r3)
  192. /* 3) disable paging again */
  193. mtmsr r9
  194. no_ld_last_inst:
  195. /* Restore bolted entries from the shadow and fix it along the way */
  196. /* We don't store anything in entry 0, so we don't need to take care of it */
  197. slbia
  198. isync
  199. #if SLB_NUM_BOLTED == 3
  200. ld r11, PACA_SLBSHADOWPTR(r13)
  201. REBOLT_SLB_ENTRY(0)
  202. REBOLT_SLB_ENTRY(1)
  203. REBOLT_SLB_ENTRY(2)
  204. #else
  205. #error unknown number of bolted entries
  206. #endif
  207. slb_do_exit:
  208. /* Register usage at this point:
  209. *
  210. * R0 = guest last inst
  211. * R1 = host R1
  212. * R2 = host R2
  213. * R3 = guest PC
  214. * R4 = guest MSR
  215. * R5 = guest DAR
  216. * R6 = guest DSISR
  217. * R12 = exit handler id
  218. * R13 = PACA
  219. * PACA.KVM.* = guest *
  220. *
  221. */
  222. /* RFI into the highmem handler */
  223. mfmsr r7
  224. ori r7, r7, MSR_IR|MSR_DR|MSR_RI /* Enable paging */
  225. mtsrr1 r7
  226. ld r8, PACA_KVM_VMHANDLER(r13) /* Highmem handler address */
  227. mtsrr0 r8
  228. RFI
  229. kvmppc_handler_trampoline_exit_end: