Skip to contentSkip to conversation

mmap /dev/mem in python

DISCUSSION Brett Graham 3 participants 8 messages Oct 20, 2009 – Oct 21, 2009 1 message not in the archive

lore
Share & tools
lore
#0 lore raw

I'm trying to mmap /dev/mem in python to access various registers on a gumstix overo (with an omap 3530 processor). Here is the code I'm using (as a minimal example):
----------------------------
import os, mmap
MAP_MASK = mmap.PAGESIZE - 1
addr = (see below)

f = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
m = mmap.mmap(f, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ, offset=addr & ~MAP_MASK)
m.seek(addr & MAP_MASK)
c = m.read_byte()
print c
m.close()
os.close(f)
----------------------------

If I use: addr = 0x48002178 everything works swimmingly. However, if I
use: addr = 0x48088024 (or all the addresses I've tried > 0x48044000) I get the following error

Unhandled fault: external abort on non-linefetch (0x1018) at 0x40020024 Bus error

and python crashes. I'm basing the mmap flags and options off of devmem2.c. If I remove the O_SYNC flag from the os.open command, I can read and write to registers fine, but randomly I will get the Unhandled fault error and things will not work correctly.

Am I using the right command to write to the omap registers? should I be using the O_SYNC flag? Any ideas where these errors are coming from?

Thanks in advance for your help.

#1 lore raw
Quoted 25 lines · Brett Graham

I'm trying to mmap /dev/mem in python to access various registers on a gumstix overo (with an omap 3530 processor). Here is the code I'm using (as a minimal example):
----------------------------
import os, mmap
MAP_MASK = mmap.PAGESIZE - 1
addr = (see below)

f = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
m = mmap.mmap(f, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ, offset=addr & ~MAP_MASK)
m.seek(addr & MAP_MASK)
c = m.read_byte()
print c
m.close()
os.close(f)
----------------------------

If I use: addr = 0x48002178 everything works swimmingly. However, if I
use: addr = 0x48088024 (or all the addresses I've tried > 0x48044000) I get the following error

Unhandled fault: external abort on non-linefetch (0x1018) at 0x40020024 Bus error

Quoted 8 lines

and python crashes. I'm basing the mmap flags and options off of devmem2.c. If I remove the O_SYNC flag from the os.open command, I can read and write to registers fine, but randomly I will get the Unhandled fault error and things will not work correctly.

Am I using the right command to write to the omap registers? should I be using the O_SYNC flag? Any ideas where these errors are coming from?

Sounds like you need to enable ick and fck for the device you're trying to read.

Tony

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

If I run the following commads:
devmem2 0x48004a40 w 0x0000038a # select 32kHz clock devmem2 0x48004a10 w 0x03007052 # enable iclk for gpt11 devmem2 0x48004a00 w 0x03007000 # enable fclk for gpt11

which all run sensibly and alter the registers, then I try to previous minimum example on address 0x48088024 (TCLK for gpt11) I get the following error:
Unhandled fault: external abort on non-linefetch (0x1018) at 0x40020024 Bus error

What puzzles me is that when I use devmem2 to read or write to the same register, everything works fine.

Quoted 35 lines · Tony Lindgren
Quoted 25 lines · Brett Graham

I'm trying to mmap /dev/mem in python to access various registers on a gumstix overo (with an omap 3530 processor). Here is the code I'm using (as a minimal example):
----------------------------
import os, mmap
MAP_MASK = mmap.PAGESIZE - 1
addr = (see below)

f = os.open("/dev/mem", os.O_RDWR | os.O_SYNC)
m = mmap.mmap(f, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ, offset=addr & ~MAP_MASK)
m.seek(addr & MAP_MASK)
c = m.read_byte()
print c
m.close()
os.close(f)
----------------------------

If I use: addr = 0x48002178 everything works swimmingly. However, if I
use: addr = 0x48088024 (or all the addresses I've tried > 0x48044000) I get the following error

Unhandled fault: external abort on non-linefetch (0x1018) at 0x40020024 Bus error

Quoted 8 lines

and python crashes. I'm basing the mmap flags and options off of devmem2.c. If I remove the O_SYNC flag from the os.open command, I can read and write to registers fine, but randomly I will get the Unhandled fault error and things will not work correctly.

Am I using the right command to write to the omap registers? should I be using the O_SYNC flag? Any ideas where these errors are coming from?

Quoted 9 lines

Sounds like you need to enable ick and fck for the device you're trying to read.

Tony
--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#4 lore raw
Quoted 5 lines · Brett Graham

If I run the following commads:
devmem2 0x48004a40 w 0x0000038a # select 32kHz clock devmem2 0x48004a10 w 0x03007052 # enable iclk for gpt11 devmem2 0x48004a00 w 0x03007000 # enable fclk for gpt11

Quoted 8 lines

which all run sensibly and alter the registers, then I try to previous minimum example on address 0x48088024 (TCLK for gpt11) I get the following error:
Unhandled fault: external abort on non-linefetch (0x1018) at 0x40020024 Bus error

What puzzles me is that when I use devmem2 to read or write to the same register, everything works fine.

Consider running strace on devmem2 and comparing it to the strace of your Python code? That should show you any differences. If devmem2 works, it's unlikely to be a kernel problem.

- Paul

#5 lore raw

It's good to at least know that this doesn't seem to be a kernel issue, and thanks for the strace advice (I am new to this level of linux development). Here is the truncated output for devmem and the minimal example:

--- devmem ---
open("/dev/mem", O_RDWR|O_SYNC) = 3
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 0), ...}) = 0 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0x4001f000
write(1, "/dev/mem opened.\n", 17) = 17
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0x48088) = 0x40020000 write(1, "Memory mapped at address 0x40020"..., 37) = 37 write(1, "Value at address 0x48088024 (0x4"..., 46) = 46
munmap(0x40020000, 4096) = 0
close(3) = 0
io_submit(0, 0, 0xfbad2088 <unfinished ... exit status 0> Process 1635 detached

--minimal python example--
open("/dev/mem", O_RDWR|O_SYNC|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFCHR|0640, st_rdev=makedev(1, 1), ...}) = 0
dup(3) = 4
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0x48088) = 0x40020000 --- SIGBUS (Bus error) @ 0 (0) ---
+++ killed by SIGBUS +++
Process 1633 detached

