Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions kernel/boot/boot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "drivers/input/input.h"
#include "net/net.h"
#include "random/random.h"
#include "sync/futex.h"

#ifdef STLX_UNIT_TESTS_ENABLED
#include "runner.h"
Expand Down Expand Up @@ -107,6 +108,8 @@ extern "C" __PRIVILEGED_CODE void stlx_init() {
log::fatal("rc::reaper::init failed");
}

sync::futex_init();

if (fs::init() != fs::OK) {
log::fatal("fs::init failed");
}
Expand Down
193 changes: 193 additions & 0 deletions kernel/sync/futex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
#include "sync/futex.h"
#include "sync/spinlock.h"
#include "sched/sched.h"
#include "sched/task.h"
#include "mm/uaccess.h"
#include "mm/vma.h"
#include "common/hash.h"
#include "common/string.h"
#include "clock/clock.h"
#include "timer/timer.h"

namespace sync {

constexpr uint32_t WAKE_BATCH_SIZE = 16;

__PRIVILEGED_BSS static futex_bucket g_futex_table[FUTEX_BUCKET_COUNT];

__PRIVILEGED_CODE void futex_init() {
for (uint32_t i = 0; i < FUTEX_BUCKET_COUNT; i++) {
g_futex_table[i].lock = SPINLOCK_INIT;
g_futex_table[i].waiters.init();
}
}

__PRIVILEGED_CODE static uint32_t futex_hash(mm::mm_context* mm, uintptr_t addr) {
uint64_t h = hash::combine(hash::ptr(mm), hash::u64(addr));
return static_cast<uint32_t>(h) & FUTEX_BUCKET_MASK;
}

__PRIVILEGED_CODE int32_t futex_wait(uintptr_t uaddr, uint32_t expected,
uint64_t timeout_ns) {
sched::task* self = sched::current();
mm::mm_context* mm = self->exec.mm_ctx;
if (uaddr & 0x3) return -22; // EINVAL

// Read the value before taking the bucket lock. copy_from_user
// acquires mm_ctx->lock (a sleeping mutex) so it must not be called
// under a spinlock. This also faults in the page so the re-read
// under the spinlock below is safe.
uint32_t pre_val;
if (mm) {
int32_t rc = mm::uaccess::copy_from_user(
&pre_val, reinterpret_cast<const void*>(uaddr), sizeof(uint32_t));
if (rc != 0) return -14; // EFAULT
} else {
string::memcpy(&pre_val, reinterpret_cast<const void*>(uaddr),
sizeof(uint32_t));
}

// Early exit: if the value already changed, no need to lock the bucket.
if (pre_val != expected) return -11; // EAGAIN

uint32_t idx = futex_hash(mm, uaddr);
futex_bucket* bucket = &g_futex_table[idx];

futex_waiter waiter;
waiter.task = self;
waiter.mm = mm;
waiter.addr = uaddr;
waiter.link = {};

irq_state irq = spin_lock_irqsave(bucket->lock);

// Re-read the futex word under the bucket lock. The page is already
// validated/faulted by the copy_from_user above, so a direct read
// is safe here. This atomic check-and-enqueue prevents lost wakeups.
uint32_t current_val;
string::memcpy(&current_val, reinterpret_cast<const void*>(uaddr),
sizeof(uint32_t));

if (current_val != expected) {
spin_unlock_irqrestore(bucket->lock, irq);
return -11; // EAGAIN
}

self->state = sched::TASK_STATE_BLOCKED;
bucket->waiters.push_back(&waiter);

if (timeout_ns > 0) {
uint64_t deadline = clock::now_ns() + timeout_ns;
timer::schedule_sleep(self, deadline);
}

spin_unlock_irqrestore(bucket->lock, irq);
sched::yield();

// Cancel any outstanding timer to prevent spurious wakes of future
// blocking operations if we were woken by futex_wake before timeout.
timer::cancel_sleep(self);

// Remove self from bucket if still linked (timeout or kill wakeup).
bool was_linked = false;
irq = spin_lock_irqsave(bucket->lock);
if (waiter.link.is_linked()) {
bucket->waiters.remove(&waiter);
was_linked = true;
}
spin_unlock_irqrestore(bucket->lock, irq);

if (sched::is_kill_pending()) return -4; // EINTR
if (was_linked) return -110; // ETIMEDOUT
return 0;
}

__PRIVILEGED_CODE int32_t futex_wake(uintptr_t uaddr, uint32_t count) {
sched::task* self = sched::current();
mm::mm_context* mm = self->exec.mm_ctx;
if (uaddr & 0x3) return -22; // EINVAL
if (count == 0) return 0;

uint32_t idx = futex_hash(mm, uaddr);
futex_bucket* bucket = &g_futex_table[idx];

uint32_t total_woken = 0;

for (;;) {
sched::task* batch[WAKE_BATCH_SIZE];
uint32_t n = 0;
bool done = false;

irq_state irq = spin_lock_irqsave(bucket->lock);

auto it = bucket->waiters.begin();
auto end = bucket->waiters.end();
while (it != end && n < WAKE_BATCH_SIZE) {
futex_waiter& w = *it;
++it; // advance before removal
if (w.mm == mm && w.addr == uaddr) {
bucket->waiters.remove(&w);
batch[n++] = w.task;
if (total_woken + n >= count) {
done = true;
break;
}
}
}

if (n == 0) done = true;
spin_unlock_irqrestore(bucket->lock, irq);

for (uint32_t i = 0; i < n; i++) {
sched::wake(batch[i]);
}
total_woken += n;

if (done) break;
}

return static_cast<int32_t>(total_woken);
}

__PRIVILEGED_CODE int32_t futex_wake_all(uintptr_t uaddr) {
sched::task* self = sched::current();
mm::mm_context* mm = self->exec.mm_ctx;
if (uaddr & 0x3) return -22; // EINVAL

uint32_t idx = futex_hash(mm, uaddr);
futex_bucket* bucket = &g_futex_table[idx];

uint32_t total_woken = 0;

for (;;) {
sched::task* batch[WAKE_BATCH_SIZE];
uint32_t n = 0;

irq_state irq = spin_lock_irqsave(bucket->lock);

auto it = bucket->waiters.begin();
auto end = bucket->waiters.end();
while (it != end && n < WAKE_BATCH_SIZE) {
futex_waiter& w = *it;
++it;
if (w.mm == mm && w.addr == uaddr) {
bucket->waiters.remove(&w);
batch[n++] = w.task;
}
}

bool drained = (n == 0);
spin_unlock_irqrestore(bucket->lock, irq);

for (uint32_t i = 0; i < n; i++) {
sched::wake(batch[i]);
}
total_woken += n;

if (drained) break;
}

return static_cast<int32_t>(total_woken);
}

} // namespace sync
56 changes: 56 additions & 0 deletions kernel/sync/futex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#ifndef STELLUX_SYNC_FUTEX_H
#define STELLUX_SYNC_FUTEX_H

