Skip to content

Kernel: Adjustments to unsafe code - #2600

Merged
jamesmunns merged 10 commits into
masterfrom
james/unsafe-tweaks
Aug 14, 2026
Merged

Kernel: Adjustments to unsafe code#2600
jamesmunns merged 10 commits into
masterfrom
james/unsafe-tweaks

Conversation

@jamesmunns

Copy link
Copy Markdown
Contributor

The following is a number of tweaks to the kernel code that in my opinion, make some of the unsafe code a bit more direct to follow. In particular, these changes:

  1. Consolidates switch_to as a single method on Task, rather than one version in syscalls.rs, and a handful of semantically-equal copy/pastes
  2. Removes as many single/double blind as casts as is reasonable, e.g. x as *const _ as *mut _, replacing them with discrete steps that note the implicit casts (references to pointer), and explicit casts, using ptr::cast()
  3. Reduces unsafe usage a bit in startup when initializing each task slot
  4. Makes some methods take &mut Task instead of &Task, as these are used for setting CURRENT_TASK_PTR, which semantically allows us to write-through to that address (often in assembly) when returning to the kernel, and that feels a little sketch to me doing from a pointer with shared provenance
  5. Adjusts the codegen for Task slots from MaybeUninit<[Task; N]> to [MaybeUninit<Task>; N], as this makes startup a little less awkward

In my opinion, none of these are strictly necessary for soundness, so if we don't want to touch it, I could definitely understand that!

I think 1, 2, and 3 are pretty clear readability/clarity wins.

I think 4 is maybe a little nitpicky, and is a bit more semantically correct, but also unlikely to cause miscompilations in practice.

I think 5 is maybe a mixed bag (we can still make the changes for 3 without 5), and I need to check it isn't going to make humility upset as the types have changed (even though the before/after are guaranteed to have the same layout and semantics).

Happy to revert any chunks, or rework these into separate commits if that makes reviewing easier.

This is extracted from #2592, and most of these were noticed while I was hacking around on that.

@jamesmunns
jamesmunns requested review from cbiffle, hawkw and labbott July 17, 2026 12:23
@jamesmunns

Copy link
Copy Markdown
Contributor Author

Annoyingly, the RAM usage for g070 didn't increase because we are using more, it's just a quirk that the linker isn't packing statics as intelligently as it possibly could.

Screenshot 2026-07-17 at 15 14 40

Comment thread sys/kern/build.rs
Comment thread sys/kern/src/startup.rs Outdated
Comment thread sys/kern/src/task.rs
Comment thread app/donglet/app-g031.toml
Comment thread sys/kern/src/arch/arm_m.rs
Comment thread sys/kern/src/arch/arm_m.rs

@aapoalas aapoalas left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Please excuse my unrequested comments; the topic called out to me.

Comment thread app/donglet/app-g031-i2c.toml Outdated
Comment thread sys/kern/src/task.rs
Comment thread sys/kern/src/arch/arm_m.rs
Comment thread sys/kern/src/task.rs Outdated
jamesmunns added a commit that referenced this pull request Aug 6, 2026
also add a contrasting comment vs static-cell

Similar to #2600.
@jamesmunns

Copy link
Copy Markdown
Contributor Author

Hey @labbott or @hawkw, any chance I could get an approve on this? I'd like to get it merged early in this release cycle if possible.

@labbott labbott left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

thought I already approved it whoops

@jamesmunns
jamesmunns merged commit 7fdd888 into master Aug 14, 2026
190 checks passed
@jamesmunns
jamesmunns deleted the james/unsafe-tweaks branch August 14, 2026 14:29
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.

4 participants