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