@@ -25,22 +25,35 @@
#define MAP_WINDOW_SIZE (64*1024*1024)
#define DEV_MEM "/dev/mem"
+#define ALIGN_MASK(x,y) (((x) + (y)) & ~(y))
+#define ALIGN(x,y) ALIGN_MASK(x, (y) - 1)
+
static void *map_addr(int fd, unsigned long size, off_t offset)
{
+ unsigned long page_size = getpagesize();
+ unsigned long map_offset = offset & (page_size - 1);
+ size_t len = ALIGN(size + map_offset, page_size);
void *result;
- result = mmap(0, size, PROT_READ, MAP_SHARED, fd, offset);
+
+ result = mmap(0, len, PROT_READ, MAP_SHARED, fd, offset - map_offset);
if (result == MAP_FAILED) {
fprintf(stderr, "Cannot mmap " DEV_MEM " offset: %llu size: %lu: %s\n",
(unsigned long long)offset, size, strerror(errno));
exit(5);
}
- return result;
+ return result + map_offset;
}
static void unmap_addr(void *addr, unsigned long size)
{
+ unsigned long page_size = getpagesize();
+ unsigned long map_offset = (uintptr_t)addr & (page_size - 1);
+ size_t len = ALIGN(size + map_offset, page_size);
int ret;
- ret = munmap(addr, size);
+
+ addr -= map_offset;
+
+ ret = munmap(addr, len);
if (ret < 0) {
fprintf(stderr, "munmap failed: %s\n",
strerror(errno));