Add a memory region type to mammoth

This commit is contained in:
Drew Galbraith 2023-06-12 23:24:05 -07:00
parent 6986f534f8
commit 6c13fdc801
3 changed files with 66 additions and 0 deletions

View File

@ -1,6 +1,7 @@
add_library(mammoth_lib STATIC
src/channel.cpp
src/debug.cpp
src/memory_region.cpp
src/process.cpp
src/thread.cpp
)

View File

@ -0,0 +1,29 @@
#pragma once
#include <stdint.h>
class MappedMemoryRegion {
public:
// FIXME: Introduce optional type to contain error or.
static MappedMemoryRegion DirectPhysical(uint64_t phys_addr, uint64_t size);
static MappedMemoryRegion ContiguousPhysical(uint64_t size);
static MappedMemoryRegion Default(uint64_t size);
MappedMemoryRegion() {}
// TODO: Disallow copy before doing any cleanup here.
~MappedMemoryRegion() {}
uint64_t paddr() { return paddr_; }
uint64_t vaddr() { return vaddr_; }
operator bool() { return vmmo_cap_ != 0; }
private:
MappedMemoryRegion(uint64_t vmmo_cap, uint64_t paddr, uint64_t vaddr,
uint64_t size)
: vmmo_cap_(vmmo_cap), paddr_(paddr), vaddr_(vaddr), size_(size) {}
uint64_t vmmo_cap_ = 0;
uint64_t paddr_ = 0;
uint64_t vaddr_ = 0;
uint64_t size_ = 0;
};

View File

@ -0,0 +1,36 @@
#include "mammoth/memory_region.h"
#include <zcall.h>
#include "mammoth/debug.h"
MappedMemoryRegion MappedMemoryRegion::DirectPhysical(uint64_t paddr,
uint64_t size) {
uint64_t vmmo_cap;
check(ZMemoryObjectCreatePhysical(paddr, size, &vmmo_cap));
uint64_t vaddr;
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, vmmo_cap, &vaddr));
return MappedMemoryRegion(vmmo_cap, paddr, vaddr, size);
}
MappedMemoryRegion MappedMemoryRegion::ContiguousPhysical(uint64_t size) {
uint64_t vmmo_cap, paddr;
check(ZMemoryObjectCreateContiguous(size, &vmmo_cap, &paddr));
uint64_t vaddr;
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, vmmo_cap, &vaddr));
return MappedMemoryRegion(vmmo_cap, paddr, vaddr, size);
}
MappedMemoryRegion MappedMemoryRegion::Default(uint64_t size) {
uint64_t vmmo_cap;
check(ZMemoryObjectCreate(size, &vmmo_cap));
uint64_t vaddr;
check(ZAddressSpaceMap(Z_INIT_VMAS_SELF, 0, vmmo_cap, &vaddr));
return MappedMemoryRegion(vmmo_cap, 0, vaddr, size);
}