LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel
User Name
Password
Linux - Kernel This forum is for all discussion relating to the Linux kernel.

Notices


Reply
  Search this Thread
Old 05-26-2011, 11:17 PM   #1
pavan8085
LQ Newbie
 
Registered: May 2011
Location: Bangalore, India
Distribution: Ubuntu
Posts: 13

Rep: Reputation: 8
PC freezes totally while accessing physical memory on Ubuntu 10.04(64-bit machine)


Hi All,

I am trying to access the physical memory of my PC using this program which uses mmap()-

Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
 
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
  __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
 
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

int main(int argc, char **argv) {
    int fd;
    void *map_base, *virt_addr;
    unsigned long read_result, writeval;
    off_t target;
    int access_type = 'w';
   
    if(argc < 2) {
        fprintf(stderr, "\nUsage:\t%s { address } [ type [ data ] ]\n"
            "\taddress : memory address to act upon\n"
            "\ttype    : access operation type : [b]yte, [h]alfword, [w]ord\n"
            "\tdata    : data to be written\n\n",
            argv[0]);
        exit(1);
    }
    target = strtoul(argv[1], 0, 0);

    if(argc > 2)
        access_type = tolower(argv[2][0]);


    if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
    printf("/dev/mem opened.\n");
    fflush(stdout);
   
    /* Map one page */
    map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
    if(map_base == (void *) -1) FATAL;
    printf("Memory mapped at address %p.\n", map_base);
    fflush(stdout);
   
    virt_addr = map_base + (target & MAP_MASK);
    switch(access_type) {
        case 'b':
            read_result = *((unsigned char *) virt_addr);
            break;
        case 'h':
            read_result = *((unsigned short *) virt_addr);
            break;
        case 'w':
            read_result = *((unsigned long *) virt_addr);
            break;
        default:
            fprintf(stderr, "Illegal data type '%c'.\n", access_type);
            exit(2);
    }
    printf("Value at address 0x%X (%p): 0x%X\n", target, virt_addr, read_result);
    fflush(stdout);

    if(argc > 3) {
        writeval = strtoul(argv[3], 0, 0);
        switch(access_type) {
            case 'b':
                *((unsigned char *) virt_addr) = writeval;
                read_result = *((unsigned char *) virt_addr);
                break;
            case 'h':
                *((unsigned short *) virt_addr) = writeval;
                read_result = *((unsigned short *) virt_addr);
                break;
            case 'w':
                *((unsigned long *) virt_addr) = writeval;
                read_result = *((unsigned long *) virt_addr);
                break;
        }
        printf("Written 0x%X; readback 0x%X\n", writeval, read_result);
        fflush(stdout);
    }
   
    if(munmap(map_base, MAP_SIZE) == -1) FATAL;
    close(fd);
    return 0;
}
When I run this program as sudo ./a.out 0xfdff8000, my system simply hangs. Mouse, keyboard, display everything freeze! Restart is the only option. I checked for 0xfdff8000 in /proc/iomem. It corresponds to ICH HD audio. I am not sure what this means.
Also kmsg, dmesg and /var/log/messages are not throwing any hints!
 
Old 05-31-2011, 02:38 PM   #2
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
It means that the address belong to system. You can't touch it.
 
Old 06-01-2011, 12:57 AM   #3
pavan8085
LQ Newbie
 
Registered: May 2011
Location: Bangalore, India
Distribution: Ubuntu
Posts: 13

Original Poster
Rep: Reputation: 8
I agree that 0xfdff8000 might correspond to some system sensitive memory but I want to know why is my system not handling this gracefully? What exactly happened that the entire system went down instead of just the process which tried this access?
 
Old 06-01-2011, 02:32 PM   #4
nini09
Senior Member
 
Registered: Apr 2009
Posts: 1,850

Rep: Reputation: 161Reputation: 161
What do you want? Normally, mmap is used to map regular file into memory.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Can we install ubuntu AMD 64 bit on intel 64 bit machine ervaneet82 Linux - General 2 11-11-2010 04:40 PM
[SOLVED] Physical and virtual memory upper limit for 32 bit Linux wagaboy Linux - Newbie 4 06-26-2010 06:36 AM
[SOLVED] Which is better on a low-memory machine: 32 or 64 bit? cantab Linux - Newbie 2 05-09-2010 07:18 PM
32-bit application accessing memory more than 4GB parthi_s Linux - Newbie 2 04-07-2009 07:58 AM
How much physical memory can a 32-bit installation address gschmitt99 Linux - General 1 09-19-2008 01:10 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software > Linux - Kernel

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration