Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redesign memory access functions #17

Merged
merged 3 commits into from
Jul 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions dbg.proto
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ message Request {

required Type type = 1; //
optional string msg = 2; // DEBUG_PRINT
optional int32 address = 3; // FREE, MEM_READ, MEM_WRITE
optional int32 size = 4; // MALLOC, MEM_READ, MEM_WRITE
optional int64 value = 5; // MEM_WRITE
optional uint32 address = 3; // FREE, MEM_READ, MEM_WRITE
optional uint32 size = 4; // MALLOC, MEM_READ
optional bytes data = 5; // MEM_WRITE
}

message Response {
Expand All @@ -37,7 +37,7 @@ message Response {
required Type type = 1; //
optional string msg = 2; //
optional SysInfo info = 3; // SYSINFO
optional int32 address = 4; // MALLOC
optional int32 size = 5; //
optional int64 value = 6; // MEM_READ
optional uint32 address = 4; // MALLOC
optional uint32 size = 5; //
optional bytes data = 6; // MEM_READ
}
26 changes: 15 additions & 11 deletions dbg.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,21 @@ def free(self, addr):
req.address = addr
return self._send_simple_request(req)

def mem(self, addr, value=None):
"""Read/write system memory"""
write = value is not None
def mem_read(self, addr, size):
"""read memory"""
req = Request()
req.type = Request.MEM_WRITE if write else Request.MEM_READ
req.type = Request.MEM_READ
req.size = size
req.address = addr
req.size = 1 # TODO: Support word, dword, qword accesses
if write:
req.value = value
res = self._send_simple_request(req)
return res if write else res.value
return self._send_simple_request(req).data

def mem_write(self, addr, data):
"""write memory"""
req = Request()
req.type = Request.MEM_WRITE
req.data = data
req.address = addr
return self._send_simple_request(req)

def debug_print(self, string):
"""Print a debug string to the screen"""
Expand Down Expand Up @@ -109,8 +113,8 @@ def main():
addr = xbox.malloc(1024)
val = 0x5A
print("Allocated memory at 0x%x" % addr)
xbox.mem(addr, val)
assert(xbox.mem(addr) == val)
xbox.mem_write(addr, bytes([val]))
assert(xbox.mem_read(addr, 1)[0] == val)
xbox.free(addr)

#xbox.reboot()
Expand Down
60 changes: 54 additions & 6 deletions dbgd.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@
#define HTTPD_DEBUG LWIP_DBG_OFF
#endif

static void* get_transfer_buffer(uint32_t size) {
static uint32_t buffer_size = 0;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Be aware using static variables kills multithreaded use.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was more of a workaround to avoid a leak and get this merged quickly.
I agree that we should eventually have an object for each connection.

I'll create an issue about improving this after merge. Thanks for pointing it out.

static void* buffer = NULL;

if (size > buffer_size) {
if (buffer != NULL) {
free(buffer);
}
buffer = malloc(size);
buffer_size = size;
}
return buffer;
}

static int dbgd_sysinfo(Dbg__Request *req, Dbg__Response *res);
static int dbgd_reboot(Dbg__Request *req, Dbg__Response *res);
static int dbgd_malloc(Dbg__Request *req, Dbg__Response *res);
Expand Down Expand Up @@ -211,21 +225,55 @@ static int dbgd_mem_read(Dbg__Request *req, Dbg__Response *res)
res->address = req->address;
res->has_address = 1;

res->size = 1; /* FIXME: add word, dword, qword support */
res->has_size = 1;
res->data.len = req->size;
res->data.data = get_transfer_buffer(res->data.len);
res->has_data = 1;

res->value = *((uint8_t*)(req->address));
res->has_value = 1;
unsigned int i = 0;
unsigned int s = req->size;

while(s >= 4) {
*(uint32_t*)&res->data.data[i] = *(volatile uint32_t*)(req->address + i);
i += 4;
s -= 4;
}
while(s >= 2) {
*(uint16_t*)&res->data.data[i] = *(volatile uint16_t*)(req->address + i);
i += 2;
s -= 2;
}
while(s >= 1) {
*(uint8_t*)&res->data.data[i] = *(volatile uint8_t*)(req->address + i);
i += 1;
s -= 1;
}

return DBG__RESPONSE__TYPE__OK;
}

static int dbgd_mem_write(Dbg__Request *req, Dbg__Response *res)
{
if (!req->has_address || !req->has_size || !req->has_value)
if (!req->has_address || !req->has_data)
return DBG__RESPONSE__TYPE__ERROR_INCOMPLETE_REQUEST;

*((uint8_t*)(req->address)) = (uint8_t)req->value;
unsigned int i = 0;
unsigned int s = req->data.len;

while(s >= 4) {
*(volatile uint32_t*)(req->address + i) = *(uint32_t*)&req->data.data[i];
i += 4;
s -= 4;
}
while(s >= 2) {
*(volatile uint16_t*)(req->address + i) = *(uint16_t*)&req->data.data[i];
i += 2;
s -= 2;
}
while(s >= 1) {
*(volatile uint8_t*)(req->address + i) = *(uint8_t*)&req->data.data[i];
i += 1;
s -= 1;
}

return DBG__RESPONSE__TYPE__OK;
}
Expand Down