10
votes

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!

3
This is a very complicated programming area and you're not likely to get the answer you need in such limited space. Have you read any documentation in this area? - trojanfoe
yeah, i know the basics.. the question is i know i can access physical memory as a device driver. Bu why does my PC freeze.. after all i'm just reading ; not writing. ? - raj
It's likely that the address 0x10 is a special register that you can't just read. You're treading in dangerous waters here. Arbitrary memory accesses can be hazardous to your health. - Chriszuma
Even the kernel has no direct access to physical memory in the way your are thinking. Basically the kind of memory you seem to imagine (linear memory with flat addressing) is something that is constructed by the kernel (from physical memory banks) and exported, it doesn't "exist". To know about these things and on how to access the different kind of addresses the kernel can handle you really would have to look deeper into the kernel documentation. (Also a value that is not divisible by 4 is certainly a bad choice to expect an unsigned). - Jens Gustedt
This code is not attempting to access physical memory. It's attempting to access an invalid (unmapped) address in virtual memory. This is rather bad. :-) - R.. GitHub STOP HELPING ICE

3 Answers

12
votes

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.

6
votes

I suggest reading the chapter about memory in this book:

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

It's available online for free. Good stuff!

4
votes

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)