swtch32.S 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* $FreeBSD$ */
  2. /* $NetBSD: locore.S,v 1.24 2000/05/31 05:09:17 thorpej Exp $ */
  3. /*-
  4. * Copyright (C) 2001 Benno Rice
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY Benno Rice ``AS IS'' AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  18. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  19. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. /*-
  28. * Copyright (C) 1995, 1996 Wolfgang Solfrank.
  29. * Copyright (C) 1995, 1996 TooLs GmbH.
  30. * All rights reserved.
  31. *
  32. * Redistribution and use in source and binary forms, with or without
  33. * modification, are permitted provided that the following conditions
  34. * are met:
  35. * 1. Redistributions of source code must retain the above copyright
  36. * notice, this list of conditions and the following disclaimer.
  37. * 2. Redistributions in binary form must reproduce the above copyright
  38. * notice, this list of conditions and the following disclaimer in the
  39. * documentation and/or other materials provided with the distribution.
  40. * 3. All advertising materials mentioning features or use of this software
  41. * must display the following acknowledgement:
  42. * This product includes software developed by TooLs GmbH.
  43. * 4. The name of TooLs GmbH may not be used to endorse or promote products
  44. * derived from this software without specific prior written permission.
  45. *
  46. * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
  47. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  48. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  49. * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  50. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  51. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  52. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  53. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  54. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  55. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  56. */
  57. #include "assym.inc"
  58. #include "opt_sched.h"
  59. #include <sys/syscall.h>
  60. #include <machine/trap.h>
  61. #include <machine/param.h>
  62. #include <machine/asm.h>
  63. #include <machine/spr.h>
  64. /*
  65. * void cpu_throw(struct thread *old, struct thread *new)
  66. */
  67. ENTRY(cpu_throw)
  68. mr %r2, %r4
  69. li %r14,0 /* Tell cpu_switchin not to release a thread */
  70. b cpu_switchin
  71. END(cpu_throw)
  72. /*
  73. * void cpu_switch(struct thread *old,
  74. * struct thread *new,
  75. * struct mutex *mtx);
  76. *
  77. * Switch to a new thread saving the current state in the old thread.
  78. */
  79. ENTRY(cpu_switch)
  80. lwz %r6,TD_PCB(%r3) /* Get the old thread's PCB ptr */
  81. stmw %r12,PCB_CONTEXT(%r6) /* Save the non-volatile GP regs.
  82. These can now be used for scratch */
  83. mfcr %r16 /* Save the condition register */
  84. stw %r16,PCB_CR(%r6)
  85. mflr %r16 /* Save the link register */
  86. stw %r16,PCB_LR(%r6)
  87. stw %r1,PCB_SP(%r6) /* Save the stack pointer */
  88. bl 1f
  89. 1:
  90. mflr %r30 /* Prepare for secure-PLT calls */
  91. addis %r30, %r30, (_GLOBAL_OFFSET_TABLE_-1b)@ha
  92. addi %r30, %r30, (_GLOBAL_OFFSET_TABLE_-1b)@l
  93. mr %r14,%r3 /* Copy the old thread ptr... */
  94. mr %r2,%r4 /* and the new thread ptr in curthread */
  95. mr %r16,%r5 /* and the new lock */
  96. mr %r17,%r6 /* and the PCB */
  97. lwz %r18,PCB_FLAGS(%r17)
  98. /* Save FPU context if needed */
  99. andi. %r7, %r18, PCB_FPU
  100. beq .L1
  101. bl save_fpu
  102. .L1:
  103. mr %r3,%r14 /* restore old thread ptr */
  104. /* Save Altivec context if needed */
  105. andi. %r7, %r18, PCB_VEC
  106. beq .L2
  107. bl save_vec
  108. .L2:
  109. #if defined(__SPE__)
  110. mfspr %r3,SPR_SPEFSCR
  111. stw %r3,PCB_VSCR(%r17)
  112. #endif
  113. mr %r3,%r14 /* restore old thread ptr */
  114. bl pmap_deactivate /* Deactivate the current pmap */
  115. sync /* Make sure all of that finished */
  116. cpu_switchin:
  117. #if defined(SMP) && defined(SCHED_ULE)
  118. /* Wait for the new thread to become unblocked */
  119. bl 1f
  120. 1:
  121. mflr %r6
  122. addis %r6,%r6,(_GLOBAL_OFFSET_TABLE_-1b)@ha
  123. addi %r6,%r6,(_GLOBAL_OFFSET_TABLE_-1b)@l
  124. mr %r30, %r6 /* Prepare for secure-PLT calls */
  125. lwz %r6,blocked_lock@got(%r6)
  126. blocked_loop:
  127. lwz %r7,TD_LOCK(%r2)
  128. cmpw %r6,%r7
  129. beq- blocked_loop
  130. isync
  131. #endif
  132. lwz %r17,TD_PCB(%r2) /* Get new current PCB */
  133. lwz %r1,PCB_SP(%r17) /* Load new stack pointer */
  134. /* Release old thread now that we have a stack pointer set up */
  135. cmpwi %r14,0
  136. beq- 1f
  137. stw %r16,TD_LOCK(%r14) /* ULE: update old thread's lock */
  138. 1: mfsprg %r7,0 /* Get the pcpu pointer */
  139. stw %r2,PC_CURTHREAD(%r7) /* Store new current thread */
  140. lwz %r17,TD_PCB(%r2) /* Store new current PCB */
  141. stw %r17,PC_CURPCB(%r7)
  142. mr %r3,%r2 /* Get new thread ptr */
  143. bl pmap_activate /* Activate the new address space */
  144. lwz %r19, PCB_FLAGS(%r17)
  145. /* Restore FPU context if needed */
  146. andi. %r6, %r19, PCB_FPU
  147. beq .L3
  148. mr %r3,%r2 /* Pass curthread to enable_fpu */
  149. bl enable_fpu
  150. .L3:
  151. /* Restore Altivec context if needed */
  152. andi. %r6, %r19, PCB_VEC
  153. beq .L4
  154. mr %r3,%r2 /* Pass curthread to enable_vec */
  155. bl enable_vec
  156. .L4:
  157. #if defined(__SPE__)
  158. lwz %r3,PCB_VSCR(%r17)
  159. mtspr SPR_SPEFSCR,%r3
  160. #endif
  161. /* thread to restore is in r3 */
  162. mr %r3,%r17 /* Recover PCB ptr */
  163. lmw %r12,PCB_CONTEXT(%r3) /* Load the non-volatile GP regs */
  164. lwz %r5,PCB_CR(%r3) /* Load the condition register */
  165. mtcr %r5
  166. lwz %r5,PCB_LR(%r3) /* Load the link register */
  167. mtlr %r5
  168. lwz %r1,PCB_SP(%r3) /* Load the stack pointer */
  169. /*
  170. * Perform a dummy stwcx. to clear any reservations we may have
  171. * inherited from the previous thread. It doesn't matter if the
  172. * stwcx succeeds or not. pcb_context[0] can be clobbered.
  173. */
  174. stwcx. %r1, 0, %r3
  175. blr
  176. END(cpu_switch)
  177. /*
  178. * savectx(pcb)
  179. * Update pcb, saving current processor state
  180. */
  181. ENTRY(savectx)
  182. stmw %r12,PCB_CONTEXT(%r3) /* Save the non-volatile GP regs */
  183. mfcr %r4 /* Save the condition register */
  184. stw %r4,PCB_CR(%r3)
  185. stw %r1,PCB_SP(%r3) /* Save the stack pointer */
  186. mflr %r4 /* Save the link register */
  187. stw %r4,PCB_LR(%r3)
  188. blr
  189. END(savectx)
  190. /*
  191. * fork_trampoline()
  192. * Set up the return from cpu_fork()
  193. */
  194. ENTRY(fork_trampoline)
  195. lwz %r3,CF_FUNC(%r1)
  196. lwz %r4,CF_ARG0(%r1)
  197. lwz %r5,CF_ARG1(%r1)
  198. bl fork_exit
  199. addi %r1,%r1,CF_SIZE-FSP /* Allow 8 bytes in front of
  200. trapframe to simulate FRAME_SETUP
  201. does when allocating space for
  202. a frame pointer/saved LR */
  203. #ifdef __SPE__
  204. li %r3,SPEFSCR_FINVE|SPEFSCR_FDBZE|SPEFSCR_FUNFE|SPEFSCR_FOVFE
  205. mtspr SPR_SPEFSCR, %r3
  206. #endif
  207. b trapexit
  208. END(fork_trampoline)