简体   繁体   English

Linux:创建 mmap()able 虚拟文件(例如 SPI 内存)

[英]Linux: create mmap()able virtual file (e.g. SPI memory)

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.我有一个 char 设备,可以访问外部 SPI memory,我想 mmap() 外部 memory 以便我可以从程序访问它,就好像它是正常的 ZCD69B4957F06CD818D7B3D691

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...如果我在 char 设备文件上使用通常的 mmap() 页面重新映射实现,它只会让我看到设备 memory 区域,而不是它的虚拟 char 文件...

Is there a trick to allow me to do that?有什么诀窍可以让我这样做吗?

TIA TIA

If the character device driver provided an mmap implementation, it would work.如果字符设备驱动程序提供了mmap实现,它就可以工作。 There's probably a good reason it doesn't:它不这样做可能有一个很好的理由:

Memory access instructions create memory transactions on the bus. Memory 访问指令在总线上创建 memory 事务。 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). SPI memory 不能以这种方式寻址(尽管 SPI controller 可能使用内存映射 I/O,这是针对其自己的寄存器级接口,而不是 ZCD69B4957F06CD828D917BF3D61980DE 内容) 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.我想你可以用 memory 总线接口构建一个 SPI memory controller,但你会失去 SPI 标准的设备独立性。

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.模拟 memory 区域是可能的(抓取 memory 的页面,将其标记为不可访问,并处理访问冲突(SIGBUS 和 SIGSEGV),但这将非常低效。有时你会发现虚拟机这样做,但性能非常坏的。

It sounds like you would need some sort of driver that translates the memory region accesses to commands sent through the character-oriented interface.听起来您需要某种驱动程序来将 memory 区域访问转换为通过面向字符的接口发送的命令。 This would probably be a pretty straight-forward block device driver.这可能是一个非常简单的块设备驱动程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM