[zion] Dynamically check Capability type.
Instead of passing an enum with the capability when creating it, relying on polymorphism and a template struct tag to determine the object type at runtime. This is cleaner and avoids errors where we pass the wrong capability type with the cap and do a bad cast at runtime.
This commit is contained in:
parent
b4902a79ef
commit
a47bac9966
13 changed files with 113 additions and 106 deletions
|
|
@ -6,6 +6,13 @@
|
|||
#include "memory/user_stack_manager.h"
|
||||
#include "object/memory_object.h"
|
||||
|
||||
class AddressSpace;
|
||||
|
||||
template <>
|
||||
struct KernelObjectTag<AddressSpace> {
|
||||
static const uint64_t type = KernelObject::ADDRESS_SPACE;
|
||||
};
|
||||
|
||||
// VirtualMemory class holds a memory space for an individual process.
|
||||
//
|
||||
// Memory Regions are predefined for simplicity for now. However, in general
|
||||
|
|
@ -26,6 +33,8 @@
|
|||
// 0xFFFFFFFF 90000000 - 0xFFFFFFFF 9FFFFFFF : KERNEL_STACK (256 MiB)
|
||||
class AddressSpace : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::ADDRESS_SPACE; }
|
||||
|
||||
enum MemoryType {
|
||||
UNSPECIFIED,
|
||||
UNMAPPED,
|
||||
|
|
|
|||
|
|
@ -10,8 +10,16 @@
|
|||
#include "object/kernel_object.h"
|
||||
#include "usr/zcall_internal.h"
|
||||
|
||||
class Channel;
|
||||
|
||||
template <>
|
||||
struct KernelObjectTag<Channel> {
|
||||
static const uint64_t type = KernelObject::CHANNEL;
|
||||
};
|
||||
|
||||
class Channel : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::CHANNEL; }
|
||||
static Pair<RefPtr<Channel>, RefPtr<Channel>> CreateChannelPair();
|
||||
|
||||
RefPtr<Channel> peer() { return peer_; }
|
||||
|
|
|
|||
|
|
@ -2,4 +2,22 @@
|
|||
|
||||
#include "lib/ref_counted.h"
|
||||
|
||||
class KernelObject : public RefCounted<KernelObject> {};
|
||||
class KernelObject : public RefCounted<KernelObject> {
|
||||
public:
|
||||
enum ObjectType {
|
||||
INVALID = 0x0,
|
||||
PROCESS = 0x1,
|
||||
THREAD = 0x2,
|
||||
ADDRESS_SPACE = 0x3,
|
||||
MEMORY_OBJECT = 0x4,
|
||||
CHANNEL = 0x5,
|
||||
PORT = 0x6,
|
||||
};
|
||||
|
||||
virtual uint64_t TypeTag() = 0;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct KernelObjectTag {
|
||||
static const int type = KernelObject::INVALID;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -3,6 +3,12 @@
|
|||
#include "lib/linked_list.h"
|
||||
#include "object/kernel_object.h"
|
||||
|
||||
class MemoryObject;
|
||||
template <>
|
||||
struct KernelObjectTag<MemoryObject> {
|
||||
static const uint64_t type = KernelObject::MEMORY_OBJECT;
|
||||
};
|
||||
|
||||
/*
|
||||
* MemoryObject is a page-aligned set of memory that corresponds
|
||||
* to physical pages.
|
||||
|
|
@ -11,6 +17,7 @@
|
|||
* */
|
||||
class MemoryObject : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::MEMORY_OBJECT; }
|
||||
MemoryObject(uint64_t size);
|
||||
|
||||
uint64_t size() { return size_; }
|
||||
|
|
|
|||
|
|
@ -6,8 +6,17 @@
|
|||
#include "object/thread.h"
|
||||
#include "usr/zcall_internal.h"
|
||||
|
||||
class Port;
|
||||
|
||||
template <>
|
||||
struct KernelObjectTag<Port> {
|
||||
static const uint64_t type = KernelObject::PORT;
|
||||
};
|
||||
|
||||
class Port : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::PORT; }
|
||||
|
||||
Port();
|
||||
|
||||
z_err_t Write(const ZMessage& msg);
|
||||
|
|
|
|||
|
|
@ -23,10 +23,8 @@ RefPtr<Process> Process::RootProcess() {
|
|||
RefPtr<Process> Process::Create() {
|
||||
auto proc = MakeRefCounted<Process>();
|
||||
proc->caps_.AddNewCapabilityWithId(Z_INIT_PROC_SELF, proc,
|
||||
Capability::PROCESS,
|
||||
ZC_PROC_SPAWN_PROC | ZC_PROC_SPAWN_THREAD);
|
||||
proc->caps_.AddNewCapabilityWithId(Z_INIT_VMAS_SELF, proc->vmas(),
|
||||
Capability::ADDRESS_SPACE, ZC_WRITE);
|
||||
proc->caps_.AddNewCapabilityWithId(Z_INIT_VMAS_SELF, proc->vmas(), ZC_WRITE);
|
||||
return proc;
|
||||
}
|
||||
|
||||
|
|
@ -77,31 +75,29 @@ uint64_t Process::AddCapability(const RefPtr<Capability>& cap) {
|
|||
return caps_.AddExistingCapability(cap);
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<Thread>& thread) {
|
||||
return caps_.AddNewCapability(thread, Capability::THREAD, ZC_WRITE);
|
||||
return caps_.AddNewCapability(thread, ZC_WRITE);
|
||||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Process>& proc) {
|
||||
return caps_.AddNewCapability(proc, Capability::PROCESS,
|
||||
ZC_WRITE | ZC_PROC_SPAWN_THREAD);
|
||||
return caps_.AddNewCapability(proc, ZC_WRITE | ZC_PROC_SPAWN_THREAD);
|
||||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<AddressSpace>& vmas) {
|
||||
return caps_.AddNewCapability(vmas, Capability::ADDRESS_SPACE, ZC_WRITE);
|
||||
return caps_.AddNewCapability(vmas, ZC_WRITE);
|
||||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<MemoryObject>& vmmo) {
|
||||
return caps_.AddNewCapability(vmmo, Capability::MEMORY_OBJECT, ZC_WRITE);
|
||||
return caps_.AddNewCapability(vmmo, ZC_WRITE);
|
||||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Channel>& chan) {
|
||||
return caps_.AddNewCapability(chan, Capability::CHANNEL, ZC_WRITE | ZC_READ);
|
||||
return caps_.AddNewCapability(chan, ZC_WRITE | ZC_READ);
|
||||
}
|
||||
|
||||
uint64_t Process::AddCapability(const RefPtr<Port>& port) {
|
||||
return caps_.AddNewCapability(port, Capability::PORT, ZC_WRITE | ZC_READ);
|
||||
return caps_.AddNewCapability(port, ZC_WRITE | ZC_READ);
|
||||
}
|
||||
|
||||
void Process::AddCapability(uint64_t cap_id, const RefPtr<MemoryObject>& vmmo) {
|
||||
caps_.AddNewCapabilityWithId(cap_id, vmmo, Capability::MEMORY_OBJECT,
|
||||
ZC_WRITE);
|
||||
caps_.AddNewCapabilityWithId(cap_id, vmmo, ZC_WRITE);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -14,8 +14,14 @@
|
|||
// Forward decl due to cyclic dependency.
|
||||
class Thread;
|
||||
|
||||
template <>
|
||||
struct KernelObjectTag<Process> {
|
||||
static const uint64_t type = KernelObject::PROCESS;
|
||||
};
|
||||
|
||||
class Process : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::PROCESS; }
|
||||
enum State {
|
||||
UNSPECIFIED,
|
||||
SETUP,
|
||||
|
|
|
|||
|
|
@ -7,9 +7,16 @@
|
|||
|
||||
// Forward decl due to cyclic dependency.
|
||||
class Process;
|
||||
class Thread;
|
||||
|
||||
template <>
|
||||
struct KernelObjectTag<Thread> {
|
||||
static const uint64_t type = KernelObject::THREAD;
|
||||
};
|
||||
|
||||
class Thread : public KernelObject {
|
||||
public:
|
||||
uint64_t TypeTag() override { return KernelObject::THREAD; }
|
||||
enum State {
|
||||
UNSPECIFIED,
|
||||
CREATED,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue