#include "vmas.h" #include "debug.h" #include #include #include VMAddrSpace::VMAddrSpace() { stacksize = DEFAULT_STACKSIZE; codesize = DEFAULT_CODESIZE; datasize = DEFAULT_DATASIZE; return; } VMAddrSpace::VMAddrSpace(uint32_t ss, uint32_t cs, uint32_t ds) { stacksize = ss; codesize = cs; datasize = ds; return; } bool VMAddrSpace::allocate(void) { DBG_INFO(("Allocating sections...\n")); if (code == NULL) { code = (uint8_t *)malloc(codesize); } if (data == NULL) { data = (uint8_t *)malloc(datasize); } if (stack == NULL) { stack = (uint8_t *)malloc(stacksize); } if (code == NULL) { DBG_ERROR(("Couldn't allocate code section.\n")); return false; } data = (uint8_t *)malloc(datasize); if (data == NULL) { DBG_ERROR(("Couldn't allocate data section.\n")); return false; } stack = (uint8_t *)malloc(stacksize); if (stack == NULL) { DBG_ERROR(("Couldn't allocate stack section.\n")); return false; } DBG_SUCC(("Done!\n")); return true; } bool VMAddrSpace::insCode(uint8_t *buf, uint8_t size) { if (code) { DBG_INFO(("Copying buffer into code section.\n")); memcpy(code, buf, size); } else { DBG_ERROR(("Couldn't write into code section.\n")); return false; } return true; } bool VMAddrSpace::insStack(uint8_t *buf, uint8_t size) { if (stack) { DBG_INFO(("Copying buffer into stack section.\n")); memcpy(stack, buf, size); } else { DBG_ERROR(("Couldn't write into stack section.\n")); return false; } return true; } bool VMAddrSpace::insData(uint8_t *buf, uint8_t size) { if (this->code) { DBG_INFO(("Copying buffer into data section.\n")); memcpy(data, buf, size); } else { DBG_ERROR(("Couldn't write into data section.\n")); return false; } return true; }