crt0.S 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. * crt0.o for MIPS r2000/r3000.
  31. *
  32. * crt stands for "C runtime".
  33. *
  34. * Basically, this is the startup code that gets invoked before main(),
  35. * and regains control when main returns.
  36. *
  37. * All we really do is save a copy of argv for use by the err* and warn*
  38. * functions, and call exit when main returns.
  39. */
  40. #include <kern/mips/regdefs.h>
  41. #include <kern/syscall.h>
  42. .set noreorder /* so we can use delay slots explicitly */
  43. .text
  44. .globl __start
  45. .type __start,@function
  46. .ent __start
  47. __start:
  48. /* Load the "global pointer" register */
  49. la gp, _gp
  50. /*
  51. * We expect that the kernel passes argc in a0 and argv in a1.
  52. * We do not expect the kernel to set up a complete stack frame,
  53. * however.
  54. *
  55. * The MIPS ABI decrees that every caller will leave 16 bytes of
  56. * space in the bottom of its stack frame for writing back the
  57. * values of a0-a3, even when calling functions that take fewer
  58. * than four arguments. It also requires the stack to be aligned
  59. * to an 8-byte boundary. (This is because of 64-bit MIPS, which
  60. * we're not dealing with... but we'll conform to the standard.)
  61. */
  62. li t0, 0xfffffff8 /* mask for stack alignment */
  63. and sp, sp, t0 /* align the stack */
  64. addiu sp, sp, -16 /* create our frame */
  65. sw a1, __argv /* save second arg (argv) in __argv for use later */
  66. jal main /* call main */
  67. nop /* delay slot */
  68. /*
  69. * Now, we have the return value of main in v0.
  70. *
  71. * Move it to s0 (which is callee-save) so we still have
  72. * it in case exit() returns.
  73. *
  74. * Also move it to a0 so it's the argument to exit.
  75. */
  76. move s0, v0 /* save return value */
  77. jal exit /* call exit() */
  78. move a0, s0 /* Set argument (in delay slot) */
  79. /*
  80. * If we got here, something is broken in exit().
  81. * Try using _exit().
  82. */
  83. jal _exit /* Try _exit() */
  84. move a0, s0 /* Set argument (in delay slot) */
  85. /*
  86. * If *that* doesn't work, try doing an _exit syscall by hand.
  87. */
  88. 1:
  89. move a0, s0
  90. li v0, SYS__exit
  91. syscall
  92. /*
  93. * ...and if we still can't exit, there's not much we can do
  94. * but keep trying.
  95. */
  96. j 1b /* loop back */
  97. nop /* delay slot */
  98. .end __start