[Mammoth] Move remaining classes to a shared folder.

This commit is contained in:
Drew Galbraith 2023-11-22 14:39:27 -08:00
parent c42fb858ba
commit 5f8d577948
39 changed files with 55 additions and 56 deletions

View file

@ -0,0 +1,34 @@
#include "util/debug.h"
#include <glacier/status/error.h>
#include <zcall.h>
void dbgln(const glcr::String& string) { (void)ZDebug(string.cstr()); }
void check(uint64_t code) {
switch (code) {
case glcr::OK:
return;
case glcr::UNIMPLEMENTED:
dbgln("crash: UNIMPLEMENTED");
break;
case glcr::CAP_NOT_FOUND:
dbgln("crash: missing capability");
break;
case glcr::CAP_WRONG_TYPE:
dbgln("crash: capability of the wrong type");
break;
case glcr::CAP_PERMISSION_DENIED:
dbgln("crash: capability permissions error");
break;
default:
dbgln("Unhandled code");
break;
}
(void)ZProcessExit(code);
}
void crash(const char* str, uint64_t code) {
dbgln(str);
(void)ZProcessExit(code);
}

20
lib/mammoth/util/debug.h Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <glacier/string/str_format.h>
#include <glacier/string/string_view.h>
#include <stdint.h>
#include <ztypes.h>
// TODO: Take StringView here instead.
void dbgln(const glcr::String& string);
template <typename... Args>
void dbgln(const glcr::StringView& fmt, Args... args) {
dbgln(StrFormat(fmt, args...));
}
// Checks that the code is ok.
// if not exits the process.
void check(uint64_t);
void crash(const char*, z_err_t);

53
lib/mammoth/util/init.cpp Normal file
View file

@ -0,0 +1,53 @@
#include "util/init.h"
#include <glacier/status/error.h>
#include <ztypes.h>
#include "ipc/port_server.h"
#include "util/debug.h"
uint64_t gSelfProcCap = 0;
uint64_t gSelfVmasCap = 0;
uint64_t gInitEndpointCap = 0;
uint64_t gBootDenaliVmmoCap = 0;
uint64_t gBootVictoriaFallsVmmoCap = 0;
uint64_t gBootPciVmmoCap = 0;
uint64_t gBootFramebufferVmmoCap = 0;
z_err_t ParseInitPort(uint64_t init_port_cap) {
PortServer port = PortServer::AdoptCap(init_port_cap);
z_err_t ret;
uint64_t init_sig, init_cap;
while ((ret = port.PollForIntCap(&init_sig, &init_cap)) != glcr::EMPTY) {
RET_ERR(ret);
switch (init_sig) {
case Z_INIT_SELF_PROC:
gSelfProcCap = init_cap;
break;
case Z_INIT_SELF_VMAS:
gSelfVmasCap = init_cap;
break;
case Z_INIT_ENDPOINT:
gInitEndpointCap = init_cap;
break;
case Z_BOOT_DENALI_VMMO:
gBootDenaliVmmoCap = init_cap;
break;
case Z_BOOT_VICTORIA_FALLS_VMMO:
gBootVictoriaFallsVmmoCap = init_cap;
break;
case Z_BOOT_PCI_VMMO:
gBootPciVmmoCap = init_cap;
break;
case Z_BOOT_FRAMEBUFFER_INFO_VMMO:
gBootFramebufferVmmoCap = init_cap;
break;
default:
dbgln("Unexpected init type {x}, continuing.", init_sig);
}
}
return glcr::OK;
}

7
lib/mammoth/util/init.h Normal file
View file

@ -0,0 +1,7 @@
#pragma once
#include <stdint.h>
#include <zglobal.h>
#include <ztypes.h>
z_err_t ParseInitPort(uint64_t init_port_cap);

View file

