Re: mmap for I/O access



In article <478286ac.198578@xxxxxxxxxxxxxxxxxxxxxx>,
Robert Scott <---@---> wrote:
My call to mmap is causing a Segmentation fault. Is this the right way to do
it?

I have some general-purpose I/O on this Embest ARM-based board. According to
the manual for the board, the control register and the data register are at
absolute physical addresses

0x56000010 and
0x56000014 respectively.

So here is how I tried to test my access to these registers:

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/mman.h>
#include <sys/ioctl.h>

main() {
int fm = open("/dev/mem",O_RDWR);
printf("handle to /dev/mem = %08x\r\n",fm);

char *mptr = (char*)mmap(

More reasonable: (you want mptr to point to an int right?)
int *mptr = mmap(

(void*)0,
16, //..16 bytes
PROT_READ | PROT_WRITE,
MAP_SHARED,
fm,
0x56000010); //..starting with Port B CON reg

printf("mptr = %08x", mptr);

printf("GPBCON = %08x\r\n", *((int*)(mptr+0)));
printf("GPBDAT = %08x\r\n", *((int*)(mptr+4)));

And then:
printf("GPBCON = %08x\r\n", mptr[0] );
printf("GPBCON = %08x\r\n", mptr[1] );

For some one who tries to be so political correct to cast the
(void *) that mmap returns, it is strange to see here a 4,
instead of sizeof(int) .

Maybe you managed to confuse the compiler. It is certainly
worthwhile to leave the second line out, see what happens.

}

When I run this program, the open() returns 4, and then the Segmentation Fault
occurs during the execution of mmap, because there was no printf telling what
mptr is. Here is the output:


handle to /dec/mem = 00000004


: [<000086b4>] lr : [<00000000>] Tainted: P

sp : bffffdd8 ip : bffff7d8 fp : bffffea4

r10: 40139280 r9 : 000084a0 r8 : 00000001

r7 : 4000ee5c r6 : 0000830c r5 : bffffec4 r4 : 40020c34

r3 : ffffffff r2 : 0000000f r1 : 00008823 r0 : 0000883c

Flags: NzCv IRQs on FIQs on Mode USER_32 Segment user

Control: C000317F Table: 33E90000 DAC: 00000015

Segmentation fault

- - - - - - - - - - - - - - - - - - - - - - - - - - -
I am running as root, so I should be able to do this, right?


Robert Scott
Ypsilanti, Michigan


--
--
Albert van der Horst, UTRECHT,THE NETHERLANDS
Economic growth -- like all pyramid schemes -- ultimately falters.
albert@spe&ar&c.xs4all.nl &=n http://home.hccnet.nl/a.w.m.van.der.horst
.