Linux:创建可 mmap() 的虚拟文件(例如 SPI 内存)
我有一个 char 设备,可以访问外部 SPI 存储器,并且我想 mmap() 外部存储器,以便我可以从程序访问它,就像它是普通存储器一样。
如果我在 char 设备文件上使用通常的 mmap() 页面重新映射实现,它只会使我能够看到设备内存区域,而不是其虚拟 char 文件...
有什么技巧可以让我做到这一点吗?
TIA
I have a char device which enables access to an external SPI memory, and I'd like to mmap() the external memory so that I can access it from a program as if it were normal memory.
If I use the usual mmap() page remapping implementation on the char device file, it just enables me to see a device memory region, not its virtual char file...
Is there a trick to allow me to do that?
TIA
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果字符设备驱动程序提供了
mmap
实现,那么它就可以工作。可能有一个很好的理由不这样做:内存访问指令在总线上创建内存事务。 SPI 存储器不能以这种方式寻址(尽管 SPI 控制器可能使用存储器映射 I/O,但这是针对其自己的寄存器级接口,而不是存储器内容)。我想,您可以构建一个带有内存总线接口的 SPI 内存控制器,但您会失去 SPI 标准的设备独立性。
模拟内存区域是可能的(抓取一页内存,将其标记为禁止访问,并处理访问违规(SIGBUS 和 SIGSEGV),但这效率非常低。有时您会发现虚拟机这样做,但性能非常低坏的。
If the character device driver provided an
mmap
implementation, it would work. There's probably a good reason it doesn't:Memory access instructions create memory transactions on the bus. An SPI memory is not addressable that way (although the SPI controller may use memory-mapped I/O, that's for its own register-level interface, not the memory content). You could build an SPI memory controller with a memory bus interface, I suppose, but you'd lose the device-indepedence of the SPI standard.
Emulating a memory region is possible (grab a page of memory, mark it for no access, and deal with the access violations (SIGBUS and SIGSEGV), but that would be horribly inefficient. Sometimes you find virtual machines doing this, but performance is very bad.
听起来您需要某种驱动程序来将内存区域访问转换为通过面向字符的接口发送的命令。这可能是一个非常简单的块设备驱动程序。
It sounds like you would need some sort of driver that translates the memory region accesses to commands sent through the character-oriented interface. This would probably be a pretty straight-forward block device driver.