#include "common/types.h"
#include "common/list.h"
#include "sync/spinlock.h"

namespace sched { struct task; }
namespace mm { struct mm_context; }

namespace sync {

struct futex_waiter {
sched::task* task;
mm::mm_context* mm;
uintptr_t addr;
list::node link;
};

struct futex_bucket {
spinlock lock;
list::head<futex_waiter, &futex_waiter::link> waiters;
};

constexpr uint32_t FUTEX_BUCKET_COUNT = 256;
constexpr uint32_t FUTEX_BUCKET_MASK = FUTEX_BUCKET_COUNT - 1;

/**
* Initialize the futex hash table. Call once during boot after sched::init().
* @note Privilege: **required**
*/
__PRIVILEGED_CODE void futex_init();

/**
* Block if *uaddr == expected. timeout_ns=0 means wait indefinitely.
* Returns 0 on wake, -EAGAIN on mismatch, -ETIMEDOUT, -EINTR, -EFAULT.
* @note Privilege: **required**
*/
__PRIVILEGED_CODE int32_t futex_wait(uintptr_t uaddr, uint32_t expected,
uint64_t timeout_ns);

/**
* Wake up to count threads waiting on uaddr. Returns number woken.
* @note Privilege: **required**
*/
__PRIVILEGED_CODE int32_t futex_wake(uintptr_t uaddr, uint32_t count);

/**
* Wake all threads waiting on uaddr. Returns number woken.
* @note Privilege: **required**
*/
__PRIVILEGED_CODE int32_t futex_wake_all(uintptr_t uaddr);

} // namespace sync

