read PCI memory and config spyce through /dev/mem

Jonathan Neuschäfer j.neuschaefer at gmx.net
Tue May 7 12:20:53 EDT 2013


On Tue, May 07, 2013 at 06:35:43AM +0000, Warlich, Christof wrote:
[...]
> Furthermore, read / write access through a simple program using /dev/mem
> _does_ work as expected:
> 
> #include <unistd.h>
> #include <stdio.h>
> #include <fcntl.h>
> #include <assert.h>
> #include <sys/mman.h>
> int main( int argc, char *argv[]) {
>   char *mem;
>   int fd;
>   fd = open ("/dev/mem", O_RDWR);
>   assert(fd >= 0);
>   mem = mmap(NULL, getpagesize(), PROT_READ|PROT_WRITE, MAP_SHARED, fd, (off_t) 0x81423000);
>   assert(mem != MAP_FAILED);
>   printf("Memory pointer: %p\n", mem);
>   printf("The PCI memory is : %#x\n", *mem);
>   *mem = *argv[1];
>   printf("The PCI memory is : %#x\n", *mem);
>   munmap(mem, getpagesize());
>   close(fd);
>   return 0;
> }
> 
> Calling it, yields:
> 
> $ led 4
> Memory pointer: 0xb7678000
> The PCI memory is : 0x33
> The PCI memory is : 0x34
> 
> And the LED matrix display being behind that address switches from 3 to
> 4 as expected.
> 
> Thus, I still cannot see why the access through /dev/mem using dd doesn't
> work.

I suspect dd uses read instead of mmap.


Thanks,
Jonathan Neuschäfer



More information about the Kernelnewbies mailing list