From 6c13fdc8013137fd6700133dd0aab2d054b26e76 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Mon, 12 Jun 2023 23:24:05 -0700 Subject: [PATCH] Add a memory region type to mammoth --- lib/mammoth/CMakeLists.txt | 1 + lib/mammoth/include/mammoth/memory_region.h | 29 +++++++++++++++++ lib/mammoth/src/memory_region.cpp | 36 +++++++++++++++++++++ 3 files changed, 66 insertions(+) create mode 100644 lib/mammoth/include/mammoth/memory_region.h create mode 100644 lib/mammoth/src/memory_region.cpp diff --git a/lib/mammoth/CMakeLists.txt b/lib/mammoth/CMakeLists.txt index e177ddd..7908dce 100644 --- a/lib/mammoth/CMakeLists.txt +++ b/lib/mammoth/CMakeLists.txt @@ -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 ) diff --git a/lib/mammoth/include/mammoth/memory_region.h b/lib/mammoth/include/mammoth/memory_region.h new file mode 100644 index 0000000..78af053 --- /dev/null +++ b/lib/mammoth/include/mammoth/memory_region.h @@ -0,0 +1,29 @@ +#pragma once + +#include + +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; +}; diff --git a/lib/mammoth/src/memory_region.cpp b/lib/mammoth/src/memory_region.cpp new file mode 100644 index 0000000..8420297 --- /dev/null +++ b/lib/mammoth/src/memory_region.cpp @@ -0,0 +1,36 @@ +#include "mammoth/memory_region.h" + +#include + +#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); +}