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 29 #include <stdint.h> 30 31 #include "py/mpconfig.h" 32 #include "py/mpthread.h" 33 #include "py/misc.h" 34 #include "py/nlr.h" 35 #include "py/obj.h" 36 #include "py/objlist.h" 37 #include "py/objexcept.h" 38 39 // This file contains structures defining the state of the MicroPython 40 // memory system, runtime and virtual machine. The state is a global 41 // variable, but in the future it is hoped that the state can become local. 42 43 // This structure contains dynamic configuration for the compiler. 44 #if MICROPY_DYNAMIC_COMPILER 45 typedef struct mp_dynamic_compiler_t { 46 uint8_t small_int_bits; // must be <= host small_int_bits 47 bool opt_cache_map_lookup_in_bytecode; 48 bool py_builtins_str_unicode; 49 uint8_t native_arch; 50 uint8_t nlr_buf_num_regs; 51 } mp_dynamic_compiler_t; 52 extern mp_dynamic_compiler_t mp_dynamic_compiler; 53 #endif 54 55 // These are the values for sched_state 56 #define MP_SCHED_IDLE (1) 57 #define MP_SCHED_LOCKED (-1) 58 #define MP_SCHED_PENDING (0) // 0 so it's a quick check in the VM 59 60 typedef struct _mp_sched_item_t { 61 mp_obj_t func; 62 mp_obj_t arg; 63 } mp_sched_item_t; 64 65 // This structure hold information about the memory allocation system. 66 typedef struct _mp_state_mem_t { 67 #if MICROPY_MEM_STATS 68 size_t total_bytes_allocated; 69 size_t current_bytes_allocated; 70 size_t peak_bytes_allocated; 71 #endif 72 73 byte *gc_alloc_table_start; 74 75 size_t gc_alloc_table_byte_len; 76 #if MICROPY_ENABLE_FINALISER 77 byte *gc_finaliser_table_start; 78 #endif 79 byte *gc_pool_start; 80 byte *gc_pool_end; 81 82 int gc_stack_overflow; 83 MICROPY_GC_STACK_ENTRY_TYPE gc_stack[MICROPY_ALLOC_GC_STACK_SIZE]; 84 uint16_t gc_lock_depth; 85 86 // This variable controls auto garbage collection. If set to 0 then the 87 // GC won't automatically run when gc_alloc can't find enough blocks. But 88 // you can still allocate/free memory and also explicitly call gc_collect. 89 uint16_t gc_auto_collect_enabled; 90 91 #if MICROPY_GC_ALLOC_THRESHOLD 92 size_t gc_alloc_amount; 93 size_t gc_alloc_threshold; 94 #endif 95 96 size_t gc_last_free_atb_index; 97 98 #if MICROPY_PY_GC_COLLECT_RETVAL 99 size_t gc_collected; 100 #endif 101 102 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL 103 // This is a global mutex used to make the GC thread-safe. 104 mp_thread_mutex_t gc_mutex; 105 #endif 106 } mp_state_mem_t; 107 108 // This structure hold runtime and VM information. It includes a section 109 // which contains root pointers that must be scanned by the GC. 110 typedef struct _mp_state_vm_t { 111 // 112 // CONTINUE ROOT POINTER SECTION 113 // This must start at the start of this structure and follows 114 // the state in the mp_state_thread_t structure, continuing 115 // the root pointer section from there. 116 // 117 118 qstr_pool_t *last_pool; 119 120 // non-heap memory for creating an exception if we can't allocate RAM 121 mp_obj_exception_t mp_emergency_exception_obj; 122 123 // memory for exception arguments if we can't allocate RAM 124 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF 125 #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0 126 // statically allocated buf (needs to be aligned to mp_obj_t) 127 mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)]; 128 #else 129 // dynamically allocated buf 130 byte *mp_emergency_exception_buf; 131 #endif 132 #endif 133 134 #if MICROPY_KBD_EXCEPTION 135 // exception object of type KeyboardInterrupt 136 mp_obj_exception_t mp_kbd_exception; 137 #endif 138 139 // dictionary with loaded modules (may be exposed as sys.modules) 140 mp_obj_dict_t mp_loaded_modules_dict; 141 142 #if MICROPY_ENABLE_SCHEDULER 143 mp_sched_item_t sched_queue[MICROPY_SCHEDULER_DEPTH]; 144 #endif 145 146 // current exception being handled, for sys.exc_info() 147 #if MICROPY_PY_SYS_EXC_INFO 148 mp_obj_base_t *cur_exception; 149 #endif 150 151 #if MICROPY_PY_SYS_ATEXIT 152 // exposed through sys.atexit function 153 mp_obj_t sys_exitfunc; 154 #endif 155 156 // dictionary for the __main__ module 157 mp_obj_dict_t dict_main; 158 159 // these two lists must be initialised per port, after the call to mp_init 160 mp_obj_list_t mp_sys_path_obj; 161 mp_obj_list_t mp_sys_argv_obj; 162 163 // dictionary for overridden builtins 164 #if MICROPY_CAN_OVERRIDE_BUILTINS 165 mp_obj_dict_t *mp_module_builtins_override_dict; 166 #endif 167 168 #if MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE 169 // An mp_obj_list_t that tracks relocated native code to prevent the GC from reclaiming them. 170 mp_obj_t track_reloc_code_list; 171 #endif 172 173 // include any root pointers defined by a port 174 MICROPY_PORT_ROOT_POINTERS 175 176 // root pointers for extmod 177 178 #if MICROPY_REPL_EVENT_DRIVEN 179 vstr_t *repl_line; 180 #endif 181 182 #if MICROPY_PY_OS_DUPTERM 183 mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM]; 184 #endif 185 186 #if MICROPY_PY_LWIP_SLIP 187 mp_obj_t lwip_slip_stream; 188 #endif 189 190 #if MICROPY_VFS 191 struct _mp_vfs_mount_t *vfs_cur; 192 struct _mp_vfs_mount_t *vfs_mount_table; 193 #endif 194 195 #if MICROPY_PY_BLUETOOTH 196 mp_obj_t bluetooth; 197 #endif 198 199 // 200 // END ROOT POINTER SECTION 201 //////////////////////////////////////////////////////////// 202 203 // pointer and sizes to store interned string data 204 // (qstr_last_chunk can be root pointer but is also stored in qstr pool) 205 byte *qstr_last_chunk; 206 size_t qstr_last_alloc; 207 size_t qstr_last_used; 208 209 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL 210 // This is a global mutex used to make qstr interning thread-safe. 211 mp_thread_mutex_t qstr_mutex; 212 #endif 213 214 #if MICROPY_ENABLE_COMPILER 215 mp_uint_t mp_optimise_value; 216 #if MICROPY_EMIT_NATIVE 217 uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx 218 #endif 219 #endif 220 221 // size of the emergency exception buf, if it's dynamically allocated 222 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0 223 mp_int_t mp_emergency_exception_buf_size; 224 #endif 225 226 #if MICROPY_ENABLE_SCHEDULER 227 volatile int16_t sched_state; 228 uint8_t sched_len; 229 uint8_t sched_idx; 230 #endif 231 232 #if MICROPY_PY_THREAD_GIL 233 // This is a global mutex used to make the VM/runtime thread-safe. 234 mp_thread_mutex_t gil_mutex; 235 #endif 236 } mp_state_vm_t; 237 238 // This structure holds state that is specific to a given thread. 239 // Everything in this structure is scanned for root pointers. 240 typedef struct _mp_state_thread_t { 241 // Stack top at the start of program 242 char *stack_top; 243 244 #if MICROPY_STACK_CHECK 245 size_t stack_limit; 246 #endif 247 248 #if MICROPY_ENABLE_PYSTACK 249 uint8_t *pystack_start; 250 uint8_t *pystack_end; 251 uint8_t *pystack_cur; 252 #endif 253 254 //////////////////////////////////////////////////////////// 255 // START ROOT POINTER SECTION 256 // Everything that needs GC scanning must start here, and 257 // is followed by state in the mp_state_vm_t structure. 258 // 259 260 mp_obj_dict_t *dict_locals; 261 mp_obj_dict_t *dict_globals; 262 263 nlr_buf_t *nlr_top; 264 265 // pending exception object (MP_OBJ_NULL if not pending) 266 volatile mp_obj_t mp_pending_exception; 267 268 // If MP_OBJ_STOP_ITERATION is propagated then this holds its argument. 269 mp_obj_t stop_iteration_arg; 270 271 #if MICROPY_PY_SYS_SETTRACE 272 mp_obj_t prof_trace_callback; 273 bool prof_callback_is_executing; 274 struct _mp_code_state_t *current_code_state; 275 #endif 276 } mp_state_thread_t; 277 278 // This structure combines the above 3 structures. 279 // The order of the entries are important for root pointer scanning in the GC to work. 280 typedef struct _mp_state_ctx_t { 281 mp_state_thread_t thread; 282 mp_state_vm_t vm; 283 mp_state_mem_t mem; 284 } mp_state_ctx_t; 285 286 extern mp_state_ctx_t mp_state_ctx; 287 288 #define MP_STATE_VM(x) (mp_state_ctx.vm.x) 289 #define MP_STATE_MEM(x) (mp_state_ctx.mem.x) 290 #define MP_STATE_MAIN_THREAD(x) (mp_state_ctx.thread.x) 291 292 #if MICROPY_PY_THREAD 293 extern mp_state_thread_t *mp_thread_get_state(void); 294 #define MP_STATE_THREAD(x) (mp_thread_get_state()->x) 295 #else 296 #define MP_STATE_THREAD(x) (mp_state_ctx.thread.x) 297 #endif 298 299 #endif // MICROPY_INCLUDED_PY_MPSTATE_H 300