Skip to content

Commit 98e7579

Browse files
authored
Merge pull request #2230 from kamtom480/circuitpython-stack
Top and limit stack addresses
2 parents bd6c7c5 + e2cb29f commit 98e7579

File tree

11 files changed

+54
-10
lines changed

11 files changed

+54
-10
lines changed

main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ void gc_collect(void) {
477477

478478
// This naively collects all object references from an approximate stack
479479
// range.
480-
gc_collect_root((void**)sp, ((uint32_t)&_estack - sp) / sizeof(uint32_t));
480+
gc_collect_root((void**)sp, ((uint32_t)port_stack_get_top() - sp) / sizeof(uint32_t));
481481
gc_collect_end();
482482
}
483483

ports/atmel-samd/supervisor/port.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,14 @@ void reset_cpu(void) {
271271
reset();
272272
}
273273

274+
uint32_t *port_stack_get_limit(void) {
275+
return &_ebss;
276+
}
277+
278+
uint32_t *port_stack_get_top(void) {
279+
return &_estack;
280+
}
281+
274282
// Place the word to save 8k from the end of RAM so we and the bootloader don't clobber it.
275283
#ifdef SAMD21
276284
uint32_t* safe_word = (uint32_t*) (HMCRAMC0_ADDR + HMCRAMC0_SIZE - 0x2000);

ports/cxd56/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ INC += \
9090
-I$(SPRESENSE_SDK)/nuttx/include \
9191
-I$(SPRESENSE_SDK)/nuttx/arch \
9292
-I$(SPRESENSE_SDK)/nuttx/arch/chip \
93+
-I$(SPRESENSE_SDK)/nuttx/arch/os \
9394
-I$(SPRESENSE_SDK)/sdk/bsp/include \
9495
-I$(SPRESENSE_SDK)/sdk/bsp/include/sdk \
9596

@@ -124,7 +125,6 @@ LDFLAGS = \
124125
--entry=__start \
125126
-nostartfiles \
126127
-nodefaultlibs \
127-
--defsym __stack=_vectors+786432 \
128128
-T$(SPRESENSE_SDK)/nuttx/build/ramconfig.ld \
129129
--gc-sections \
130130
-Map=$(BUILD)/output.map \

ports/cxd56/mpconfigport.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727
#ifndef __INCLUDED_MPCONFIGPORT_H
2828
#define __INCLUDED_MPCONFIGPORT_H
2929

30-
// 24kiB stack
31-
#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000
30+
// 64kiB stack
31+
#define CIRCUITPY_DEFAULT_STACK_SIZE 0x10000
3232

3333
#include "py/circuitpy_mpconfig.h"
3434

ports/cxd56/supervisor/port.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include <stdint.h>
2828
#include <sys/boardctl.h>
2929

30+
#include "sched/sched.h"
31+
3032
#include "boards/board.h"
3133

3234
#include "supervisor/port.h"
@@ -67,6 +69,18 @@ void reset_port(void) {
6769
void reset_to_bootloader(void) {
6870
}
6971

72+
uint32_t *port_stack_get_limit(void) {
73+
struct tcb_s *rtcb = this_task();
74+
75+
return rtcb->adj_stack_ptr - (uint32_t)rtcb->adj_stack_size;
76+
}
77+
78+
uint32_t *port_stack_get_top(void) {
79+
struct tcb_s *rtcb = this_task();
80+
81+
return rtcb->adj_stack_ptr;
82+
}
83+
7084
extern uint32_t _ebss;
7185

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

ports/nrf/supervisor/port.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,14 @@ void reset_cpu(void) {
146146
NVIC_SystemReset();
147147
}
148148

149+
uint32_t *port_stack_get_limit(void) {
150+
return &_ebss;
151+
}
152+
153+
uint32_t *port_stack_get_top(void) {
154+
return &_estack;
155+
}
156+
149157
extern uint32_t _ebss;
150158
// Place the word to save just after our BSS section that gets blanked.
151159
void port_set_saved_word(uint32_t value) {

ports/stm32f4/supervisor/port.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ void reset_cpu(void) {
6767
NVIC_SystemReset();
6868
}
6969

70+
uint32_t *port_stack_get_limit(void) {
71+
return &_ebss;
72+
}
73+
74+
uint32_t *port_stack_get_top(void) {
75+
return &_estack;
76+
}
77+
7078
extern uint32_t _ebss;
7179
// Place the word to save just after our BSS section that gets blanked.
7280
void port_set_saved_word(uint32_t value) {

supervisor/port.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ void reset_board(void);
5454
// Reset to the bootloader
5555
void reset_to_bootloader(void);
5656

57+
// Get stack limit address
58+
uint32_t *port_stack_get_limit(void);
59+
60+
// Get stack top address
61+
uint32_t *port_stack_get_top(void);
62+
5763
// Save and retrieve a word from memory that is preserved over reset. Used for safe mode.
5864
void port_set_saved_word(uint32_t);
5965
uint32_t port_get_saved_word(void);

supervisor/shared/memory.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
*/
2626

2727
#include "supervisor/memory.h"
28+
#include "supervisor/port.h"
2829

2930
#include <stddef.h>
3031

@@ -36,12 +37,10 @@ static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
3637
// We use uint32_t* to ensure word (4 byte) alignment.
3738
uint32_t* low_address;
3839
uint32_t* high_address;
39-
extern uint32_t _ebss;
40-
extern uint32_t _estack;
4140

4241
void memory_init(void) {
43-
low_address = &_ebss;
44-
high_address = &_estack;
42+
low_address = port_stack_get_limit();
43+
high_address = port_stack_get_top();
4544
}
4645

4746
void free_memory(supervisor_allocation* allocation) {

supervisor/shared/stack.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "py/mpconfig.h"
3030
#include "py/runtime.h"
3131
#include "supervisor/cpu.h"
32+
#include "supervisor/port.h"
3233
#include "supervisor/shared/safe_mode.h"
3334

3435
extern uint32_t _estack;
@@ -43,7 +44,7 @@ void allocate_stack(void) {
4344
mp_uint_t regs[10];
4445
mp_uint_t sp = cpu_get_regs_and_sp(regs);
4546

46-
mp_uint_t c_size = (uint32_t) &_estack - sp;
47+
mp_uint_t c_size = (uint32_t) port_stack_get_top() - sp;
4748

4849
stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true);
4950
if (stack_alloc == NULL) {

0 commit comments

Comments
 (0)