wasm_thread.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef _WASM_RUNTIME_THREAD_H
  17. #define _WASM_RUNTIME_THREAD_H
  18. #include "wasm_assert.h"
  19. #include "wa_thread.h"
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. struct WASMModuleInstance;
  24. struct WASMInterpFrame;
  25. typedef struct WASMStack {
  26. /* The bottom of the stack, must be 8-bytes align. */
  27. uint8 *bottom;
  28. /* Top cell index which is free. */
  29. uint8 *top;
  30. /* The top boundary of the stack. */
  31. uint8 *top_boundary;
  32. } WASMStack;
  33. typedef struct WASMThread {
  34. /* Previous thread's tlr of an instance. */
  35. struct WASMThread *prev;
  36. /* Next thread's tlr of an instance. */
  37. struct WASMThread *next;
  38. /* The WASM module instance of current thread */
  39. struct WASMModuleInstance *module_inst;
  40. /* Current frame of current thread. */
  41. struct WASMInterpFrame *cur_frame;
  42. /* The boundary of native stack. When interpreter detects that native
  43. frame may overrun this boundary, it will throw a stack overflow
  44. exception. */
  45. void *native_stack_boundary;
  46. /* The WASM stack of current thread. */
  47. WASMStack wasm_stack;
  48. /* The native thread handle of current thread. */
  49. korp_tid handle;
  50. /* Current suspend count of this thread. */
  51. uint32 suspend_count;
  52. } WASMThread;
  53. /**
  54. * Allocate a WASM frame from the WASM stack.
  55. *
  56. * @param tlr the current thread
  57. * @param size size of the WASM frame, it must be a multiple of 4
  58. *
  59. * @return the WASM frame if there is enough space in the stack area
  60. * with a protection area, NULL otherwise
  61. */
  62. static inline void*
  63. wasm_thread_alloc_wasm_frame(WASMThread *tlr, unsigned size)
  64. {
  65. uint8 *addr = tlr->wasm_stack.top;
  66. wasm_assert(!(size & 3));
  67. /* The outs area size cannot be larger than the frame size, so
  68. multiplying by 2 is enough. */
  69. if (addr + size * 2 > tlr->wasm_stack.top_boundary) {
  70. /* WASM stack overflow. */
  71. /* When throwing SOE, the preserved space must be enough. */
  72. /*wasm_assert(!tlr->throwing_soe);*/
  73. return NULL;
  74. }
  75. tlr->wasm_stack.top += size;
  76. return addr;
  77. }
  78. static inline void
  79. wasm_thread_free_wasm_frame(WASMThread *tlr, void *prev_top)
  80. {
  81. wasm_assert((uint8 *)prev_top >= tlr->wasm_stack.bottom);
  82. tlr->wasm_stack.top = (uint8 *)prev_top;
  83. }
  84. /**
  85. * Get the current WASM stack top pointer.
  86. *
  87. * @param tlr the current thread
  88. *
  89. * @return the current WASM stack top pointer
  90. */
  91. static inline void*
  92. wasm_thread_wasm_stack_top(WASMThread *tlr)
  93. {
  94. return tlr->wasm_stack.top;
  95. }
  96. /**
  97. * Set the current frame pointer.
  98. *
  99. * @param tlr the current thread
  100. * @param frame the WASM frame to be set for the current thread
  101. */
  102. static inline void
  103. wasm_thread_set_cur_frame(WASMThread *tlr, struct WASMInterpFrame *frame)
  104. {
  105. tlr->cur_frame = frame;
  106. }
  107. /**
  108. * Get the current frame pointer.
  109. *
  110. * @param tlr the current thread
  111. *
  112. * @return the current frame pointer
  113. */
  114. static inline struct WASMInterpFrame*
  115. wasm_thread_get_cur_frame(WASMThread *tlr)
  116. {
  117. return tlr->cur_frame;
  118. }
  119. #ifdef __cplusplus
  120. }
  121. #endif
  122. #endif /* end of _WASM_RUNTIME_THREAD_H */