mpstate.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /*
  2. * This file is part of the MicroPython project, http://micropython.org/
  3. *
  4. * The MIT License (MIT)
  5. *
  6. * Copyright (c) 2014 Damien P. George
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. */
  26. #ifndef MICROPY_INCLUDED_PY_MPSTATE_H
  27. #define MICROPY_INCLUDED_PY_MPSTATE_H
  28. #include <stdint.h>
  29. #include "py/mpconfig.h"
  30. #include "py/mpthread.h"
  31. #include "py/misc.h"
  32. #include "py/nlr.h"
  33. #include "py/obj.h"
  34. #include "py/objlist.h"
  35. #include "py/objexcept.h"
  36. // This file contains structures defining the state of the MicroPython
  37. // memory system, runtime and virtual machine. The state is a global
  38. // variable, but in the future it is hoped that the state can become local.
  39. // This structure contains dynamic configuration for the compiler.
  40. #if MICROPY_DYNAMIC_COMPILER
  41. typedef struct mp_dynamic_compiler_t {
  42. uint8_t small_int_bits; // must be <= host small_int_bits
  43. bool opt_cache_map_lookup_in_bytecode;
  44. bool py_builtins_str_unicode;
  45. uint8_t native_arch;
  46. uint8_t nlr_buf_num_regs;
  47. } mp_dynamic_compiler_t;
  48. extern mp_dynamic_compiler_t mp_dynamic_compiler;
  49. #endif
  50. // These are the values for sched_state
  51. #define MP_SCHED_IDLE (1)
  52. #define MP_SCHED_LOCKED (-1)
  53. #define MP_SCHED_PENDING (0) // 0 so it's a quick check in the VM
  54. typedef struct _mp_sched_item_t {
  55. mp_obj_t func;
  56. mp_obj_t arg;
  57. } mp_sched_item_t;
  58. // This structure hold information about the memory allocation system.
  59. typedef struct _mp_state_mem_t {
  60. #if MICROPY_MEM_STATS
  61. size_t total_bytes_allocated;
  62. size_t current_bytes_allocated;
  63. size_t peak_bytes_allocated;
  64. #endif
  65. byte *gc_alloc_table_start;
  66. size_t gc_alloc_table_byte_len;
  67. #if MICROPY_ENABLE_FINALISER
  68. byte *gc_finaliser_table_start;
  69. #endif
  70. byte *gc_pool_start;
  71. byte *gc_pool_end;
  72. int gc_stack_overflow;
  73. MICROPY_GC_STACK_ENTRY_TYPE gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
  74. uint16_t gc_lock_depth;
  75. // This variable controls auto garbage collection. If set to 0 then the
  76. // GC won't automatically run when gc_alloc can't find enough blocks. But
  77. // you can still allocate/free memory and also explicitly call gc_collect.
  78. uint16_t gc_auto_collect_enabled;
  79. #if MICROPY_GC_ALLOC_THRESHOLD
  80. size_t gc_alloc_amount;
  81. size_t gc_alloc_threshold;
  82. #endif
  83. size_t gc_last_free_atb_index;
  84. #if MICROPY_PY_GC_COLLECT_RETVAL
  85. size_t gc_collected;
  86. #endif
  87. #if MICROPY_PY_THREAD
  88. // This is a global mutex used to make the GC thread-safe.
  89. mp_thread_mutex_t gc_mutex;
  90. #endif
  91. } mp_state_mem_t;
  92. // This structure hold runtime and VM information. It includes a section
  93. // which contains root pointers that must be scanned by the GC.
  94. typedef struct _mp_state_vm_t {
  95. //
  96. // CONTINUE ROOT POINTER SECTION
  97. // This must start at the start of this structure and follows
  98. // the state in the mp_state_thread_t structure, continuing
  99. // the root pointer section from there.
  100. //
  101. qstr_pool_t *last_pool;
  102. // non-heap memory for creating an exception if we can't allocate RAM
  103. mp_obj_exception_t mp_emergency_exception_obj;
  104. // memory for exception arguments if we can't allocate RAM
  105. #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
  106. #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
  107. // statically allocated buf (needs to be aligned to mp_obj_t)
  108. mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
  109. #else
  110. // dynamically allocated buf
  111. byte *mp_emergency_exception_buf;
  112. #endif
  113. #endif
  114. #if MICROPY_KBD_EXCEPTION
  115. // exception object of type KeyboardInterrupt
  116. mp_obj_exception_t mp_kbd_exception;
  117. #endif
  118. // dictionary with loaded modules (may be exposed as sys.modules)
  119. mp_obj_dict_t mp_loaded_modules_dict;
  120. // pending exception object (MP_OBJ_NULL if not pending)
  121. volatile mp_obj_t mp_pending_exception;
  122. #if MICROPY_ENABLE_SCHEDULER
  123. mp_sched_item_t sched_queue[MICROPY_SCHEDULER_DEPTH];
  124. #endif
  125. // current exception being handled, for sys.exc_info()
  126. #if MICROPY_PY_SYS_EXC_INFO
  127. mp_obj_base_t *cur_exception;
  128. #endif
  129. #if MICROPY_PY_SYS_ATEXIT
  130. // exposed through sys.atexit function
  131. mp_obj_t sys_exitfunc;
  132. #endif
  133. // dictionary for the __main__ module
  134. mp_obj_dict_t dict_main;
  135. // these two lists must be initialised per port, after the call to mp_init
  136. mp_obj_list_t mp_sys_path_obj;
  137. mp_obj_list_t mp_sys_argv_obj;
  138. // dictionary for overridden builtins
  139. #if MICROPY_CAN_OVERRIDE_BUILTINS
  140. mp_obj_dict_t *mp_module_builtins_override_dict;
  141. #endif
  142. // include any root pointers defined by a port
  143. MICROPY_PORT_ROOT_POINTERS
  144. // root pointers for extmod
  145. #if MICROPY_REPL_EVENT_DRIVEN
  146. vstr_t *repl_line;
  147. #endif
  148. #if MICROPY_PY_OS_DUPTERM
  149. mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM];
  150. #endif
  151. #if MICROPY_PY_LWIP_SLIP
  152. mp_obj_t lwip_slip_stream;
  153. #endif
  154. #if MICROPY_VFS
  155. struct _mp_vfs_mount_t *vfs_cur;
  156. struct _mp_vfs_mount_t *vfs_mount_table;
  157. #endif
  158. #if MICROPY_PY_BLUETOOTH
  159. mp_obj_t bluetooth;
  160. #endif
  161. //
  162. // END ROOT POINTER SECTION
  163. ////////////////////////////////////////////////////////////
  164. // pointer and sizes to store interned string data
  165. // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
  166. byte *qstr_last_chunk;
  167. size_t qstr_last_alloc;
  168. size_t qstr_last_used;
  169. #if MICROPY_PY_THREAD
  170. // This is a global mutex used to make qstr interning thread-safe.
  171. mp_thread_mutex_t qstr_mutex;
  172. #endif
  173. #if MICROPY_ENABLE_COMPILER
  174. mp_uint_t mp_optimise_value;
  175. #if MICROPY_EMIT_NATIVE
  176. uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx
  177. #endif
  178. #endif
  179. // size of the emergency exception buf, if it's dynamically allocated
  180. #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
  181. mp_int_t mp_emergency_exception_buf_size;
  182. #endif
  183. #if MICROPY_ENABLE_SCHEDULER
  184. volatile int16_t sched_state;
  185. uint8_t sched_len;
  186. uint8_t sched_idx;
  187. #endif
  188. #if MICROPY_PY_THREAD_GIL
  189. // This is a global mutex used to make the VM/runtime thread-safe.
  190. mp_thread_mutex_t gil_mutex;
  191. #endif
  192. } mp_state_vm_t;
  193. // This structure holds state that is specific to a given thread.
  194. // Everything in this structure is scanned for root pointers.
  195. typedef struct _mp_state_thread_t {
  196. // Stack top at the start of program
  197. char *stack_top;
  198. #if MICROPY_STACK_CHECK
  199. size_t stack_limit;
  200. #endif
  201. #if MICROPY_ENABLE_PYSTACK
  202. uint8_t *pystack_start;
  203. uint8_t *pystack_end;
  204. uint8_t *pystack_cur;
  205. #endif
  206. ////////////////////////////////////////////////////////////
  207. // START ROOT POINTER SECTION
  208. // Everything that needs GC scanning must start here, and
  209. // is followed by state in the mp_state_vm_t structure.
  210. //
  211. mp_obj_dict_t *dict_locals;
  212. mp_obj_dict_t *dict_globals;
  213. nlr_buf_t *nlr_top;
  214. #if MICROPY_PY_SYS_SETTRACE
  215. mp_obj_t prof_trace_callback;
  216. bool prof_callback_is_executing;
  217. struct _mp_code_state_t *current_code_state;
  218. #endif
  219. } mp_state_thread_t;
  220. // This structure combines the above 3 structures.
  221. // The order of the entries are important for root pointer scanning in the GC to work.
  222. typedef struct _mp_state_ctx_t {
  223. mp_state_thread_t thread;
  224. mp_state_vm_t vm;
  225. mp_state_mem_t mem;
  226. } mp_state_ctx_t;
  227. extern mp_state_ctx_t mp_state_ctx;
  228. #define MP_STATE_VM(x) (mp_state_ctx.vm.x)
  229. #define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
  230. #if MICROPY_PY_THREAD
  231. extern mp_state_thread_t *mp_thread_get_state(void);
  232. #define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
  233. #else
  234. #define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
  235. #endif
  236. #endif // MICROPY_INCLUDED_PY_MPSTATE_H