stack.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * File : stack.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2008 - 2012, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2016Äê9ÔÂ8ÈÕ Urey the first version
  23. */
  24. #include <rtthread.h>
  25. #include "../common/mips.h"
  26. register U32 $GP __asm__ ("$28");
  27. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter, rt_uint8_t *stack_addr, void *texit)
  28. {
  29. static rt_uint32_t wSR=0;
  30. static rt_uint32_t wGP;
  31. mips_reg_ctx *regCtx;
  32. mips_arg_ctx *argCtx;
  33. rt_uint32_t i;
  34. if (wSR == 0)
  35. {
  36. wSR = read_c0_status();
  37. wSR &= 0xfffffffe;
  38. wSR |= 0x0403;
  39. wGP = $GP;
  40. }
  41. if ((rt_uint32_t) stack_addr & 0x7)
  42. {
  43. stack_addr = (rt_uint8_t *)((rt_uint32_t)stack_addr - 4);
  44. }
  45. argCtx = (mips_arg_ctx *)((rt_uint32_t)stack_addr - sizeof(mips_arg_ctx));
  46. regCtx = (mips_reg_ctx *)((rt_uint32_t)stack_addr - sizeof(mips_arg_ctx) - sizeof(mips_reg_ctx));
  47. for (i = 0; i < 4; ++i)
  48. {
  49. argCtx->args[i] = i;
  50. }
  51. //±£´æÍ¨ÓüĴæÆ÷
  52. for (i = 0; i < 32; ++i)
  53. {
  54. regCtx->regs[i] = i;
  55. }
  56. regCtx->regs[REG_SP] = (rt_uint32_t)stack_addr;
  57. regCtx->regs[REG_A0] = (rt_uint32_t)parameter;
  58. regCtx->regs[REG_GP] = (rt_uint32_t)wGP;
  59. regCtx->regs[REG_FP] = (rt_uint32_t)0x0;
  60. regCtx->regs[REG_RA] = (rt_uint32_t)texit;
  61. regCtx->CP0DataLO = 0x00;
  62. regCtx->CP0DataHI = 0x00;
  63. regCtx->CP0Cause = read_c0_cause();
  64. regCtx->CP0Status = wSR;
  65. regCtx->CP0EPC = (rt_uint32_t)tentry;
  66. regCtx->CP0BadVAddr= 0x00;
  67. return (rt_uint8_t *)(regCtx);
  68. }