Compare commits
3 Commits
eb454300e6
...
6c10c57bfa
Author | SHA1 | Date |
---|---|---|
Drew Galbraith | 6c10c57bfa | |
Drew Galbraith | a8a4f8d9ab | |
Drew Galbraith | e246f28d9c |
|
@ -1,6 +1,7 @@
|
|||
#include "include/mammoth/process.h"
|
||||
|
||||
#include <zcall.h>
|
||||
#include <zerrors.h>
|
||||
|
||||
#include "include/mammoth/debug.h"
|
||||
|
||||
|
@ -68,7 +69,7 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
|
|||
|
||||
dbgln("Map Local");
|
||||
uint64_t vaddr;
|
||||
check(ZAddressSpaceMap(Z_INIT_AS_SELF, 0, mem_cap, &vaddr));
|
||||
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, mem_cap, &vaddr));
|
||||
|
||||
dbgln("Copy");
|
||||
memcpy(base + program.offset, program.filesz, vaddr);
|
||||
|
@ -94,4 +95,6 @@ uint64_t SpawnProcessFromElfRegion(uint64_t program) {
|
|||
|
||||
dbgln("Thread start");
|
||||
check(ZThreadStart(thread_cap, entry_point, 0, 0));
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
#include "include/mammoth/thread.h"
|
||||
#include "mammoth/thread.h"
|
||||
|
||||
#include <zcall.h>
|
||||
|
||||
#include "mammoth/debug.h"
|
||||
|
||||
namespace {
|
||||
|
||||
extern "C" void thread_entry(Thread::Entry entry, void* arg1) {
|
||||
|
@ -13,7 +15,8 @@ extern "C" void thread_entry(Thread::Entry entry, void* arg1) {
|
|||
} // namespace
|
||||
|
||||
Thread::Thread(Entry e, const void* arg1) {
|
||||
ZThreadCreate(Z_INIT_PROC_SELF, &thread_cap_);
|
||||
ZThreadStart(thread_cap_, reinterpret_cast<uint64_t>(thread_entry),
|
||||
reinterpret_cast<uint64_t>(e), reinterpret_cast<uint64_t>(arg1));
|
||||
check(ZThreadCreate(Z_INIT_PROC_SELF, &thread_cap_));
|
||||
check(ZThreadStart(thread_cap_, reinterpret_cast<uint64_t>(thread_entry),
|
||||
reinterpret_cast<uint64_t>(e),
|
||||
reinterpret_cast<uint64_t>(arg1)));
|
||||
}
|
||||
|
|
|
@ -1,11 +1,12 @@
|
|||
#include <mammoth/debug.h>
|
||||
#include <mammoth/process.h>
|
||||
|
||||
constexpr uint64_t prog2 = 0x00000020'00000000;
|
||||
#include <zcall.h>
|
||||
|
||||
int main() {
|
||||
dbgln("Testing");
|
||||
check(SpawnProcessFromElfRegion(prog2));
|
||||
uint64_t vaddr;
|
||||
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, Z_INIT_BOOT_VMMO, &vaddr));
|
||||
check(SpawnProcessFromElfRegion(vaddr));
|
||||
dbgln("Return");
|
||||
return 0;
|
||||
}
|
||||
|
|
|
@ -16,7 +16,6 @@
|
|||
#define ZC_PROC_SPAWN_THREAD 0x101
|
||||
|
||||
#define Z_INIT_PROC_SELF 0x1
|
||||
#define Z_INIT_AS_SELF 0x2
|
||||
|
||||
// Thread Calls.
|
||||
#define Z_THREAD_CREATE 0x10
|
||||
|
@ -27,15 +26,19 @@
|
|||
#define Z_ADDRESS_SPACE_MAP 0x21
|
||||
#define Z_ADDRESS_SPACE_UNMAP 0x22
|
||||
|
||||
#define Z_INIT_VMAS_SELF 0x20
|
||||
|
||||
#define Z_MEMORY_OBJECT_CREATE 0x30
|
||||
|
||||
#define Z_INIT_BOOT_VMMO 0x31
|
||||
|
||||
// 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_as_cap);
|
||||
uint64_t* new_vmas_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,
|
||||
|
@ -49,8 +52,8 @@ void ZProcessExit(uint64_t code);
|
|||
|
||||
void ZThreadExit();
|
||||
|
||||
[[nodiscard]] uint64_t ZAddressSpaceMap(uint64_t as_cap, uint64_t offset,
|
||||
uint64_t mem_cap, uint64_t* vaddr);
|
||||
[[nodiscard]] uint64_t ZMemoryObjectCreate(uint64_t size, uint64_t* mem_cap);
|
||||
[[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]] uint64_t ZDebug(const char* message);
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
#include "boot/boot_info.h"
|
||||
#include "debug/debug.h"
|
||||
#include "include/zcall.h"
|
||||
#include "lib/ref_ptr.h"
|
||||
#include "memory/paging_util.h"
|
||||
#include "object/process.h"
|
||||
|
@ -107,7 +108,6 @@ const limine_file& GetInitProgram(const char* path) {
|
|||
void LoadInitProgram() {
|
||||
DumpModules();
|
||||
const limine_file& init_prog = GetInitProgram("/sys/test");
|
||||
const limine_file& prog2 = GetInitProgram("/sys/test2");
|
||||
|
||||
RefPtr<Process> proc = Process::Create();
|
||||
gProcMan->InsertProcess(proc);
|
||||
|
@ -115,9 +115,11 @@ void LoadInitProgram() {
|
|||
uint64_t entry = LoadElfProgram(
|
||||
*proc, reinterpret_cast<uint64_t>(init_prog.address), init_prog.size);
|
||||
|
||||
CopyIntoNonResidentProcess(reinterpret_cast<uint64_t>(prog2.address),
|
||||
prog2.size, *proc,
|
||||
proc->vmas()->GetNextMemMapAddr(prog2.size));
|
||||
const limine_file& prog2 = GetInitProgram("/sys/test2");
|
||||
RefPtr<MemoryObject> prog2_vmmo = MakeRefCounted<MemoryObject>(prog2.size);
|
||||
prog2_vmmo->CopyBytesToObject(reinterpret_cast<uint64_t>(prog2.address),
|
||||
prog2.size);
|
||||
proc->AddCapability(Z_INIT_BOOT_VMMO, prog2_vmmo);
|
||||
|
||||
proc->CreateThread()->Start(entry, 0, 0);
|
||||
}
|
||||
|
|
|
@ -97,22 +97,6 @@ uint64_t CurrCr3() {
|
|||
return pml4_addr;
|
||||
}
|
||||
|
||||
void CopyPageIntoNonResidentProcess(uint64_t base, uint64_t size,
|
||||
Process& dest_proc, uint64_t dest_virt) {
|
||||
if (size > 0x1000) {
|
||||
panic("NR copy > 1 page");
|
||||
}
|
||||
if (dest_virt & 0xFFF) {
|
||||
panic("NR copy to non page aligned");
|
||||
}
|
||||
uint64_t phys = AllocatePageIfNecessary(dest_virt, dest_proc.vmas()->cr3());
|
||||
uint8_t* src = reinterpret_cast<uint8_t*>(base);
|
||||
uint8_t* dest =
|
||||
reinterpret_cast<uint8_t*>(phys + boot::GetHigherHalfDirectMap());
|
||||
for (uint64_t i = 0; i < size; i++) {
|
||||
dest[i] = src[i];
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
void InitializePml4(uint64_t pml4_physical_addr) {
|
||||
|
@ -167,10 +151,8 @@ void MapPage(uint64_t cr3, uint64_t vaddr, uint64_t paddr) {
|
|||
}
|
||||
}
|
||||
|
||||
uint64_t AllocatePageIfNecessary(uint64_t addr, uint64_t cr3) {
|
||||
if (cr3 == 0) {
|
||||
cr3 = CurrCr3();
|
||||
}
|
||||
uint64_t AllocatePageIfNecessary(uint64_t addr) {
|
||||
uint64_t cr3 = CurrCr3();
|
||||
uint64_t phys = PagePhysIfResident(cr3, addr);
|
||||
if (phys) {
|
||||
return phys;
|
||||
|
@ -191,16 +173,3 @@ void EnsureResident(uint64_t addr, uint64_t size) {
|
|||
addr += 0x1000;
|
||||
}
|
||||
}
|
||||
|
||||
void CopyIntoNonResidentProcess(uint64_t base, uint64_t size,
|
||||
Process& dest_proc, uint64_t dest_virt) {
|
||||
while (size > 0) {
|
||||
uint64_t to_copy = size > 0x1000 ? 0x1000 : size;
|
||||
|
||||
CopyPageIntoNonResidentProcess(base, to_copy, dest_proc, dest_virt);
|
||||
|
||||
size -= to_copy;
|
||||
base += 0x1000;
|
||||
dest_virt += 0x1000;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,8 +8,5 @@ void InitializePml4(uint64_t pml4_physical_addr);
|
|||
|
||||
void MapPage(uint64_t cr3, uint64_t vaddr, uint64_t paddr);
|
||||
|
||||
uint64_t AllocatePageIfNecessary(uint64_t addr, uint64_t cr3 = 0);
|
||||
uint64_t AllocatePageIfNecessary(uint64_t addr);
|
||||
void EnsureResident(uint64_t addr, uint64_t size);
|
||||
|
||||
void CopyIntoNonResidentProcess(uint64_t base, uint64_t size,
|
||||
Process& dest_proc, uint64_t dest_virt);
|
||||
|
|
|
@ -26,12 +26,12 @@ RefPtr<Process> Process::Create() {
|
|||
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_AS_SELF, ZC_WRITE));
|
||||
Z_INIT_VMAS_SELF, ZC_WRITE));
|
||||
return proc;
|
||||
}
|
||||
|
||||
Process::Process()
|
||||
: id_(gNextId++), vmm_(MakeRefCounted<AddressSpace>()), state_(RUNNING) {}
|
||||
: id_(gNextId++), vmas_(MakeRefCounted<AddressSpace>()), state_(RUNNING) {}
|
||||
|
||||
RefPtr<Thread> Process::CreateThread() {
|
||||
RefPtr<Thread> thread = MakeRefCounted<Thread>(*this, next_thread_id_++);
|
||||
|
@ -87,15 +87,20 @@ uint64_t Process::AddCapability(const RefPtr<Process>& p) {
|
|||
ZC_WRITE | ZC_PROC_SPAWN_THREAD));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<AddressSpace>& as) {
|
||||
uint64_t Process::AddCapability(const RefPtr<AddressSpace>& vmas) {
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(
|
||||
new Capability(as, Capability::ADDRESS_SPACE, cap_id, ZC_WRITE));
|
||||
new Capability(vmas, Capability::ADDRESS_SPACE, cap_id, ZC_WRITE));
|
||||
return cap_id;
|
||||
}
|
||||
uint64_t Process::AddCapability(const RefPtr<MemoryObject>& mo) {
|
||||
uint64_t Process::AddCapability(const RefPtr<MemoryObject>& vmmo) {
|
||||
uint64_t cap_id = next_cap_id_++;
|
||||
caps_.PushBack(
|
||||
new Capability(mo, Capability::MEMORY_OBJECT, cap_id, ZC_WRITE));
|
||||
new Capability(vmmo, Capability::MEMORY_OBJECT, cap_id, ZC_WRITE));
|
||||
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));
|
||||
}
|
||||
|
|
|
@ -23,7 +23,7 @@ class Process : public KernelObject {
|
|||
static RefPtr<Process> Create();
|
||||
|
||||
uint64_t id() const { return id_; }
|
||||
RefPtr<AddressSpace> vmas() { return vmm_; }
|
||||
RefPtr<AddressSpace> vmas() { return vmas_; }
|
||||
|
||||
RefPtr<Thread> CreateThread();
|
||||
RefPtr<Thread> GetThread(uint64_t tid);
|
||||
|
@ -31,8 +31,10 @@ class Process : public KernelObject {
|
|||
SharedPtr<Capability> GetCapability(uint64_t cid);
|
||||
uint64_t AddCapability(const RefPtr<Thread>& t);
|
||||
uint64_t AddCapability(const RefPtr<Process>& p);
|
||||
uint64_t AddCapability(const RefPtr<AddressSpace>& as);
|
||||
uint64_t AddCapability(const RefPtr<MemoryObject>& mo);
|
||||
uint64_t AddCapability(const RefPtr<AddressSpace>& vmas);
|
||||
uint64_t AddCapability(const RefPtr<MemoryObject>& vmmo);
|
||||
|
||||
void AddCapability(uint64_t cap_id, const RefPtr<MemoryObject>& vmmo);
|
||||
// Checks the state of all child threads and transitions to
|
||||
// finished if all have finished.
|
||||
void CheckState();
|
||||
|
@ -42,9 +44,9 @@ class Process : public KernelObject {
|
|||
private:
|
||||
friend class MakeRefCountedFriend<Process>;
|
||||
Process();
|
||||
Process(uint64_t id) : id_(id), vmm_(AddressSpace::ForRoot()) {}
|
||||
Process(uint64_t id) : id_(id), vmas_(AddressSpace::ForRoot()) {}
|
||||
uint64_t id_;
|
||||
RefPtr<AddressSpace> vmm_;
|
||||
RefPtr<AddressSpace> vmas_;
|
||||
State state_;
|
||||
|
||||
uint64_t next_thread_id_ = 0;
|
||||
|
|
|
@ -72,7 +72,7 @@ uint64_t ProcessSpawn(ZProcessSpawnReq* req, ZProcessSpawnResp* resp) {
|
|||
gProcMan->InsertProcess(proc);
|
||||
|
||||
resp->proc_cap = curr_proc.AddCapability(proc);
|
||||
resp->as_cap = curr_proc.AddCapability(proc->vmas());
|
||||
resp->vmas_cap = curr_proc.AddCapability(proc->vmas());
|
||||
|
||||
return Z_OK;
|
||||
}
|
||||
|
@ -120,33 +120,34 @@ uint64_t ThreadStart(ZThreadStartReq* req) {
|
|||
|
||||
uint64_t AddressSpaceMap(ZAddressSpaceMapReq* req, ZAddressSpaceMapResp* resp) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
auto as_cap = curr_proc.GetCapability(req->as_cap);
|
||||
auto mem_cap = curr_proc.GetCapability(req->mem_cap);
|
||||
if (as_cap.empty() || mem_cap.empty()) {
|
||||
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 (!as_cap->CheckType(Capability::ADDRESS_SPACE) ||
|
||||
!mem_cap->CheckType(Capability::MEMORY_OBJECT)) {
|
||||
if (!vmas_cap->CheckType(Capability::ADDRESS_SPACE) ||
|
||||
!vmmo_cap->CheckType(Capability::MEMORY_OBJECT)) {
|
||||
return ZE_INVALID;
|
||||
}
|
||||
if (!as_cap->HasPermissions(ZC_WRITE) || !mem_cap->HasPermissions(ZC_WRITE)) {
|
||||
if (!vmas_cap->HasPermissions(ZC_WRITE) ||
|
||||
!vmmo_cap->HasPermissions(ZC_WRITE)) {
|
||||
return ZE_DENIED;
|
||||
}
|
||||
auto as = as_cap->obj<AddressSpace>();
|
||||
auto mo = mem_cap->obj<MemoryObject>();
|
||||
auto vmas = vmas_cap->obj<AddressSpace>();
|
||||
auto vmmo = vmmo_cap->obj<MemoryObject>();
|
||||
// FIXME: Validation necessary.
|
||||
if (req->offset != 0) {
|
||||
as->MapInMemoryObject(req->offset, mo);
|
||||
resp->vaddr = req->offset;
|
||||
if (req->vmas_offset != 0) {
|
||||
vmas->MapInMemoryObject(req->vmas_offset, vmmo);
|
||||
resp->vaddr = req->vmas_offset;
|
||||
} else {
|
||||
resp->vaddr = as->MapInMemoryObject(mo);
|
||||
resp->vaddr = vmas->MapInMemoryObject(vmmo);
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t MemoryObjectCreate(ZMemoryObjectCreateReq* req,
|
||||
ZMemoryObjectCreateResp* resp) {
|
||||
auto& curr_proc = gScheduler->CurrentProcess();
|
||||
resp->mem_cap =
|
||||
resp->vmmo_cap =
|
||||
curr_proc.AddCapability(MakeRefCounted<MemoryObject>(req->size));
|
||||
return Z_OK;
|
||||
}
|
||||
|
|
|
@ -30,14 +30,14 @@ void ZProcessExit(uint64_t code) {
|
|||
}
|
||||
|
||||
uint64_t ZProcessSpawn(uint64_t proc_cap, uint64_t* new_proc_cap,
|
||||
uint64_t* new_as_cap) {
|
||||
uint64_t* new_vmas_cap) {
|
||||
ZProcessSpawnReq req{
|
||||
.proc_cap = proc_cap,
|
||||
};
|
||||
ZProcessSpawnResp resp;
|
||||
uint64_t ret = SysCall2(Z_PROCESS_SPAWN, &req, &resp);
|
||||
*new_proc_cap = resp.proc_cap;
|
||||
*new_as_cap = resp.as_cap;
|
||||
*new_vmas_cap = resp.vmas_cap;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -64,25 +64,25 @@ uint64_t ZThreadStart(uint64_t thread_cap, uint64_t entry, uint64_t arg1,
|
|||
|
||||
void ZThreadExit() { SysCall0(Z_THREAD_EXIT); }
|
||||
|
||||
uint64_t ZAddressSpaceMap(uint64_t as_cap, uint64_t offset, uint64_t mem_cap,
|
||||
uint64_t* vaddr) {
|
||||
uint64_t ZAddressSpaceMap(uint64_t vmas_cap, uint64_t vmas_offset,
|
||||
uint64_t vmmo_cap, uint64_t* vaddr) {
|
||||
ZAddressSpaceMapReq req{
|
||||
.as_cap = as_cap,
|
||||
.offset = offset,
|
||||
.mem_cap = mem_cap,
|
||||
.vmas_cap = vmas_cap,
|
||||
.vmas_offset = vmas_offset,
|
||||
.vmmo_cap = vmmo_cap,
|
||||
};
|
||||
ZAddressSpaceMapResp resp;
|
||||
uint64_t ret = SysCall2(Z_ADDRESS_SPACE_MAP, &req, &resp);
|
||||
*vaddr = resp.vaddr;
|
||||
return ret;
|
||||
}
|
||||
uint64_t ZMemoryObjectCreate(uint64_t size, uint64_t* mem_cap) {
|
||||
uint64_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);
|
||||
*mem_cap = resp.mem_cap;
|
||||
*vmmo_cap = resp.vmmo_cap;
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
|
@ -8,7 +8,7 @@ struct ZProcessSpawnReq {
|
|||
|
||||
struct ZProcessSpawnResp {
|
||||
uint64_t proc_cap;
|
||||
uint64_t as_cap;
|
||||
uint64_t vmas_cap;
|
||||
};
|
||||
|
||||
struct ZThreadCreateReq {
|
||||
|
@ -27,9 +27,9 @@ struct ZThreadStartReq {
|
|||
};
|
||||
|
||||
struct ZAddressSpaceMapReq {
|
||||
uint64_t as_cap;
|
||||
uint64_t offset;
|
||||
uint64_t mem_cap;
|
||||
uint64_t vmas_cap;
|
||||
uint64_t vmas_offset;
|
||||
uint64_t vmmo_cap;
|
||||
};
|
||||
|
||||
struct ZAddressSpaceMapResp {
|
||||
|
@ -41,5 +41,5 @@ struct ZMemoryObjectCreateReq {
|
|||
};
|
||||
|
||||
struct ZMemoryObjectCreateResp {
|
||||
uint64_t mem_cap;
|
||||
uint64_t vmmo_cap;
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue