Skip to content

Commit

Permalink
Merge pull request #21 from stevenewald/stack-alignment
Browse files Browse the repository at this point in the history
Hardcode 8 byte exception stack alignment
  • Loading branch information
stevenewald authored Nov 8, 2024
2 parents 70b14ad + 4fb1571 commit b5c0b4f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 23 deletions.
3 changes: 3 additions & 0 deletions include/scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ struct stack_registers {
unsigned FP_REGS[16]{};
unsigned FPSCR{};

// diagram: https://shorturl.at/85lyY
unsigned RESERVED_FOR_STACK_ALIGNMENT[2];

stack_registers(unsigned return_addr) : RETURN_ADDR(return_addr) {}
};

Expand Down
25 changes: 2 additions & 23 deletions src/scheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,19 +121,8 @@ __attribute__((used, naked)) void restore_regs()
// Pop caller saved FP registers
asm volatile("vpop {s0-s15}");

// *sigh*
// We need to account for whether sp is 4- or 8-byte aligned
// diagram: https://shorturl.at/85lyY
asm volatile("push {r0}\n"
"mrs r0, psp\n"
"tst r0, #0x4\n"
"pop {r0}\n"
"ite eq\n"
"ADDEQ SP, #8\n"
"ADDNE SP, #4");

// Skip FPSCR (already loaded)
asm volatile("add sp, #4");
// Skip FPSCR and 2 reserved regs
asm volatile("add sp, #12");

asm volatile("ldr pc, [sp, #-84]");
}
Expand All @@ -155,16 +144,6 @@ void Scheduler::yield_current_task()
t.stack_ptr_loc = (unsigned*)__get_PSP();
auto stored_registers = reinterpret_cast<stack_registers*>(t.stack_ptr_loc);

// Account for stack pointer alignment
// diagram: https://shorturl.at/85lyY
bool eight_byte_aligned = ((unsigned)t.stack_ptr_loc) & 0x7;
if (eight_byte_aligned) {
t.stack_ptr_loc -= 1;
}
else {
t.stack_ptr_loc -= 2;
}

// "Push" registers, create a fake stack frame
// This will be popped by the exception handler
t.stack_ptr_loc -= (sizeof(stack_registers) / sizeof(unsigned));
Expand Down

0 comments on commit b5c0b4f

Please sign in to comment.