#include "VitualMemory.hpp" #include #include "utils.h" #include // #include #include #include #include VitualMemory::VitualMemory(void *addr, int size) { int fd; roundSize = PAGE_ROUNDUP(size); if ((fd = open("/dev/mem", O_RDWR | O_SYNC | O_CLOEXEC)) < 0) { throw std::runtime_error("Error: can't open /dev/mem, run using sudo"); } mem = mmap(0, roundSize, PROT_WRITE | PROT_READ, MAP_SHARED, fd, (uint32_t)addr); close(fd); #if _DEBUG printf("Map %p -> %p\n", (void *)addr, mem); #endif // _DEBUG if (mem == MAP_FAILED) { throw std::runtime_error("Error: can't map memory"); } } VitualMemory::~VitualMemory() { if (mem) { munmap(mem, roundSize); } }