2023-06-07 09:37:16 -07:00
|
|
|
#include "mammoth/process.h"
|
2023-06-07 00:04:53 -07:00
|
|
|
|
2023-06-21 18:28:54 -07:00
|
|
|
#include <glacier/status/error.h>
|
2023-06-07 00:04:53 -07:00
|
|
|
#include <zcall.h>
|
|
|
|
|
2023-06-07 09:37:16 -07:00
|
|
|
#include "mammoth/debug.h"
|
2023-06-21 21:26:24 -07:00
|
|
|
#include "mammoth/endpoint_server.h"
|
2023-06-17 00:17:43 -07:00
|
|
|
#include "mammoth/init.h"
|
2023-06-26 08:59:28 -07:00
|
|
|
#include "mammoth/port_client.h"
|
|
|
|
#include "mammoth/port_server.h"
|
2023-06-07 00:04:53 -07:00
|
|
|
|
2023-06-07 13:51:13 -07:00
|
|
|
#define MAM_PROC_DEBUG 0
|
|
|
|
|
2023-06-07 00:04:53 -07:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char ident[16];
|
|
|
|
uint16_t type;
|
|
|
|
uint16_t machine;
|
|
|
|
uint32_t version;
|
|
|
|
uint64_t entry;
|
|
|
|
uint64_t phoff;
|
|
|
|
uint64_t shoff;
|
|
|
|
uint32_t flags;
|
|
|
|
uint16_t ehsize;
|
|
|
|
uint16_t phentsize;
|
|
|
|
uint16_t phnum;
|
|
|
|
uint16_t shentsize;
|
|
|
|
uint16_t shnum;
|
|
|
|
uint16_t shstrndx;
|
|
|
|
} Elf64Header;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t name;
|
|
|
|
uint32_t type;
|
|
|
|
uint64_t flags;
|
|
|
|
uint64_t addr;
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t size;
|
|
|
|
uint32_t link;
|
|
|
|
uint32_t info;
|
|
|
|
uint64_t addralign;
|
|
|
|
uint64_t entsize;
|
|
|
|
} Elf64SectionHeader;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
uint32_t type;
|
|
|
|
uint32_t flags;
|
|
|
|
uint64_t offset;
|
|
|
|
uint64_t vaddr;
|
|
|
|
uint64_t paddr;
|
|
|
|
uint64_t filesz;
|
|
|
|
uint64_t memsz;
|
|
|
|
uint64_t align;
|
|
|
|
} Elf64ProgramHeader;
|
|
|
|
|
|
|
|
void memcpy(uint64_t base, uint64_t len, uint64_t dest) {
|
|
|
|
uint8_t* srcptr = reinterpret_cast<uint8_t*>(base);
|
|
|
|
uint8_t* destptr = reinterpret_cast<uint8_t*>(dest);
|
|
|
|
for (uint64_t i = 0; i < len; i++) {
|
|
|
|
destptr[i] = srcptr[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) {
|
|
|
|
Elf64Header* header = reinterpret_cast<Elf64Header*>(base);
|
|
|
|
Elf64ProgramHeader* programs =
|
|
|
|
reinterpret_cast<Elf64ProgramHeader*>(base + header->phoff);
|
|
|
|
for (uint64_t i = 0; i < header->phnum; i++) {
|
|
|
|
Elf64ProgramHeader& program = programs[i];
|
2023-06-07 13:51:13 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Create mem object");
|
2023-06-07 13:51:13 -07:00
|
|
|
#endif
|
2023-06-07 00:04:53 -07:00
|
|
|
uint64_t mem_cap;
|
2023-06-16 01:29:49 -07:00
|
|
|
uint64_t size = program.memsz;
|
2023-06-07 00:04:53 -07:00
|
|
|
check(ZMemoryObjectCreate(size, &mem_cap));
|
|
|
|
|
2023-06-07 13:51:13 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Map Local");
|
2023-06-07 13:51:13 -07:00
|
|
|
#endif
|
2023-06-07 00:04:53 -07:00
|
|
|
uint64_t vaddr;
|
2023-06-17 00:17:43 -07:00
|
|
|
check(ZAddressSpaceMap(gSelfVmasCap, 0, mem_cap, &vaddr));
|
2023-06-07 00:04:53 -07:00
|
|
|
|
2023-06-07 13:51:13 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Copy");
|
2023-06-07 13:51:13 -07:00
|
|
|
#endif
|
2023-06-07 00:04:53 -07:00
|
|
|
memcpy(base + program.offset, program.filesz, vaddr);
|
|
|
|
|
2023-06-07 13:51:13 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Map Foreign");
|
2023-06-07 13:51:13 -07:00
|
|
|
#endif
|
2023-06-07 00:04:53 -07:00
|
|
|
check(ZAddressSpaceMap(as_cap, program.vaddr, mem_cap, &vaddr));
|
|
|
|
}
|
|
|
|
return header->entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
2023-06-22 02:19:16 -07:00
|
|
|
glcr::ErrorCode SpawnProcessFromElfRegion(uint64_t program,
|
|
|
|
EndpointClient client) {
|
2023-06-17 01:30:47 -07:00
|
|
|
uint64_t proc_cap;
|
|
|
|
uint64_t as_cap;
|
|
|
|
uint64_t foreign_port_id;
|
|
|
|
uint64_t port_cap;
|
2023-06-21 21:26:24 -07:00
|
|
|
|
2023-06-17 01:30:47 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
|
|
|
dbgln("Port Create");
|
|
|
|
#endif
|
2023-06-26 08:59:28 -07:00
|
|
|
ASSIGN_OR_RETURN(PortServer server, PortServer::Create());
|
|
|
|
ASSIGN_OR_RETURN(PortClient pclient, server.CreateClient());
|
2023-06-17 01:30:47 -07:00
|
|
|
|
2023-06-07 13:51:13 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Spawn");
|
2023-06-07 13:51:13 -07:00
|
|
|
#endif
|
2023-06-26 08:59:28 -07:00
|
|
|
RET_ERR(ZProcessSpawn(gSelfProcCap, server.cap(), &proc_cap, &as_cap,
|
2023-06-22 02:19:16 -07:00
|
|
|
&foreign_port_id));
|
2023-06-07 00:04:53 -07:00
|
|
|
|
|
|
|
uint64_t entry_point = LoadElfProgram(program, as_cap);
|
2023-06-17 01:30:47 -07:00
|
|
|
|
2023-06-07 13:51:13 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Thread Create");
|
2023-06-07 13:51:13 -07:00
|
|
|
#endif
|
2023-06-07 00:04:53 -07:00
|
|
|
uint64_t thread_cap;
|
2023-06-22 02:19:16 -07:00
|
|
|
RET_ERR(ZThreadCreate(proc_cap, &thread_cap));
|
2023-06-07 00:04:53 -07:00
|
|
|
|
2023-06-26 08:59:28 -07:00
|
|
|
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_PROC, proc_cap));
|
|
|
|
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_SELF_VMAS, as_cap));
|
|
|
|
RET_ERR(pclient.WriteMessage<uint64_t>(Z_INIT_ENDPOINT, client.GetCap()));
|
2023-06-17 01:30:47 -07:00
|
|
|
|
2023-06-07 13:51:13 -07:00
|
|
|
#if MAM_PROC_DEBUG
|
2023-06-07 00:04:53 -07:00
|
|
|
dbgln("Thread start");
|
2023-06-07 13:51:13 -07:00
|
|
|
#endif
|
2023-06-22 02:19:16 -07:00
|
|
|
RET_ERR(ZThreadStart(thread_cap, entry_point, foreign_port_id, 0));
|
2023-06-07 08:24:10 -07:00
|
|
|
|
2023-06-22 02:19:16 -07:00
|
|
|
return glcr::OK;
|
2023-06-07 00:04:53 -07:00
|
|
|
}
|