From 30c9ad2b2a8687f962847f73f2533ce26118ddca Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 18 Oct 2019 10:29:32 +0200 Subject: [PATCH 1/5] Update Spresense SDK to 1.4.1 --- ports/cxd56/spresense-exported-sdk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/cxd56/spresense-exported-sdk b/ports/cxd56/spresense-exported-sdk index b473b28a14..7f6568c7f4 160000 --- a/ports/cxd56/spresense-exported-sdk +++ b/ports/cxd56/spresense-exported-sdk @@ -1 +1 @@ -Subproject commit b473b28a14a03f3d416b6e2c071bcfd4fb92cb63 +Subproject commit 7f6568c7f4898cdb24a2f06040784a836050686e From 96756b3945ec3388986c7ebda8b924aeb42cd7fb Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 18 Oct 2019 11:00:09 +0200 Subject: [PATCH 2/5] Add functions to get top and limit stack --- ports/atmel-samd/supervisor/port.c | 8 ++++++++ ports/cxd56/supervisor/port.c | 8 ++++++++ ports/nrf/supervisor/port.c | 8 ++++++++ ports/stm32f4/supervisor/port.c | 8 ++++++++ supervisor/port.h | 6 ++++++ 5 files changed, 38 insertions(+) diff --git a/ports/atmel-samd/supervisor/port.c b/ports/atmel-samd/supervisor/port.c index 512fd8eb8d..e962281c1e 100644 --- a/ports/atmel-samd/supervisor/port.c +++ b/ports/atmel-samd/supervisor/port.c @@ -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); diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c index 3fddfe52c6..9688cf2333 100644 --- a/ports/cxd56/supervisor/port.c +++ b/ports/cxd56/supervisor/port.c @@ -67,6 +67,14 @@ void reset_port(void) { void reset_to_bootloader(void) { } +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. diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index c69101070e..af858aa4a1 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -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) { diff --git a/ports/stm32f4/supervisor/port.c b/ports/stm32f4/supervisor/port.c index df7a6ca427..502202f522 100644 --- a/ports/stm32f4/supervisor/port.c +++ b/ports/stm32f4/supervisor/port.c @@ -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) { diff --git a/supervisor/port.h b/supervisor/port.h index 1430c6a505..c8a0119788 100644 --- a/supervisor/port.h +++ b/supervisor/port.h @@ -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); From f3151bb6c4adac5ed9cf03d3977ab6d9662c0e05 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 18 Oct 2019 11:05:08 +0200 Subject: [PATCH 3/5] Use get top and limit stack functions --- main.c | 2 +- supervisor/shared/memory.c | 7 +++---- supervisor/shared/stack.c | 3 ++- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/main.c b/main.c index 6b56f5ff6f..8715672ea9 100755 --- a/main.c +++ b/main.c @@ -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(); } diff --git a/supervisor/shared/memory.c b/supervisor/shared/memory.c index 11133415d1..38040d11d9 100755 --- a/supervisor/shared/memory.c +++ b/supervisor/shared/memory.c @@ -25,6 +25,7 @@ */ #include "supervisor/memory.h" +#include "supervisor/port.h" #include @@ -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) { diff --git a/supervisor/shared/stack.c b/supervisor/shared/stack.c index 311fa31b22..dcecf2067b 100755 --- a/supervisor/shared/stack.c +++ b/supervisor/shared/stack.c @@ -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; @@ -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) { From 4338511b28b98d66ad5f03a36a21a45d83a86a90 Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 18 Oct 2019 11:10:22 +0200 Subject: [PATCH 4/5] Add get top and limit functions for Spresense --- ports/cxd56/Makefile | 2 +- ports/cxd56/supervisor/port.c | 10 ++++++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/ports/cxd56/Makefile b/ports/cxd56/Makefile index 38ebd83050..d65c2e2666 100644 --- a/ports/cxd56/Makefile +++ b/ports/cxd56/Makefile @@ -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 \ @@ -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 \ diff --git a/ports/cxd56/supervisor/port.c b/ports/cxd56/supervisor/port.c index 9688cf2333..78d2f13f5e 100644 --- a/ports/cxd56/supervisor/port.c +++ b/ports/cxd56/supervisor/port.c @@ -27,6 +27,8 @@ #include #include +#include "sched/sched.h" + #include "boards/board.h" #include "supervisor/port.h" @@ -68,11 +70,15 @@ void reset_to_bootloader(void) { } uint32_t *port_stack_get_limit(void) { - return &_ebss; + struct tcb_s *rtcb = this_task(); + + return rtcb->adj_stack_ptr - (uint32_t)rtcb->adj_stack_size; } uint32_t *port_stack_get_top(void) { - return &_estack; + struct tcb_s *rtcb = this_task(); + + return rtcb->adj_stack_ptr; } extern uint32_t _ebss; From e2cb29f2a0ca07c8b8b089675969e11d62cd13ac Mon Sep 17 00:00:00 2001 From: Kamil Tomaszewski Date: Fri, 18 Oct 2019 11:12:47 +0200 Subject: [PATCH 5/5] Change default stack size to 64kiB for Spresense --- ports/cxd56/mpconfigport.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ports/cxd56/mpconfigport.h b/ports/cxd56/mpconfigport.h index 233b7b9a7b..91ebfd98f6 100644 --- a/ports/cxd56/mpconfigport.h +++ b/ports/cxd56/mpconfigport.h @@ -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"