This commit is contained in:
2026-08-11 17:48:20 +02:00
committed by Marvin Friedrich
parent cf33867e68
commit 0e2d4ed8c6
55 changed files with 279 additions and 346 deletions
+6 -6
View File
@@ -21,12 +21,12 @@ zn_status_t channel_create(struct channel** ep0, struct channel** ep1) {
a->peer = b;
a->lock = (struct spinlock){0};
TAILQ_INIT(&a->messages);
tailq_init(&a->messages);
wait_queue_init(&a->waiters);
b->peer = a;
b->lock = (struct spinlock){0};
TAILQ_INIT(&b->messages);
tailq_init(&b->messages);
wait_queue_init(&b->waiters);
*ep0 = a;
@@ -64,7 +64,7 @@ zn_status_t channel_write(
// Enqueue on the peer's message queue and wake a waiter.
spin_lock(&peer->lock);
TAILQ_INSERT_TAIL(&peer->messages, msg, next);
tailq_insert_tail(&peer->messages, msg, next);
spin_unlock(&peer->lock);
struct sched_percpu* sched = &percpu_get()->sched;
@@ -75,10 +75,10 @@ zn_status_t channel_write(
zn_status_t channel_read(struct channel* ch, struct channel_msg** msg_out) {
spin_lock(&ch->lock);
struct channel_msg* msg = TAILQ_FIRST(&ch->messages);
struct channel_msg* msg = tailq_first(&ch->messages);
if (msg != nullptr) {
TAILQ_REMOVE(&ch->messages, msg, next);
tailq_remove(&ch->messages, msg, next);
spin_unlock(&ch->lock);
*msg_out = msg;
return ZN_OK;
@@ -97,7 +97,7 @@ zn_status_t channel_read(struct channel* ch, struct channel_msg** msg_out) {
zn_status_t channel_poll(struct channel* ch) {
spin_lock(&ch->lock);
if (!TAILQ_EMPTY(&ch->messages)) {
if (!tailq_empty(&ch->messages)) {
spin_unlock(&ch->lock);
return ZN_OK;
}
+1 -1
View File
@@ -49,7 +49,7 @@ zn_status_t elf_load(struct exec_info* info, struct task** out) {
if (phdr.p_flags & PF_X)
prot |= ZN_VM_MAP_EXEC;
ASSERT(phdr.p_offset % phdr.p_align == phdr.p_vaddr % phdr.p_align, "");
assert(phdr.p_offset % phdr.p_align == phdr.p_vaddr % phdr.p_align, "");
const uintptr_t misalign = phdr.p_vaddr & (page_size - 1);
const uintptr_t map_address = phdr.p_vaddr - misalign;
+7 -7
View File
@@ -15,7 +15,7 @@ zn_status_t namespace_new(struct namespace** out) {
namespace->mutex = (struct mutex){0};
namespace->next_handle = 1;
HASHMAP_INIT(&namespace->descriptors);
hashmap_init(&namespace->descriptors);
*out = namespace;
return ZN_OK;
@@ -31,10 +31,10 @@ zn_status_t namespace_add_desc(struct namespace* namespace, struct namespace_des
handle = atomic_fetch_add_explicit(&namespace->next_handle, 1, memory_order_acq_rel);
if (handle <= ZN_HANDLE_INVALID)
continue;
} while (HASHMAP_GET(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default) != nullptr);
} while (hashmap_get(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default) != nullptr);
zn_status_t status =
HASHMAP_INSERT(&namespace->descriptors, handle, desc, hashmap_hash_default, hashmap_eq_default);
hashmap_insert(&namespace->descriptors, handle, desc, hashmap_hash_default, hashmap_eq_default);
if (status != ZN_OK)
return status;
@@ -49,10 +49,10 @@ zn_status_t namespace_add_desc_at(struct namespace* namespace, struct namespace_
return ZN_ERR_BAD_HANDLE;
// Fail if a descriptor already exists at this handle.
if (HASHMAP_GET(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default) != nullptr)
if (hashmap_get(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default) != nullptr)
return ZN_ERR_ALREADY_EXISTS;
return HASHMAP_INSERT(&namespace->descriptors, handle, desc, hashmap_hash_default, hashmap_eq_default);
return hashmap_insert(&namespace->descriptors, handle, desc, hashmap_hash_default, hashmap_eq_default);
}
zn_status_t namespace_get(struct namespace* namespace, zn_handle_t handle, struct namespace_desc* out) {
@@ -65,7 +65,7 @@ zn_status_t namespace_get(struct namespace* namespace, zn_handle_t handle, struc
return ZN_ERR_BAD_HANDLE;
struct namespace_desc* desc =
HASHMAP_GET(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default);
hashmap_get(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default);
if (!desc)
return ZN_ERR_BAD_HANDLE;
@@ -80,7 +80,7 @@ zn_status_t namespace_del_desc(struct namespace* namespace, zn_handle_t handle)
if (handle <= ZN_HANDLE_INVALID)
return ZN_ERR_BAD_HANDLE;
if (!HASHMAP_REMOVE(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default))
if (!hashmap_remove(&namespace->descriptors, handle, hashmap_hash_default, hashmap_eq_default))
return ZN_ERR_BAD_HANDLE;
return ZN_OK;
+13 -13
View File
@@ -1,6 +1,5 @@
#include <zinnia/status.h>
#include <kernel/alloc.h>
#include <kernel/assert.h>
#include <kernel/irq.h>
#include <kernel/list.h>
#include <kernel/percpu.h>
@@ -8,6 +7,7 @@
#include <kernel/sched.h>
#include <kernel/spin.h>
#include <kernel/tailq.h>
#include <assert.h>
#include <bits/irq.h>
#include <stdatomic.h>
#include <stdint.h>
@@ -35,30 +35,30 @@ void sched_to_user_context(uintptr_t ctx) {
void sched_init(struct sched_percpu* sched) {
struct task* idle_task;
ASSERT(
assert(
task_create("idle", &kernel_vas, nullptr, idle_func, 0, &idle_task) == ZN_OK,
"Unable to create idle task!\n"
);
sched->idle_task = idle_task;
sched->current = idle_task;
TAILQ_INIT(&sched->run_queue);
tailq_init(&sched->run_queue);
kprintf("Scheduler initialized\n");
}
void sched_add_task(struct sched_percpu* sched, struct task* task) {
ASSERT(task != nullptr, "Tried to add NULL to the run queue\n");
TAILQ_INSERT_TAIL(&sched->run_queue, task, next);
assert(task != nullptr, "Tried to add NULL to the run queue\n");
tailq_insert_tail(&sched->run_queue, task, next);
}
static struct task* next_task(struct sched_percpu* sched) {
struct task* t = TAILQ_FIRST(&sched->run_queue);
struct task* t = tailq_first(&sched->run_queue);
if (t == nullptr)
return sched->idle_task;
TAILQ_REMOVE(&sched->run_queue, t, next);
tailq_remove(&sched->run_queue, t, next);
return t;
}
@@ -143,7 +143,7 @@ void task_entry(task_fn_t entry, uintptr_t arg0) {
}
void wait_queue_init(struct wait_queue* wq) {
TAILQ_INIT(&wq->waiters);
tailq_init(&wq->waiters);
wq->lock = (struct spinlock){0};
}
@@ -152,7 +152,7 @@ void sched_block(struct sched_percpu* sched, struct wait_queue* wq) {
spin_lock(&wq->lock);
sched->current->state = TASK_STATE_BLOCKED;
TAILQ_INSERT_TAIL(&wq->waiters, sched->current, next);
tailq_insert_tail(&wq->waiters, sched->current, next);
spin_unlock(&wq->lock);
do_reschedule(sched);
@@ -161,9 +161,9 @@ void sched_block(struct sched_percpu* sched, struct wait_queue* wq) {
void sched_wake(struct sched_percpu* sched, struct wait_queue* wq) {
spin_lock(&wq->lock);
struct task* t = TAILQ_FIRST(&wq->waiters);
struct task* t = tailq_first(&wq->waiters);
if (t != nullptr) {
TAILQ_REMOVE(&wq->waiters, t, next);
tailq_remove(&wq->waiters, t, next);
t->state = TASK_STATE_READY;
sched_add_task(sched, t);
}
@@ -175,8 +175,8 @@ void sched_wake_all(struct sched_percpu* sched, struct wait_queue* wq) {
spin_lock(&wq->lock);
struct task* t;
while ((t = TAILQ_FIRST(&wq->waiters)) != nullptr) {
TAILQ_REMOVE(&wq->waiters, t, next);
while ((t = tailq_first(&wq->waiters)) != nullptr) {
tailq_remove(&wq->waiters, t, next);
t->state = TASK_STATE_READY;
sched_add_task(sched, t);
}
+5 -6
View File
@@ -1,16 +1,15 @@
#include <kernel/spin.h>
#include <stdatomic.h>
// TODO: Ticketing system
void spin_lock(struct spinlock* spin) {
while (atomic_exchange_explicit(&spin->locked, true, memory_order_acquire)) {
uint32_t my = atomic_fetch_add_explicit(&spin->next, 1, memory_order_relaxed);
while (atomic_load_explicit(&spin->owner, memory_order_acquire) != my) {
asm volatile("pause");
}
atomic_store_explicit(&spin->locked, true, memory_order_release);
}
void spin_unlock(struct spinlock* spin) {
atomic_store_explicit(&spin->locked, false, memory_order_release);
uint32_t val = atomic_load_explicit(&spin->owner, memory_order_relaxed);
atomic_store_explicit(&spin->owner, val + 1, memory_order_release);
}