cpuport.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. * 2018/10/28 Bernard The unify RISC-V porting code.
  9. * 2021-02-11 lizhirui add gp support
  10. */
  11. #include <rthw.h>
  12. #include <rtthread.h>
  13. #include "cpuport.h"
  14. #include "stack.h"
  15. /**
  16. * @brief from thread used interrupt context switch
  17. *
  18. */
  19. volatile rt_ubase_t rt_interrupt_from_thread = 0;
  20. /**
  21. * @brief to thread used interrupt context switch
  22. *
  23. */
  24. volatile rt_ubase_t rt_interrupt_to_thread = 0;
  25. /**
  26. * @brief flag to indicate context switch in interrupt or not
  27. *
  28. */
  29. volatile rt_ubase_t rt_thread_switch_interrupt_flag = 0;
  30. /**
  31. * This function will initialize thread stack
  32. *
  33. * @param tentry the entry of thread
  34. * @param parameter the parameter of entry
  35. * @param stack_addr the beginning stack address
  36. * @param texit the function will be called when thread exit
  37. *
  38. * @return stack address
  39. */
  40. rt_uint8_t *rt_hw_stack_init(void *tentry,
  41. void *parameter,
  42. rt_uint8_t *stack_addr,
  43. void *texit)
  44. {
  45. struct rt_hw_stack_frame *frame;
  46. rt_uint8_t *stk;
  47. int i;
  48. extern int __global_pointer$;
  49. stk = stack_addr + sizeof(rt_ubase_t);
  50. stk = (rt_uint8_t *)RT_ALIGN_DOWN((rt_ubase_t)stk, REGBYTES);
  51. stk -= sizeof(struct rt_hw_stack_frame);
  52. frame = (struct rt_hw_stack_frame *)stk;
  53. for (i = 0; i < sizeof(struct rt_hw_stack_frame) / sizeof(rt_ubase_t); i++)
  54. {
  55. ((rt_ubase_t *)frame)[i] = 0xdeadbeef;
  56. }
  57. frame->ra = (rt_ubase_t)texit;
  58. frame->gp = (rt_ubase_t)&__global_pointer$;
  59. frame->a0 = (rt_ubase_t)parameter;
  60. frame->epc = (rt_ubase_t)tentry;
  61. frame->user_sp_exc_stack = (rt_ubase_t)(((rt_ubase_t)stk) + sizeof(struct rt_hw_stack_frame));
  62. #ifndef RISCV_S_MODE
  63. frame->xstatus = 0x00007880;
  64. #else
  65. frame->xstatus = 0x00040120;
  66. #endif
  67. return stk;
  68. }
  69. /*
  70. * #ifdef RT_USING_SMP
  71. * void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  72. * #else
  73. * void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to);
  74. * #endif
  75. */
  76. #ifndef RT_USING_SMP
  77. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to)
  78. {
  79. if (rt_thread_switch_interrupt_flag == 0)
  80. rt_interrupt_from_thread = from;
  81. rt_interrupt_to_thread = to;
  82. rt_thread_switch_interrupt_flag = 1;
  83. return ;
  84. }
  85. #endif /* end of RT_USING_SMP */
  86. /** shutdown CPU */
  87. void rt_hw_cpu_shutdown()
  88. {
  89. rt_uint32_t level;
  90. rt_kprintf("shutdown...\n");
  91. level = rt_hw_interrupt_disable();
  92. while (level)
  93. {
  94. RT_ASSERT(0);
  95. }
  96. }
  97. rt_thread_t rt_thread_sp_to_thread(void *spmember_addr)
  98. {
  99. return (rt_thread_t)(((rt_ubase_t)spmember_addr) - (offsetof(struct rt_thread,sp)));
  100. }
  101. void *get_thread_kernel_stack_top(rt_thread_t thread)
  102. {
  103. return (void *)(((rt_size_t)thread -> stack_addr) + ((rt_size_t)thread -> stack_size));
  104. }