setjmp.S 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * setjmp and longjmp for MIPS.
  31. */
  32. #include <kern/mips/regdefs.h>
  33. .text
  34. .set noreorder
  35. /*
  36. * int setjmp(jmp_buf jb);
  37. *
  38. * Save the current state so we can return again from the call later
  39. * if/when longjmp is called. (If the function that called setjmp
  40. * returns before longjmp is called, the results are undefined. We
  41. * only need to save registers, not the whole contents of the stack.)
  42. */
  43. .globl setjmp
  44. .type setjmp,@function
  45. .ent setjmp
  46. setjmp:
  47. /*
  48. * jmp_buf is in a0. We need to save s0-s8, sp, and ra in it.
  49. * Don't store more registers without adjusting machine/setjmp.h.
  50. */
  51. sw sp, 0(a0) /* save registers */
  52. sw ra, 4(a0)
  53. sw s0, 8(a0)
  54. sw s1, 12(a0)
  55. sw s2, 16(a0)
  56. sw s3, 20(a0)
  57. sw s4, 24(a0)
  58. sw s5, 28(a0)
  59. sw s6, 32(a0)
  60. sw s7, 36(a0)
  61. sw s8, 40(a0)
  62. j ra /* done */
  63. li v0, 0 /* return 0 (in delay slot) */
  64. .end setjmp
  65. /*
  66. * void longjmp(jmp_buf jb, int code);
  67. */
  68. .globl longjmp
  69. .type longjmp,@function
  70. .ent longjmp
  71. longjmp:
  72. /*
  73. * jmp_buf is in a0. Return code is in a1.
  74. * We need to restore s0-s8, sp, and ra from the jmp_buf.
  75. * The return code is forced to 1 if 0 is passed in.
  76. */
  77. sltiu t0, a1, 1 /* set t0 to 1 if return code is 0... otherwise 0 */
  78. addu a1, a1, t0 /* update the return code */
  79. lw sp, 0(a0) /* restore registers */
  80. lw ra, 4(a0)
  81. lw s0, 8(a0)
  82. lw s1, 12(a0)
  83. lw s2, 16(a0)
  84. lw s3, 20(a0)
  85. lw s4, 24(a0)
  86. lw s5, 28(a0)
  87. lw s6, 32(a0)
  88. lw s7, 36(a0)
  89. lw s8, 40(a0)
  90. j ra /* return, to where setjmp was called from */
  91. move v0, a1 /* set return value */
  92. .end longjmp