Skip to content

threads: set stack pointer correctly for x86-64#120

Merged
lsf37 merged 1 commit into
masterfrom
lsf37/threads-stack
Jun 1, 2026
Merged

threads: set stack pointer correctly for x86-64#120
lsf37 merged 1 commit into
masterfrom
lsf37/threads-stack

Conversation

@lsf37
Copy link
Copy Markdown
Member

@lsf37 lsf37 commented Jun 1, 2026

The tutorial was setting the stack pointer on x86-64 such that it was not aligned to 16 bytes. gcc 14.2 emits SSE instructions that require 16 byte alignment and fault when the stack is not correctly aligned.

The misalignment stems from the fact that x86-64 expects a return address on the stack when the function is entered, but we were passing the top of the stack instead. The Arm ABI has the return address in the link register instead.

Explain this difference in the tutorial and adjust the code accordingly. The later helper function sel4utils_arch_init_local_context already did this directly.

The tutorial was setting the stack pointer on x86-64 such that it was
not aligned to 16 bytes. gcc 14.2 emits SSE instructions that require
16 byte alignment and fault when the stack is not correctly aligned.

The misaligned stems from the fact that x86-64 expects a return address
on the stack when the function is entered, but we were passing the top
of the stack instead. The Arm ABI has the return address in the link
register instead.

Explain this difference in the tutorial and adjust the code accordingly.
The later helper function `sel4utils_arch_init_local_context` already
did this directly.

Signed-off-by: Gerwin Klein <gerwin.klein@proofcraft.systems>
Copy link
Copy Markdown

@midnightveil midnightveil left a comment

Choose a reason for hiding this comment

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

Is that TODO for you or the tutorial user? (the latter seems a bit weird?).

@lsf37
Copy link
Copy Markdown
Member Author

lsf37 commented Jun 1, 2026

Is that TODO for you or the tutorial user? (the latter seems a bit weird?).

It's for the tutorial user, the tutorials all use TODO tags to mark the bits in the template that should be filled in.

This one is indeed a bit weird, but it was worse before, because the text, the template, and the solution all said different things about whether or not to use sel4utils_arch_init_local_context function. Introducing the sel4utils_arch_init_local_context function earlier would theoretically work, but then the stack pointer issue would be hidden and would start to hit people if they actually started using things.

@lsf37 lsf37 merged commit dd76f3d into master Jun 1, 2026
27 checks passed
@lsf37 lsf37 deleted the lsf37/threads-stack branch June 1, 2026 01:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants