blob: 080dc1380bcbf5d71a37b9791e57a8e428ce05d5 [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
Jim Mussared7d2ee8a2023-06-05 22:38:36 +100043#if MICROPY_PY_SYS_ATTR_DELEGATION
44// Must be kept in sync with sys_mutable_keys in modsys.c.
Damien Georgebc181552021-07-27 00:39:04 +100045enum {
Jim Mussared5e509752023-06-05 16:52:29 +100046 #if MICROPY_PY_SYS_PATH
47 MP_SYS_MUTABLE_PATH,
48 #endif
Damien Georgeac229312021-07-27 00:43:35 +100049 #if MICROPY_PY_SYS_PS1_PS2
50 MP_SYS_MUTABLE_PS1,
51 MP_SYS_MUTABLE_PS2,
52 #endif
Damien Georgecac939d2021-07-27 00:41:27 +100053 #if MICROPY_PY_SYS_TRACEBACKLIMIT
54 MP_SYS_MUTABLE_TRACEBACKLIMIT,
55 #endif
Damien Georgebc181552021-07-27 00:39:04 +100056 MP_SYS_MUTABLE_NUM,
57};
Jim Mussared7d2ee8a2023-06-05 22:38:36 +100058#endif // MICROPY_PY_SYS_ATTR_DELEGATION
Damien Georgebc181552021-07-27 00:39:04 +100059
Damien Georgeea235202016-02-11 22:30:53 +000060// This structure contains dynamic configuration for the compiler.
61#if MICROPY_DYNAMIC_COMPILER
62typedef struct mp_dynamic_compiler_t {
63 uint8_t small_int_bits; // must be <= host small_int_bits
Damien Georged9d92f22019-03-09 10:59:25 +110064 uint8_t native_arch;
Damien Georgeb5966382019-09-18 13:45:20 +100065 uint8_t nlr_buf_num_regs;
Damien Georgeea235202016-02-11 22:30:53 +000066} mp_dynamic_compiler_t;
67extern mp_dynamic_compiler_t mp_dynamic_compiler;
68#endif
69
Damien George6e74d242017-02-16 18:05:06 +110070// These are the values for sched_state
71#define MP_SCHED_IDLE (1)
72#define MP_SCHED_LOCKED (-1)
73#define MP_SCHED_PENDING (0) // 0 so it's a quick check in the VM
74
75typedef struct _mp_sched_item_t {
76 mp_obj_t func;
77 mp_obj_t arg;
78} mp_sched_item_t;
79
Ayke van Laethembcc827d2018-01-24 02:09:58 +010080// This structure holds information about a single contiguous area of
81// memory reserved for the memory manager.
82typedef struct _mp_state_mem_area_t {
83 #if MICROPY_GC_SPLIT_HEAP
84 struct _mp_state_mem_area_t *next;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000085 #endif
86
87 byte *gc_alloc_table_start;
Damien Georged977d262015-12-16 20:09:11 -050088 size_t gc_alloc_table_byte_len;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000089 #if MICROPY_ENABLE_FINALISER
90 byte *gc_finaliser_table_start;
91 #endif
Damien George94fe6e52015-11-27 13:07:48 +000092 byte *gc_pool_start;
93 byte *gc_pool_end;
Damien Georgeb4b10fd2015-01-01 23:30:53 +000094
Ayke van Laethembcc827d2018-01-24 02:09:58 +010095 size_t gc_last_free_atb_index;
96} mp_state_mem_area_t;
97
Ayke van Laethembcc827d2018-01-24 02:09:58 +010098// This structure hold information about the memory allocation system.
99typedef struct _mp_state_mem_t {
100 #if MICROPY_MEM_STATS
101 size_t total_bytes_allocated;
102 size_t current_bytes_allocated;
103 size_t peak_bytes_allocated;
104 #endif
105
106 mp_state_mem_area_t area;
107
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000108 int gc_stack_overflow;
Rob Knegjens4a485312022-04-12 21:26:16 -0700109 MICROPY_GC_STACK_ENTRY_TYPE gc_block_stack[MICROPY_ALLOC_GC_STACK_SIZE];
110 #if MICROPY_GC_SPLIT_HEAP
111 // Array that tracks the area for each block on gc_block_stack.
112 mp_state_mem_area_t *gc_area_stack[MICROPY_ALLOC_GC_STACK_SIZE];
113 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000114
115 // This variable controls auto garbage collection. If set to 0 then the
116 // GC won't automatically run when gc_alloc can't find enough blocks. But
117 // you can still allocate/free memory and also explicitly call gc_collect.
118 uint16_t gc_auto_collect_enabled;
119
Paul Sokolovsky93e353e2016-07-21 00:37:30 +0300120 #if MICROPY_GC_ALLOC_THRESHOLD
121 size_t gc_alloc_amount;
122 size_t gc_alloc_threshold;
123 #endif
124
Ayke van Laethembcc827d2018-01-24 02:09:58 +0100125 #if MICROPY_GC_SPLIT_HEAP
126 mp_state_mem_area_t *gc_last_free_area;
127 #endif
Damien Georgee1e359f2015-02-07 17:24:10 +0000128
129 #if MICROPY_PY_GC_COLLECT_RETVAL
Damien Georged977d262015-12-16 20:09:11 -0500130 size_t gc_collected;
Damien Georgee1e359f2015-02-07 17:24:10 +0000131 #endif
Damien Georgec93d9ca2016-04-25 15:28:57 +0000132
David Lechnerccc18f02020-01-22 11:19:37 -0600133 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien Georgec93d9ca2016-04-25 15:28:57 +0000134 // This is a global mutex used to make the GC thread-safe.
135 mp_thread_mutex_t gc_mutex;
136 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000137} mp_state_mem_t;
138
139// This structure hold runtime and VM information. It includes a section
140// which contains root pointers that must be scanned by the GC.
141typedef struct _mp_state_vm_t {
Damien George749b1612018-05-12 22:09:34 +1000142 //
143 // CONTINUE ROOT POINTER SECTION
144 // This must start at the start of this structure and follows
145 // the state in the mp_state_thread_t structure, continuing
146 // the root pointer section from there.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000147 //
148
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000149 qstr_pool_t *last_pool;
150
Damien Georgefca57012022-05-04 12:12:11 +1000151 #if MICROPY_TRACKED_ALLOC
152 struct _m_tracked_node_t *m_tracked_head;
153 #endif
154
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000155 // non-heap memory for creating an exception if we can't allocate RAM
156 mp_obj_exception_t mp_emergency_exception_obj;
157
158 // memory for exception arguments if we can't allocate RAM
159 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF
160 #if MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE > 0
Damien Georgeee86de12017-04-10 16:02:56 +1000161 // statically allocated buf (needs to be aligned to mp_obj_t)
162 mp_obj_t mp_emergency_exception_buf[MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE / sizeof(mp_obj_t)];
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000163 #else
164 // dynamically allocated buf
165 byte *mp_emergency_exception_buf;
166 #endif
167 #endif
168
Damien George7f1da0a2016-12-15 13:00:19 +1100169 #if MICROPY_KBD_EXCEPTION
170 // exception object of type KeyboardInterrupt
171 mp_obj_exception_t mp_kbd_exception;
172 #endif
173
Paul Sokolovsky1a1d11f2015-12-05 00:09:10 +0200174 // dictionary with loaded modules (may be exposed as sys.modules)
175 mp_obj_dict_t mp_loaded_modules_dict;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000176
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000177 // dictionary for the __main__ module
178 mp_obj_dict_t dict_main;
179
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000180 // dictionary for overridden builtins
181 #if MICROPY_CAN_OVERRIDE_BUILTINS
182 mp_obj_dict_t *mp_module_builtins_override_dict;
183 #endif
184
David Lechnerfc3d7ae2022-07-01 12:29:08 -0500185 // Include any root pointers registered with MP_REGISTER_ROOT_POINTER().
186 #ifndef NO_QSTR
187 // Only include root pointer definitions when not doing qstr extraction, because
188 // the qstr extraction stage also generates the root pointers header file.
189 #include "genhdr/root_pointers.h"
190 #endif
191
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000192 //
193 // END ROOT POINTER SECTION
194 ////////////////////////////////////////////////////////////
195
Damien Georgeade9a052015-06-13 21:53:22 +0100196 // pointer and sizes to store interned string data
197 // (qstr_last_chunk can be root pointer but is also stored in qstr pool)
Artyom Skrobov18b1ba02021-05-03 14:17:36 -0400198 char *qstr_last_chunk;
Damien George25784852015-12-17 12:41:40 +0000199 size_t qstr_last_alloc;
200 size_t qstr_last_used;
Damien Georgeade9a052015-06-13 21:53:22 +0100201
David Lechneredbb73a2020-01-22 11:19:37 -0600202 #if MICROPY_PY_THREAD && !MICROPY_PY_THREAD_GIL
Damien George1f54ad22016-05-26 09:06:46 +0000203 // This is a global mutex used to make qstr interning thread-safe.
204 mp_thread_mutex_t qstr_mutex;
205 #endif
206
Damien George3f420c02018-04-04 14:24:03 +1000207 #if MICROPY_ENABLE_COMPILER
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000208 mp_uint_t mp_optimise_value;
Damien Georgeaf20c2e2019-08-23 11:20:50 +1000209 #if MICROPY_EMIT_NATIVE
210 uint8_t default_emit_opt; // one of MP_EMIT_OPT_xxx
211 #endif
Damien George3f420c02018-04-04 14:24:03 +1000212 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000213
214 // size of the emergency exception buf, if it's dynamically allocated
215 #if MICROPY_ENABLE_EMERGENCY_EXCEPTION_BUF && MICROPY_EMERGENCY_EXCEPTION_BUF_SIZE == 0
216 mp_int_t mp_emergency_exception_buf_size;
217 #endif
Damien George4cec63a2016-05-26 10:42:53 +0000218
Damien George749b1612018-05-12 22:09:34 +1000219 #if MICROPY_ENABLE_SCHEDULER
220 volatile int16_t sched_state;
Damien George75506e42022-03-23 17:13:03 +1100221
222 #if MICROPY_SCHEDULER_STATIC_NODES
223 // These will usually point to statically allocated memory. They are not
224 // traced by the GC. They are assumed to be zero'd out before mp_init() is
225 // called (usually because this struct lives in the BSS).
226 struct _mp_sched_node_t *sched_head;
227 struct _mp_sched_node_t *sched_tail;
228 #endif
229
230 // These index sched_queue.
Andrew Leech8977c7e2019-03-21 11:52:10 +1100231 uint8_t sched_len;
232 uint8_t sched_idx;
Damien George749b1612018-05-12 22:09:34 +1000233 #endif
234
Damien Georged54208a2022-12-16 17:31:21 +1100235 #if MICROPY_ENABLE_VM_ABORT
236 bool vm_abort;
237 nlr_buf_t *nlr_abort;
238 #endif
239
Damien George4cec63a2016-05-26 10:42:53 +0000240 #if MICROPY_PY_THREAD_GIL
241 // This is a global mutex used to make the VM/runtime thread-safe.
242 mp_thread_mutex_t gil_mutex;
243 #endif
Jim Mussared11ef8f22021-08-18 14:52:48 +1000244
245 #if MICROPY_OPT_MAP_LOOKUP_CACHE
246 // See mp_map_lookup.
247 uint8_t map_lookup_cache[MICROPY_OPT_MAP_LOOKUP_CACHE_SIZE];
248 #endif
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000249} mp_state_vm_t;
250
Damien George330165a2016-04-22 22:44:56 +0000251// This structure holds state that is specific to a given thread.
252// Everything in this structure is scanned for root pointers.
253typedef struct _mp_state_thread_t {
Damien George330165a2016-04-22 22:44:56 +0000254 // Stack top at the start of program
Damien George330165a2016-04-22 22:44:56 +0000255 char *stack_top;
256
257 #if MICROPY_STACK_CHECK
258 size_t stack_limit;
259 #endif
Damien George02d830c2017-11-26 23:28:40 +1100260
261 #if MICROPY_ENABLE_PYSTACK
262 uint8_t *pystack_start;
263 uint8_t *pystack_end;
264 uint8_t *pystack_cur;
265 #endif
Damien George749b1612018-05-12 22:09:34 +1000266
Damien Georgeb6b39bf2021-05-04 23:56:43 +1000267 // Locking of the GC is done per thread.
268 uint16_t gc_lock_depth;
269
Damien George749b1612018-05-12 22:09:34 +1000270 ////////////////////////////////////////////////////////////
271 // START ROOT POINTER SECTION
272 // Everything that needs GC scanning must start here, and
273 // is followed by state in the mp_state_vm_t structure.
274 //
275
276 mp_obj_dict_t *dict_locals;
277 mp_obj_dict_t *dict_globals;
278
279 nlr_buf_t *nlr_top;
Damien George2757acf2023-05-09 11:03:04 +1000280 nlr_jump_callback_node_t *nlr_jump_callback_top;
Milan Rossa310b3d12019-08-14 16:09:36 +0200281
David Lechnerca920f72021-05-10 21:53:22 -0500282 // pending exception object (MP_OBJ_NULL if not pending)
283 volatile mp_obj_t mp_pending_exception;
284
Damien Georgebb001252021-06-29 17:34:34 +1000285 // If MP_OBJ_STOP_ITERATION is propagated then this holds its argument.
286 mp_obj_t stop_iteration_arg;
287
Milan Rossa310b3d12019-08-14 16:09:36 +0200288 #if MICROPY_PY_SYS_SETTRACE
289 mp_obj_t prof_trace_callback;
290 bool prof_callback_is_executing;
291 struct _mp_code_state_t *current_code_state;
292 #endif
Damien George330165a2016-04-22 22:44:56 +0000293} mp_state_thread_t;
294
Damien George05fe66f2017-02-27 23:56:46 +1100295// This structure combines the above 3 structures.
296// The order of the entries are important for root pointer scanning in the GC to work.
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000297typedef struct _mp_state_ctx_t {
Damien George330165a2016-04-22 22:44:56 +0000298 mp_state_thread_t thread;
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000299 mp_state_vm_t vm;
300 mp_state_mem_t mem;
301} mp_state_ctx_t;
302
303extern mp_state_ctx_t mp_state_ctx;
304
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000305#define MP_STATE_VM(x) (mp_state_ctx.vm.x)
306#define MP_STATE_MEM(x) (mp_state_ctx.mem.x)
David Lechner259d9b62021-05-10 22:18:17 -0500307#define MP_STATE_MAIN_THREAD(x) (mp_state_ctx.thread.x)
Damien Georgeb4b10fd2015-01-01 23:30:53 +0000308
Damien George27cc0772016-04-22 22:52:33 +0000309#if MICROPY_PY_THREAD
310extern mp_state_thread_t *mp_thread_get_state(void);
311#define MP_STATE_THREAD(x) (mp_thread_get_state()->x)
Damien George5d4bfce2022-12-16 17:30:26 +1100312#define mp_thread_is_main_thread() (mp_thread_get_state() == &mp_state_ctx.thread)
Damien George27cc0772016-04-22 22:52:33 +0000313#else
David Lechner259d9b62021-05-10 22:18:17 -0500314#define MP_STATE_THREAD(x) MP_STATE_MAIN_THREAD(x)
Damien George5d4bfce2022-12-16 17:30:26 +1100315#define mp_thread_is_main_thread() (true)
Damien George27cc0772016-04-22 22:52:33 +0000316#endif
Damien George330165a2016-04-22 22:44:56 +0000317
Alexander Steffen299bc622017-06-29 23:14:58 +0200318#endif // MICROPY_INCLUDED_PY_MPSTATE_H