wasm_thread.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _WASM_RUNTIME_THREAD_H
  6. #define _WASM_RUNTIME_THREAD_H
  7. #include "wasm_assert.h"
  8. #include "wa_thread.h"
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. struct WASMModuleInstance;
  13. struct WASMInterpFrame;
  14. typedef struct WASMStack {
  15. /* The bottom of the stack, must be 8-bytes align. */
  16. uint8 *bottom;
  17. /* Top cell index which is free. */
  18. uint8 *top;
  19. /* The top boundary of the stack. */
  20. uint8 *top_boundary;
  21. } WASMStack;
  22. typedef struct WASMThread {
  23. /* Previous thread's tlr of an instance. */
  24. struct WASMThread *prev;
  25. /* Next thread's tlr of an instance. */
  26. struct WASMThread *next;
  27. /* The WASM module instance of current thread */
  28. struct WASMModuleInstance *module_inst;
  29. /* Current frame of current thread. */
  30. struct WASMInterpFrame *cur_frame;
  31. /* The boundary of native stack. When interpreter detects that native
  32. frame may overrun this boundary, it will throw a stack overflow
  33. exception. */
  34. void *native_stack_boundary;
  35. /* The WASM stack of current thread. */
  36. WASMStack wasm_stack;
  37. /* The native thread handle of current thread. */
  38. korp_tid handle;
  39. /* Current suspend count of this thread. */
  40. uint32 suspend_count;
  41. } WASMThread;
  42. /**
  43. * Allocate a WASM frame from the WASM stack.
  44. *
  45. * @param tlr the current thread
  46. * @param size size of the WASM frame, it must be a multiple of 4
  47. *
  48. * @return the WASM frame if there is enough space in the stack area
  49. * with a protection area, NULL otherwise
  50. */
  51. static inline void*
  52. wasm_thread_alloc_wasm_frame(WASMThread *tlr, unsigned size)
  53. {
  54. uint8 *addr = tlr->wasm_stack.top;
  55. wasm_assert(!(size & 3));
  56. /* The outs area size cannot be larger than the frame size, so
  57. multiplying by 2 is enough. */
  58. if (addr + size * 2 > tlr->wasm_stack.top_boundary) {
  59. /* WASM stack overflow. */
  60. /* When throwing SOE, the preserved space must be enough. */
  61. /*wasm_assert(!tlr->throwing_soe);*/
  62. return NULL;
  63. }
  64. tlr->wasm_stack.top += size;
  65. return addr;
  66. }
  67. static inline void
  68. wasm_thread_free_wasm_frame(WASMThread *tlr, void *prev_top)
  69. {
  70. wasm_assert((uint8 *)prev_top >= tlr->wasm_stack.bottom);
  71. tlr->wasm_stack.top = (uint8 *)prev_top;
  72. }
  73. /**
  74. * Get the current WASM stack top pointer.
  75. *
  76. * @param tlr the current thread
  77. *
  78. * @return the current WASM stack top pointer
  79. */
  80. static inline void*
  81. wasm_thread_wasm_stack_top(WASMThread *tlr)
  82. {
  83. return tlr->wasm_stack.top;
  84. }
  85. /**
  86. * Set the current frame pointer.
  87. *
  88. * @param tlr the current thread
  89. * @param frame the WASM frame to be set for the current thread
  90. */
  91. static inline void
  92. wasm_thread_set_cur_frame(WASMThread *tlr, struct WASMInterpFrame *frame)
  93. {
  94. tlr->cur_frame = frame;
  95. }
  96. /**
  97. * Get the current frame pointer.
  98. *
  99. * @param tlr the current thread
  100. *
  101. * @return the current frame pointer
  102. */
  103. static inline struct WASMInterpFrame*
  104. wasm_thread_get_cur_frame(WASMThread *tlr)
  105. {
  106. return tlr->cur_frame;
  107. }
  108. #ifdef __cplusplus
  109. }
  110. #endif
  111. #endif /* end of _WASM_RUNTIME_THREAD_H */