Compare commits
4 Commits
6c10c57bfa
...
a5c4d40575
Author | SHA1 | Date |
---|---|---|
Drew Galbraith | a5c4d40575 | |
Drew Galbraith | 81b925eea0 | |
Drew Galbraith | b79ec07636 | |
Drew Galbraith | 55340e2917 |
|
@ -12,17 +12,17 @@ void check(uint64_t code) {
|
|||
switch (code) {
|
||||
case Z_OK:
|
||||
return;
|
||||
case ZE_NOT_FOUND:
|
||||
dbgln("crash: NOT_FOUND");
|
||||
break;
|
||||
case ZE_INVALID:
|
||||
dbgln("crash: INVALID");
|
||||
break;
|
||||
case ZE_DENIED:
|
||||
dbgln("crash: DENIED");
|
||||
break;
|
||||
case ZE_UNIMPLEMENTED:
|
||||
case Z_ERR_UNIMPLEMENTED:
|
||||
dbgln("crash: UNIMPLEMENTED");
|
||||
case Z_ERR_CAP_NOT_FOUND:
|
||||
dbgln("crash: missing capability");
|
||||
break;
|
||||
case Z_ERR_CAP_TYPE:
|
||||
dbgln("crash: capability of the wrong type");
|
||||
break;
|
||||
case Z_ERR_CAP_DENIED:
|
||||
dbgln("crash: capability permissions error");
|
||||
break;
|
||||
default:
|
||||
dbgln("Unhandled code");
|
||||
break;
|
||||
|
|
|
@ -83,10 +83,16 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
|
|||
} // namespace
|
||||
|
||||
uint64_t SpawnProcessFromElfRegion(uint64_t program) {
|
||||
dbgln("Channel Create");
|
||||
uint64_t local_chan;
|
||||
uint64_t foreign_chan;
|
||||
check(ZChannelCreate(&local_chan, &foreign_chan));
|
||||
|
||||
dbgln("Spawn");
|
||||
uint64_t proc_cap;
|
||||
uint64_t as_cap;
|
||||
check(ZProcessSpawn(Z_INIT_PROC_SELF, &proc_cap, &as_cap));
|
||||
check(ZProcessSpawn(Z_INIT_PROC_SELF, foreign_chan, &proc_cap, &as_cap,
|
||||
&foreign_chan));
|
||||
|
||||
uint64_t entry_point = LoadElfProgram(program, as_cap);
|
||||
dbgln("Thread Create");
|
||||
|
@ -94,7 +100,10 @@ uint64_t SpawnProcessFromElfRegion(uint64_t program) {
|
|||
check(ZThreadCreate(proc_cap, &thread_cap));
|
||||
|
||||
dbgln("Thread start");
|
||||
check(ZThreadStart(thread_cap, entry_point, 0, 0));
|
||||
check(ZThreadStart(thread_cap, entry_point, foreign_chan, 0));
|
||||
|
||||
const uint8_t* msg = reinterpret_cast<const uint8_t*>("Hello!");
|
||||
check(ZChannelSend(local_chan, 0, 7, msg, 0, 0));
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
#include <mammoth/debug.h>
|
||||
#include <mammoth/thread.h>
|
||||
#include <zcall.h>
|
||||
|
||||
#define CHECK(expr) \
|
||||
{ \
|
||||
|
@ -15,7 +16,7 @@ void thread_entry(void* a) {
|
|||
dbgln(static_cast<const char*>(a));
|
||||
}
|
||||
|
||||
int main() {
|
||||
int main(uint64_t bootstrap_cap) {
|
||||
dbgln("Main thread");
|
||||
|
||||
const char* a = "a";
|
||||
|
@ -24,5 +25,13 @@ int main() {
|
|||
const char* d = "dee";
|
||||
Thread t1(thread_entry, a);
|
||||
Thread t2(thread_entry, b);
|
||||
|
||||
uint64_t num_bytes = 10;
|
||||
uint64_t num_caps;
|
||||
uint8_t bytes[10];
|
||||
uint64_t type;
|
||||
check(ZChannelRecv(bootstrap_cap, num_bytes, bytes, 0, 0, &type, &num_bytes,
|
||||
&num_caps));
|
||||
dbgln(reinterpret_cast<char*>(bytes));
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -14,6 +14,7 @@ add_executable(zion
|
|||
memory/physical_memory.cpp
|
||||
memory/user_stack_manager.cpp
|
||||
object/address_space.cpp
|
||||
object/channel.cpp
|
||||
object/memory_object.cpp
|
||||
object/process.cpp
|
||||
object/thread.cpp
|
||||
|
|
|
@ -34,3 +34,11 @@ RefPtr<MemoryObject> Capability::obj<MemoryObject>() {
|
|||
}
|
||||
return StaticCastRefPtr<MemoryObject>(obj_);
|
||||
}
|
||||
|
||||
template <>
|
||||
RefPtr<Channel> Capability::obj<Channel>() {
|
||||
if (type_ != CHANNEL) {
|
||||
panic("Accessing %u cap as object.", type_);
|
||||
}
|
||||
return StaticCastRefPtr<Channel>(obj_);
|
||||
}
|
||||
|
|
|
@ -8,7 +8,7 @@
|
|||
class Process;
|
||||
class Thread;
|
||||
|
||||
class Capability {
|
||||
class Capability : public RefCounted<Capability> {
|
||||
public:
|
||||
enum Type {
|
||||
UNDEFINED,
|
||||
|
@ -16,6 +16,7 @@ class Capability {
|
|||
THREAD,
|
||||
ADDRESS_SPACE,
|
||||
MEMORY_OBJECT,
|
||||
CHANNEL,
|
||||
};
|
||||
Capability(const RefPtr<KernelObject>& obj, Type type, uint64_t id,
|
||||
uint64_t permissions)
|
||||
|
@ -30,6 +31,7 @@ class Capability {
|
|||
RefPtr<T> obj();
|
||||
|
||||
uint64_t id() { return id_; }
|
||||
void set_id(uint64_t id) { id_ = id; }
|
||||
|
||||
bool CheckType(Type type) { return type_ == type; }
|
||||
|
||||
|
|
|
@ -2,6 +2,20 @@
|
|||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "include/zerrors.h"
|
||||
|
||||
void dbg(const char* fmt, ...);
|
||||
void dbgln(const char* str, ...);
|
||||
void panic(const char* str, ...);
|
||||
|
||||
#define RET_ERR(expr) \
|
||||
{ \
|
||||
z_err_t _tmp_err = expr; \
|
||||
if (_tmp_err != Z_OK) { \
|
||||
return _tmp_err; \
|
||||
} \
|
||||
}
|
||||
|
||||
#define UNREACHABLE \
|
||||
panic("Unreachable %s, %s", __FILE__, __LINE__); \
|
||||
__builtin_unreachable();
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "zerrors.h"
|
||||
|
||||
#define Z_INVALID 0x0
|
||||
|
||||
#define ZC_WRITE 0x01
|
||||
|
@ -32,28 +34,46 @@
|
|||
|
||||
#define Z_INIT_BOOT_VMMO 0x31
|
||||
|
||||
// IPC Calls
|
||||
#define Z_CHANNEL_CREATE 0x40
|
||||
#define Z_CHANNEL_SEND 0x41
|
||||
#define Z_CHANNEL_RECV 0x42
|
||||
#define Z_CHANNEL_SENDRECV 0x43
|
||||
|
||||
// Debugging Calls.
|
||||
#define Z_DEBUG_PRINT 0x10000000
|
||||
|
||||
void ZProcessExit(uint64_t code);
|
||||
|
||||
[[nodiscard]] uint64_t ZProcessSpawn(uint64_t proc_cap, uint64_t* new_proc_cap,
|
||||
uint64_t* new_vmas_cap);
|
||||
[[nodiscard]] z_err_t ZProcessSpawn(uint64_t proc_cap, uint64_t bootstrap_cap,
|
||||
uint64_t* new_proc_cap,
|
||||
uint64_t* new_vmas_cap,
|
||||
uint64_t* new_bootstrap_cap);
|
||||
|
||||
// UNUSED for now, I think we can get away with just starting a thread.
|
||||
[[nodiscard]] uint64_t ZProcessStart(uint64_t proc_cap, uint64_t thread_cap,
|
||||
uint64_t entry, uint64_t arg1,
|
||||
uint64_t arg2);
|
||||
[[nodiscard]] z_err_t ZProcessStart(uint64_t proc_cap, uint64_t thread_cap,
|
||||
uint64_t entry, uint64_t arg1,
|
||||
uint64_t arg2);
|
||||
|
||||
[[nodiscard]] uint64_t ZThreadCreate(uint64_t proc_cap, uint64_t* thread_cap);
|
||||
[[nodiscard]] z_err_t ZThreadCreate(uint64_t proc_cap, uint64_t* thread_cap);
|
||||
|
||||
[[nodiscard]] uint64_t ZThreadStart(uint64_t thread_cap, uint64_t entry,
|
||||
uint64_t arg1, uint64_t arg2);
|
||||
[[nodiscard]] z_err_t ZThreadStart(uint64_t thread_cap, uint64_t entry,
|
||||
uint64_t arg1, uint64_t arg2);
|
||||
|
||||
void ZThreadExit();
|
||||
|
||||
[[nodiscard]] uint64_t ZAddressSpaceMap(uint64_t vmas_cap, uint64_t vmas_offset,
|
||||
uint64_t vmmo_cap, uint64_t* vaddr);
|
||||
[[nodiscard]] uint64_t ZMemoryObjectCreate(uint64_t size, uint64_t* vmmo_cap);
|
||||
[[nodiscard]] z_err_t ZAddressSpaceMap(uint64_t vmas_cap, uint64_t vmas_offset,
|
||||
uint64_t vmmo_cap, uint64_t* vaddr);
|
||||
[[nodiscard]] z_err_t ZMemoryObjectCreate(uint64_t size, uint64_t* vmmo_cap);
|
||||
|
||||
[[nodiscard]] uint64_t ZDebug(const char* message);
|
||||
[[nodiscard]] z_err_t ZChannelCreate(uint64_t* channel1, uint64_t* channel2);
|
||||
[[nodiscard]] z_err_t ZChannelSend(uint64_t chan_cap, uint64_t type,
|
||||
uint64_t num_bytes, const uint8_t* bytes,
|
||||
uint64_t num_caps, const uint64_t* caps);
|
||||
[[nodiscard]] z_err_t ZChannelRecv(uint64_t chan_cap, uint64_t num_bytes,
|
||||
uint8_t* bytes, uint64_t num_caps,
|
||||
uint64_t* caps, uint64_t* type,
|
||||
uint64_t* actual_bytes,
|
||||
uint64_t* actual_caps);
|
||||
|
||||
[[nodiscard]] z_err_t ZDebug(const char* message);
|
||||
|
|
|
@ -1,7 +1,16 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#define Z_OK 0x0
|
||||
#define ZE_NOT_FOUND 0x1
|
||||
#define ZE_INVALID 0x2
|
||||
#define ZE_DENIED 0x4
|
||||
#define ZE_UNIMPLEMENTED 0x8
|
||||
#define Z_ERR_NOT_FOUND 0x1
|
||||
#define Z_ERR_INVALID 0x2
|
||||
#define Z_ERR_DENIED 0x3
|
||||
#define Z_ERR_UNIMPLEMENTED 0x4
|
||||
#define Z_ERR_BUFF_SIZE 0x05
|
||||
|
||||
#define Z_ERR_CAP_NOT_FOUND 0x100
|
||||
#define Z_ERR_CAP_TYPE 0x101
|
||||
#define Z_ERR_CAP_DENIED 0x102
|
||||
|
||||
typedef uint64_t z_err_t;
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
template <typename T, typename U>
|
||||
class Pair {
|
||||
public:
|
||||
Pair(const T& first, const U& second) : first_(first), second_(second) {}
|
||||
T& first() { return first_; }
|
||||
U& second() { return second_; }
|
||||
|
||||
private:
|
||||
T first_;
|
||||
U second_;
|
||||
};
|
|
@ -1,89 +0,0 @@
|
|||
#pragma once
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "debug/debug.h"
|
||||
|
||||
template <typename T>
|
||||
class SharedPtr {
|
||||
public:
|
||||
SharedPtr() : init_(false), ptr_(0), ref_cnt_(0) {}
|
||||
// Takes ownership.
|
||||
SharedPtr(T* ptr) {
|
||||
ptr_ = ptr;
|
||||
ref_cnt_ = new uint64_t(1);
|
||||
}
|
||||
|
||||
SharedPtr(const SharedPtr<T>& other)
|
||||
: init_(other.init_), ptr_(other.ptr_), ref_cnt_(other.ref_cnt_) {
|
||||
(*ref_cnt_)++;
|
||||
}
|
||||
|
||||
SharedPtr& operator=(const SharedPtr<T>& other) {
|
||||
Cleanup();
|
||||
init_ = other.init_;
|
||||
ptr_ = other.ptr_;
|
||||
ref_cnt_ = other.ref_cnt_;
|
||||
(*ref_cnt_)++;
|
||||
return *this;
|
||||
}
|
||||
|
||||
~SharedPtr() { Cleanup(); }
|
||||
|
||||
T& operator*() {
|
||||
CheckValid();
|
||||
return *ptr_;
|
||||
}
|
||||
const T& operator*() const {
|
||||
CheckValid();
|
||||
return *ptr_;
|
||||
}
|
||||
T* operator->() {
|
||||
CheckValid();
|
||||
return ptr_;
|
||||
}
|
||||
const T* operator->() const {
|
||||
CheckValid();
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
T* ptr() {
|
||||
CheckValid();
|
||||
return ptr_;
|
||||
}
|
||||
|
||||
bool operator==(const SharedPtr<T>& other) {
|
||||
CheckValid();
|
||||
other.CheckValid();
|
||||
return ptr_ == other.ptr_;
|
||||
}
|
||||
|
||||
bool empty() { return !init_; }
|
||||
|
||||
private:
|
||||
bool init_ = true;
|
||||
T* ptr_;
|
||||
uint64_t* ref_cnt_;
|
||||
|
||||
void Cleanup() {
|
||||
if (!init_) {
|
||||
return;
|
||||
}
|
||||
if (--(*ref_cnt_) == 0) {
|
||||
dbgln("Deleting shared ptr: %m", ptr_);
|
||||
delete ptr_;
|
||||
delete ref_cnt_;
|
||||
}
|
||||
}
|
||||
|
||||
void CheckValid() const {
|
||||
if (!init_) {
|
||||
panic("Accessing invalid shared ptr");
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, class... A>
|
||||
SharedPtr<T> MakeShared(A... args) {
|
||||
return {new T(args...)};
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
#include "object/channel.h"
|
||||
|
||||
#include "include/zerrors.h"
|
||||
|
||||
Pair<RefPtr<Channel>, RefPtr<Channel>> Channel::CreateChannelPair() {
|
||||
auto c1 = MakeRefCounted<Channel>();
|
||||
auto c2 = MakeRefCounted<Channel>();
|
||||
c1->SetPeer(c2);
|
||||
c2->SetPeer(c1);
|
||||
return {c1, c2};
|
||||
}
|
||||
|
||||
z_err_t Channel::Write(const ZMessage& msg) {
|
||||
return peer_->EnqueueMessage(msg);
|
||||
}
|
||||
|
||||
z_err_t Channel::Read(ZMessage& msg) {
|
||||
if (pending_messages_.size() == 0) {
|
||||
dbgln("Unimplemented add blocking.");
|
||||
return Z_ERR_UNIMPLEMENTED;
|
||||
}
|
||||
Message next_msg = pending_messages_.PeekFront();
|
||||
if (next_msg.num_bytes > msg.num_bytes) {
|
||||
return Z_ERR_BUFF_SIZE;
|
||||
}
|
||||
|
||||
msg.type = next_msg.type;
|
||||
msg.num_bytes = next_msg.num_bytes;
|
||||
msg.num_caps = 0;
|
||||
|
||||
for (uint64_t i = 0; i < msg.num_bytes; i++) {
|
||||
msg.bytes[i] = next_msg.bytes[i];
|
||||
}
|
||||
|
||||
pending_messages_.PopFront();
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t Channel::EnqueueMessage(const ZMessage& msg) {
|
||||
if (msg.num_caps > 0) {
|
||||
dbgln("Unimplemented passing caps on channel");
|
||||
return Z_ERR_UNIMPLEMENTED;
|
||||
}
|
||||
|
||||
if (msg.num_bytes > 0x1000) {
|
||||
dbgln("Large message size unimplemented: %x", msg.num_bytes);
|
||||
return Z_ERR_INVALID;
|
||||
}
|
||||
|
||||
Message message{
|
||||
.type = msg.type,
|
||||
.num_bytes = msg.num_bytes,
|
||||
.bytes = new uint8_t[msg.num_bytes],
|
||||
};
|
||||
for (uint64_t i = 0; i < msg.num_bytes; i++) {
|
||||
message.bytes[i] = msg.bytes[i];
|
||||
}
|
||||
pending_messages_.PushBack(message);
|
||||
return Z_OK;
|
||||
}
|
|
@ -0,0 +1,41 @@
|
|||
#pragma once
|
||||
|
||||
#include "capability/capability.h"
|
||||
#include "include/zerrors.h"
|
||||
#include "lib/linked_list.h"
|
||||
#include "lib/pair.h"
|
||||
#include "lib/ref_ptr.h"
|
||||
#include "object/kernel_object.h"
|
||||
#include "usr/zcall_internal.h"
|
||||
|
||||
class Channel : public KernelObject {
|
||||
public:
|
||||
static Pair<RefPtr<Channel>, RefPtr<Channel>> CreateChannelPair();
|
||||
|
||||
RefPtr<Channel> peer() { return peer_; }
|
||||
|
||||
z_err_t Write(const ZMessage& msg);
|
||||
z_err_t Read(ZMessage& msg);
|
||||
|
||||
private:
|
||||
// FIXME: We will likely never close the channel based on this
|
||||
// circular dependency.
|
||||
RefPtr<Channel> peer_{nullptr};
|
||||
|
||||
struct Message {
|
||||
uint64_t type;
|
||||
|
||||
uint64_t num_bytes;
|
||||
uint8_t* bytes;
|
||||
};
|
||||
|
||||
// FIXME: This is probably dangerous because of an
|
||||
// implicit shallow copy.
|
||||
LinkedList<Message> pending_messages_;
|
||||
|
||||
friend class MakeRefCountedFriend<Channel>;
|
||||
Channel() {}
|
||||
void SetPeer(const RefPtr<Channel>& peer) { peer_ = peer; }
|
||||
|
||||
z_err_t EnqueueMessage(const ZMessage& msg);
|
||||
};
|
|
@ -23,10 +23,10 @@ RefPtr<Process> Process::RootProcess() {
|
|||
RefPtr<Process> Process::Create() {
|
||||
auto proc = MakeRefCounted<Process>();
|
||||
proc->caps_.PushBack(
|
||||
new Capability(proc, Capability::PROCESS, Z_INIT_PROC_SELF,
|
||||
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD));
|
||||
proc->caps_.PushBack(new Capability(proc->vmas(), Capability::ADDRESS_SPACE,
|
||||
Z_INIT_VMAS_SELF, ZC_WRITE));
|
||||
MakeRefCounted<Capability>(proc, Capability::PROCESS, Z_INIT_PROC_SELF,
|
||||
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD));
|
||||
proc->caps_.PushBack(MakeRefCounted<Capability>(
|
||||
proc->vmas(), Capability::ADDRESS_SPACE, Z_INIT_VMAS_SELF, ZC_WRITE));
|
||||
return proc;
|
||||
}
|
||||
|
||||
|
@ -62,10 +62,25 @@ void Process::CheckState() {
|
|||
state_ = FINISHED;
|
||||
}
|
||||
|
||||
SharedPtr<Capability> Process::GetCapability(uint64_t cid) {
|
||||
RefPtr<Capability> Process::ReleaseCapability(uint64_t cid) {
|
||||
auto iter = caps_.begin();
|
||||
while (iter != caps_.end()) {
|
||||
if (iter->id() == cid) {
|
||||
if (*iter && iter->id() == cid) {
|
||||
auto cap = *iter;
|
||||
*iter = {nullptr};
|
||||
return cap;
|
||||
}
|
||||
++iter;
|
||||
}
|
||||
dbgln("Bad cap access");
|
||||
dbgln("Num caps: %u", caps_.size());
|
||||
return {};
|
||||
}
|
||||
|
||||
RefPtr<Capability> Process::GetCapability(uint64_t cid) {
|
||||
auto iter = caps_.begin();
|
||||
while (iter != caps_.end()) {
|
||||
if (*iter && iter->id() == cid) {
|
||||
return *iter;
|
||||
}
|
||||
++iter;
|
||||
|
@ -75,32 +90,44 @@ SharedPtr<Capability> Process::GetCapability(uint64_t cid) {
|
|||
return {};
|
||||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Capability>& cap) {
|
||||
cap->set_id(next_cap_id_++);
|
||||
caps_.PushBack(cap);
|
||||
return cap->id();
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<Thread>& thread) {
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(new Capability(thread, Capability::THREAD, cap_id, ZC_WRITE));
|
||||
caps_.PushBack(
|
||||
MakeRefCounted<Capability>(thread, Capability::THREAD, cap_id, ZC_WRITE));
|
||||
return cap_id;
|
||||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Process>& p) {
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(new Capability(p, Capability::PROCESS, cap_id,
|
||||
ZC_WRITE | ZC_PROC_SPAWN_THREAD));
|
||||
caps_.PushBack(MakeRefCounted<Capability>(p, Capability::PROCESS, cap_id,
|
||||
ZC_WRITE | ZC_PROC_SPAWN_THREAD));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<AddressSpace>& vmas) {
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(
|
||||
new Capability(vmas, Capability::ADDRESS_SPACE, cap_id, ZC_WRITE));
|
||||
caps_.PushBack(MakeRefCounted<Capability>(vmas, Capability::ADDRESS_SPACE,
|
||||
cap_id, ZC_WRITE));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<MemoryObject>& vmmo) {
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(
|
||||
new Capability(vmmo, Capability::MEMORY_OBJECT, cap_id, ZC_WRITE));
|
||||
caps_.PushBack(MakeRefCounted<Capability>(vmmo, Capability::MEMORY_OBJECT,
|
||||
cap_id, ZC_WRITE));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<Channel>& chan) {
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(MakeRefCounted<Capability>(chan, Capability::CHANNEL, cap_id,
|
||||
ZC_WRITE | ZC_READ));
|
||||
return cap_id;
|
||||
}
|
||||
|
||||
void Process::AddCapability(uint64_t cap_id, const RefPtr<MemoryObject>& vmmo) {
|
||||
caps_.PushBack(
|
||||
new Capability(vmmo, Capability::MEMORY_OBJECT, cap_id, ZC_WRITE));
|
||||
caps_.PushBack(MakeRefCounted<Capability>(vmmo, Capability::MEMORY_OBJECT,
|
||||
cap_id, ZC_WRITE));
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
#include "capability/capability.h"
|
||||
#include "lib/linked_list.h"
|
||||
#include "lib/ref_ptr.h"
|
||||
#include "lib/shared_ptr.h"
|
||||
#include "object/address_space.h"
|
||||
#include "object/channel.h"
|
||||
|
||||
// Forward decl due to cyclic dependency.
|
||||
class Thread;
|
||||
|
@ -28,11 +28,15 @@ class Process : public KernelObject {
|
|||
RefPtr<Thread> CreateThread();
|
||||
RefPtr<Thread> GetThread(uint64_t tid);
|
||||
|
||||
SharedPtr<Capability> GetCapability(uint64_t cid);
|
||||
RefPtr<Capability> ReleaseCapability(uint64_t cid);
|
||||
RefPtr<Capability> GetCapability(uint64_t cid);
|
||||
// FIXME: We can't reset the cap id here.
|
||||
uint64_t AddCapability(const RefPtr<Capability>& cap);
|
||||
uint64_t AddCapability(const RefPtr<Thread>& t);
|
||||
uint64_t AddCapability(const RefPtr<Process>& p);
|
||||
uint64_t AddCapability(const RefPtr<AddressSpace>& vmas);
|
||||
uint64_t AddCapability(const RefPtr<MemoryObject>& vmmo);
|
||||
uint64_t AddCapability(const RefPtr<Channel>& chan);
|
||||
|
||||
void AddCapability(uint64_t cap_id, const RefPtr<MemoryObject>& vmmo);
|
||||
// Checks the state of all child threads and transitions to
|
||||
|
@ -53,5 +57,5 @@ class Process : public KernelObject {
|
|||
uint64_t next_cap_id_ = 0x100;
|
||||
|
||||
LinkedList<RefPtr<Thread>> threads_;
|
||||
LinkedList<SharedPtr<Capability>> caps_;
|
||||
LinkedList<RefPtr<Capability>> caps_;
|
||||
};
|
||||
|
|
|
@ -5,6 +5,7 @@
|
|||
#include "debug/debug.h"
|
||||
#include "include/zcall.h"
|
||||
#include "include/zerrors.h"
|
||||
#include "object/channel.h"
|
||||
#include "object/process.h"
|
||||
#include "scheduler/process_manager.h"
|
||||
#include "scheduler/scheduler.h"
|
||||
|
@ -56,40 +57,47 @@ void InitSyscall() {
|
|||
SetMSR(LSTAR, reinterpret_cast<uint64_t>(syscall_enter));
|
||||
}
|
||||
|
||||
uint64_t ProcessSpawn(ZProcessSpawnReq* req, ZProcessSpawnResp* resp) {
|
||||
z_err_t ValidateCap(const RefPtr<Capability>& cap, Capability::Type type,
|
||||
uint64_t permissions) {
|
||||
if (!cap) {
|
||||
return Z_ERR_CAP_NOT_FOUND;
|
||||
}
|
||||
if (!cap->CheckType(type)) {
|
||||
return Z_ERR_CAP_TYPE;
|
||||
}
|
||||
if (!cap->HasPermissions(permissions)) {
|
||||
return Z_ERR_CAP_DENIED;
|
||||
}
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t ProcessSpawn(ZProcessSpawnReq* req, ZProcessSpawnResp* resp) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
auto cap = curr_proc.GetCapability(req->proc_cap);
|
||||
if (cap.empty()) {
|
||||
return ZE_NOT_FOUND;
|
||||
}
|
||||
if (!cap->CheckType(Capability::PROCESS)) {
|
||||
return ZE_INVALID;
|
||||
}
|
||||
if (!cap->HasPermissions(ZC_PROC_SPAWN_PROC)) {
|
||||
return ZE_DENIED;
|
||||
}
|
||||
RET_ERR(ValidateCap(cap, Capability::PROCESS, ZC_PROC_SPAWN_PROC));
|
||||
|
||||
RefPtr<Process> proc = Process::Create();
|
||||
gProcMan->InsertProcess(proc);
|
||||
|
||||
resp->proc_cap = curr_proc.AddCapability(proc);
|
||||
resp->vmas_cap = curr_proc.AddCapability(proc->vmas());
|
||||
|
||||
if (req->bootstrap_cap != 0) {
|
||||
auto cap = curr_proc.ReleaseCapability(req->bootstrap_cap);
|
||||
if (!cap) {
|
||||
return Z_ERR_CAP_NOT_FOUND;
|
||||
}
|
||||
// FIXME: Check permissions.
|
||||
resp->bootstrap_cap = proc->AddCapability(cap);
|
||||
}
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
uint64_t ThreadCreate(ZThreadCreateReq* req, ZThreadCreateResp* resp) {
|
||||
z_err_t ThreadCreate(ZThreadCreateReq* req, ZThreadCreateResp* resp) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
auto cap = curr_proc.GetCapability(req->proc_cap);
|
||||
if (cap.empty()) {
|
||||
return ZE_NOT_FOUND;
|
||||
}
|
||||
if (!cap->CheckType(Capability::PROCESS)) {
|
||||
return ZE_INVALID;
|
||||
}
|
||||
|
||||
if (!cap->HasPermissions(ZC_PROC_SPAWN_THREAD)) {
|
||||
return ZE_DENIED;
|
||||
}
|
||||
RET_ERR(ValidateCap(cap, Capability::PROCESS, ZC_PROC_SPAWN_THREAD));
|
||||
|
||||
auto parent_proc = cap->obj<Process>();
|
||||
auto thread = parent_proc->CreateThread();
|
||||
|
@ -98,19 +106,10 @@ uint64_t ThreadCreate(ZThreadCreateReq* req, ZThreadCreateResp* resp) {
|
|||
return Z_OK;
|
||||
}
|
||||
|
||||
uint64_t ThreadStart(ZThreadStartReq* req) {
|
||||
z_err_t ThreadStart(ZThreadStartReq* req) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
auto cap = curr_proc.GetCapability(req->thread_cap);
|
||||
if (cap.empty()) {
|
||||
return ZE_NOT_FOUND;
|
||||
}
|
||||
if (!cap->CheckType(Capability::THREAD)) {
|
||||
return ZE_INVALID;
|
||||
}
|
||||
|
||||
if (!cap->HasPermissions(ZC_WRITE)) {
|
||||
return ZE_DENIED;
|
||||
}
|
||||
RET_ERR(ValidateCap(cap, Capability::THREAD, ZC_WRITE));
|
||||
|
||||
auto thread = cap->obj<Thread>();
|
||||
// FIXME: validate entry point is in user space.
|
||||
|
@ -118,21 +117,13 @@ uint64_t ThreadStart(ZThreadStartReq* req) {
|
|||
return Z_OK;
|
||||
}
|
||||
|
||||
uint64_t AddressSpaceMap(ZAddressSpaceMapReq* req, ZAddressSpaceMapResp* resp) {
|
||||
z_err_t AddressSpaceMap(ZAddressSpaceMapReq* req, ZAddressSpaceMapResp* resp) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
auto vmas_cap = curr_proc.GetCapability(req->vmas_cap);
|
||||
auto vmmo_cap = curr_proc.GetCapability(req->vmmo_cap);
|
||||
if (vmas_cap.empty() || vmmo_cap.empty()) {
|
||||
return ZE_NOT_FOUND;
|
||||
}
|
||||
if (!vmas_cap->CheckType(Capability::ADDRESS_SPACE) ||
|
||||
!vmmo_cap->CheckType(Capability::MEMORY_OBJECT)) {
|
||||
return ZE_INVALID;
|
||||
}
|
||||
if (!vmas_cap->HasPermissions(ZC_WRITE) ||
|
||||
!vmmo_cap->HasPermissions(ZC_WRITE)) {
|
||||
return ZE_DENIED;
|
||||
}
|
||||
RET_ERR(ValidateCap(vmas_cap, Capability::ADDRESS_SPACE, ZC_WRITE));
|
||||
RET_ERR(ValidateCap(vmmo_cap, Capability::MEMORY_OBJECT, ZC_WRITE));
|
||||
|
||||
auto vmas = vmas_cap->obj<AddressSpace>();
|
||||
auto vmmo = vmmo_cap->obj<MemoryObject>();
|
||||
// FIXME: Validation necessary.
|
||||
|
@ -142,17 +133,46 @@ uint64_t AddressSpaceMap(ZAddressSpaceMapReq* req, ZAddressSpaceMapResp* resp) {
|
|||
} else {
|
||||
resp->vaddr = vmas->MapInMemoryObject(vmmo);
|
||||
}
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
uint64_t MemoryObjectCreate(ZMemoryObjectCreateReq* req,
|
||||
ZMemoryObjectCreateResp* resp) {
|
||||
z_err_t MemoryObjectCreate(ZMemoryObjectCreateReq* req,
|
||||
ZMemoryObjectCreateResp* resp) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
resp->vmmo_cap =
|
||||
curr_proc.AddCapability(MakeRefCounted<MemoryObject>(req->size));
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
extern "C" uint64_t SyscallHandler(uint64_t call_id, void* req, void* resp) {
|
||||
z_err_t ChannelCreate(ZChannelCreateResp* resp) {
|
||||
auto& proc = gScheduler->CurrentProcess();
|
||||
auto chan_pair = Channel::CreateChannelPair();
|
||||
resp->chan_cap1 = proc.AddCapability(chan_pair.first());
|
||||
resp->chan_cap2 = proc.AddCapability(chan_pair.second());
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t ChannelSend(ZChannelSendReq* req) {
|
||||
auto& proc = gScheduler->CurrentProcess();
|
||||
auto chan_cap = proc.GetCapability(req->chan_cap);
|
||||
RET_ERR(ValidateCap(chan_cap, Capability::CHANNEL, ZC_WRITE));
|
||||
|
||||
auto chan = chan_cap->obj<Channel>();
|
||||
chan->Write(req->message);
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
z_err_t ChannelRecv(ZChannelRecvReq* req) {
|
||||
auto& proc = gScheduler->CurrentProcess();
|
||||
auto chan_cap = proc.GetCapability(req->chan_cap);
|
||||
RET_ERR(ValidateCap(chan_cap, Capability::CHANNEL, ZC_READ));
|
||||
|
||||
auto chan = chan_cap->obj<Channel>();
|
||||
chan->Read(req->message);
|
||||
return Z_OK;
|
||||
}
|
||||
|
||||
extern "C" z_err_t SyscallHandler(uint64_t call_id, void* req, void* resp) {
|
||||
Thread& thread = gScheduler->CurrentThread();
|
||||
switch (call_id) {
|
||||
case Z_PROCESS_EXIT:
|
||||
|
@ -181,11 +201,18 @@ extern "C" uint64_t SyscallHandler(uint64_t call_id, void* req, void* resp) {
|
|||
return MemoryObjectCreate(
|
||||
reinterpret_cast<ZMemoryObjectCreateReq*>(req),
|
||||
reinterpret_cast<ZMemoryObjectCreateResp*>(resp));
|
||||
case Z_CHANNEL_CREATE:
|
||||
return ChannelCreate(reinterpret_cast<ZChannelCreateResp*>(resp));
|
||||
case Z_CHANNEL_SEND:
|
||||
return ChannelSend(reinterpret_cast<ZChannelSendReq*>(req));
|
||||
case Z_CHANNEL_RECV:
|
||||
return ChannelRecv(reinterpret_cast<ZChannelRecvReq*>(req));
|
||||
case Z_DEBUG_PRINT:
|
||||
dbgln("[Debug] %s", req);
|
||||
return Z_OK;
|
||||
break;
|
||||
default:
|
||||
panic("Unhandled syscall number: %x", call_id);
|
||||
}
|
||||
return 1;
|
||||
UNREACHABLE
|
||||
}
|
||||
|
|
|
@ -4,20 +4,8 @@
|
|||
|
||||
#include "usr/zcall_internal.h"
|
||||
|
||||
uint64_t SysCall0(uint64_t number) {
|
||||
uint64_t return_code;
|
||||
asm("syscall" : "=a"(return_code) : "D"(number));
|
||||
return return_code;
|
||||
}
|
||||
|
||||
uint64_t SysCall1(uint64_t number, const void* first) {
|
||||
uint64_t return_code;
|
||||
asm("syscall" : "=a"(return_code) : "D"(number), "S"(first) : "rcx", "r11");
|
||||
return return_code;
|
||||
}
|
||||
|
||||
uint64_t SysCall2(uint64_t number, const void* first, const void* second) {
|
||||
uint64_t return_code;
|
||||
z_err_t SysCall2(uint64_t number, const void* first, const void* second) {
|
||||
z_err_t return_code;
|
||||
asm("syscall"
|
||||
: "=a"(return_code)
|
||||
: "D"(number), "S"(first), "d"(second)
|
||||
|
@ -25,34 +13,43 @@ uint64_t SysCall2(uint64_t number, const void* first, const void* second) {
|
|||
return return_code;
|
||||
}
|
||||
|
||||
z_err_t SysCall0(uint64_t number) { return SysCall2(number, 0, 0); }
|
||||
|
||||
z_err_t SysCall1(uint64_t number, const void* first) {
|
||||
return SysCall2(number, first, 0);
|
||||
}
|
||||
|
||||
void ZProcessExit(uint64_t code) {
|
||||
SysCall1(Z_PROCESS_EXIT, reinterpret_cast<void*>(code));
|
||||
}
|
||||
|
||||
uint64_t ZProcessSpawn(uint64_t proc_cap, uint64_t* new_proc_cap,
|
||||
uint64_t* new_vmas_cap) {
|
||||
z_err_t ZProcessSpawn(uint64_t proc_cap, uint64_t bootstrap_cap,
|
||||
uint64_t* new_proc_cap, uint64_t* new_vmas_cap,
|
||||
uint64_t* new_bootstrap_cap) {
|
||||
ZProcessSpawnReq req{
|
||||
.proc_cap = proc_cap,
|
||||
.bootstrap_cap = bootstrap_cap,
|
||||
};
|
||||
ZProcessSpawnResp resp;
|
||||
uint64_t ret = SysCall2(Z_PROCESS_SPAWN, &req, &resp);
|
||||
z_err_t ret = SysCall2(Z_PROCESS_SPAWN, &req, &resp);
|
||||
*new_proc_cap = resp.proc_cap;
|
||||
*new_vmas_cap = resp.vmas_cap;
|
||||
*new_bootstrap_cap = resp.bootstrap_cap;
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint64_t ZThreadCreate(uint64_t proc_cap, uint64_t* thread_cap) {
|
||||
z_err_t ZThreadCreate(uint64_t proc_cap, uint64_t* thread_cap) {
|
||||
ZThreadCreateReq req{
|
||||
.proc_cap = proc_cap,
|
||||
};
|
||||
ZThreadCreateResp resp;
|
||||
uint64_t ret = SysCall2(Z_THREAD_CREATE, &req, &resp);
|
||||
z_err_t ret = SysCall2(Z_THREAD_CREATE, &req, &resp);
|
||||
*thread_cap = resp.thread_cap;
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint64_t ZThreadStart(uint64_t thread_cap, uint64_t entry, uint64_t arg1,
|
||||
uint64_t arg2) {
|
||||
z_err_t ZThreadStart(uint64_t thread_cap, uint64_t entry, uint64_t arg1,
|
||||
uint64_t arg2) {
|
||||
ZThreadStartReq req{
|
||||
.thread_cap = thread_cap,
|
||||
.entry = entry,
|
||||
|
@ -64,28 +61,72 @@ uint64_t ZThreadStart(uint64_t thread_cap, uint64_t entry, uint64_t arg1,
|
|||
|
||||
void ZThreadExit() { SysCall0(Z_THREAD_EXIT); }
|
||||
|
||||
uint64_t ZAddressSpaceMap(uint64_t vmas_cap, uint64_t vmas_offset,
|
||||
uint64_t vmmo_cap, uint64_t* vaddr) {
|
||||
z_err_t ZAddressSpaceMap(uint64_t vmas_cap, uint64_t vmas_offset,
|
||||
uint64_t vmmo_cap, uint64_t* vaddr) {
|
||||
ZAddressSpaceMapReq req{
|
||||
.vmas_cap = vmas_cap,
|
||||
.vmas_offset = vmas_offset,
|
||||
.vmmo_cap = vmmo_cap,
|
||||
};
|
||||
ZAddressSpaceMapResp resp;
|
||||
uint64_t ret = SysCall2(Z_ADDRESS_SPACE_MAP, &req, &resp);
|
||||
z_err_t ret = SysCall2(Z_ADDRESS_SPACE_MAP, &req, &resp);
|
||||
*vaddr = resp.vaddr;
|
||||
return ret;
|
||||
}
|
||||
uint64_t ZMemoryObjectCreate(uint64_t size, uint64_t* vmmo_cap) {
|
||||
z_err_t ZMemoryObjectCreate(uint64_t size, uint64_t* vmmo_cap) {
|
||||
ZMemoryObjectCreateReq req{
|
||||
.size = size,
|
||||
};
|
||||
ZMemoryObjectCreateResp resp;
|
||||
uint64_t ret = SysCall2(Z_MEMORY_OBJECT_CREATE, &req, &resp);
|
||||
z_err_t ret = SysCall2(Z_MEMORY_OBJECT_CREATE, &req, &resp);
|
||||
*vmmo_cap = resp.vmmo_cap;
|
||||
return ret;
|
||||
}
|
||||
|
||||
uint64_t ZDebug(const char* message) {
|
||||
return SysCall1(Z_DEBUG_PRINT, message);
|
||||
z_err_t ZChannelCreate(uint64_t* channel1, uint64_t* channel2) {
|
||||
ZChannelCreateResp resp;
|
||||
z_err_t ret = SysCall2(Z_CHANNEL_CREATE, 0, &resp);
|
||||
*channel1 = resp.chan_cap1;
|
||||
*channel2 = resp.chan_cap2;
|
||||
return ret;
|
||||
}
|
||||
|
||||
z_err_t ZChannelSend(uint64_t chan_cap, uint64_t type, uint64_t num_bytes,
|
||||
const uint8_t* bytes, uint64_t num_caps,
|
||||
const uint64_t* caps) {
|
||||
ZChannelSendReq req{
|
||||
.chan_cap = chan_cap,
|
||||
.message =
|
||||
{
|
||||
.type = type,
|
||||
.num_bytes = num_bytes,
|
||||
.bytes = const_cast<uint8_t*>(bytes),
|
||||
.num_caps = num_caps,
|
||||
.caps = const_cast<uint64_t*>(caps),
|
||||
},
|
||||
};
|
||||
return SysCall1(Z_CHANNEL_SEND, &req);
|
||||
}
|
||||
|
||||
z_err_t ZChannelRecv(uint64_t chan_cap, uint64_t num_bytes, uint8_t* bytes,
|
||||
uint64_t num_caps, uint64_t* caps, uint64_t* type,
|
||||
uint64_t* actual_bytes, uint64_t* actual_caps) {
|
||||
ZChannelRecvReq req{
|
||||
.chan_cap = chan_cap,
|
||||
.message =
|
||||
{
|
||||
.type = 0,
|
||||
.num_bytes = num_bytes,
|
||||
.bytes = bytes,
|
||||
.num_caps = num_caps,
|
||||
.caps = caps,
|
||||
},
|
||||
};
|
||||
z_err_t ret = SysCall1(Z_CHANNEL_RECV, &req);
|
||||
*type = req.message.type;
|
||||
*actual_bytes = req.message.num_bytes;
|
||||
*actual_caps = req.message.num_caps;
|
||||
return ret;
|
||||
}
|
||||
|
||||
z_err_t ZDebug(const char* message) { return SysCall1(Z_DEBUG_PRINT, message); }
|
||||
|
|
|
@ -4,11 +4,13 @@
|
|||
|
||||
struct ZProcessSpawnReq {
|
||||
uint64_t proc_cap;
|
||||
uint64_t bootstrap_cap;
|
||||
};
|
||||
|
||||
struct ZProcessSpawnResp {
|
||||
uint64_t proc_cap;
|
||||
uint64_t vmas_cap;
|
||||
uint64_t bootstrap_cap;
|
||||
};
|
||||
|
||||
struct ZThreadCreateReq {
|
||||
|
@ -43,3 +45,28 @@ struct ZMemoryObjectCreateReq {
|
|||
struct ZMemoryObjectCreateResp {
|
||||
uint64_t vmmo_cap;
|
||||
};
|
||||
|
||||
struct ZChannelCreateResp {
|
||||
uint64_t chan_cap1;
|
||||
uint64_t chan_cap2;
|
||||
};
|
||||
|
||||
struct ZMessage {
|
||||
uint64_t type;
|
||||
|
||||
uint64_t num_bytes;
|
||||
uint8_t* bytes;
|
||||
|
||||
uint64_t num_caps;
|
||||
uint64_t* caps;
|
||||
};
|
||||
|
||||
struct ZChannelSendReq {
|
||||
uint64_t chan_cap;
|
||||
ZMessage message;
|
||||
};
|
||||
|
||||
struct ZChannelRecvReq {
|
||||
uint64_t chan_cap;
|
||||
ZMessage message;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue