Skip to content

Commit 22497eb

Browse files
committed
Fix build for ports with fixed stack (mimxrt10xx, esp32s2).
1 parent b238824 commit 22497eb

File tree

11 files changed

+50
-41
lines changed

11 files changed

+50
-41
lines changed

main.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,15 +123,15 @@ void start_mp(supervisor_allocation* heap) {
123123
// to recover from limit hit. (Limit is measured in bytes.)
124124
mp_stack_ctrl_init();
125125

126-
if (stack_alloc != NULL) {
127-
mp_stack_set_limit(get_allocation_length(stack_alloc) - 1024);
126+
if (stack_get_bottom() != NULL) {
127+
mp_stack_set_limit(stack_get_length() - 1024);
128128
}
129129

130130

131131
#if MICROPY_MAX_STACK_USAGE
132132
// _ezero (same as _ebss) is an int, so start 4 bytes above it.
133-
if (stack_alloc != NULL) {
134-
mp_stack_set_bottom(stack_alloc->ptr);
133+
if (stack_get_bottom() != NULL) {
134+
mp_stack_set_bottom(stack_get_bottom());
135135
mp_stack_fill_with_sentinel();
136136
}
137137
#endif

ports/atmel-samd/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -390,8 +390,8 @@ void reset_cpu(void) {
390390
reset();
391391
}
392392

393-
supervisor_allocation* port_fixed_stack(void) {
394-
return NULL;
393+
bool port_has_fixed_stack(void) {
394+
return false;
395395
}
396396

397397
uint32_t *port_stack_get_limit(void) {

ports/cxd56/supervisor/port.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,9 @@ void reset_to_bootloader(void) {
9898
}
9999
}
100100

101-
supervisor_allocation* port_fixed_stack(void) {
102-
return NULL;
101+
bool port_has_fixed_stack(void) {
102+
//TODO this doesn't look right
103+
return false;
103104
}
104105

105106
uint32_t *port_stack_get_limit(void) {

ports/esp32s2/supervisor/port.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,8 @@ uint32_t *port_stack_get_top(void) {
169169
return port_stack_get_limit() + ESP_TASK_MAIN_STACK / (sizeof(uint32_t) / sizeof(StackType_t));
170170
}
171171

172-
supervisor_allocation _fixed_stack;
173-
174-
supervisor_allocation* port_fixed_stack(void) {
175-
_fixed_stack.ptr = port_stack_get_limit();
176-
_fixed_stack.length = (port_stack_get_top() - port_stack_get_limit()) * sizeof(uint32_t);
177-
return &_fixed_stack;
172+
bool port_has_fixed_stack(void) {
173+
return true;
178174
}
179175

180176
// Place the word to save just after our BSS section that gets blanked.

ports/litex/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ void reset_cpu(void) {
9898
for(;;) {}
9999
}
100100

101-
supervisor_allocation* port_fixed_stack(void) {
102-
return NULL;
101+
bool port_has_fixed_stack(void) {
102+
return false;
103103
}
104104

105105
uint32_t *port_heap_get_bottom(void) {

ports/mimxrt10xx/supervisor/port.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,8 @@ uint32_t *port_stack_get_top(void) {
334334
return &_ld_stack_top;
335335
}
336336

337-
supervisor_allocation _fixed_stack;
338-
supervisor_allocation* port_fixed_stack(void) {
339-
_fixed_stack.ptr = port_stack_get_limit();
340-
_fixed_stack.length = (port_stack_get_top() - port_stack_get_limit()) * sizeof(uint32_t);
341-
return &_fixed_stack;
337+
bool port_has_fixed_stack(void) {
338+
return true;
342339
}
343340

344341
uint32_t *port_heap_get_bottom(void) {

ports/nrf/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,8 @@ uint32_t *port_heap_get_top(void) {
251251
return port_stack_get_top();
252252
}
253253

254-
supervisor_allocation* port_fixed_stack(void) {
255-
return NULL;
254+
bool port_has_fixed_stack(void) {
255+
return false;
256256
}
257257

258258
uint32_t *port_stack_get_limit(void) {

ports/stm/supervisor/port.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,8 @@ uint32_t *port_heap_get_top(void) {
267267
return &_ld_heap_end;
268268
}
269269

270-
supervisor_allocation* port_fixed_stack(void) {
271-
return NULL;
270+
bool port_has_fixed_stack(void) {
271+
return false;
272272
}
273273

274274
uint32_t *port_stack_get_limit(void) {

supervisor/port.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,15 @@ uint32_t *port_stack_get_limit(void);
6161
// Get stack top address
6262
uint32_t *port_stack_get_top(void);
6363

64-
supervisor_allocation* port_fixed_stack(void);
64+
// True if stack is not located inside heap (at the top)
65+
bool port_has_fixed_stack(void);
6566

6667
// Get heap bottom address
6768
uint32_t *port_heap_get_bottom(void);
6869

6970
// Get heap top address
7071
uint32_t *port_heap_get_top(void);
7172

72-
supervisor_allocation* port_fixed_heap(void);
73-
7473
// Save and retrieve a word from memory that is preserved over reset. Used for safe mode.
7574
void port_set_saved_word(uint32_t);
7675
uint32_t port_get_saved_word(void);

supervisor/shared/stack.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,42 @@
3434

3535
extern uint32_t _estack;
3636

37+
// Requested size.
3738
static uint32_t next_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
3839
static uint32_t current_stack_size = 0;
39-
supervisor_allocation* stack_alloc = NULL;
40+
// Actual location and size, may be larger than requested.
41+
static uint32_t* stack_limit = NULL;
42+
static size_t stack_length = 0;
4043

4144
#define EXCEPTION_STACK_SIZE 1024
4245

4346
void allocate_stack(void) {
4447

45-
if (port_fixed_stack() != NULL) {
46-
stack_alloc = port_fixed_stack();
47-
current_stack_size = get_allocation_length(stack_alloc);
48+
if (port_has_fixed_stack()) {
49+
stack_limit = port_stack_get_limit();
50+
stack_length = (port_stack_get_top() - stack_limit)*sizeof(uint32_t);
51+
current_stack_size = stack_length;
4852
} else {
4953
mp_uint_t regs[10];
5054
mp_uint_t sp = cpu_get_regs_and_sp(regs);
5155

5256
mp_uint_t c_size = (uint32_t) port_stack_get_top() - sp;
53-
stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true, false);
57+
supervisor_allocation* stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true, false);
5458
if (stack_alloc == NULL) {
5559
stack_alloc = allocate_memory(c_size + CIRCUITPY_DEFAULT_STACK_SIZE + EXCEPTION_STACK_SIZE, true, false);
5660
current_stack_size = CIRCUITPY_DEFAULT_STACK_SIZE;
5761
} else {
5862
current_stack_size = next_stack_size;
5963
}
64+
stack_limit = stack_alloc->ptr;
65+
stack_length = get_allocation_length(stack_alloc);
6066
}
6167

62-
*stack_alloc->ptr = STACK_CANARY_VALUE;
68+
*stack_limit = STACK_CANARY_VALUE;
6369
}
6470

6571
inline bool stack_ok(void) {
66-
return stack_alloc == NULL || *stack_alloc->ptr == STACK_CANARY_VALUE;
72+
return stack_limit == NULL || *stack_limit == STACK_CANARY_VALUE;
6773
}
6874

6975
inline void assert_heap_ok(void) {
@@ -77,18 +83,26 @@ void stack_init(void) {
7783
}
7884

7985
void stack_resize(void) {
80-
if (stack_alloc == NULL) {
86+
if (stack_limit == NULL) {
8187
return;
8288
}
8389
if (next_stack_size == current_stack_size) {
84-
*stack_alloc->ptr = STACK_CANARY_VALUE;
90+
*stack_limit = STACK_CANARY_VALUE;
8591
return;
8692
}
87-
free_memory(stack_alloc);
88-
stack_alloc = NULL;
93+
free_memory(allocation_from_ptr(stack_limit));
94+
stack_limit = NULL;
8995
allocate_stack();
9096
}
9197

98+
uint32_t* stack_get_bottom(void) {
99+
return stack_limit;
100+
}
101+
102+
size_t stack_get_length(void) {
103+
return stack_length;
104+
}
105+
92106
void set_next_stack_size(uint32_t size) {
93107
next_stack_size = size;
94108
}

supervisor/shared/stack.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,12 @@
3131

3232
#include "supervisor/memory.h"
3333

34-
extern supervisor_allocation* stack_alloc;
35-
3634
void stack_init(void);
3735
void stack_resize(void);
36+
// Actual stack location and size, may be larger than requested.
37+
uint32_t* stack_get_bottom(void);
38+
size_t stack_get_length(void);
39+
// Next/current requested stack size.
3840
void set_next_stack_size(uint32_t size);
3941
uint32_t get_current_stack_size(void);
4042
bool stack_ok(void);

0 commit comments

Comments
 (0)