accessing physical memory from linux kernel

20,943

Solution 1

To access real physical memory you should use phys_to_virt function. In case it is io memory (e.g. PCI memory) you should have a closer look at ioremap.

This whole topic is very complex, if you are a beginner I would suggest some kernel/driver development books/doc.

Solution 2

I suggest reading the chapter about memory in this book:

http://lwn.net/Kernel/LDD3/

It's available online for free. Good stuff!

Solution 3

Inside the kernel, memory is still mapped virtually, just not the same way as in userspace.

The chances are that 0x10 is in a guard page or something, to catch null pointers, so it generates an unhandled page fault in the kernel when you touch it.

Normally this causes an OOPS not a hang (but it can be configured to cause a panic). OOPS is an unexpected kernel condition which can be recovered from in some cases, and does not necessarily bring down the whole system. Normally it kills the task (in this case, insmod)

Did you do this on a desktop Linux system with a GUI loaded? I recommend that you set up a Linux VM (Vmware, virtualbox etc) with a simple (i.e. quick to reboot) text-based distribution if you want to hack around with the kernel. You're going to crash it a bit and you want it to reboot as quickly as possible. Also by using a text-based distribution, it is easier to see kernel crash messages (Oops or panic)

Share:
20,943
raj
Author by

raj

#include<string.h> #define St_TGI7 * #include<stdio.h> int a;int main(){ #define CH_T char #define pT printf CH_T St_TGI7 p = " CL)\"EHSH)\"1*" "/T-*/$; $/$-*/$" "=\"/$C& ?\"-$A:" "-\"-$C* ;\"-$-*" "/$?\"-$ -*/\"A" "\"-$-*; (/\"=*;" "(/\"=*Q \"=*/\"" "_*/$ 5$ 5l -X)," "-V/&/VCX?^9f-*"; #define RAJ "%c" #define In_5_ int int G5Y=0,G62T=8; for ( int i = 0 ; i < strlen(p);i++ ) {In_5_ C =p[i]; In_5_ T ;if(C==32 ) continue ;C-=32 ; In_5_ A= C & 1; A += 32 ; G5Y=G5Y ;C >>= 1; while ( C--) { pT (RAJ,A) ; G5Y++;if(G5Y%44 ==0) pT (RAJ,10); }}} CH_T GAH876_;

Updated on October 30, 2020

Comments

  • raj
    raj over 3 years

    Can we access any physical memory via some kernel code.? Because, i wrote a device driver which only had init_module and exit_module.. the code is following.

    int init_module(void) {
        unsigned char *p = (unsigned char*)(0x10);
        printk( KERN_INFO  "I got %u \n", *p);
        return 0;
    }
    

    and a dummy exit_module.. the problem is the computer gets hung when i do lsmod.. What happens? Should i get some kinda permission to access the mem location?

    kindly explain.. I'm a beginner!