2023-05-18 12:43:53 -07:00
|
|
|
#pragma once
|
|
|
|
|
2023-06-26 15:46:03 -07:00
|
|
|
#include <glacier/container/vector.h>
|
2023-06-21 15:07:40 -07:00
|
|
|
#include <glacier/memory/ref_ptr.h>
|
2023-05-18 12:43:53 -07:00
|
|
|
#include <stdint.h>
|
|
|
|
|
2023-05-30 23:55:42 -07:00
|
|
|
#include "capability/capability.h"
|
2023-06-16 14:25:45 -07:00
|
|
|
#include "capability/capability_table.h"
|
2023-06-06 20:43:15 -07:00
|
|
|
#include "object/address_space.h"
|
2023-06-07 08:24:10 -07:00
|
|
|
#include "object/channel.h"
|
2023-10-25 14:47:45 -07:00
|
|
|
#include "object/mutex.h"
|
2023-06-12 19:20:51 -07:00
|
|
|
#include "object/port.h"
|
2023-05-29 15:08:02 -07:00
|
|
|
|
2023-05-18 12:43:53 -07:00
|
|
|
// Forward decl due to cyclic dependency.
|
|
|
|
class Thread;
|
|
|
|
|
2023-06-16 14:53:57 -07:00
|
|
|
template <>
|
|
|
|
struct KernelObjectTag<Process> {
|
|
|
|
static const uint64_t type = KernelObject::PROCESS;
|
|
|
|
};
|
|
|
|
|
2023-06-06 20:13:07 -07:00
|
|
|
class Process : public KernelObject {
|
2023-05-18 12:43:53 -07:00
|
|
|
public:
|
2023-06-16 14:53:57 -07:00
|
|
|
uint64_t TypeTag() override { return KernelObject::PROCESS; }
|
2023-08-01 18:22:41 -07:00
|
|
|
static uint64_t DefaultPermissions() {
|
|
|
|
return kZionPerm_Write | kZionPerm_Read | kZionPerm_SpawnThread |
|
2023-08-01 18:43:48 -07:00
|
|
|
kZionPerm_SpawnProcess | kZionPerm_Duplicate | kZionPerm_Transmit;
|
2023-08-01 18:22:41 -07:00
|
|
|
}
|
|
|
|
|
2023-05-29 13:51:00 -07:00
|
|
|
enum State {
|
|
|
|
UNSPECIFIED,
|
|
|
|
SETUP,
|
|
|
|
RUNNING,
|
|
|
|
FINISHED,
|
|
|
|
};
|
2023-06-21 15:07:40 -07:00
|
|
|
static glcr::RefPtr<Process> RootProcess();
|
|
|
|
static glcr::RefPtr<Process> Create();
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-05-29 15:50:38 -07:00
|
|
|
uint64_t id() const { return id_; }
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<AddressSpace> vmas() { return vmas_; }
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Thread> CreateThread();
|
|
|
|
glcr::RefPtr<Thread> GetThread(uint64_t tid);
|
2023-05-18 12:43:53 -07:00
|
|
|
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<Capability> ReleaseCapability(uint64_t cid);
|
|
|
|
glcr::RefPtr<Capability> GetCapability(uint64_t cid);
|
2023-06-16 15:04:33 -07:00
|
|
|
|
|
|
|
template <typename T>
|
2023-06-21 15:07:40 -07:00
|
|
|
uint64_t AddNewCapability(const glcr::RefPtr<T>& obj, uint64_t permissions) {
|
2023-06-16 15:04:33 -07:00
|
|
|
return caps_.AddNewCapability(obj, permissions);
|
|
|
|
}
|
2023-08-01 18:22:41 -07:00
|
|
|
template <typename T>
|
|
|
|
uint64_t AddNewCapability(const glcr::RefPtr<T>& obj) {
|
|
|
|
return caps_.AddNewCapability(obj);
|
|
|
|
}
|
2023-06-21 15:07:40 -07:00
|
|
|
uint64_t AddExistingCapability(const glcr::RefPtr<Capability>& cap);
|
2023-06-16 15:09:15 -07:00
|
|
|
|
2023-05-29 13:51:00 -07:00
|
|
|
// Checks the state of all child threads and transitions to
|
|
|
|
// finished if all have finished.
|
|
|
|
void CheckState();
|
|
|
|
|
|
|
|
State GetState() { return state_; }
|
|
|
|
|
2023-11-16 23:03:27 -08:00
|
|
|
void Exit();
|
|
|
|
|
2023-05-18 12:43:53 -07:00
|
|
|
private:
|
2023-06-21 15:07:40 -07:00
|
|
|
friend class glcr::MakeRefCountedFriend<Process>;
|
2023-06-06 19:12:46 -07:00
|
|
|
Process();
|
2023-06-07 00:30:26 -07:00
|
|
|
Process(uint64_t id) : id_(id), vmas_(AddressSpace::ForRoot()) {}
|
2023-06-12 20:56:25 -07:00
|
|
|
|
2023-10-25 14:47:45 -07:00
|
|
|
glcr::RefPtr<Mutex> mutex_ = Mutex::Create();
|
2023-06-12 20:56:25 -07:00
|
|
|
|
2023-05-18 12:43:53 -07:00
|
|
|
uint64_t id_;
|
2023-06-21 15:07:40 -07:00
|
|
|
glcr::RefPtr<AddressSpace> vmas_;
|
2023-05-29 13:51:00 -07:00
|
|
|
State state_;
|
2023-05-18 12:43:53 -07:00
|
|
|
|
|
|
|
uint64_t next_thread_id_ = 0;
|
|
|
|
|
2023-06-26 15:46:03 -07:00
|
|
|
glcr::Vector<glcr::RefPtr<Thread>> threads_;
|
2023-06-16 14:25:45 -07:00
|
|
|
CapabilityTable caps_;
|
2023-05-18 12:43:53 -07:00
|
|
|
};
|