Skip to content

Top and limit stack addresses #2230

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ void gc_collect(void) {

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

Expand Down
8 changes: 8 additions & 0 deletions ports/atmel-samd/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,14 @@ void reset_cpu(void) {
reset();
}

uint32_t *port_stack_get_limit(void) {
return &_ebss;
}

uint32_t *port_stack_get_top(void) {
return &_estack;
}

// Place the word to save 8k from the end of RAM so we and the bootloader don't clobber it.
#ifdef SAMD21
uint32_t* safe_word = (uint32_t*) (HMCRAMC0_ADDR + HMCRAMC0_SIZE - 0x2000);
Expand Down
2 changes: 1 addition & 1 deletion ports/cxd56/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ INC += \
-I$(SPRESENSE_SDK)/nuttx/include \
-I$(SPRESENSE_SDK)/nuttx/arch \
-I$(SPRESENSE_SDK)/nuttx/arch/chip \
-I$(SPRESENSE_SDK)/nuttx/arch/os \
-I$(SPRESENSE_SDK)/sdk/bsp/include \
-I$(SPRESENSE_SDK)/sdk/bsp/include/sdk \

Expand Down Expand Up @@ -124,7 +125,6 @@ LDFLAGS = \
--entry=__start \
-nostartfiles \
-nodefaultlibs \
--defsym __stack=_vectors+786432 \
-T$(SPRESENSE_SDK)/nuttx/build/ramconfig.ld \
--gc-sections \
-Map=$(BUILD)/output.map \
Expand Down
4 changes: 2 additions & 2 deletions ports/cxd56/mpconfigport.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
#ifndef __INCLUDED_MPCONFIGPORT_H
#define __INCLUDED_MPCONFIGPORT_H

// 24kiB stack
#define CIRCUITPY_DEFAULT_STACK_SIZE 0x6000
// 64kiB stack
#define CIRCUITPY_DEFAULT_STACK_SIZE 0x10000

#include "py/circuitpy_mpconfig.h"

Expand Down
14 changes: 14 additions & 0 deletions ports/cxd56/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <stdint.h>
#include <sys/boardctl.h>

#include "sched/sched.h"

#include "boards/board.h"

#include "supervisor/port.h"
Expand Down Expand Up @@ -67,6 +69,18 @@ void reset_port(void) {
void reset_to_bootloader(void) {
}

uint32_t *port_stack_get_limit(void) {
struct tcb_s *rtcb = this_task();

return rtcb->adj_stack_ptr - (uint32_t)rtcb->adj_stack_size;
}

uint32_t *port_stack_get_top(void) {
struct tcb_s *rtcb = this_task();

return rtcb->adj_stack_ptr;
}

extern uint32_t _ebss;

// Place the word to save just after our BSS section that gets blanked.
Expand Down
8 changes: 8 additions & 0 deletions ports/nrf/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ void reset_cpu(void) {
NVIC_SystemReset();
}

uint32_t *port_stack_get_limit(void) {
return &_ebss;
}

uint32_t *port_stack_get_top(void) {
return &_estack;
}

extern uint32_t _ebss;
// Place the word to save just after our BSS section that gets blanked.
void port_set_saved_word(uint32_t value) {
Expand Down
8 changes: 8 additions & 0 deletions ports/stm32f4/supervisor/port.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ void reset_cpu(void) {
NVIC_SystemReset();
}

uint32_t *port_stack_get_limit(void) {
return &_ebss;
}

uint32_t *port_stack_get_top(void) {
return &_estack;
}

extern uint32_t _ebss;
// Place the word to save just after our BSS section that gets blanked.
void port_set_saved_word(uint32_t value) {
Expand Down
6 changes: 6 additions & 0 deletions supervisor/port.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,12 @@ void reset_board(void);
// Reset to the bootloader
void reset_to_bootloader(void);

// Get stack limit address
uint32_t *port_stack_get_limit(void);

// Get stack top address
uint32_t *port_stack_get_top(void);

// Save and retrieve a word from memory that is preserved over reset. Used for safe mode.
void port_set_saved_word(uint32_t);
uint32_t port_get_saved_word(void);
Expand Down
7 changes: 3 additions & 4 deletions supervisor/shared/memory.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
*/

#include "supervisor/memory.h"
#include "supervisor/port.h"

#include <stddef.h>

Expand All @@ -36,12 +37,10 @@ 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;
low_address = port_stack_get_limit();
high_address = port_stack_get_top();
}

void free_memory(supervisor_allocation* allocation) {
Expand Down
3 changes: 2 additions & 1 deletion supervisor/shared/stack.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "py/mpconfig.h"
#include "py/runtime.h"
#include "supervisor/cpu.h"
#include "supervisor/port.h"
#include "supervisor/shared/safe_mode.h"

extern uint32_t _estack;
Expand All @@ -43,7 +44,7 @@ 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;
mp_uint_t c_size = (uint32_t) port_stack_get_top() - sp;

stack_alloc = allocate_memory(c_size + next_stack_size + EXCEPTION_STACK_SIZE, true);
if (stack_alloc == NULL) {
Expand Down