Skip to content

Cleanup ELF loading and handle non page-aligned section ends corretly#1155

Draft
joergroedel wants to merge 14 commits into
coconut-svsm:mainfrom
joergroedel:elf-fixes
Draft

Cleanup ELF loading and handle non page-aligned section ends corretly#1155
joergroedel wants to merge 14 commits into
coconut-svsm:mainfrom
joergroedel:elf-fixes

Conversation

@joergroedel

Copy link
Copy Markdown
Member

This cleans up the ELF loading code for user-mode binaries and adds handling for non page-aligned section-ends. When a writable section does not end at a page boundary, the rest of the page is zeroed out.

With these changes the user-mode binaries do no longer need a custom linker script which enforces the alignment. So this is removed as well.

These changes depend on #1152 and #1153.
Fixes: #483

@joergroedel joergroedel changed the title kernel: Cleanup ELF loading and handle non page-aligned section ends corretly Cleanup ELF loading and handle non page-aligned section ends corretly Jul 3, 2026
Initialize the kernel-stack of a new user-mode task using one
structure and initialize it in one place.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Initialize the stack of a new kernel task from a single structure to
make it more similar to user-task stack initialization.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Check at compile time whether the stack-frame after ret_addr is
16b-aligned. Also consolidate the check for kernel and user tasks.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
The initialization routines for user- and kernel-tasks basically do
the same thing, except that they use different context structures for
each case.

Consolidate the common parts in allocate_stack_common() and make that
function generic.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
User-tasks use a different launch path than kernel-tasks. Kernel-tasks
go directly to a Rust function on the `ret` in `switch_context` while
for user-tasks the `ret`-target is an assembly function which calls
the `setup_user_task()` helper and then goes into the `iret` path.

This causes some fraction in how the initial stack layouts of both
task types are set up, so consolidate this to be more similar in both
cases.

Kernel- and User thread now use a common thread entry in assembly code,
which unifies the initial thread setup.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
When loading a user ELF binary for execution the COCONUT kernel created a new
Task object and mapped the ELF sections into the new tasks user-VMR. This all
happened from the context of the old task.

This behavior is problematic as it creates unnecessary complexity for cases
when the kernel needs to write values into the user address space. Fix this
behavior by moving the actual ELF loading into the context of the new task.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Make the shadow stack initialization more readable by introducing an
initializer struct with push semantics.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
The label must point at the potentially faulting RIP in order for the
exception to be handled correctly. Fix the label in copy_bytes().

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
The page-fault handler did not handle faults to the user memory region
correctly when they were coming from kernel code. Rewrite the handler
to implement the right behavior.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Some structs in the ELF parser code miss the Copy + Clone annotations,
so add them.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Provide a function to safely zero user-space memory from kernel mode.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Re-work the ELF loader code and make it more spec compliant. Also make
sure to zero any remaining page-memory when a writable segment does
not end at a page boundary.

Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
Now that the kernel ELF loader can handle unaligned segments
correctly there is no need anymore for a custom linker script for
user-mode binaries.

Fixes: coconut-svsm#483
Signed-off-by: Joerg Roedel <joerg.roedel@amd.com>
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.

Fix ELF Parser Alignment Handling, Remove Custom Linker Script, and Improve Handling of .bss Section in Binary Parsing

1 participant