从Linux内核访问物理内存
我们可以通过一些内核代码访问任何物理内存吗?因为,我编写了一个只有 init_module 和 exit_module 的设备驱动程序。代码如下。
int init_module(void) {
unsigned char *p = (unsigned char*)(0x10);
printk( KERN_INFO "I got %u \n", *p);
return 0;
}
和一个虚拟的 exit_module ..问题是当我执行 lsmod 时计算机挂起.. 会发生什么?我应该获得某种权限来访问内存位置吗?
请解释一下..我是初学者!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要访问真实的物理内存,您应该使用 phys_to_virt 函数。如果是 io 内存(例如 PCI 内存),您应该仔细查看 ioremap。
整个主题非常复杂,如果您是初学者,我会建议一些内核/驱动程序开发书籍/文档。
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.
我建议阅读本书中有关内存的章节:
http://lwn.net/Kernel/LDD3/
可免费在线获取。好东西!
I suggest reading the chapter about memory in this book:
http://lwn.net/Kernel/LDD3/
It's available online for free. Good stuff!
在内核内部,内存仍然是虚拟映射的,只是与用户空间中的方式不同。
0x10 很可能位于保护页或其他内容中,以捕获空指针,因此当您触摸它时,它会在内核中生成未处理的页面错误。
通常这会导致 OOPS 而不是挂起(但可以将其配置为导致恐慌)。 OOPS 是一种意外的内核情况,在某些情况下可以恢复,但不一定会导致整个系统崩溃。通常它会终止任务(在本例中为 insmod)
您是否在加载了 GUI 的桌面 Linux 系统上执行此操作?如果您想修改内核,我建议您使用简单(即快速重新启动)的基于文本的发行版设置 Linux VM(Vmware、virtualbox 等)。你会让它崩溃一点,并且你希望它尽快重新启动。此外,通过使用基于文本的发行版,可以更容易地看到内核崩溃消息(哎呀或恐慌)
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)