Skip to content
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

cpu/{atmega_common,atxmega}: increase idle thread stack size #18263

Merged
merged 2 commits into from
Jun 28, 2022

Conversation

maribu
Copy link
Member

@maribu maribu commented Jun 27, 2022

Contribution description

Our AVR port doesn't make use of an ISR stack and just victimizes the
stack of whatever thread happens to be running, which in most cases is
the idle thread. Hence, the idle stack has to be large enough to
support the ztimer ISR.

Testing procedure

Run program given in #18245 (comment) - with master you'll get something like

2022-06-27 12:49:44,725 # main(): This is RIOT! (Version: 2022.07-devel-898-gd9fc08)
2022-06-27 12:49:45,770 # scheduler(): stack overflow detected, pid=1
2022-06-27 12:49:46,815 # scheduler(): stack overflow detected, pid=1
2022-06-27 12:49:47,859 # scheduler(): stack overflow detected, pid=1

but with this PR the stack overflow should no longer be detected. (Test successfully with both Alpine's AVR toolchain in BUILD_IN_DOCKER=1.)

Issues/PRs references

Reported in #18245 (comment)

@maribu maribu requested a review from kYc0o as a code owner June 27, 2022 11:08
@maribu maribu added Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors) Platform: AVR Platform: This PR/issue effects AVR-based platforms labels Jun 27, 2022
@github-actions github-actions bot added the Area: cpu Area: CPU/MCU ports label Jun 27, 2022
@benpicco benpicco requested a review from kaspar030 June 27, 2022 12:08
@benpicco benpicco added the CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR label Jun 27, 2022
#else
#define THREAD_STACKSIZE_IDLE (192)
#define THREAD_STACKSIZE_IDLE (128)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same, 192 -> 128

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy-pasted error :/ Good catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does ATXMega need more stack @nandojve?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To some degree this can be explained with larger AVRs the RAMPZ, RAMPY, RAMPX, RAMPD and the 3 byte program counter, so that 5 bytes more are pushed onto the stack in case of context switching or IRQs. But the diff is quite a bit more than 5 bytes.

(Note that these registers are also not unique to ATXMegas, the better ATMegas can also have them.)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benpicco , @maribu is correct! It is important mention that call on large devices have a 24-bit PC. So, depending how code is generated it may be good have some additional stack space. This is not restricted to ATXmega it is about devices that have EIND.

(Rare) models with >128 KiB of ROM have a 3-byte program counter. Subroutine calls and returns use an additional byte of stack space, there is a new EIND register to provide additional high bits for indirect jumps and calls, and there are new extended instructions EIJMP and EICALL which use EIND:Z as the destination address. (The previous IJMP and ICALL instructions use zero-extended Z.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, of course, the add additional byte for $pc is not only needed in the context switch but for every function call :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still does this require to double the required stack size?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can later run a test to measure what exact size it needs. I also intend to merge this to a common header in avr8_common, which takes __AVR_3_BYTE_PC__, __AVR_HAVE_RAMPZ__, __AVR_HAVE_RAMPY__, __AVR_HAVE_RAMPX__, and __AVR_HAVE_RAMPD__ into account for the stack size, as some ATmega boards also have large stack requirements as well.

Our AVR port doesn't make use of an ISR stack and just victimizes the
stack of whatever thread happens to be running, which in most cases is
the idle thread. Hence, the idle stack has to be large enough to
support the ztimer ISR.
@maribu maribu force-pushed the cpu/avr8/idle_stack branch from 1a3a31f to 8cc0199 Compare June 27, 2022 12:40
@maribu maribu requested a review from miri64 as a code owner June 27, 2022 15:07
@github-actions github-actions bot added the Area: tests Area: tests and testing framework label Jun 27, 2022
@maribu maribu removed the Area: tests Area: tests and testing framework label Jun 27, 2022
@maribu
Copy link
Member Author

maribu commented Jun 28, 2022

Hmpf, the unrelated hash mismatches are still a thing

@maribu maribu added CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR and removed CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR labels Jun 28, 2022
@benpicco benpicco merged commit a1ee44e into RIOT-OS:master Jun 28, 2022
@maribu maribu deleted the cpu/avr8/idle_stack branch June 29, 2022 06:41
@chrysn chrysn added this to the Release 2022.07 milestone Aug 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: cpu Area: CPU/MCU ports CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: skip compile test If set, CI server will run only non-compile jobs, but no compile jobs or their dependent jobs Platform: AVR Platform: This PR/issue effects AVR-based platforms Type: bug The issue reports a bug / The PR fixes a bug (including spelling errors)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants