mmap(/dev/mem) causing sigbus
It seems that there is an issue with either mmap or mmap'ing /dev/mem on sparc64. A tool (mstflint) for flashing firmware for Mellanox Infiniband HCA's is crashing on sparc64. It mmap's /dev/mem and when it accesses it, a bus error is generated.
Should this work?
Attached is a small test program to demonstrate. Change BAR0 to something in your PCI address space (grab an address from /proc/iomem).
# cat /proc/iomem
<snip>
1ff00100000-1ff001fffff : 0000:81:00.0
1ff00180680-1ff0018070f : ib_mthca
1ff001f00d8-1ff001f00df : ib_mthca
1ff00800000-1ff00ffffff : 0000:81:00.0
1ff00800000-1ff00ffffff : ib_mthca
1ff08000000-1ff0fffffff : 0000:81:00.0# gdb ./mmap-test (gdb) run Starting program: /root/mmap-test warning: linux_test_for_tracefork: unexpected result from waitpid (3298, status 0xb7f)
Program received signal SIGBUS, Bus error.0x00010630 in main () at mmap-test.c:29
29 value= *( (unsigned*)(ptr + 0x000f0014) );
(gdb) print ptr
$1 = (void *) 0x7018c000
(gdb) print ptr + 0x000f0014
$2 = (void *) 0x7027c014
(gdb) x 0x7027c0140x7027c014: Cannot access memory at address 0x7027c014
# cat /proc/3317/maps <snip> 7018c000-7028c000 rw-s 1ff00100000 00:0d 316 /dev/mem
# uname -a Linux tat 2.6.10 #14 SMP Mon Jan 3 12:30:51 PST 2005 sparc64 GNU/Linux
-tduffy
#define BAR0 0x1ff00100000ull
#define _XOPEN_SOURCE 500
#define _FILE_OFFSET_BITS 64
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
int fd;
void *ptr;
unsigned value;
unsigned long long offset = BAR0;
fd = open("/dev/mem",O_RDWR | O_SYNC);
if (fd<0) {
perror("open");
return 1;
}
ptr = mmap(NULL, 0x100000, PROT_READ | PROT_WRITE,
MAP_SHARED, fd, offset);
if ( (! ptr) || (ptr == MAP_FAILED) ) {
perror("map");
return 1;
}
value= *( (unsigned*)(ptr + 0x000f0014) );
printf("value at 0x000f0014 is %#8.8x\n", value);
return 0;
err:
return 1;
}Quoted 6 lines · On Thu, 13 Jan 2005 15:12:14 -0800 Tom Duffy <[email protected]> wrote:
It seems that there is an issue with either mmap or mmap'ing
/dev/memon sparc64. A tool (mstflint) for flashing firmware for Mellanox Infiniband HCA's is crashing on sparc64. It mmap's/dev/memand when it accesses it, a bus error is generated.Should this work?
It's not meant to be used at all. We have abstracted out a way to portably mmap() pci devices by opening /proc/bus/pci/* nodes, running the appropriate ioctl() to choose I/O or MEM space, then mmap()'ing on the fd to mmap the PCI device resources.
At the very least, i386 and ppc support this interface as well.
Quoted 6 lines · David S. Miller
It's not meant to be used at all. We have abstracted out a way to portably
mmap()pci devices by opening/proc/bus/pci/*nodes, running the appropriateioctl()to choose I/O or MEM space, then mmap()'ing on the fd to mmap the PCI device resources.At the very least, i386 and ppc support this interface as well.
Dave, thanks for the info. The maintainer of the tool has updated it to use the /proc/bus/pci method.
-tduffy