Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 5091

General • Re: How can I ensure memory coherence between cores?

$
0
0
So, I think I now understand what's happening in the full application. When I'm only using a single core, the stack can extend beyond the default 2kb limit because the memory it overwrites isn't used. The heap is the only other dynamic consumer of memory, and it's at the other end of the address space, so as long we don't allocate too much it won't use the area that the over-sized stack is consuming.

That means that programs that use more than 2kb of stack appear to run with no apparent issues, *until* something is run on the second core. As far as I can tell, the first core's stack starts at SCRATCH_Y (0x20041000) and proceeds downwards from there. The second core's stack is placed at SCRATCH_X (0x20040000), which is 4kb below the first core's stack. This means that it will stomp on values in the first core's stack if that stack consumes more than 4kb. All values are derived from https://github.com/raspberrypi/pico-sdk ... default.ld

The underlying bug is that the application code is using more than the maximum of 2kb of stack. What's tricky is that this won't cause any issues until code is run on the second core (or the heap impinges on the same area). Luckily my actual application code is pretty sparing of stack space, so this only affects the TFLM tests which use the stack for data arrays. I will look into how I can rearrange the memory map to avoid this issue (maybe the stack for the second core starts at SCRATCH_Y, with the first core's stack at SCRATCH_X?) but I wanted to leave a trail of breadcrumbs for anyone else who might run into this in the future. Thanks everyone for the help and suggestions!
Thanks for the update. We had a bit of trouble with overflowing stacks when porting BBC Basic to the Pico. Sometimes stack frames get surprisingly large due to the way temporary variables are allocated when the default -O3 optimization aggressively inlines code.

Our trail of breadcrumbs starts at

viewtopic.php?t=316761

and eventually led to custom linker scripts. It seems there were a lot of breadcrumbs.

Statistics: Posted by ejolson — Mon Jan 01, 2024 11:08 pm



Viewing all articles
Browse latest Browse all 5091

Trending Articles