Re: memory map io function



Louis Lu wrote:
>    I just wondering whether or not anyone who has the experiences on memory map io function before?
> 
> My code is as following:
>   int *base;
>   unsigned int fd;
>   fd = open("dev/mem", O_RDWR);
>   base = mmap(0, 0x80000, PROT_READ|PROT_WRITE,MAP_SHARED,   fd, 0xd800000);
> 
> (and I tried to write a value to a certian register of vga)
> 
>   *(int *)(base+0x60000/4) = 0x30f027f;
>   munmap(base, 0x800000);
>   close(fd);
> 
> The compile is ok, but when I runt it, I got segmentation fault (core dump).

Hi Louis, I guess it's a permission thing. Are you running this as root? 

Add some checks to the results of open() and mmap(), and see where its
failing, and what error you get from the system.

	if( (fd = open( "/dev/mem", O_RDWR )) == -1 ) {
		fprintf( stderr, "open /dev/mem failed: %s\n", strerror( errno ) );
		exit( -1 );
	}
	if( (base = mmap( ... )) == (void *) -1 ) {
		fprintf( stderr, "mmap failed: %s\n", strerror( errno ) );
		exit( -1 );
	}

etc.

This sounds incredibly dangerous :-) You are in extreme danger of
toasting your system.

Good luck, John




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]