Skip to content

Instantly share code, notes, and snippets.

@alex-eg
Created June 21, 2016 12:40
Show Gist options
  • Select an option

  • Save alex-eg/e5e051c665b67801e7322bf4cff1aede to your computer and use it in GitHub Desktop.

Select an option

Save alex-eg/e5e051c665b67801e7322bf4cff1aede to your computer and use it in GitHub Desktop.
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
int main(void) {
off_t offset = 0;
size_t size=33554432;
int open_flags = O_RDWR;
int prot = PROT_READ | PROT_WRITE;
int fd = 0;
const char reg_name[] = "/sys/bus/pci/devices/0000:01:00.0/resource1";
fd = open(reg_name, open_flags | O_CLOEXEC);
if (fd != -1) {
printf("Opened %s\n", reg_name);
printf(" Try to map device memory (size=%lu)... ", size);
void *vaddr = mmap(NULL, size, prot, MAP_SHARED, fd, offset);
if (vaddr == MAP_FAILED) {
perror("Error ");
printf("FAILED (err=%d)\n", errno);
}
else {
printf("Mapped at {virtualAddr=%p}... ", vaddr);
munmap(vaddr, size);
printf("Unmapped\n");
}
close(fd);
}
else {
printf("FAILED to open %s (err=%d)\n", reg_name, errno );
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment