blob: 9bb87f0ba7fb915125edd3f754a5352a605de57b [file] [log] [blame]
Damien Georgeb4b10fd2015-01-01 23:30:53 +00001/*
Alexander Steffen55f33242017-06-30 09:22:17 +02002 * This file is part of the MicroPython project, http://micropython.org/
Damien Georgeb4b10fd2015-01-01 23:30:53 +00003 *
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 */
Alexander Steffen299bc622017-06-29 23:14:58 +020026#ifndef MICROPY_INCLUDED_PY_MPSTATE_H
27#define MICROPY_INCLUDED_PY_MPSTATE_H
Damien Georgeb4b10fd2015-01-01 23:30:53 +000028
29#include <stdint.h>
30
31#include "py/mpconfig.h"
Damien Georgec93d9ca2016-04-25 15:28:57 +000032#include "py/mpthread.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000033#include "py/misc.h"
34#include "py/nlr.h"
35#include "py/obj.h"
Damien Georgee1e359f2015-02-07 17:24:10 +000036#include "py/objlist.h"
Damien Georgeb4b10fd2015-01-01 23:30:53 +000037#include "py/objexcept.h"
38
Alexander Steffen55f33242017-06-30 09:22:17 +020039// This file contains structures defining the state of the MicroPython
Damien Georgeb4b10fd2015-01-01 23:30:53 +000040// 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
Damien Georgeea235202016-02-11 22:30:53 +000043// This structure contains dynamic configuration for the compiler.
44#if MICROPY_DYNAMIC_COMPILER
45typedef 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;
Damien Georged9d92f22019-03-09 10:59:25 +110049 uint8_t native_arch;
Damien Georgeb5966382019-09-18 13:45:20 +100050 uint8_t nlr_buf_num_regs;
Damien Georgeea235202016-02-11 22:30:53 +000051} mp_dynamic_compiler_t;
52extern mp_dynamic_compiler_t mp_dynamic_compiler;
53#endif
54
Damien George6e74d242017-02-16 18:05:06 +110055// 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
60typedef struct _mp_sched_item_t {
61 mp_obj_t func;
62 mp_obj_t arg;
63} mp_sched_item_t;
64
Damien Georgeb4b10fd2015-01-01 23:30:53 +000065// This structure hold information about the memory allocation system.
66typedef 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;
Damien Georged977d262015-12-16 20:09:11 -050074 size_t gc_alloc_table_byte_len;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000075 #if MICROPY_ENABLE_FINALISER
76 byte *gc_finaliser_table_start;
77 #endif
Damien George94fe6e52015-11-27 13:07:48 +000078 byte *gc_pool_start;
79 byte *gc_pool_end;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000080
81 int gc_stack_overflow;
Ayke van Laethem31cf5282018-02-23 18:59:31 +010082 MICROPY_GC_STACK_ENTRY_TYPE gc_stack[MICROPY_ALLOC_GC_STACK_SIZE];
Damien Georgeb4b10fd2015-01-01 23:30:53 +000083 uint16_t gc_lock_depth;
84
85 // This variable controls auto garbage collection. If set to 0 then the
86 // GC won't automatically run when gc_alloc can't find enough blocks. But
87 // you can still allocate/free memory and also explicitly call gc_collect.
88 uint16_t gc_auto_collect_enabled;
89
Paul Sokolovsky93e353e2016-07-21 00:37:30 +030090 #if MICROPY_GC_ALLOC_THRESHOLD
91 size_t gc_alloc_amount;
92 size_t gc_alloc_threshold;
93 #endif
94
Damien Georged977d262015-12-16 20:09:11 -050095 size_t gc_last_free_atb_index;
Damien Georgee1e359f2015-02-07 17:24:10 +000096
97 #if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georged977d262015-12-16 20:09:11 -050098 size_t gc_collected;
Damien Georgee1e359f2015-02-07 17:24:10 +000099 #endif
Damien Georgec93d9ca2016-04-25 15:28:57 +0000100
David Lechnerccc18f02020-01-22 11:19:37 -0600101 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien Georgec93d9ca2016-04-25 15:28:57 +0000102 // This is a global mutex used to make the GC thread-safe.
103 mp_thread_mutex_t gc_mutex;
104 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000105} mp_state_mem_t;
106
107// This structure hold runtime and VM information. It includes a section
108// which contains root pointers that must be scanned by the GC.
109typedef struct _mp_state_vm_t {
Damien George749b1612018-05-12 22:09:34 +1000110 //
111 // CONTINUE ROOT POINTER SECTION
112 // This must start at the start of this structure and follows
113 // the state in the mp_state_thread_t structure, continuing
114 // the root pointer section from there.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000115 //
116
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000117 qstr_pool_t *last_pool;
118
119 // non-heap memory for creating an exception if we can't allocate RAM
120 mp_obj_exception_t mp_emergency_exception_obj;
121
122 // memory for exception arguments if we can't allocate RAM
123 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
124 #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
Damien Georgeee86de12017-04-10 16:02:56 +1000125 // statically allocated buf (needs to be aligned to mp_obj_t)
126 mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000127 #else
128 // dynamically allocated buf
129 byte *mp_emergency_exception_buf;
130 #endif
131 #endif
132
Damien George7f1da0a2016-12-15 13:00:19 +1100133 #if MICROPY_KBD_EXCEPTION
134 // exception object of type KeyboardInterrupt
135 mp_obj_exception_t mp_kbd_exception;
136 #endif
137
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200138 // dictionary with loaded modules (may be exposed as sys.modules)
139 mp_obj_dict_t mp_loaded_modules_dict;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000140
141 // pending exception object (MP_OBJ_NULL if not pending)
Damien George994ff732015-11-17 14:27:21 +0000142 volatile mp_obj_t mp_pending_exception;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000143
Damien George6e74d242017-02-16 18:05:06 +1100144 #if MICROPY_ENABLE_SCHEDULER
Jim Mussaredbc66fe92019-07-11 11:55:31 +1000145 mp_sched_item_t sched_queue[MICROPY_SCHEDULER_DEPTH];
Damien George6e74d242017-02-16 18:05:06 +1100146 #endif
147
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300148 // current exception being handled, for sys.exc_info()
149 #if MICROPY_PY_SYS_EXC_INFO
Damien George999cedb2015-11-27 17:01:44 +0000150 mp_obj_base_t *cur_exception;
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300151 #endif
152
Milan Rossacb364702019-08-05 15:06:41 +0200153 #if MICROPY_PY_SYS_ATEXIT
154 // exposed through sys.atexit function
155 mp_obj_t sys_exitfunc;
156 #endif
157
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000158 // dictionary for the __main__ module
159 mp_obj_dict_t dict_main;
160
Damien Georgee1e359f2015-02-07 17:24:10 +0000161 // these two lists must be initialised per port, after the call to mp_init
162 mp_obj_list_t mp_sys_path_obj;
163 mp_obj_list_t mp_sys_argv_obj;
164
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000165 // dictionary for overridden builtins
166 #if MICROPY_CAN_OVERRIDE_BUILTINS
167 mp_obj_dict_t *mp_module_builtins_override_dict;
168 #endif
169
170 // include any root pointers defined by a port
171 MICROPY_PORT_ROOT_POINTERS
172
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300173 // root pointers for extmod
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200174
Damien George9d8347a2018-02-26 16:08:58 +1100175 #if MICROPY_REPL_EVENT_DRIVEN
176 vstr_t *repl_line;
177 #endif
178
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200179 #if MICROPY_PY_OS_DUPTERM
Damien George37282f82017-10-13 20:01:57 +1100180 mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM];
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200181 #endif
182
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300183 #if MICROPY_PY_LWIP_SLIP
184 mp_obj_t lwip_slip_stream;
185 #endif
186
Damien Georgedcb9ea72017-01-27 15:10:09 +1100187 #if MICROPY_VFS
Damien George3f6b4e02017-01-27 22:40:15 +1100188 struct _mp_vfs_mount_t *vfs_cur;
189 struct _mp_vfs_mount_t *vfs_mount_table;
Damien Georgedcb9ea72017-01-27 15:10:09 +1100190 #endif
191
Jim Mussared16f8cee2019-07-24 00:49:06 +1000192 #if MICROPY_PY_BLUETOOTH
193 mp_obj_t bluetooth;
194 #endif
195
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000196 //
197 // END ROOT POINTER SECTION
198 ////////////////////////////////////////////////////////////
199
Damien Georgeade9a052015-06-13 21:53:22 +0100200 // pointer and sizes to store interned string data
201 // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
202 byte *qstr_last_chunk;
Damien George25784852015-12-17 12:41:40 +0000203 size_t qstr_last_alloc;
204 size_t qstr_last_used;
Damien Georgeade9a052015-06-13 21:53:22 +0100205
Damien George1f54ad22016-05-26 09:06:46 +0000206 #if MICROPY_PY_THREAD
207 // This is a global mutex used to make qstr interning thread-safe.
208 mp_thread_mutex_t qstr_mutex;
209 #endif
210
Damien George3f420c02018-04-04 14:24:03 +1000211 #if MICROPY_ENABLE_COMPILER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000212 mp_uint_t mp_optimise_value;
Damien Georgeaf20c2e2019-08-23 11:20:50 +1000213 #if MICROPY_EMIT_NATIVE
214 uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx
215 #endif
Damien George3f420c02018-04-04 14:24:03 +1000216 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000217
218 // size of the emergency exception buf, if it's dynamically allocated
219 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
220 mp_int_t mp_emergency_exception_buf_size;
221 #endif
Damien George4cec63a2016-05-26 10:42:53 +0000222
Damien George749b1612018-05-12 22:09:34 +1000223 #if MICROPY_ENABLE_SCHEDULER
224 volatile int16_t sched_state;
Andrew Leech8977c7e2019-03-21 11:52:10 +1100225 uint8_t sched_len;
226 uint8_t sched_idx;
Damien George749b1612018-05-12 22:09:34 +1000227 #endif
228
Damien George4cec63a2016-05-26 10:42:53 +0000229 #if MICROPY_PY_THREAD_GIL
230 // This is a global mutex used to make the VM/runtime thread-safe.
231 mp_thread_mutex_t gil_mutex;
232 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000233} mp_state_vm_t;
234
Damien George330165a2016-04-22 22:44:56 +0000235// This structure holds state that is specific to a given thread.
236// Everything in this structure is scanned for root pointers.
237typedef struct _mp_state_thread_t {
Damien George330165a2016-04-22 22:44:56 +0000238 // Stack top at the start of program
Damien George330165a2016-04-22 22:44:56 +0000239 char *stack_top;
240
241 #if MICROPY_STACK_CHECK
242 size_t stack_limit;
243 #endif
Damien George02d830c2017-11-26 23:28:40 +1100244
245 #if MICROPY_ENABLE_PYSTACK
246 uint8_t *pystack_start;
247 uint8_t *pystack_end;
248 uint8_t *pystack_cur;
249 #endif
Damien George749b1612018-05-12 22:09:34 +1000250
251 ////////////////////////////////////////////////////////////
252 // START ROOT POINTER SECTION
253 // Everything that needs GC scanning must start here, and
254 // is followed by state in the mp_state_vm_t structure.
255 //
256
257 mp_obj_dict_t *dict_locals;
258 mp_obj_dict_t *dict_globals;
259
260 nlr_buf_t *nlr_top;
Milan Rossa310b3d12019-08-14 16:09:36 +0200261
262 #if MICROPY_PY_SYS_SETTRACE
263 mp_obj_t prof_trace_callback;
264 bool prof_callback_is_executing;
265 struct _mp_code_state_t *current_code_state;
266 #endif
Damien George330165a2016-04-22 22:44:56 +0000267} mp_state_thread_t;
268
Damien George05fe66f2017-02-27 23:56:46 +1100269// This structure combines the above 3 structures.
270// The order of the entries are important for root pointer scanning in the GC to work.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000271typedef struct _mp_state_ctx_t {
Damien George330165a2016-04-22 22:44:56 +0000272 mp_state_thread_t thread;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000273 mp_state_vm_t vm;
274 mp_state_mem_t mem;
275} mp_state_ctx_t;
276
277extern mp_state_ctx_t mp_state_ctx;
278
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000279#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
280#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
281
Damien George27cc0772016-04-22 22:52:33 +0000282#if MICROPY_PY_THREAD
283extern mp_state_thread_t *mp_thread_get_state(void);
284#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
285#else
Damien George330165a2016-04-22 22:44:56 +0000286#define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
Damien George27cc0772016-04-22 22:52:33 +0000287#endif
Damien George330165a2016-04-22 22:44:56 +0000288
Alexander Steffen299bc622017-06-29 23:14:58 +0200289#endif // MICROPY_INCLUDED_PY_MPSTATE_H