Reputation: 49
I want to access(read and write) all the RAM(using physical addresses) from Linux kernel(either through user space or kernel space) can we do that? what are the possibilities and limitations in userspace? Up to what extent we can do that using loadable kernel modules?
Thanks and Regards, Veerendranath
Upvotes: 0
Views: 1168
Reputation: 11
You can access to physical address from user space using mmap.
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#define PHY_ADDR 0x807feff
int main()
{
int i;
unsigned int *addr;
int fd = open( "/dev/mem", O_RDWR | O_SYNC );
if( fd < 0 )
{
perror( "Error opening file /dev/mem" );
return 1;
}
addr = (unsigned int *) mmap( 0, getpagesize(), PROT_READ | PROT_WRITE, MAP_SHARED, fd, PHY_ADDR );
if( addr == NULL )
{
perror( "Error mapping" );
return 1;
}
for( i = 0; i < 256 / 4; i++ )
printf( "addr: %X\tval: %X\n", addr + i, *( addr + i ) );
if( munmap( addr, getpagesize() ) == -1 )
{
perror( "Error unmaping" );
return 1
}
close( fd );
return 0;
}
Upvotes: 1