简体   繁体   English

如何将 mmap() 64 位地址转换为 32 位进程?

[英]How to mmap() 64-bit address into 32-bit process?

I'm trying to map device memory residing on 64-bit address into 32-bit process on 64-bit OS.我正在尝试将驻留在 64 位地址上的设备内存映射到 64 位操作系统上的 32 位进程。 I'm using the following lines我正在使用以下几行

baseaddr = addr & ~(sysconf(_SC_PAGE_SIZE) - 1);
fd = open("/dev/mem", O_RDONLY | O_SYNC);
base_ptr = mmap(0, 4096, PROT_READ, MAP_PRIVATE, fd, baseaddr);

baseaddr is uint64_t and is higher than 4GB. baseaddr 是 uint64_t 并且大于 4GB。

and I compile with -D_FILE_OFFSET_BITS=64 .我用-D_FILE_OFFSET_BITS=64编译。 When I run the program it returns EINVAL .当我运行程序时,它返回EINVAL It worked before without the -D_FILE_OFFSET_BITS=64 , but it'd only use the lower 32-bits of baseaddr - that I conclude by output of pmap -d showing the lower 32-bits of my desired address.它以前在没有-D_FILE_OFFSET_BITS=64情况下工作,但它只使用 baseaddr 的低 32 位 - 我通过 pmap -d 的输出显示我所需地址的低 32 位得出结论。

Any ideas what am I doing wrong?任何想法我做错了什么?

You should be using mmap64 here.您应该在这里使用mmap64 The address has to be mapped into an area that a 32-bit process can use.地址必须映射到 32 位进程可以使用的区域。 However, I strongly advise that you get a true 64-bit version of this application.但是,我强烈建议您获得此应用程序的真正 64 位版本。 You're heading down a rabbit-hole here and there's a lot of rabbit-poo in that hole, if you catch my drift ...你正朝着这里的一个兔子洞前进,那个洞里有很多兔子便便,如果你注意到我的漂移......

Ok my final solution was to add mixed 32/64 assembly code that switches to long mode, loads long registers with desired values and then syscalls without any sort of wrapper and switches back to protected.好的,我的最终解决方案是添加混合的 32/64 汇编代码,该代码切换到长模式,加载具有所需值的长寄存器,然后在没有任何类型的包装器的情况下进行系统调用并切换回受保护。 Works like a charm.奇迹般有效。

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

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