summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHristo Venev <hristo@venev.name>2020-06-11 11:51:26 +0300
committerHristo Venev <hristo@venev.name>2020-06-11 11:51:26 +0300
commit3d21b290e1058ed80bb64b9524d21cc0fb1c6c8e (patch)
tree52ab1e7cc0f6205a9f7bf2bbc6a4f6c3704627c4
parent312af3705d02b198f09d6efa69c68ba1598fb721 (diff)
Use pread() instead of mmap().HEADmaster
-rw-r--r--vc-log.c56
1 files changed, 7 insertions, 49 deletions
diff --git a/vc-log.c b/vc-log.c
index 27aedd3..e57bf7e 100644
--- a/vc-log.c
+++ b/vc-log.c
@@ -11,7 +11,6 @@
static int vc_fd;
static uint32_t vc_mem_size, vc_mem_base, vc_mem_load, vc_mem_phys;
-static void *vc_mem;
static uint32_t vc_syms_off;
#define VC_MEM_IOC_MAGIC 'v'
@@ -26,43 +25,14 @@ static uint32_t vc_syms_off;
#define VC_SYMBOL_BASE_OFFSET VC_DEBUG_HEADER_OFFSET
static void vc_read(void *buf, size_t off, size_t size) {
- // if(off < vc_mem_base) {
- // fprintf(stderr, "small address\n");
- // abort();
- // }
- if(off > vc_mem_size || size > vc_mem_size - off) {
- fprintf(stderr, "out of bounds read: %zx+%zx/%x\n", off, size, vc_mem_size);
+ ssize_t r = pread(vc_fd, buf, size, off);
+ if(r < 0) {
+ fprintf(stderr, "pread() failed: %m\n");
abort();
}
-
- if(size == 0) return;
-
- size_t skip = off % 8;
- volatile uint64_t *inp = (volatile uint64_t*)((char*)vc_mem + (off - skip));
- char *out = buf;
- if(skip) {
- uint64_t v = *inp;
- inp++;
- size_t have = 8 - skip;
- if(have >= size) {
- memcpy(out, (char*)&v + skip, size);
- return;
- }
- memcpy(out, (char*)&v + skip, have);
- out += have;
- size -= have;
- }
-
- while(1) {
- uint64_t v = *inp;
- inp++;
- if(size < 8) {
- memcpy(out, &v, size);
- return;
- }
- memcpy(out, &v, 8);
- out += 8;
- size -= 8;
+ if((size_t)r != size) {
+ fprintf(stderr, "pread() short: %zu/%zu\n", (size_t)r, size);
+ abort();
}
}
@@ -85,13 +55,7 @@ static uint8_t vc_read_u8(size_t off) {
}
static size_t vc_read_ptr(size_t off) {
- uint32_t r = vc_read_u32(off);
- if(r == 0) return 0;
- if(r <= 0xc0000000) {
- fprintf(stderr, "invalid pointer: %x\n", r);
- abort();
- }
- return r - 0xc0000000;
+ return vc_read_u32(off);
}
static char *vc_read_str(size_t off) {
@@ -152,12 +116,6 @@ static void vc_open(void) {
abort();
}
- vc_mem = mmap(NULL, vc_mem_size, PROT_READ, MAP_SHARED, vc_fd, 0);
- if(vc_mem == MAP_FAILED) {
- perror("mmap");
- abort();
- }
-
uint32_t dbg_off = vc_mem_load + VC_DEBUG_HEADER_OFFSET;
uint32_t dbg_magic = vc_read_u32(dbg_off + 4);
if(dbg_magic != 0x48444356) {