@ -0,0 +1,70 @@
#include "util/memory_region.h"
#include <zcall.h>
#include "util/debug.h"
#include "util/init.h"
OwnedMemoryRegion::OwnedMemoryRegion(OwnedMemoryRegion&& other)
: OwnedMemoryRegion(other.vmmo_cap_, other.vaddr_, other.size_) {
other.vmmo_cap_ = 0;
other.vaddr_ = 0;
other.size_ = 0;
}
OwnedMemoryRegion& OwnedMemoryRegion::operator=(OwnedMemoryRegion&& other) {
if (vmmo_cap_) {
check(ZCapRelease(vmmo_cap_));
}
vmmo_cap_ = other.vmmo_cap_;
vaddr_ = other.vaddr_;
size_ = other.size_;
other.vmmo_cap_ = 0;
other.vaddr_ = 0;
other.size_ = 0;
return *this;
}
OwnedMemoryRegion::~OwnedMemoryRegion() {
if (vmmo_cap_ != 0) {
check(ZAddressSpaceUnmap(gSelfVmasCap, vaddr_, vaddr_ + size_));
check(ZCapRelease(vmmo_cap_));
}
}
OwnedMemoryRegion OwnedMemoryRegion::FromCapability(z_cap_t vmmo_cap) {
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
uint64_t size;
check(ZMemoryObjectInspect(vmmo_cap, &size));
// FIXME: get the size here.
return OwnedMemoryRegion(vmmo_cap, vaddr, size);
}
OwnedMemoryRegion OwnedMemoryRegion::ContiguousPhysical(uint64_t size,
uint64_t* paddr) {
uint64_t vmmo_cap;
check(ZMemoryObjectCreateContiguous(size, &vmmo_cap, paddr));
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
return OwnedMemoryRegion(vmmo_cap, vaddr, size);
}
OwnedMemoryRegion OwnedMemoryRegion::DirectPhysical(uint64_t paddr,
uint64_t size) {
uint64_t vmmo_cap;
check(ZMemoryObjectCreatePhysical(paddr, size, &vmmo_cap));
uint64_t vaddr;
check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr));
return OwnedMemoryRegion(vmmo_cap, vaddr, size);
}
z_cap_t OwnedMemoryRegion::DuplicateCap() {
z_cap_t cap;
check(ZCapDuplicate(vmmo_cap_, kZionPerm_All, &cap));
return cap;
}

View file

@ -0,0 +1,43 @@
#pragma once
#include <stdint.h>
#include <ztypes.h>
/*
* Memory Region class that unmaps its memory and releases its
* capability when it goes out of scope.
*/
class OwnedMemoryRegion {
public:
OwnedMemoryRegion() = default;
OwnedMemoryRegion(const OwnedMemoryRegion&) = delete;
OwnedMemoryRegion& operator=(const OwnedMemoryRegion&) = delete;
OwnedMemoryRegion(OwnedMemoryRegion&&);
OwnedMemoryRegion& operator=(OwnedMemoryRegion&&);
~OwnedMemoryRegion();
static OwnedMemoryRegion FromCapability(z_cap_t vmmo_cap);
// TODO: Consider making this its own class.
static OwnedMemoryRegion ContiguousPhysical(uint64_t size, uint64_t* paddr);
static OwnedMemoryRegion DirectPhysical(uint64_t paddr, uint64_t size);
uint64_t vaddr() { return vaddr_; }
uint64_t size() { return size_; }
z_cap_t cap() { return vmmo_cap_; }
z_cap_t DuplicateCap();
bool empty() { return vmmo_cap_ != 0; }
explicit operator bool() { return vmmo_cap_ != 0; }
private:
OwnedMemoryRegion(uint64_t vmmo_cap, uint64_t vaddr, uint64_t size)
: vmmo_cap_(vmmo_cap), vaddr_(vaddr), size_(size) {}
uint64_t vmmo_cap_ = 0;
uint64_t vaddr_ = 0;
// TODO: We may want to differentiate between VMMO size and mapped size?
uint64_t size_ = 0;
};

9
lib/mammoth/util/new.cpp Normal file
View file

@ -0,0 +1,9 @@
#include <stdint.h>
#include <stdlib.h>
[[nodiscard]] void* operator new(uint64_t size) { return malloc(size); }
[[nodiscard]] void* operator new[](uint64_t size) { return malloc(size); }
void operator delete(void*, uint64_t) {}
void operator delete[](void*) {}
void operator delete[](void*, uint64_t) {}