Skip to content

Spinlocks and hartlocks#87

Open
auctumnus wants to merge 32 commits intoUMN-Kernel-Object:remexre/tasksfrom
auctumnus:remexre/tasks
Open

Spinlocks and hartlocks#87
auctumnus wants to merge 32 commits intoUMN-Kernel-Object:remexre/tasksfrom
auctumnus:remexre/tasks

Conversation

@auctumnus
Copy link
Copy Markdown

@auctumnus auctumnus commented Jan 30, 2026

This PR adds spinlock and hartlock functionality.

Somewhat supersedes part of #33.

@auctumnus auctumnus changed the title Remexre/tasks Spinlocks Jan 30, 2026
// TODO: move this over to insns.h?
#define RISCV64_CSR_SSTATUS_SIE (1 << 1)

// TODO: consider making these common across architectures somehow?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i kinda want these to be private to hartlocks, but they'd still need to be per-arch...

tbh, could you prototype them in hartlock.c and implement them in arch/riscv64/hartlock.c or smth?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

private to hartlocks as in the spinlocks would take a hartlock instead?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

or, well, in addition to being a spinlock, they would also take the hartlock

Comment on lines +28 to +29
// Otherwise, we might get interrupted and then attempt to re-acquire the same
// spinlock, leading to a deadlock.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

i'm missing what the problematic sequence of events here is; shouldn't the atomics make this not a problem?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

hmm .... i guess on second thought it shouldn't be a huge problem, i forgot that if we get interrupted we would end up dropping the lock before returning back

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

(e.g. if we went

  1. loop trying to acquire it while it's locked
  2. get interrupted
  3. try to acquire the same lock
    then this is fine, it's locked by a different hart which should eventually drop it)

#include <insns.h>
#include <types.h>

// TODO: move this over to insns.h?
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

yes; make it an enum blah : u64 too, so we get warnings if we try to use it in a too-small type

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

ig i didn't write down my problem here which is that nominally it's more of a struct than an enum; i.e. there are fields in sstatus that are more than 1 bit in size, and i'm unsure how to nicely represent that

@remexre remexre force-pushed the remexre/tasks branch 6 times, most recently from 241654e to cf06aaf Compare February 14, 2026 17:07
@remexre
Copy link
Copy Markdown
Member

remexre commented Feb 19, 2026

still todo:

  • hartlocks
  • bitfields
  • terrible awful ub at the end of the file

@remexre remexre force-pushed the remexre/tasks branch 7 times, most recently from ba047e3 to e277ac4 Compare February 28, 2026 19:21
Signed-off-by: Amy Ringo <me@remexre.com>
These functions are for mapping IO memory.

Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Closes UMN-Kernel-Object#90.

Note that this depends on GCC 15 (UMN-Kernel-Object#82) as well. GCC 16 will let us
simplify the -march line to -march=rva23s64, but it's not released at
the time of writing.

Signed-off-by: Amy Ringo <me@remexre.com>
This makes it easier to test concurrency-related code.

Signed-off-by: Amy Ringo <me@remexre.com>
This is used by make watch.

Signed-off-by: Amy Ringo <me@remexre.com>
The {bytes:?} behavior doesn't match what Rust does, but it's a
useful behavior for "typically a string" fields.

Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
@remexre remexre force-pushed the remexre/tasks branch 2 times, most recently from 0d69730 to 56e119a Compare February 28, 2026 20:41
We need to ensure that tasks are only scheduled on harts with the same
register_save_area size, meaning we need to track which harts have which
size.

It's easiest to keep track of this if we hook that tracking into
ordinary device detection, so this device class exists for that purpose.

Because init_boothart_hart_locals_late panics if it cannot find the hart
device for the boothart, this will not boot at the moment, though it
will build. The next patch fixes this.

Signed-off-by: Amy Ringo <me@remexre.com>
This restores the ability to boot, since the hart will now be properly
detected.

Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
Signed-off-by: Amy Ringo <me@remexre.com>
@auctumnus auctumnus changed the title Spinlocks Spinlocks and hartlocks Mar 14, 2026
remexre and others added 7 commits March 14, 2026 15:46
Signed-off-by: Autumn <auctumnus@pm.me>
Signed-off-by: Autumn <auctumnus@pm.me>
Signed-off-by: Autumn <auctumnus@pm.me>
Signed-off-by: Autumn <auctumnus@pm.me>
Co-authored-by: remexre <me@remexre.com>
Signed-off-by: autumn <auctumnus@pm.me>
Signed-off-by: Autumn <auctumnus@pm.me>
Signed-off-by: Autumn <auctumnus@pm.me>
Signed-off-by: Autumn <auctumnus@pm.me>
@auctumnus
Copy link
Copy Markdown
Author

responsible ai disclosure: i got claudeパイセン to thread struct hartlock *hartlock thru the call graph in the alloc init stuff

@remexre remexre force-pushed the remexre/tasks branch 4 times, most recently from c62a624 to 3709edb Compare March 28, 2026 17:47
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