stack.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2016-9-8 Urey the first version
  9. */
  10. #include <rtthread.h>
  11. #include "../common/mips.h"
  12. register U32 $GP __asm__ ("$28");
  13. rt_uint8_t *rt_hw_stack_init(void *tentry, void *parameter, rt_uint8_t *stack_addr, void *texit)
  14. {
  15. static rt_uint32_t wSR=0;
  16. static rt_uint32_t wGP;
  17. mips_reg_ctx *regCtx;
  18. mips_arg_ctx *argCtx;
  19. rt_uint32_t i;
  20. if (wSR == 0)
  21. {
  22. wSR = read_c0_status();
  23. wSR &= 0xfffffffe;
  24. wSR |= 0x0403;
  25. wGP = $GP;
  26. }
  27. if ((rt_uint32_t) stack_addr & 0x7)
  28. {
  29. stack_addr = (rt_uint8_t *)((rt_uint32_t)stack_addr - 4);
  30. }
  31. argCtx = (mips_arg_ctx *)((rt_uint32_t)stack_addr - sizeof(mips_arg_ctx));
  32. regCtx = (mips_reg_ctx *)((rt_uint32_t)stack_addr - sizeof(mips_arg_ctx) - sizeof(mips_reg_ctx));
  33. for (i = 0; i < 4; ++i)
  34. {
  35. argCtx->args[i] = i;
  36. }
  37. //保存通用寄存器
  38. for (i = 0; i < 32; ++i)
  39. {
  40. regCtx->regs[i] = i;
  41. }
  42. regCtx->regs[REG_SP] = (rt_uint32_t)stack_addr;
  43. regCtx->regs[REG_A0] = (rt_uint32_t)parameter;
  44. regCtx->regs[REG_GP] = (rt_uint32_t)wGP;
  45. regCtx->regs[REG_FP] = (rt_uint32_t)0x0;
  46. regCtx->regs[REG_RA] = (rt_uint32_t)texit;
  47. regCtx->CP0DataLO = 0x00;
  48. regCtx->CP0DataHI = 0x00;
  49. regCtx->CP0Cause = read_c0_cause();
  50. regCtx->CP0Status = wSR;
  51. regCtx->CP0EPC = (rt_uint32_t)tentry;
  52. regCtx->CP0BadVAddr= 0x00;
  53. return (rt_uint8_t *)(regCtx);
  54. }