Hi all,
I want to have a routine that just scans the physical memory from
location 0x000000 to 0xfffff based on the RAM size. I implemented it
using /dev/mem as shown below:
read_mem_linux()
{
int mem_fd, ret;
char var_char[4];
char *charp;
int count = 0;
if((mem_fd = open("/dev/mem",O_RDONLY)) < 0){
printf("\n\nProblem in opening /dev/mem");
exit(-1);
}
ret = 1;
charp = (char *)malloc(sizeof(char) * 1024 * 1024);
while(ret != 0){
ret = read(mem_fd, charp,sizeof(char) * 1024 *1024);
printf("\nret:%d",ret);
if (ret > 0)
count++;
}
printf("\nMemory Read:%d MB",count);
}
My systems is having 2GB of RAM and kernel is of 2.6. But this routine is reading only the
first MB of data and then returns BAD ADDRESS continuously.
But when the same code runs on 2.4, it is working fine.I am running the application as super user at both the places.
Can anybody help how to over come this problem so that i can read the
whole memory.
Thanks in advance,
Works fine here. I had to mod
Works fine here. I had to modify the code to properly abort when read() returns -1, though.
ret:1048576
ret:1048576
ret:1048576
ret:1048576
....
ret:1048576
ret:1048576
ret:-1 (Bad address)
Memory Read:512 MB
You could try to use /proc/kcore instead.
No, /proc/kcore is not ok
That is not raw memory, but a core dump file. I.e. it has a file header and so on. Well, I guess it should also contain the actual memory data, but to extract it you must properly study ELF and write some code. Below you find some evidence:
root # file -s /dev/mem
/dev/mem: data
root # file /proc/kcore
/proc/kcore: ELF 32-bit LSB core file Intel 80386, version 1 (SYSV), SVR4-style, SVR4-style, from 'vmlinux'
It is good that it's working
It is good that it's working on your machine. When tried on my machine, i got
[root@diag026 pmem-modified]# uname -a
Linux diag026 2.6.5-1.358custom #1 SMP Mon Sep 27 11:47:11 IST 2004 i686 i686 i386 GNU/Linux
[root@diag026 pmem-modified]# ./a.out
ret:1048576
1 MB of /dev/mem read
Bad address
ret:-1
Bad address
ret:-1
Bad address
ret:-1
Bad address
ret:-1
Bad address
ret:-1
Bad address
ret:-1
Bad address
ret:-1
Bad address
ret:-1
Bad address
I couldn't understood the proper reason why it's not working on my system.
Yes, I got the same problem:
Yes, I got the same problem: after read 1MB, then the read error occured.
I wonder the difference between read /dev/mem directly and using mmap()function?
When I tried to open /proc/kc
When I tried to open /proc/kcore, i am getting error message:
:Operation not permitted
I am trying to open it in the normal way in which we can open any file.
Where's the problem?????
Thanks,
Kasi.
/proc/meminfo
The routine takes about 20 seconds on 1G of RAM on a 2.6.8.1
Anyway /proc/meminfo presents the information in an easily parsable format. Additionally, any user can read the file unlike the root-only /proc/mem.
/proc/meminfo
#include < stlib.h>
#include < stdio.h>
#include < stdint.h>
#include < string.h>
#include < ctype.h>
#define MEMTOTAL "MemTotal:"
int
main(int argc,char *argv[])
{
FILE *fp;
char b[64];
char *cp;
const size_t l = strlen(MEMTOTAL);
long kb;
long mb = 0;
if((fp = fopen("/proc/meminfo","r")) == NULL) {
perror("fopen( )");
exit(1);
}while(fgets(b,sizeof b,fp)) {
if(strncmp(b,MEMTOTAL,l) != 0)
continue;
cp = b + l;
if(sscanf(cp,"%ld",&kb) != 1)
continue;
mb = kb / 1024;
printf("Memory: %ld\n",mb);
break;
}
fclose(fp);
return 0;
}
/dev/mem doesn't allow you to map real RAM
Unless it's changed recently, /dev/mem does not allow you to map real physical RAM for various manky reasons involving page reference counting... This is due to be fixed one day but I wasn't aware of it being fixed yet.
If I'm right, you'll probably just end up reading zeros for most addresses. IIRC, /dev/mem only allows you to map addresses that are either non-RAM (e.g. IO memory addresses) or that have had some special flag set in the kernel's data structures.
This is not as stupid as it sounds, given /dev/mem's main user is probably the X server, for accessing IO memory addresses related to the graphics hardware!
So how could I access physical memory?
I want to write a program that can transform virtual address into physical address. I came across the problem mentioned above: most of the content read from /dev/mem is "zero".
So how could I access the physical memory directly?
permission on /dev/mem
Hi, I am trying to get access to the device file /dev/mem by opening the the file in a short user
mode program.
Even with group read, write access, the program can't seem to open this file.
Here is the linux and computer information.
Linux linux 2.6.11.4-20a-default #1 Wed Mar 23 21:52:37 UTC 2005 i686 athlon i386 GNU/Linux
And the following is the c program.
sfan@linux:~/c/pci/work> cat ./bt878bar1.c
/* program to learn about how to read and write to
sensoray bt878 chip using mmap and /dev/mem memmory device file
11/26/08
Taken from Linux Journal article 3/21/02
on User Mode Drivers
*/
#define BT878_baddr0 0xeb000008
#include
#include
#include
#include
int main(int argc, char **argv)
{
int fd;
/*printf("%s\n",argv[1]);*/
fd = open(argv[1], O_RDWR);
if (fd < 0) { printf ("file %s open failed %d\n",argv[1],fd); }
else
{
printf ("file %s open ok\nfile descriptor is: %d\n",argv[1],fd);
close(fd);
}
return 0;
} /* end main */
Here is the last couple of lines from running a strace command to display the open statments from the above program.
sfan@linux:~/c/pci/work> strace -e trace=open ./bt878bar1 /dev/mem
open("/dev/mem", O_RDWR) = -1 EPERM (Operation not permitted)
file /dev/mem open failed -1
I am guessing that this may be due to something about capabilities on RAWIO, but not really sure.
Any useful comments or suggestions would be appreciated.
Thanks.
S. Fan
CA, US
11/27/08
did you try running your app
did you try running your app as root?
CONFIG_STRICT_DEVMEM
Without looking at the code:
Did you check CONFIG_STRICT_DEVMEM on your kernel?