From d44be91099f92407a63a8feb54f0615497c5e1eb Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 23 Nov 2023 18:49:01 -0800 Subject: [PATCH] [Zion] Add an argument to memory align a mapping. --- lib/libc/src/malloc.cpp | 6 +++--- lib/mammoth/proc/process.cpp | 6 +++--- lib/mammoth/util/debug.cpp | 4 +++- lib/mammoth/util/debug.h | 2 +- lib/mammoth/util/memory_region.cpp | 6 +++--- sys/yellowstone/hw/pcie.cpp | 2 +- zion/include/zcall.h | 6 +++--- zion/object/address_space.cpp | 12 +++++++++--- zion/object/address_space.h | 4 ++-- zion/syscall/address_space.cpp | 2 +- zion/syscall/debug.cpp | 2 +- 11 files changed, 30 insertions(+), 22 deletions(-) diff --git a/lib/libc/src/malloc.cpp b/lib/libc/src/malloc.cpp index c8dff91..5165ce5 100644 --- a/lib/libc/src/malloc.cpp +++ b/lib/libc/src/malloc.cpp @@ -13,9 +13,9 @@ class NaiveAllocator { uint64_t vmmo_cap; uint64_t err = ZMemoryObjectCreate(kSize, &vmmo_cap); if (err != 0) { - ZProcessExit(err); + (void)ZProcessExit(err); } - err = ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &next_addr_); + err = ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, 0, &next_addr_); max_addr_ = next_addr_ + kSize; } @@ -23,7 +23,7 @@ class NaiveAllocator { uint64_t addr = next_addr_; next_addr_ += size; if (next_addr_ >= max_addr_) { - ZProcessExit(0xBEEF); + (void)ZProcessExit(0xBEEF); return 0; } return reinterpret_cast(addr); diff --git a/lib/mammoth/proc/process.cpp b/lib/mammoth/proc/process.cpp index c4622d0..5c4596d 100644 --- a/lib/mammoth/proc/process.cpp +++ b/lib/mammoth/proc/process.cpp @@ -81,7 +81,7 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) { dbgln("Map Local"); #endif uint64_t vaddr; - check(ZAddressSpaceMap(gSelfVmasCap, 0, mem_cap, &vaddr)); + check(ZAddressSpaceMap(gSelfVmasCap, 0, mem_cap, 0, &vaddr)); uint8_t* offset = reinterpret_cast(vaddr); for (uint64_t j = 0; j < size; j++) { offset[j] = 0; @@ -95,8 +95,8 @@ uint64_t LoadElfProgram(uint64_t base, uint64_t as_cap) { #if MAM_PROC_DEBUG dbgln("Map Foreign"); #endif - check( - ZAddressSpaceMap(as_cap, program.vaddr - page_offset, mem_cap, &vaddr)); + check(ZAddressSpaceMap(as_cap, program.vaddr - page_offset, mem_cap, 0, + &vaddr)); } return header->entry; } diff --git a/lib/mammoth/util/debug.cpp b/lib/mammoth/util/debug.cpp index a9016ee..01145d2 100644 --- a/lib/mammoth/util/debug.cpp +++ b/lib/mammoth/util/debug.cpp @@ -3,7 +3,9 @@ #include #include -void dbgln(const glcr::String& string) { (void)ZDebug(string.cstr()); } +void dbgln(glcr::StringView string) { + (void)ZDebug(string.data(), string.size()); +} void check(uint64_t code) { switch (code) { diff --git a/lib/mammoth/util/debug.h b/lib/mammoth/util/debug.h index f06bf99..4aeab01 100644 --- a/lib/mammoth/util/debug.h +++ b/lib/mammoth/util/debug.h @@ -6,7 +6,7 @@ #include // TODO: Take StringView here instead. -void dbgln(const glcr::String& string); +void dbgln(glcr::StringView string); template void dbgln(const glcr::StringView& fmt, Args... args) { diff --git a/lib/mammoth/util/memory_region.cpp b/lib/mammoth/util/memory_region.cpp index 7675227..a4989f2 100644 --- a/lib/mammoth/util/memory_region.cpp +++ b/lib/mammoth/util/memory_region.cpp @@ -36,7 +36,7 @@ OwnedMemoryRegion::~OwnedMemoryRegion() { OwnedMemoryRegion OwnedMemoryRegion::FromCapability(z_cap_t vmmo_cap) { uint64_t vaddr; - check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr)); + check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, 0, &vaddr)); uint64_t size; check(ZMemoryObjectInspect(vmmo_cap, &size)); @@ -50,7 +50,7 @@ OwnedMemoryRegion OwnedMemoryRegion::ContiguousPhysical(uint64_t size, check(ZMemoryObjectCreateContiguous(size, &vmmo_cap, paddr)); uint64_t vaddr; - check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr)); + check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, 0, &vaddr)); return OwnedMemoryRegion(vmmo_cap, vaddr, size); } @@ -61,7 +61,7 @@ OwnedMemoryRegion OwnedMemoryRegion::DirectPhysical(uint64_t paddr, check(ZMemoryObjectCreatePhysical(paddr, size, &vmmo_cap)); uint64_t vaddr; - check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, &vaddr)); + check(ZAddressSpaceMap(gSelfVmasCap, 0, vmmo_cap, 0, &vaddr)); return OwnedMemoryRegion(vmmo_cap, vaddr, size); } diff --git a/sys/yellowstone/hw/pcie.cpp b/sys/yellowstone/hw/pcie.cpp index 1a89233..08a2e7b 100644 --- a/sys/yellowstone/hw/pcie.cpp +++ b/sys/yellowstone/hw/pcie.cpp @@ -18,7 +18,7 @@ PciDeviceHeader* PciHeader(uint64_t base, uint64_t bus, uint64_t dev, PciReader::PciReader() { uint64_t vaddr; - check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootPciVmmoCap, &vaddr)); + check(ZAddressSpaceMap(gSelfVmasCap, 0, gBootPciVmmoCap, 0, &vaddr)); PciDump(vaddr); diff --git a/zion/include/zcall.h b/zion/include/zcall.h index 7823650..2cc1a4c 100644 --- a/zion/include/zcall.h +++ b/zion/include/zcall.h @@ -17,8 +17,8 @@ SYS4(ThreadStart, z_cap_t, thread_cap, uint64_t, entry, uint64_t, arg1, SYS0(ThreadExit); SYS1(ThreadWait, z_cap_t, thread_cap); -SYS4(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t, - vmmo_cap, uint64_t*, vaddr); +SYS5(AddressSpaceMap, z_cap_t, vmas_cap, uint64_t, vmas_offset, z_cap_t, + vmmo_cap, uint64_t, align, uint64_t*, vaddr); SYS3(AddressSpaceUnmap, z_cap_t, vmas_cap, uint64_t, lower_addr, uint64_t, upper_addr); @@ -68,4 +68,4 @@ SYS1(SemaphoreCreate, z_cap_t*, semaphore_cap); SYS1(SemaphoreWait, z_cap_t, semaphore_cap); SYS1(SemaphoreSignal, z_cap_t, semaphore_cap); -SYS1(Debug, const char*, message); +SYS2(Debug, const char*, message, uint64_t, size); diff --git a/zion/object/address_space.cpp b/zion/object/address_space.cpp index b41e4c7..398bfc2 100644 --- a/zion/object/address_space.cpp +++ b/zion/object/address_space.cpp @@ -26,11 +26,17 @@ void AddressSpace::FreeUserStack(uint64_t rsp) { return user_stacks_.FreeUserStack(rsp); } -uint64_t AddressSpace::GetNextMemMapAddr(uint64_t size) { +uint64_t AddressSpace::GetNextMemMapAddr(uint64_t size, uint64_t align) { if (size == 0) { panic("Zero size memmap"); } size = ((size - 1) & ~0xFFF) + 0x1000; + // FIXME: We need to validate that align is a power of 2; + if (align > 0) { + while ((next_memmap_addr_ & (align - 1)) != 0) { + next_memmap_addr_ += kPageSize; + } + } uint64_t addr = next_memmap_addr_; next_memmap_addr_ += size; if (next_memmap_addr_ >= 0x30'00000000) { @@ -45,8 +51,8 @@ glcr::ErrorCode AddressSpace::MapInMemoryObject( } glcr::ErrorOr AddressSpace::MapInMemoryObject( - const glcr::RefPtr& mem_obj) { - uint64_t vaddr = GetNextMemMapAddr(mem_obj->size()); + const glcr::RefPtr& mem_obj, uint64_t align) { + uint64_t vaddr = GetNextMemMapAddr(mem_obj->size(), align); RET_ERR(mapping_tree_.AddInMemoryObject(vaddr, mem_obj)); return vaddr; } diff --git a/zion/object/address_space.h b/zion/object/address_space.h index 56d58e7..d6aebd7 100644 --- a/zion/object/address_space.h +++ b/zion/object/address_space.h @@ -67,7 +67,7 @@ class AddressSpace : public KernelObject { // User Mappings. uint64_t AllocateUserStack(); void FreeUserStack(uint64_t); - uint64_t GetNextMemMapAddr(uint64_t size); + uint64_t GetNextMemMapAddr(uint64_t size, uint64_t align); // Maps in a memory object at a specific address. // Note this is unsafe for now as it may clobber other mappings. @@ -75,7 +75,7 @@ class AddressSpace : public KernelObject { uint64_t vaddr, const glcr::RefPtr& mem_obj); [[nodiscard]] glcr::ErrorOr MapInMemoryObject( - const glcr::RefPtr& mem_obj); + const glcr::RefPtr& mem_obj, uint64_t align); [[nodiscard]] glcr::ErrorCode FreeAddressRange(uint64_t vaddr_base, uint64_t vaddr_limit) { diff --git a/zion/syscall/address_space.cpp b/zion/syscall/address_space.cpp index 24fbec1..b40124b 100644 --- a/zion/syscall/address_space.cpp +++ b/zion/syscall/address_space.cpp @@ -18,7 +18,7 @@ z_err_t AddressSpaceMap(ZAddressSpaceMapReq* req) { RET_ERR(vmas->MapInMemoryObject(req->vmas_offset, vmmo)); *req->vaddr = req->vmas_offset; } else { - ASSIGN_OR_RETURN(*req->vaddr, vmas->MapInMemoryObject(vmmo)); + ASSIGN_OR_RETURN(*req->vaddr, vmas->MapInMemoryObject(vmmo, req->align)); } return glcr::OK; } diff --git a/zion/syscall/debug.cpp b/zion/syscall/debug.cpp index a4c728e..3588b64 100644 --- a/zion/syscall/debug.cpp +++ b/zion/syscall/debug.cpp @@ -5,6 +5,6 @@ #include "debug/debug.h" z_err_t Debug(ZDebugReq* req) { - dbgln_large("[Debug] {}", req->message); + dbgln_large("[Debug] {}", glcr::StringView(req->message, req->size)); return glcr::OK; }