/* simple test for mmap() system call 
 *
 * Does mmap() on the /dev/mem device 
 */
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <asm/page.h>
#include <sys/mman.h>

int main(int argc, char *argv[]) {
  int fd;
  unsigned char *mem;
  unsigned long offset=0, size=64;

  if (argc==3) {
    offset = strtoul(argv[1], NULL, 0);
    size = strtoul(argv[2], NULL, 0);
  }

  /* Check for alignment */
  if (offset & ~PAGE_MASK) {
    printf("offset must be page-size aligned (%dK).\n", PAGE_SIZE>>10);
    return -1;
  }

  fd = open("/dev/mem", O_RDWR);
  if (fd == -1) {
    printf("open() failed: %d\n", fd);
    return -1;
  }

  mem = mmap(0, size, PROT_READ, MAP_PRIVATE, fd, (void *)offset);
  if ((int)mem == -1) {
    printf("mmap() failed: errno=%d\n", errno);
  }
  else {
    unsigned long i, *p = (unsigned long *)mem;

    printf("  phys    |virt(user): value\n");
    printf("---------------------------------\n");
    for(i=0; i<size; i+=sizeof(p)) {
      printf("0x%08x|0x%08x: 0x%08x\n", offset+i, p, *p++);
    }

    munmap((void *)mem, size);
  }

  close(fd);
}