It looks like python decided to add a O_LARGEFILE and duplicate the file descriptor. I'm still not sure what's going on here that's causing the problem as both mmap2 calls are identical.

I don't mean to bombard this list with a problem that isn't related to the kernel, so I'll post this to a python list somewhere. Thanks a million for your help so far, this is driving me nutty! Also if any more info would help just let me know.

Quoted 16 lines · Paul Walmsley
Quoted 5 lines · Brett Graham

If I run the following commads:
devmem2 0x48004a40 w 0x0000038a # select 32kHz clock devmem2 0x48004a10 w 0x03007052 # enable iclk for gpt11 devmem2 0x48004a00 w 0x03007000 # enable fclk for gpt11

Quoted 8 lines

which all run sensibly and alter the registers, then I try to previous minimum example on address 0x48088024 (TCLK for gpt11) I get the following error:
Unhandled fault: external abort on non-linefetch (0x1018) at 0x40020024 Bus error

What puzzles me is that when I use devmem2 to read or write to the same register, everything works fine.

Quoted 7 lines

Consider running strace on devmem2 and comparing it to the strace of your Python code?  That should show you any differences.  If devmem2 works, it's unlikely to be a kernel problem.

- Paul

--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw
Quoted 8 lines · Brett Graham

--minimal python example--
open("/dev/mem", O_RDWR|O_SYNC|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFCHR|0640, st_rdev=makedev(1, 1), ...}) = 0
dup(3) = 4
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0x48088) = 0x40020000

killed by SIGBUS +++ renamed from SIGBUS (Bus error) @ 0 (0) --- +0 -0

Process 1633 detached

Your script:

Quoted 3 lines
Quoted 3 lines

m = mmap.mmap(f, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ, offset=addr & ~MAP_MASK)
m.seek(addr & MAP_MASK)

So the mmap() is working fine, based on the output above...

Quoted 1 line
Quoted 1 line

c = m.read_byte()

... but the above line is going to fail. GPTIMER registers need 16- or 32-bit accesses[1].

- Paul

1. OMAP34xx TRM 16.3.2:

"CAUTION
The GP timer registers are limited to 32-bit and 16-bit data accesses; 8-bit access is not allowed and can corrupt the register content."

#7 lore raw

Doh! Thank you! This looks to be the problem, I've made some changes to the code (calling mmap.read(4) rather than mmap.read_bye()) and everything seems to be working.

Quoted 25 lines · Paul Walmsley
Quoted 8 lines · Brett Graham

--minimal python example--
open("/dev/mem", O_RDWR|O_SYNC|O_LARGEFILE) = 3
fstat64(3, {st_mode=S_IFCHR|0640, st_rdev=makedev(1, 1), ...}) = 0 dup(3)                                  = 4
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_SHARED, 3, 0x48088) = 0x40020000

killed by SIGBUS +++ renamed from SIGBUS (Bus error) @ 0 (0) --- +0 -0

Process 1633 detached

Your script:

Quoted 3 lines
Quoted 3 lines

m = mmap.mmap(f, mmap.PAGESIZE, mmap.MAP_SHARED, mmap.PROT_WRITE | mmap.PROT_READ, offset=addr & ~MAP_MASK)
m.seek(addr & MAP_MASK)

So the mmap() is working fine, based on the output above...

Quoted 1 line
Quoted 1 line

c = m.read_byte()

... but the above line is going to fail.  GPTIMER registers need 16- or 32-bit accesses[1].

Quoted 10 lines

- Paul

1. OMAP34xx TRM 16.3.2:

"CAUTION
The GP timer registers are limited to 32-bit and 16-bit data accesses; 8-bit access is not allowed and can corrupt the register content."

--
To unsubscribe from this list: send the line "unsubscribe linux-omap" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
In reply to Brett Graham
Quoted 6 lines · Brett Graham

If I use: addr = 0x48002178 everything works swimmingly. However, if I
use: addr = 0x48088024 (or all the addresses I've tried > 0x48044000) I get the following error

Unhandled fault: external abort on non-linefetch (0x1018) at 0x40020024 Bus error

0x40020024 != 0x48088024 ? That's probably not the kernel's fault.

- Paul