From 449f0a0248721cea6dccf82c091ae01261236c34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=97=B1=20PixelyIon?= Date: Sun, 18 Aug 2019 03:48:27 +0530 Subject: [PATCH] Addition of Remapping and Unmapping to Memory class --- app/src/main/cpp/switch/hw/memory.cpp | 27 +++++++++++++++++++-------- app/src/main/cpp/switch/hw/memory.h | 7 ++++++- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/app/src/main/cpp/switch/hw/memory.cpp b/app/src/main/cpp/switch/hw/memory.cpp index ba3e11b8..59d4948b 100644 --- a/app/src/main/cpp/switch/hw/memory.cpp +++ b/app/src/main/cpp/switch/hw/memory.cpp @@ -1,23 +1,34 @@ #include -#include -#include +#include #include "memory.h" #include "../constant.h" namespace lightSwitch::hw { - // TODO: Boundary checks Memory::Memory() { - // Map stack memory - // Memory::Map(constant::stack_addr, constant::stack_size, stack); // Map TLS memory Memory::Map(constant::tls_addr, constant::tls_size, tls); } void Memory::Map(uint64_t address, size_t size, Region region) { - region_map.insert(std::pair(region, {address, size})); void *ptr = mmap((void *) address, size, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANON | MAP_FIXED, 0, 0); - if (!ptr) - throw exception("An occurred while mapping region"); + if (ptr == MAP_FAILED) + throw exception("An occurred while mapping region at " + std::to_string(address)); + region_map.insert(std::pair(region, {address, size})); + } + + void Memory::Remap(Region region, size_t size) { + RegionData region_data = region_map.at(region); + void *ptr = mremap((void *) region_data.address, region_data.size, size, 0); + if (ptr == MAP_FAILED) + throw exception("An occurred while unmapping region: " + std::string(strerror(errno))); + region_map[region].size = size; + } + + void Memory::Unmap(Region region) { + RegionData region_data = region_map.at(region); + int err = munmap((void *) region_data.address, region_data.size); + if (err == -1) + throw exception("An occurred while unmapping region: " + std::string(strerror(errno))); } void Memory::Write(void *data, uint64_t offset, size_t size) { diff --git a/app/src/main/cpp/switch/hw/memory.h b/app/src/main/cpp/switch/hw/memory.h index 6500528c..ddbc0a6f 100644 --- a/app/src/main/cpp/switch/hw/memory.h +++ b/app/src/main/cpp/switch/hw/memory.h @@ -1,5 +1,6 @@ #pragma once +#include "../constant.h" #include #include @@ -7,7 +8,7 @@ namespace lightSwitch::hw { class Memory { public: enum Region { - stack, tls, text, rodata, data, bss + heap, tls, text, rodata, data, bss }; struct RegionData { uint64_t address; @@ -19,6 +20,10 @@ namespace lightSwitch::hw { void Map(uint64_t address, size_t size, Region region); + void Remap(Region region, size_t size); + + void Unmap(Region region); + void Write(void *data, uint64_t offset, size_t size); template