Allow for resizing the stack area.
This commit is contained in:
parent
777542c716
commit
12cf5e51c2
20
main.c
20
main.c
|
@ -52,6 +52,7 @@
|
|||
#include "supervisor/messages/en-US.h"
|
||||
#include "supervisor/shared/autoreload.h"
|
||||
#include "supervisor/shared/rgb_led_status.h"
|
||||
#include "supervisor/shared/stack.h"
|
||||
#include "supervisor/serial.h"
|
||||
|
||||
void do_str(const char *src, mp_parse_input_kind_t input_kind) {
|
||||
|
@ -81,11 +82,11 @@ void start_mp(supervisor_allocation* heap) {
|
|||
// Stack limit should be less than real stack size, so we have a chance
|
||||
// to recover from limit hit. (Limit is measured in bytes.)
|
||||
mp_stack_ctrl_init();
|
||||
mp_stack_set_limit((char*)&_estack - (char*)&_ebss - 1024);
|
||||
mp_stack_set_limit(stack_alloc->length - 1024);
|
||||
|
||||
#if MICROPY_MAX_STACK_USAGE
|
||||
// _ezero (same as _ebss) is an int, so start 4 bytes above it.
|
||||
mp_stack_set_bottom(&_ezero + 1);
|
||||
mp_stack_set_bottom(stack_alloc->ptr);
|
||||
mp_stack_fill_with_sentinel();
|
||||
#endif
|
||||
|
||||
|
@ -97,7 +98,7 @@ void start_mp(supervisor_allocation* heap) {
|
|||
readline_init0();
|
||||
|
||||
#if MICROPY_ENABLE_GC
|
||||
gc_init(heap->ptr, heap->ptr + heap->length);
|
||||
gc_init(heap->ptr, heap->ptr + heap->length / 4);
|
||||
#endif
|
||||
mp_init();
|
||||
mp_obj_list_init(mp_sys_path, 0);
|
||||
|
@ -171,6 +172,8 @@ bool run_code_py(safe_mode_t safe_mode) {
|
|||
const char *double_extension_filenames[] = STRING_LIST("code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
|
||||
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
|
||||
|
||||
stack_resize();
|
||||
filesystem_flush();
|
||||
supervisor_allocation* heap = allocate_remaining_memory();
|
||||
start_mp(heap);
|
||||
found_main = maybe_run_list(supported_filenames, &result);
|
||||
|
@ -310,6 +313,12 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
|
|||
}
|
||||
#endif
|
||||
|
||||
stack_init();
|
||||
// TODO(tannewt): Allocate temporary space to hold custom usb descriptors.
|
||||
filesystem_flush();
|
||||
supervisor_allocation* heap = allocate_remaining_memory();
|
||||
start_mp(heap);
|
||||
|
||||
// TODO(tannewt): Re-add support for flashing boot error output.
|
||||
bool found_boot = maybe_run_list(boot_py_filenames, NULL);
|
||||
(void) found_boot;
|
||||
|
@ -325,11 +334,16 @@ void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
|
|||
// Reset to remove any state that boot.py setup. It should only be used to
|
||||
// change internal state that's not in the heap.
|
||||
reset_port();
|
||||
reset_board();
|
||||
stop_mp();
|
||||
free_memory(heap);
|
||||
}
|
||||
}
|
||||
|
||||
int run_repl(void) {
|
||||
int exit_code = PYEXEC_FORCED_EXIT;
|
||||
stack_resize();
|
||||
filesystem_flush();
|
||||
supervisor_allocation* heap = allocate_remaining_memory();
|
||||
start_mp(heap);
|
||||
autoreload_suspend();
|
||||
|
|
|
@ -338,4 +338,8 @@ void run_background_tasks(void);
|
|||
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
|
||||
#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
|
||||
|
||||
// TODO(tannewt): Make this 6k+ for any non-express M4 boards because they cache sectors on the
|
||||
// stack.
|
||||
#define CIRCUITPY_DEFAULT_STACK_SIZE 2048
|
||||
|
||||
#endif // __INCLUDED_MPCONFIGPORT_H
|
||||
|
|
|
@ -51,7 +51,6 @@ void filesystem_init(bool create_allowed, bool force_create) {
|
|||
|
||||
// try to mount the flash
|
||||
FRESULT res = f_mount(&vfs_fat->fatfs);
|
||||
force_create = true;
|
||||
|
||||
if ((res == FR_NO_FILESYSTEM && create_allowed) || force_create) {
|
||||
// No filesystem so create a fresh one, or reformat has been requested.
|
||||
|
|
|
@ -40,9 +40,6 @@ extern uint32_t _estack;
|
|||
void memory_init(void) {
|
||||
low_address = &_ebss;
|
||||
high_address = &_estack;
|
||||
|
||||
// snag 3k for stack
|
||||
allocate_memory(1024*4, true);
|
||||
}
|
||||
|
||||
void free_memory(supervisor_allocation* allocation) {
|
||||
|
@ -56,8 +53,23 @@ void free_memory(supervisor_allocation* allocation) {
|
|||
}
|
||||
if (allocation->ptr == high_address) {
|
||||
high_address += allocation->length / 4;
|
||||
for (index++; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) {
|
||||
if (allocations[index].ptr != NULL) {
|
||||
break;
|
||||
}
|
||||
high_address += allocations[index].length / 4;
|
||||
}
|
||||
} else if (allocation->ptr + allocation->length / 4 == low_address) {
|
||||
low_address = allocation->ptr;
|
||||
for (index--; index >= 0; index--) {
|
||||
if (allocations[index].ptr != NULL) {
|
||||
break;
|
||||
}
|
||||
low_address -= allocations[index].length / 4;
|
||||
}
|
||||
} else {
|
||||
// Freed memory isn't in the middle so skip updating bounds. The memory will be added to the
|
||||
// middle when the memory to the inside is freed.
|
||||
}
|
||||
allocation->ptr = NULL;
|
||||
}
|
||||
|
|
|
@ -233,5 +233,6 @@ void run_background_tasks(void);
|
|||
#define MICROPY_VM_HOOK_RETURN run_background_tasks();
|
||||
|
||||
//#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
|
||||
#define CIRCUITPY_DEFAULT_STACK_SIZE 2048
|
||||
|
||||
#endif
|
||||
|
|
|
@ -0,0 +1,112 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "supervisor/memory.h"
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT 8
|
||||
|
||||
static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
|
||||
// We use uint32_t* to ensure word (4 byte) alignment.
|
||||
uint32_t* low_address;
|
||||
uint32_t* high_address;
|
||||
extern uint32_t _ebss;
|
||||
extern uint32_t _estack;
|
||||
|
||||
void memory_init(void) {
|
||||
low_address = &_ebss;
|
||||
high_address = &_estack;
|
||||
}
|
||||
|
||||
void free_memory(supervisor_allocation* allocation) {
|
||||
uint8_t index = 0;
|
||||
bool found = false;
|
||||
for (index = 0; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) {
|
||||
found = allocation == &allocations[index];
|
||||
if (found) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allocation->ptr == high_address) {
|
||||
high_address += allocation->length / 4;
|
||||
for (index++; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) {
|
||||
if (allocations[index].ptr != NULL) {
|
||||
break;
|
||||
}
|
||||
high_address += allocations[index].length / 4;
|
||||
}
|
||||
} else if (allocation->ptr + allocation->length / 4 == low_address) {
|
||||
low_address = allocation->ptr;
|
||||
for (index--; index >= 0; index--) {
|
||||
if (allocations[index].ptr != NULL) {
|
||||
break;
|
||||
}
|
||||
low_address -= allocations[index].length / 4;
|
||||
}
|
||||
} else {
|
||||
// Freed memory isn't in the middle so skip updating bounds. The memory will be added to the
|
||||
// middle when the memory to the inside is freed.
|
||||
}
|
||||
allocation->ptr = NULL;
|
||||
}
|
||||
|
||||
supervisor_allocation* allocate_remaining_memory(void) {
|
||||
if (low_address == high_address) {
|
||||
return NULL;
|
||||
}
|
||||
return allocate_memory((high_address - low_address) * 4, false);
|
||||
}
|
||||
|
||||
supervisor_allocation* allocate_memory(uint32_t length, bool high) {
|
||||
if ((high_address - low_address) * 4 < (int32_t) length) {
|
||||
return NULL;
|
||||
}
|
||||
uint8_t index = 0;
|
||||
int8_t direction = 1;
|
||||
if (high) {
|
||||
index = CIRCUITPY_SUPERVISOR_ALLOC_COUNT - 1;
|
||||
direction = -1;
|
||||
}
|
||||
for (; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index += direction) {
|
||||
if (allocations[index].ptr == NULL) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (index >= CIRCUITPY_SUPERVISOR_ALLOC_COUNT) {
|
||||
return NULL;
|
||||
}
|
||||
supervisor_allocation* alloc = &allocations[index];
|
||||
if (high) {
|
||||
high_address -= length / 4;
|
||||
alloc->ptr = high_address;
|
||||
} else {
|
||||
alloc->ptr = low_address;
|
||||
low_address += length / 4;
|
||||
}
|
||||
alloc->length = length;
|
||||
return alloc;
|
||||
}
|
|
@ -29,8 +29,8 @@
|
|||
|
||||
#include "lib/utils/interrupt_char.h"
|
||||
#include "supervisor/shared/autoreload.h"
|
||||
|
||||
#include "supervisor/shared/rgb_led_status.h"
|
||||
#include "supervisor/shared/stack.h"
|
||||
|
||||
#include "shared-bindings/supervisor/__init__.h"
|
||||
#include "shared-bindings/supervisor/Runtime.h"
|
||||
|
@ -107,15 +107,21 @@ STATIC mp_obj_t supervisor_reload(void) {
|
|||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload);
|
||||
|
||||
//| .. method:: set_main_file(filename)
|
||||
//| .. method:: set_next_stack_limit(size)
|
||||
//|
|
||||
//| Set which file to run after a reload.
|
||||
//| Set the size of the stack for the next vm run. If its too large, the default will be used.
|
||||
//|
|
||||
STATIC mp_obj_t supervisor_set_main_file(void) {
|
||||
STATIC mp_obj_t supervisor_set_next_stack_limit(mp_obj_t size_obj) {
|
||||
mp_int_t size = mp_obj_get_int(size_obj);
|
||||
|
||||
if (size < 256) {
|
||||
mp_raise_ValueError("Stack size must be at least 256");
|
||||
}
|
||||
set_next_stack_size(size);
|
||||
|
||||
return mp_const_none;
|
||||
}
|
||||
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_set_main_file_obj, supervisor_set_main_file);
|
||||
MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_next_stack_limit_obj, supervisor_set_next_stack_limit);
|
||||
|
||||
STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
|
||||
{ MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_supervisor) },
|
||||
|
@ -124,7 +130,7 @@ STATIC const mp_rom_map_elem_t supervisor_module_globals_table[] = {
|
|||
{ MP_OBJ_NEW_QSTR(MP_QSTR_set_rgb_status_brightness), MP_ROM_PTR(&supervisor_set_rgb_status_brightness_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_runtime), MP_ROM_PTR(&common_hal_supervisor_runtime_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_reload), MP_ROM_PTR(&supervisor_reload_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_main_file), MP_ROM_PTR(&supervisor_set_main_file_obj) },
|
||||
{ MP_ROM_QSTR(MP_QSTR_set_next_stack_limit), MP_ROM_PTR(&supervisor_set_next_stack_limit_obj) },
|
||||
|
||||
};
|
||||
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "stack.h"
|
||||
|
||||
#include "py/mpconfig.h"
|
||||
#include "supervisor/cpu.h"
|
||||
|
||||
extern uint32_t _estack;
|
||||
|
||||
static uint32_t next_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
|
||||
static uint32_t current_stack_size = 0;
|
||||
supervisor_allocation* stack_alloc = NULL;
|
||||
|
||||
#define EXCEPTION_STACK_SIZE 1024
|
||||
|
||||
void allocate_stack(void) {
|
||||
mp_uint_t regs[10];
|
||||
mp_uint_t sp = cpu_get_regs_and_sp(regs);
|
||||
|
||||
mp_uint_t c_size = (uint32_t) &_estack - sp;
|
||||
|
||||
stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true);
|
||||
if (stack_alloc == NULL) {
|
||||
stack_alloc = allocate_memory(c_size + CIRCUITPY_DEFAULT_STACK_SIZE + EXCEPTION_STACK_SIZE, true);
|
||||
current_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
|
||||
} else {
|
||||
current_stack_size = next_stack_size;
|
||||
}
|
||||
}
|
||||
|
||||
inline void stack_init(void) {
|
||||
allocate_stack();
|
||||
}
|
||||
|
||||
inline void stack_resize(void) {
|
||||
if (next_stack_size == current_stack_size) {
|
||||
return;
|
||||
}
|
||||
free_memory(stack_alloc);
|
||||
stack_alloc = NULL;
|
||||
allocate_stack();
|
||||
}
|
||||
|
||||
void set_next_stack_size(uint32_t size) {
|
||||
next_stack_size = size;
|
||||
}
|
||||
|
||||
uint32_t get_current_stack_size(void) {
|
||||
return current_stack_size;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
/*
|
||||
* This file is part of the MicroPython project, http://micropython.org/
|
||||
*
|
||||
* The MIT License (MIT)
|
||||
*
|
||||
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#ifndef MICROPY_INCLUDED_SUPERVISOR_STACK_H
|
||||
#define MICROPY_INCLUDED_SUPERVISOR_STACK_H
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
#include "supervisor/memory.h"
|
||||
|
||||
extern supervisor_allocation* stack_alloc;
|
||||
|
||||
void stack_init(void);
|
||||
void stack_resize(void);
|
||||
void set_next_stack_size(uint32_t size);
|
||||
uint32_t get_current_stack_size(void);
|
||||
|
||||
#endif // MICROPY_INCLUDED_SUPERVISOR_STACK_H
|
|
@ -3,7 +3,8 @@ SRC_SUPERVISOR = \
|
|||
supervisor/memory.c \
|
||||
supervisor/port.c \
|
||||
supervisor/shared/autoreload.c \
|
||||
supervisor/shared/rgb_led_status.c
|
||||
supervisor/shared/rgb_led_status.c \
|
||||
supervisor/shared/stack.c
|
||||
|
||||
ifeq ($(wildcard atmel-samd/supervisor/filesystem.c),)
|
||||
SRC_SUPERVISOR += supervisor/filesystem.c
|
||||
|
|
Loading…
Reference in New Issue