Make a KernelObject base class for all Capabilities.
This commit is contained in:
parent
d358c1d672
commit
4e278a4664
|
@ -1,11 +1,14 @@
|
||||||
#include "capability/capability.h"
|
#include "capability/capability.h"
|
||||||
|
|
||||||
|
#include "scheduler/process.h"
|
||||||
|
#include "scheduler/thread.h"
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
Process& Capability::obj<Process>() {
|
Process& Capability::obj<Process>() {
|
||||||
if (type_ != PROCESS) {
|
if (type_ != PROCESS) {
|
||||||
panic("Accessing %u cap as object.", type_);
|
panic("Accessing %u cap as object.", type_);
|
||||||
}
|
}
|
||||||
return *static_cast<Process*>(obj_);
|
return *static_cast<Process*>(obj_.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
template <>
|
template <>
|
||||||
|
@ -13,5 +16,5 @@ Thread& Capability::obj<Thread>() {
|
||||||
if (type_ != THREAD) {
|
if (type_ != THREAD) {
|
||||||
panic("Accessing %u cap as object.", type_);
|
panic("Accessing %u cap as object.", type_);
|
||||||
}
|
}
|
||||||
return *static_cast<Thread*>(obj_);
|
return *static_cast<Thread*>(obj_.get());
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,8 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "debug/debug.h"
|
#include "lib/ref_ptr.h"
|
||||||
|
#include "object/kernel_object.h"
|
||||||
|
|
||||||
class Process;
|
class Process;
|
||||||
class Thread;
|
class Thread;
|
||||||
|
@ -14,9 +15,15 @@ class Capability {
|
||||||
PROCESS,
|
PROCESS,
|
||||||
THREAD,
|
THREAD,
|
||||||
};
|
};
|
||||||
Capability(void* obj, Type type, uint64_t id, uint64_t permissions)
|
Capability(const RefPtr<KernelObject>& obj, Type type, uint64_t id,
|
||||||
|
uint64_t permissions)
|
||||||
: obj_(obj), type_(type), id_(id), permissions_(permissions) {}
|
: obj_(obj), type_(type), id_(id), permissions_(permissions) {}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Capability(const RefPtr<T>& obj, Type type, uint64_t id, uint64_t permissions)
|
||||||
|
: Capability(StaticCastRefPtr<KernelObject>(obj), type, id, permissions) {
|
||||||
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
T& obj();
|
T& obj();
|
||||||
|
|
||||||
|
@ -30,8 +37,7 @@ class Capability {
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// FIXME: This should somehow be a shared ptr to keep the object alive.
|
RefPtr<KernelObject> obj_;
|
||||||
void* obj_;
|
|
||||||
Type type_;
|
Type type_;
|
||||||
uint64_t id_;
|
uint64_t id_;
|
||||||
uint64_t permissions_;
|
uint64_t permissions_;
|
||||||
|
|
|
@ -6,6 +6,9 @@ class RefPtr;
|
||||||
template <typename T>
|
template <typename T>
|
||||||
RefPtr<T> AdoptPtr(T* ptr);
|
RefPtr<T> AdoptPtr(T* ptr);
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
RefPtr<T> StaticCastRefPtr(const RefPtr<U>& ref);
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
class RefPtr {
|
class RefPtr {
|
||||||
public:
|
public:
|
||||||
|
@ -38,6 +41,11 @@ class RefPtr {
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
enum DontAdoptTag {
|
||||||
|
DontAdopt,
|
||||||
|
};
|
||||||
|
RefPtr(T* ptr, DontAdoptTag) : ptr_(ptr) { ptr->Acquire(); }
|
||||||
|
|
||||||
T* get() const { return ptr_; };
|
T* get() const { return ptr_; };
|
||||||
T& operator*() const { return *ptr_; }
|
T& operator*() const { return *ptr_; }
|
||||||
T* operator->() const { return ptr_; }
|
T* operator->() const { return ptr_; }
|
||||||
|
@ -74,3 +82,8 @@ template <typename T>
|
||||||
RefPtr<T> AdoptPtr(T* ptr) {
|
RefPtr<T> AdoptPtr(T* ptr) {
|
||||||
return RefPtr(ptr);
|
return RefPtr(ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template <typename T, typename U>
|
||||||
|
RefPtr<T> StaticCastRefPtr(const RefPtr<U>& ref) {
|
||||||
|
return RefPtr(static_cast<T*>(ref.get()), RefPtr<T>::DontAdopt);
|
||||||
|
}
|
||||||
|
|
|
@ -50,7 +50,7 @@ void LoadInitProgram() {
|
||||||
const limine_file& init_prog = GetInitProgram("/sys/test");
|
const limine_file& init_prog = GetInitProgram("/sys/test");
|
||||||
const limine_file& prog2 = GetInitProgram("/sys/test2");
|
const limine_file& prog2 = GetInitProgram("/sys/test2");
|
||||||
|
|
||||||
RefPtr<Process> proc = MakeRefCounted<Process>();
|
RefPtr<Process> proc = Process::Create();
|
||||||
gProcMan->InsertProcess(proc);
|
gProcMan->InsertProcess(proc);
|
||||||
|
|
||||||
uint64_t entry = LoadElfProgram(
|
uint64_t entry = LoadElfProgram(
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "lib/ref_counted.h"
|
||||||
|
|
||||||
|
class KernelObject : public RefCounted<KernelObject> {};
|
|
@ -20,13 +20,16 @@ RefPtr<Process> Process::RootProcess() {
|
||||||
|
|
||||||
return proc;
|
return proc;
|
||||||
}
|
}
|
||||||
RefPtr<Process> Process::Create() { return MakeRefCounted<Process>(); }
|
RefPtr<Process> Process::Create() {
|
||||||
|
auto proc = MakeRefCounted<Process>();
|
||||||
Process::Process() : id_(gNextId++), state_(RUNNING) {
|
proc->caps_.PushBack(
|
||||||
caps_.PushBack(new Capability(this, Capability::PROCESS, Z_INIT_PROC_SELF,
|
new Capability(proc, Capability::PROCESS, Z_INIT_PROC_SELF,
|
||||||
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD));
|
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD));
|
||||||
|
return proc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Process::Process() : id_(gNextId++), state_(RUNNING) {}
|
||||||
|
|
||||||
RefPtr<Thread> Process::CreateThread() {
|
RefPtr<Thread> Process::CreateThread() {
|
||||||
RefPtr<Thread> thread = MakeRefCounted<Thread>(*this, next_thread_id_++);
|
RefPtr<Thread> thread = MakeRefCounted<Thread>(*this, next_thread_id_++);
|
||||||
threads_.PushBack(thread);
|
threads_.PushBack(thread);
|
||||||
|
@ -65,12 +68,12 @@ SharedPtr<Capability> Process::GetCapability(uint64_t cid) {
|
||||||
++iter;
|
++iter;
|
||||||
}
|
}
|
||||||
dbgln("Bad cap access");
|
dbgln("Bad cap access");
|
||||||
|
dbgln("Num caps: %u", caps_.size());
|
||||||
return {};
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t Process::AddCapability(RefPtr<Thread>& thread) {
|
uint64_t Process::AddCapability(const RefPtr<Thread>& thread) {
|
||||||
uint64_t cap_id = next_cap_id_++;
|
uint64_t cap_id = next_cap_id_++;
|
||||||
caps_.PushBack(
|
caps_.PushBack(new Capability(thread, Capability::THREAD, cap_id, ZC_WRITE));
|
||||||
new Capability(thread.get(), Capability::THREAD, cap_id, ZC_WRITE));
|
|
||||||
return cap_id;
|
return cap_id;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@
|
||||||
|
|
||||||
#include "capability/capability.h"
|
#include "capability/capability.h"
|
||||||
#include "lib/linked_list.h"
|
#include "lib/linked_list.h"
|
||||||
#include "lib/ref_counted.h"
|
|
||||||
#include "lib/ref_ptr.h"
|
#include "lib/ref_ptr.h"
|
||||||
#include "lib/shared_ptr.h"
|
#include "lib/shared_ptr.h"
|
||||||
#include "memory/virtual_memory.h"
|
#include "memory/virtual_memory.h"
|
||||||
|
@ -12,7 +11,7 @@
|
||||||
// Forward decl due to cyclic dependency.
|
// Forward decl due to cyclic dependency.
|
||||||
class Thread;
|
class Thread;
|
||||||
|
|
||||||
class Process : public RefCounted<Process> {
|
class Process : public KernelObject {
|
||||||
public:
|
public:
|
||||||
enum State {
|
enum State {
|
||||||
UNSPECIFIED,
|
UNSPECIFIED,
|
||||||
|
@ -30,7 +29,7 @@ class Process : public RefCounted<Process> {
|
||||||
RefPtr<Thread> GetThread(uint64_t tid);
|
RefPtr<Thread> GetThread(uint64_t tid);
|
||||||
|
|
||||||
SharedPtr<Capability> GetCapability(uint64_t cid);
|
SharedPtr<Capability> GetCapability(uint64_t cid);
|
||||||
uint64_t AddCapability(RefPtr<Thread>& t);
|
uint64_t AddCapability(const RefPtr<Thread>& t);
|
||||||
// Checks the state of all child threads and transitions to
|
// Checks the state of all child threads and transitions to
|
||||||
// finished if all have finished.
|
// finished if all have finished.
|
||||||
void CheckState();
|
void CheckState();
|
||||||
|
|
|
@ -2,13 +2,13 @@
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
#include "lib/ref_counted.h"
|
|
||||||
#include "lib/ref_ptr.h"
|
#include "lib/ref_ptr.h"
|
||||||
|
#include "object/kernel_object.h"
|
||||||
|
|
||||||
// Forward decl due to cyclic dependency.
|
// Forward decl due to cyclic dependency.
|
||||||
class Process;
|
class Process;
|
||||||
|
|
||||||
class Thread : public RefCounted<Thread> {
|
class Thread : public KernelObject {
|
||||||
public:
|
public:
|
||||||
enum State {
|
enum State {
|
||||||
UNSPECIFIED,
|
UNSPECIFIED,
|
||||||
|
|
|
@ -71,7 +71,7 @@ uint64_t ProcessSpawnElf(ZProcessSpawnElfReq* req) {
|
||||||
return ZE_DENIED;
|
return ZE_DENIED;
|
||||||
}
|
}
|
||||||
dbgln("Proc spawn: %u:%u", req->elf_base, req->elf_size);
|
dbgln("Proc spawn: %u:%u", req->elf_base, req->elf_size);
|
||||||
RefPtr<Process> proc = MakeRefCounted<Process>();
|
RefPtr<Process> proc = Process::Create();
|
||||||
gProcMan->InsertProcess(proc);
|
gProcMan->InsertProcess(proc);
|
||||||
uint64_t entry = LoadElfProgram(*proc, req->elf_base, req->elf_size);
|
uint64_t entry = LoadElfProgram(*proc, req->elf_base, req->elf_size);
|
||||||
proc->CreateThread()->Start(entry, 0, 0);
|
proc->CreateThread()->Start(entry, 0, 0);
|
||||||
|
|
Loading…
Reference in New Issue