Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have 2GiB RAM, running Perl 5.8.8 on Linux 2.6.

Whenever I open /dev/mem the call succeeds (that is I can get a filehandle on it), but also error 25 "Inappropriate ioctl for device" is returned. I don't know what that is supposed to mean.

$ok = sysopen my $mem, '/dev/mem', O_RDWR;

Following some suggestions in the open(2) manpage I tried to get rid of it by |ing (binary or) the mode with O_DIRECT and O_NONBLOCK and variations thereof, but it only made it worse, I get 22 "Invalid argument" and the call fails.

sysread()ing works up to address hex 38000000 (only ~896 MiB), then fails with error 14 "Bad address". Experiment from the shell supports it:

$ sudo cat /dev/mem > memdump cat: /dev/mem: Bad address

However, I can successfully sysseek() to any address in the file (even though error 25 is set again).

sysread()ing from /proc/$pid/mem is totally useless, it always fails with error 3 "No such process", even though sysopen()ing and sysseek()ing do succeed.

How comes all that strange behaviour, and why is that so?

How do I know where each program is located, anyway? I had a look at /proc/$pid/maps, but it's impossible that dozens of processes occupy overlapping or even exact same space after hex 8000000. Reading from /dev/mem at that point also returns garbage, well, at least not the garbage I expect.

Replies are listed 'Best First'.
Re: messing with memory
by sgifford (Prior) on May 16, 2007 at 20:13 UTC
    You could try Sys::Mmap. This seems to work for me:
    #!/usr/bin/perl use strict; use warnings; use Fcntl qw(:DEFAULT O_ASYNC O_DIRECT); use Sys::Mmap; sysopen(my $FH,"/dev/mem", O_RDONLY, 0666) or die "Couldn't open /dev/mem\n"; my $mem; mmap($mem,1_000_000_000,PROT_READ,MAP_SHARED,$FH) or die "Couldn't mmap\n"; print "Billionth byte of memory: ",substr($mem,999_999_999,1),"\n";
Re: messing with memory
by cdarke (Prior) on May 16, 2007 at 14:48 UTC
    And your Perl question is?
    We might be able to help if we knew what it was you were trying to achieve.
    You would be better asking this on a Linux site. Linux and most other UNIXs use virtual memory, so most yes, potentially dozens of processes could occupy the same area of RAM. They occupy swap areas (usually on disk) when they are not there. The algorithms and methods vary, and can be complex.