blob: a0e3d4f14309685f48370e6990fd0fcba45c83c5 [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
84 // This variable controls auto garbage collection. If set to 0 then the
85 // GC won't automatically run when gc_alloc can't find enough blocks. But
86 // you can still allocate/free memory and also explicitly call gc_collect.
87 uint16_t gc_auto_collect_enabled;
88
Paul Sokolovsky93e353e2016-07-21 00:37:30 +030089 #if MICROPY_GC_ALLOC_THRESHOLD
90 size_t gc_alloc_amount;
91 size_t gc_alloc_threshold;
92 #endif
93
Damien Georged977d262015-12-16 20:09:11 -050094 size_t gc_last_free_atb_index;
Damien Georgee1e359f2015-02-07 17:24:10 +000095
96 #if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georged977d262015-12-16 20:09:11 -050097 size_t gc_collected;
Damien Georgee1e359f2015-02-07 17:24:10 +000098 #endif
Damien Georgec93d9ca2016-04-25 15:28:57 +000099
David Lechnerccc18f02020-01-22 11:19:37 -0600100 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien Georgec93d9ca2016-04-25 15:28:57 +0000101 // This is a global mutex used to make the GC thread-safe.
102 mp_thread_mutex_t gc_mutex;
103 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000104} mp_state_mem_t;
105
106// This structure hold runtime and VM information. It includes a section
107// which contains root pointers that must be scanned by the GC.
108typedef struct _mp_state_vm_t {
Damien George749b1612018-05-12 22:09:34 +1000109 //
110 // CONTINUE ROOT POINTER SECTION
111 // This must start at the start of this structure and follows
112 // the state in the mp_state_thread_t structure, continuing
113 // the root pointer section from there.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000114 //
115
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000116 qstr_pool_t *last_pool;
117
118 // non-heap memory for creating an exception if we can't allocate RAM
119 mp_obj_exception_t mp_emergency_exception_obj;
120
121 // memory for exception arguments if we can't allocate RAM
122 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
123 #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
Damien Georgeee86de12017-04-10 16:02:56 +1000124 // statically allocated buf (needs to be aligned to mp_obj_t)
125 mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000126 #else
127 // dynamically allocated buf
128 byte *mp_emergency_exception_buf;
129 #endif
130 #endif
131
Damien George7f1da0a2016-12-15 13:00:19 +1100132 #if MICROPY_KBD_EXCEPTION
133 // exception object of type KeyboardInterrupt
134 mp_obj_exception_t mp_kbd_exception;
135 #endif
136
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200137 // dictionary with loaded modules (may be exposed as sys.modules)
138 mp_obj_dict_t mp_loaded_modules_dict;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000139
140 // pending exception object (MP_OBJ_NULL if not pending)
Damien George994ff732015-11-17 14:27:21 +0000141 volatile mp_obj_t mp_pending_exception;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000142
Damien George6e74d242017-02-16 18:05:06 +1100143 #if MICROPY_ENABLE_SCHEDULER
Jim Mussaredbc66fe92019-07-11 11:55:31 +1000144 mp_sched_item_t sched_queue[MICROPY_SCHEDULER_DEPTH];
Damien George6e74d242017-02-16 18:05:06 +1100145 #endif
146
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300147 // current exception being handled, for sys.exc_info()
148 #if MICROPY_PY_SYS_EXC_INFO
Damien George999cedb2015-11-27 17:01:44 +0000149 mp_obj_base_t *cur_exception;
Paul Sokolovsky8b85d142015-04-25 03:17:41 +0300150 #endif
151
Milan Rossacb364702019-08-05 15:06:41 +0200152 #if MICROPY_PY_SYS_ATEXIT
153 // exposed through sys.atexit function
154 mp_obj_t sys_exitfunc;
155 #endif
156
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000157 // dictionary for the __main__ module
158 mp_obj_dict_t dict_main;
159
Damien Georgee1e359f2015-02-07 17:24:10 +0000160 // these two lists must be initialised per port, after the call to mp_init
161 mp_obj_list_t mp_sys_path_obj;
162 mp_obj_list_t mp_sys_argv_obj;
163
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000164 // dictionary for overridden builtins
165 #if MICROPY_CAN_OVERRIDE_BUILTINS
166 mp_obj_dict_t *mp_module_builtins_override_dict;
167 #endif
168
Damien George9883d8e2020-07-27 23:52:38 +1000169 #if MICROPY_PERSISTENT_CODE_TRACK_RELOC_CODE
170 // An mp_obj_list_t that tracks relocated native code to prevent the GC from reclaiming them.
171 mp_obj_t track_reloc_code_list;
172 #endif
173
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000174 // include any root pointers defined by a port
175 MICROPY_PORT_ROOT_POINTERS
176
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300177 // root pointers for extmod
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200178
Damien George9d8347a2018-02-26 16:08:58 +1100179 #if MICROPY_REPL_EVENT_DRIVEN
180 vstr_t *repl_line;
181 #endif
182
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200183 #if MICROPY_PY_OS_DUPTERM
Damien George37282f82017-10-13 20:01:57 +1100184 mp_obj_t dupterm_objs[MICROPY_PY_OS_DUPTERM];
Paul Sokolovsky00ee84e2016-01-01 14:19:26 +0200185 #endif
186
Paul Sokolovskye0d77402015-10-27 00:04:33 +0300187 #if MICROPY_PY_LWIP_SLIP
188 mp_obj_t lwip_slip_stream;
189 #endif
190
Damien Georgedcb9ea72017-01-27 15:10:09 +1100191 #if MICROPY_VFS
Damien George3f6b4e02017-01-27 22:40:15 +1100192 struct _mp_vfs_mount_t *vfs_cur;
193 struct _mp_vfs_mount_t *vfs_mount_table;
Damien Georgedcb9ea72017-01-27 15:10:09 +1100194 #endif
195
Jim Mussared16f8cee2019-07-24 00:49:06 +1000196 #if MICROPY_PY_BLUETOOTH
197 mp_obj_t bluetooth;
198 #endif
199
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000200 //
201 // END ROOT POINTER SECTION
202 ////////////////////////////////////////////////////////////
203
Damien Georgeade9a052015-06-13 21:53:22 +0100204 // pointer and sizes to store interned string data
205 // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
206 byte *qstr_last_chunk;
Damien George25784852015-12-17 12:41:40 +0000207 size_t qstr_last_alloc;
208 size_t qstr_last_used;
Damien Georgeade9a052015-06-13 21:53:22 +0100209
David Lechneredbb73a2020-01-22 11:19:37 -0600210 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien George1f54ad22016-05-26 09:06:46 +0000211 // This is a global mutex used to make qstr interning thread-safe.
212 mp_thread_mutex_t qstr_mutex;
213 #endif
214
Damien George3f420c02018-04-04 14:24:03 +1000215 #if MICROPY_ENABLE_COMPILER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000216 mp_uint_t mp_optimise_value;
Damien Georgeaf20c2e2019-08-23 11:20:50 +1000217 #if MICROPY_EMIT_NATIVE
218 uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx
219 #endif
Damien George3f420c02018-04-04 14:24:03 +1000220 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000221
222 // size of the emergency exception buf, if it's dynamically allocated
223 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
224 mp_int_t mp_emergency_exception_buf_size;
225 #endif
Damien George4cec63a2016-05-26 10:42:53 +0000226
Damien George749b1612018-05-12 22:09:34 +1000227 #if MICROPY_ENABLE_SCHEDULER
228 volatile int16_t sched_state;
Andrew Leech8977c7e2019-03-21 11:52:10 +1100229 uint8_t sched_len;
230 uint8_t sched_idx;
Damien George749b1612018-05-12 22:09:34 +1000231 #endif
232
Damien George4cec63a2016-05-26 10:42:53 +0000233 #if MICROPY_PY_THREAD_GIL
234 // This is a global mutex used to make the VM/runtime thread-safe.
235 mp_thread_mutex_t gil_mutex;
236 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000237} mp_state_vm_t;
238
Damien George330165a2016-04-22 22:44:56 +0000239// This structure holds state that is specific to a given thread.
240// Everything in this structure is scanned for root pointers.
241typedef struct _mp_state_thread_t {
Damien George330165a2016-04-22 22:44:56 +0000242 // Stack top at the start of program
Damien George330165a2016-04-22 22:44:56 +0000243 char *stack_top;
244
245 #if MICROPY_STACK_CHECK
246 size_t stack_limit;
247 #endif
Damien George02d830c2017-11-26 23:28:40 +1100248
249 #if MICROPY_ENABLE_PYSTACK
250 uint8_t *pystack_start;
251 uint8_t *pystack_end;
252 uint8_t *pystack_cur;
253 #endif
Damien George749b1612018-05-12 22:09:34 +1000254
Damien Georgeb6b39bf2021-05-04 23:56:43 +1000255 // Locking of the GC is done per thread.
256 uint16_t gc_lock_depth;
257
Damien George749b1612018-05-12 22:09:34 +1000258 ////////////////////////////////////////////////////////////
259 // START ROOT POINTER SECTION
260 // Everything that needs GC scanning must start here, and
261 // is followed by state in the mp_state_vm_t structure.
262 //
263
264 mp_obj_dict_t *dict_locals;
265 mp_obj_dict_t *dict_globals;
266
267 nlr_buf_t *nlr_top;
Milan Rossa310b3d12019-08-14 16:09:36 +0200268
269 #if MICROPY_PY_SYS_SETTRACE
270 mp_obj_t prof_trace_callback;
271 bool prof_callback_is_executing;
272 struct _mp_code_state_t *current_code_state;
273 #endif
Damien George330165a2016-04-22 22:44:56 +0000274} mp_state_thread_t;
275
Damien George05fe66f2017-02-27 23:56:46 +1100276// This structure combines the above 3 structures.
277// The order of the entries are important for root pointer scanning in the GC to work.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000278typedef struct _mp_state_ctx_t {
Damien George330165a2016-04-22 22:44:56 +0000279 mp_state_thread_t thread;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000280 mp_state_vm_t vm;
281 mp_state_mem_t mem;
282} mp_state_ctx_t;
283
284extern mp_state_ctx_t mp_state_ctx;
285
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000286#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
287#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
288
Damien George27cc0772016-04-22 22:52:33 +0000289#if MICROPY_PY_THREAD
290extern mp_state_thread_t *mp_thread_get_state(void);
291#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
292#else
Damien George330165a2016-04-22 22:44:56 +0000293#define MP_STATE_THREAD(x) (mp_state_ctx.thread.x)
Damien George27cc0772016-04-22 22:52:33 +0000294#endif
Damien George330165a2016-04-22 22:44:56 +0000295
Alexander Steffen299bc622017-06-29 23:14:58 +0200296#endif // MICROPY_INCLUDED_PY_MPSTATE_H