[Zion] Add a mutex object with appropriate syscalls.
This commit is contained in:
parent
4c2237fa72
commit
4c04f9d561
|
@ -11,7 +11,6 @@ add_executable(zion
|
|||
interrupt/interrupt.cpp
|
||||
interrupt/interrupt_enter.s
|
||||
interrupt/timer.cpp
|
||||
lib/mutex.cpp
|
||||
lib/message_queue.cpp
|
||||
loader/init_loader.cpp
|
||||
memory/kernel_heap.cpp
|
||||
|
@ -24,6 +23,7 @@ add_executable(zion
|
|||
object/endpoint.cpp
|
||||
object/ipc_object.cpp
|
||||
object/memory_object.cpp
|
||||
object/mutex.cpp
|
||||
object/port.cpp
|
||||
object/process.cpp
|
||||
object/reply_port.cpp
|
||||
|
@ -38,6 +38,7 @@ add_executable(zion
|
|||
syscall/ipc.cpp
|
||||
syscall/memory_object.cpp
|
||||
syscall/process.cpp
|
||||
syscall/synchronization.cpp
|
||||
syscall/syscall.cpp
|
||||
syscall/syscall_enter.s
|
||||
syscall/thread.cpp
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
#include <glacier/memory/ref_ptr.h>
|
||||
|
||||
#include "capability/capability.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/mutex.h"
|
||||
|
||||
class CapabilityTable {
|
||||
public:
|
||||
|
@ -25,7 +25,7 @@ class CapabilityTable {
|
|||
glcr::RefPtr<Capability> ReleaseCapability(uint64_t id);
|
||||
|
||||
private:
|
||||
Mutex lock_{"cap table"};
|
||||
glcr::RefPtr<Mutex> lock_ = Mutex::Create();
|
||||
// TODO: Do some randomization.
|
||||
uint64_t next_cap_id_ = 0x100;
|
||||
// FIXME: use a map data structure.
|
||||
|
|
|
@ -57,4 +57,8 @@ SYS5(ReplyPortRecv, z_cap_t, reply_port_cap, uint64_t*, num_bytes, void*, data,
|
|||
|
||||
SYS2(CapDuplicate, z_cap_t, cap_in, z_cap_t*, cap_out);
|
||||
|
||||
SYS1(MutexCreate, z_cap_t*, mutex_cap);
|
||||
SYS1(MutexLock, z_cap_t, mutex_cap);
|
||||
SYS1(MutexRelease, z_cap_t, mutex_cap);
|
||||
|
||||
SYS1(Debug, const char*, message);
|
||||
|
|
|
@ -54,6 +54,11 @@ const uint64_t kZionEndpointCall = 0x65;
|
|||
// Capability Calls
|
||||
const uint64_t kZionCapDuplicate = 0x70;
|
||||
|
||||
// Syncronization Calls
|
||||
const uint64_t kZionMutexCreate = 0x80;
|
||||
const uint64_t kZionMutexLock = 0x81;
|
||||
const uint64_t kZionMutexRelease = 0x82;
|
||||
|
||||
// Debugging Calls.
|
||||
const uint64_t kZionDebug = 0x1'0000;
|
||||
|
||||
|
@ -78,6 +83,10 @@ const uint64_t kZionPerm_Duplicate = 0x20;
|
|||
const uint64_t kZionPerm_SpawnProcess = 0x100;
|
||||
const uint64_t kZionPerm_SpawnThread = 0x200;
|
||||
|
||||
// Permissions on mutexes.
|
||||
const uint64_t kZionPerm_Lock = 0x100;
|
||||
const uint64_t kZionPerm_Release = 0x200;
|
||||
|
||||
/* ------------------------------
|
||||
* Process Init Types
|
||||
*
|
||||
|
|
|
@ -54,16 +54,16 @@ z_err_t UnboundedMessageQueue::PushBack(uint64_t num_bytes, const void* bytes,
|
|||
z_err_t UnboundedMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
|
||||
uint64_t* num_caps, z_cap_t* caps,
|
||||
z_cap_t* reply_cap) {
|
||||
mutex_.Lock();
|
||||
mutex_->Lock();
|
||||
while (pending_messages_.empty()) {
|
||||
auto thread = gScheduler->CurrentThread();
|
||||
thread->SetState(Thread::BLOCKED);
|
||||
blocked_threads_.PushBack(thread);
|
||||
mutex_.Unlock();
|
||||
mutex_->Release();
|
||||
gScheduler->Yield();
|
||||
mutex_.Lock();
|
||||
mutex_->Lock();
|
||||
}
|
||||
mutex_.Unlock();
|
||||
mutex_->Release();
|
||||
|
||||
MutexHolder lock(mutex_);
|
||||
auto next_msg = pending_messages_.PeekFront();
|
||||
|
@ -162,16 +162,16 @@ glcr::ErrorCode SingleMessageQueue::PushBack(uint64_t num_bytes,
|
|||
glcr::ErrorCode SingleMessageQueue::PopFront(uint64_t* num_bytes, void* bytes,
|
||||
uint64_t* num_caps, z_cap_t* caps,
|
||||
z_cap_t* reply_port) {
|
||||
mutex_.Lock();
|
||||
mutex_->Lock();
|
||||
while (!has_written_) {
|
||||
auto thread = gScheduler->CurrentThread();
|
||||
thread->SetState(Thread::BLOCKED);
|
||||
blocked_threads_.PushBack(thread);
|
||||
mutex_.Unlock();
|
||||
mutex_->Release();
|
||||
gScheduler->Yield();
|
||||
mutex_.Lock();
|
||||
mutex_->Lock();
|
||||
}
|
||||
mutex_.Unlock();
|
||||
mutex_->Release();
|
||||
|
||||
MutexHolder lock(mutex_);
|
||||
if (has_read_) {
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
|
||||
#include "capability/capability.h"
|
||||
#include "include/ztypes.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/mutex.h"
|
||||
|
||||
class MessageQueue {
|
||||
public:
|
||||
|
@ -23,7 +23,7 @@ class MessageQueue {
|
|||
virtual bool empty() = 0;
|
||||
|
||||
protected:
|
||||
Mutex mutex_{"message"};
|
||||
glcr::RefPtr<Mutex> mutex_ = Mutex::Create();
|
||||
// FIXME: This maybe shouldn't be shared between classes since the
|
||||
// SingleMessageQueue should only ever have one blocked thread.
|
||||
glcr::IntrusiveList<Thread> blocked_threads_;
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
#include "lib/mutex.h"
|
||||
|
||||
#include "debug/debug.h"
|
||||
#include "scheduler/scheduler.h"
|
||||
|
||||
void Mutex::Lock() {
|
||||
while (__atomic_fetch_or(&lock_, 0x1, __ATOMIC_SEQ_CST) == 0x1) {
|
||||
// dbgln("Lock sleep: %s", name_);
|
||||
gScheduler->Preempt();
|
||||
}
|
||||
}
|
|
@ -1,30 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
class Mutex {
|
||||
public:
|
||||
Mutex(const char* name) : name_(name) {}
|
||||
|
||||
// FIXME: Block thread on lock rather than "preempting"
|
||||
void Lock();
|
||||
void Unlock() { lock_ = false; }
|
||||
|
||||
private:
|
||||
const char* name_;
|
||||
|
||||
uint8_t lock_ = 0;
|
||||
};
|
||||
|
||||
class MutexHolder {
|
||||
public:
|
||||
MutexHolder(Mutex& mutex) : mutex_(mutex) { mutex_.Lock(); }
|
||||
|
||||
~MutexHolder() { mutex_.Unlock(); }
|
||||
|
||||
MutexHolder(MutexHolder&) = delete;
|
||||
MutexHolder(MutexHolder&&) = delete;
|
||||
|
||||
private:
|
||||
Mutex& mutex_;
|
||||
};
|
|
@ -6,7 +6,6 @@
|
|||
#include "capability/capability.h"
|
||||
#include "include/ztypes.h"
|
||||
#include "lib/message_queue.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/ipc_object.h"
|
||||
#include "object/kernel_object.h"
|
||||
#include "usr/zcall_internal.h"
|
||||
|
|
|
@ -5,7 +5,6 @@
|
|||
#include <glacier/status/error.h>
|
||||
|
||||
#include "lib/message_queue.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/ipc_object.h"
|
||||
#include "object/kernel_object.h"
|
||||
|
||||
|
|
|
@ -14,6 +14,7 @@ class KernelObject : public glcr::RefCounted<KernelObject> {
|
|||
PORT = 0x6,
|
||||
ENDPOINT = 0x7,
|
||||
REPLY_PORT = 0x8,
|
||||
MUTEX = 0x9,
|
||||
};
|
||||
|
||||
virtual uint64_t TypeTag() = 0;
|
||||
|
|
|
@ -0,0 +1,30 @@
|
|||
#include "object/mutex.h"
|
||||
|
||||
#include "scheduler/scheduler.h"
|
||||
|
||||
glcr::RefPtr<Mutex> Mutex::Create() {
|
||||
return glcr::AdoptPtr<Mutex>(new Mutex());
|
||||
}
|
||||
|
||||
// FIXME: We almost certainly have some race conditions
|
||||
// between this and unlock where we could end up with
|
||||
// a thread in the blocked_threads_ queue while noone is holding the lock.
|
||||
void Mutex::Lock() {
|
||||
while (__atomic_fetch_or(&lock_, 0x1, __ATOMIC_SEQ_CST) == 0x1) {
|
||||
auto thread = gScheduler->CurrentThread();
|
||||
thread->SetState(Thread::BLOCKED);
|
||||
blocked_threads_.PushBack(thread);
|
||||
gScheduler->Yield();
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: Check that this thread is the one that is holding the mutex before
|
||||
// releasing it.
|
||||
void Mutex::Release() {
|
||||
lock_ = 0;
|
||||
if (blocked_threads_.size() > 0) {
|
||||
auto thread = blocked_threads_.PopFront();
|
||||
thread->SetState(Thread::RUNNABLE);
|
||||
gScheduler->Enqueue(thread);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,55 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/container/intrusive_list.h>
|
||||
#include <glacier/memory/ref_ptr.h>
|
||||
|
||||
#include "include/ztypes.h"
|
||||
#include "object/kernel_object.h"
|
||||
#include "object/thread.h"
|
||||
|
||||
class Mutex;
|
||||
|
||||
template <>
|
||||
struct KernelObjectTag<Mutex> {
|
||||
static const uint64_t type = KernelObject::MUTEX;
|
||||
};
|
||||
|
||||
class Mutex : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::MUTEX; }
|
||||
static uint64_t DefaultPermissions() {
|
||||
return kZionPerm_Lock | kZionPerm_Release;
|
||||
}
|
||||
|
||||
static glcr::RefPtr<Mutex> Create();
|
||||
|
||||
// Attempts to lock the mutex, suspends the current thread
|
||||
// until the lock is acquired.
|
||||
void Lock();
|
||||
|
||||
// Releases the mutex and activates the next thread in the
|
||||
// blocked queue if it exists.
|
||||
void Release();
|
||||
|
||||
private:
|
||||
uint8_t lock_ = 0;
|
||||
|
||||
glcr::IntrusiveList<Thread> blocked_threads_;
|
||||
|
||||
Mutex() {}
|
||||
};
|
||||
|
||||
class MutexHolder {
|
||||
public:
|
||||
MutexHolder(const glcr::RefPtr<Mutex>& mutex) : mutex_(mutex) {
|
||||
mutex_->Lock();
|
||||
}
|
||||
|
||||
~MutexHolder() { mutex_->Release(); }
|
||||
|
||||
MutexHolder(MutexHolder&) = delete;
|
||||
MutexHolder(MutexHolder&&) = delete;
|
||||
|
||||
private:
|
||||
glcr::RefPtr<Mutex> mutex_;
|
||||
};
|
|
@ -5,7 +5,6 @@
|
|||
|
||||
#include "capability/capability.h"
|
||||
#include "lib/message_queue.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/ipc_object.h"
|
||||
#include "object/kernel_object.h"
|
||||
#include "object/thread.h"
|
||||
|
|
|
@ -6,9 +6,9 @@
|
|||
|
||||
#include "capability/capability.h"
|
||||
#include "capability/capability_table.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/address_space.h"
|
||||
#include "object/channel.h"
|
||||
#include "object/mutex.h"
|
||||
#include "object/port.h"
|
||||
|
||||
// Forward decl due to cyclic dependency.
|
||||
|
@ -66,7 +66,7 @@ class Process : public KernelObject {
|
|||
Process();
|
||||
Process(uint64_t id) : id_(id), vmas_(AddressSpace::ForRoot()) {}
|
||||
|
||||
Mutex mutex_{"Process"};
|
||||
glcr::RefPtr<Mutex> mutex_ = Mutex::Create();
|
||||
|
||||
uint64_t id_;
|
||||
glcr::RefPtr<AddressSpace> vmas_;
|
||||
|
|
|
@ -3,7 +3,6 @@
|
|||
#include <glacier/memory/ref_ptr.h>
|
||||
|
||||
#include "lib/message_queue.h"
|
||||
#include "lib/mutex.h"
|
||||
#include "object/ipc_object.h"
|
||||
#include "object/kernel_object.h"
|
||||
|
||||
|
|
|
@ -0,0 +1,31 @@
|
|||
#include "syscall/synchronization.h"
|
||||
|
||||
#include "object/mutex.h"
|
||||
#include "scheduler/scheduler.h"
|
||||
|
||||
glcr::ErrorCode MutexCreate(ZMutexCreateReq* req) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
*req->mutex_cap = curr_proc.AddNewCapability(Mutex::Create());
|
||||
return glcr::OK;
|
||||
}
|
||||
|
||||
glcr::ErrorCode MutexLock(ZMutexLockReq* req) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
auto cap = curr_proc.GetCapability(req->mutex_cap);
|
||||
RET_ERR(ValidateCapability<Mutex>(cap, kZionPerm_Lock));
|
||||
|
||||
auto mutex = cap->obj<Mutex>();
|
||||
mutex->Lock();
|
||||
return glcr::OK;
|
||||
}
|
||||
|
||||
glcr::ErrorCode MutexRelease(ZMutexReleaseReq* req) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
auto cap = curr_proc.GetCapability(req->mutex_cap);
|
||||
// TODO: We may not want a separate permission for releasing the mutex.
|
||||
RET_ERR(ValidateCapability<Mutex>(cap, kZionPerm_Release));
|
||||
|
||||
auto mutex = cap->obj<Mutex>();
|
||||
mutex->Release();
|
||||
return glcr::OK;
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <glacier/status/error.h>
|
||||
|
||||
#include "include/zcall.h"
|
||||
|
||||
glcr::ErrorCode MutexCreate(ZMutexCreateReq* req);
|
||||
glcr::ErrorCode MutexLock(ZMutexLockReq* req);
|
||||
glcr::ErrorCode MutexRelease(ZMutexReleaseReq* req);
|
|
@ -11,6 +11,7 @@
|
|||
#include "syscall/ipc.h"
|
||||
#include "syscall/memory_object.h"
|
||||
#include "syscall/process.h"
|
||||
#include "syscall/synchronization.h"
|
||||
#include "syscall/thread.h"
|
||||
|
||||
#define EFER 0xC0000080
|
||||
|
@ -80,6 +81,10 @@ extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req) {
|
|||
CASE(ReplyPortRecv);
|
||||
// syscall/capability.h
|
||||
CASE(CapDuplicate);
|
||||
// syscall/syncronization.h
|
||||
CASE(MutexCreate);
|
||||
CASE(MutexLock);
|
||||
CASE(MutexRelease);
|
||||
// syscall/debug.h
|
||||
CASE(Debug);
|
||||
default:
|
||||
|
|
Loading…
Reference in New Issue