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

🐛 fix bug in crt0.S interrupt setup #297

Merged
merged 4 commits into from
Apr 8, 2022
Merged

🐛 fix bug in crt0.S interrupt setup #297

merged 4 commits into from
Apr 8, 2022

Conversation

stnolting
Copy link
Owner

@stnolting stnolting commented Apr 8, 2022

This PR fixes a bug in the processor startup code sw/common/crt0.S.

Bug Description

Imagine the following scenario:

  • We are running the bootloader and want to upload a new program using the on-chip debugger. The bootloader uses the MTI interrupt (machine timer interrupt) for the "heart beat" LED and also for the auto-boot timeout.
  • We halt the CPU using openOCD + GDB - so we are starting a debug session. The CPU is halted and the machine timer interrupt will enqueue in the mip CSR.
  • We upload the new program and start executing it.
  • The crt0 code of the new program problem will (re-)init the stackpointer and will also override the entry point for all exceptions (mtvec) as well as disabling all interrupts.
  • However, before all this is tone, the pending machine timer interrupt might kick in.
  • At this point the stack might have already been setup for the new program while the exception entry point (mtvec) and all the interrupt configuration have not. Hence, the bootloader's trap handler gets called but with a modified stack that has already been setup for the new program.
  • The result is a stack corruption making the CPU crash. 💥

Bug Fix

The very first thing crt0.S has to do is to clear the global interrupts enable flag (mstatus.mie).

The first thing crt0 has t do is to disable interrupts globally!
@stnolting stnolting added bug Something isn't working SW software-related labels Apr 8, 2022
@stnolting stnolting self-assigned this Apr 8, 2022
@stnolting stnolting changed the title 🐛 fix crt0 IRQ setup 🐛 fix bug in crt0.S interrupt setup Apr 8, 2022
@stnolting stnolting marked this pull request as ready for review April 8, 2022 08:04
@stnolting stnolting merged commit f6bb7fe into main Apr 8, 2022
@stnolting stnolting deleted the bugfix_crt0_irqs branch April 8, 2022 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working SW software-related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant