从linux内核访问物理内存
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7894160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
accessing physical memory from linux kernel
提问by raj
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.
我们可以通过一些内核代码访问任何物理内存吗?因为,我写了一个只有 init_module 和 exit_module 的设备驱动程序。代码如下。
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?
和一个虚拟的 exit_module .. 问题是当我执行 lsmod 时计算机挂了 .. 会发生什么?我应该获得访问内存位置的某种许可吗?
kindly explain.. I'm a beginner!
请解释..我是初学者!
采纳答案by flolo
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.
要访问真正的物理内存,您应该使用 phys_to_virt 函数。如果是 io 内存(例如 PCI 内存),您应该仔细查看 ioremap。
This whole topic is very complex, if you are a beginner I would suggest some kernel/driver development books/doc.
整个主题非常复杂,如果您是初学者,我会推荐一些内核/驱动程序开发书籍/文档。
回答by hochl
I suggest reading the chapter about memory in this book:
我建议阅读本书中关于记忆的章节:
It's available online for free. Good stuff!
它可以免费在线获得。好东西!
回答by MarkR
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.
有可能 0x10 位于保护页面或其他内容中,以捕获空指针,因此当您触摸它时,它会在内核中生成未处理的页面错误。
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)
通常这会导致 OOPS 而不是挂起(但它可以配置为导致恐慌)。OOPS 是一种意外的内核状况,在某些情况下可以从中恢复,并不一定会导致整个系统瘫痪。通常它会终止任务(在本例中为 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)
您是否在加载了 GUI 的桌面 Linux 系统上执行此操作?我建议你设置一个 Linux VM(Vmware、virtualbox 等),如果你想修改内核,那么你可以使用基于文本的简单(即快速重启)分发版。您将使其崩溃并希望它尽快重新启动。同样通过使用基于文本的分发,更容易看到内核崩溃消息(糟糕或恐慌)