Make set_stack_limit respect fixed stack

Fixes #2830
This commit is contained in:
Scott Shawcroft 2023-03-21 16:42:49 -07:00
parent 461d833c1c
commit 0eb08509f0
No known key found for this signature in database
GPG Key ID: 0DFD512649C052DA
3 changed files with 16 additions and 4 deletions

View File

@ -197,7 +197,11 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_runtime_get_next_stack_limit_obj, superviso
STATIC mp_obj_t supervisor_runtime_set_next_stack_limit(mp_obj_t self, mp_obj_t size_obj) {
mp_int_t size = mp_obj_get_int(size_obj);
mp_arg_validate_int_min(size, 256, MP_QSTR_size);
set_next_stack_size(size);
if (!set_next_stack_size(size)) {
mp_raise_msg_varg(&mp_type_AttributeError,
MP_ERROR_TEXT("can't set attribute '%q'"),
MP_QSTR_next_stack_limit);
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(supervisor_runtime_set_next_stack_limit_obj, supervisor_runtime_set_next_stack_limit);

View File

@ -35,7 +35,7 @@
extern uint32_t _estack;
// Requested size.
static uint32_t next_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
static uint32_t next_stack_size = 0;
static uint32_t current_stack_size = 0;
// Actual location and size, may be larger than requested.
static uint32_t *stack_limit = NULL;
@ -49,11 +49,15 @@ static void allocate_stack(void) {
stack_limit = port_stack_get_limit();
stack_length = (port_stack_get_top() - stack_limit) * sizeof(uint32_t);
current_stack_size = stack_length;
next_stack_size = stack_length;
} else {
mp_uint_t regs[10];
mp_uint_t sp = cpu_get_regs_and_sp(regs);
mp_uint_t c_size = (mp_uint_t)port_stack_get_top() - sp;
if (next_stack_size == 0) {
next_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
}
supervisor_allocation *stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true, false);
if (stack_alloc == NULL) {
stack_alloc = allocate_memory(c_size + CIRCUITPY_DEFAULT_STACK_SIZE + EXCEPTION_STACK_SIZE, true, false);
@ -103,8 +107,12 @@ size_t stack_get_length(void) {
return stack_length;
}
void set_next_stack_size(uint32_t size) {
bool set_next_stack_size(uint32_t size) {
if (port_has_fixed_stack()) {
return false;
}
next_stack_size = size;
return true;
}
uint32_t get_next_stack_size(void) {

View File

@ -37,7 +37,7 @@ void stack_resize(void);
uint32_t *stack_get_bottom(void);
size_t stack_get_length(void);
// Next/current requested stack size.
void set_next_stack_size(uint32_t size);
bool set_next_stack_size(uint32_t size);
uint32_t get_next_stack_size(void);
uint32_t get_current_stack_size(void);
bool stack_ok(void);