#endif // STELLUX_SYNC_FUTEX_H
19 changes: 19 additions & 0 deletions kernel/syscall/handlers/sys_futex.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "syscall/handlers/sys_futex.h"
#include "sync/futex.h"

DEFINE_SYSCALL3(futex_wait, uaddr, expected, timeout_ns) {
return sync::futex_wait(
static_cast<uintptr_t>(uaddr),
static_cast<uint32_t>(expected),
timeout_ns);
}

DEFINE_SYSCALL2(futex_wake, uaddr, count) {
return sync::futex_wake(
static_cast<uintptr_t>(uaddr),
static_cast<uint32_t>(count));
}

DEFINE_SYSCALL1(futex_wake_all, uaddr) {
return sync::futex_wake_all(static_cast<uintptr_t>(uaddr));
}
10 changes: 10 additions & 0 deletions kernel/syscall/handlers/sys_futex.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef STELLUX_SYSCALL_HANDLERS_SYS_FUTEX_H
#define STELLUX_SYSCALL_HANDLERS_SYS_FUTEX_H

#include "syscall/syscall_table.h"

DECLARE_SYSCALL(futex_wait);
DECLARE_SYSCALL(futex_wake);
DECLARE_SYSCALL(futex_wake_all);

#endif // STELLUX_SYSCALL_HANDLERS_SYS_FUTEX_H
5 changes: 5 additions & 0 deletions kernel/syscall/syscall.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ constexpr uint64_t SYS_PROC_KILL_TID = 1018;
// PTY
constexpr uint64_t SYS_PTY_CREATE = 1020;

// Futex
constexpr uint64_t SYS_FUTEX_WAIT = 1030;
constexpr uint64_t SYS_FUTEX_WAKE = 1031;
constexpr uint64_t SYS_FUTEX_WAKE_ALL = 1032;

/**
* Architecture-specific syscall initialization (MSRs on x86, etc.)
* @note Privilege: **required**
Expand Down
5 changes: 5 additions & 0 deletions kernel/syscall/syscall_table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "syscall/handlers/sys_select.h"
#include "syscall/handlers/sys_pipe.h"
#include "syscall/handlers/sys_uname.h"
#include "syscall/handlers/sys_futex.h"

namespace syscall {

Expand Down Expand Up @@ -112,6 +113,10 @@ __PRIVILEGED_CODE void init_syscall_table() {

REGISTER_SYSCALL(SYS_PTY_CREATE, pty_create);

REGISTER_SYSCALL(SYS_FUTEX_WAIT, futex_wait);
REGISTER_SYSCALL(SYS_FUTEX_WAKE, futex_wake);
REGISTER_SYSCALL(SYS_FUTEX_WAKE_ALL, futex_wake_all);

register_arch_syscalls();
}

Expand Down
1 change: 1 addition & 0 deletions kernel/syscall/syscall_table.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ constexpr int64_t EAFNOSUPPORT = -97;
constexpr int64_t EADDRINUSE = -98;
constexpr int64_t EISCONN = -106;
constexpr int64_t ENOTCONN = -107;
constexpr int64_t ETIMEDOUT = -110;
constexpr int64_t ECONNREFUSED = -111;

extern handler_t g_syscall_table[MAX_SYSCALL_NUM];
Expand Down
Loading
Loading