将多个数据数组映射到任意固定内存地址 [英] Mapping multiple data arrays to arbitrary fixed memory addresses

查看:122
本文介绍了将多个数据数组映射到任意固定内存地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在64位Linux机器上开发一个程序,该程序需要将多个任意长度的数据数组映射到我无法控制的固定内存地址.我以为带有MAP_FIXED和MAP_ANONYMOUS的mmap()是可行的方法,例如:

I'm working on a program on a 64-bit Linux machine that needs to map multiple data arrays, of arbitrary length, to fixed memory addresses over which I have no control. I thought mmap() with MAP_FIXED and MAP_ANONYMOUS was the way to go, for example:

mmap((void *) 0x401000, 0x18e, PROT_NONE, MAP_ANONYMOUS | MAP_FIXED, -1, 0);

但是,每次我调用此函数时,它都会返回MAP_FAILED.我已将 fd 设置为-1,我知道某些系统需要此地址,该地址是我的页面大小(4096)的倍数,并且长度是非负数,所以我想知道我的系统是否已经使用了0x401000;我还尝试了0x402000、0x403000和0x404000,但结果相同.

However, every time I call this function, it returns MAP_FAILED. I've set fd to -1, which I know is required by some systems, the address is a multiple of my page size (4096), and the length is non-negative, so I'm wondering if perhaps 0x401000 is already being used by my system; I've also tried 0x402000, 0x403000, and 0x404000 with the same result.

关于mmap()是否缺少我的信息,或者是否有办法找出此地址上已有的信息?更好的是,由于我无法控制需要的地址,是否有更好的方法来避免遇到现有的映射?

Is there something I'm missing about mmap(), or is there a way to find out what's already at this address? Even better, since I can't control which addresses I'll need, is there some better way to avoid running into existing mappings?

编辑:检查errno后,我发现获取的代码是无效的参数,因此,根据手册页所述,我们不喜欢addr,length,或偏移(例如,它们太大或在页面边界上未对齐)."不过,我还不能弄清楚是哪个问题.

Edit: After checking errno, I found that the code I'm getting is an invalid argument, so, according to the man pages, "We don't like addr, length, or offset (e.g., they are too large, or not aligned on a page boundary)." I can't yet figure out which one is the problem, though.

推荐答案

鉴于0x400000似乎是我的amd64 Debian系统上的进程的.text地址(x86是不同的),我怀疑您之所以失败,是因为您试图按照您的想法映射现有的内容.不确定(以编程方式)确定要映射/可用的段的最佳方法,但是您可以首先检查/proc/<pid>/maps中是否存在许多典型"过程,以查看系统如何将地址范围分配给程序和库.地址空间随机化还会在其中增加一些额外的皱纹...

Given that 0x400000 seems to be the .text address for processes on my amd64 Debian systems (x86 is different), I suspect you're getting the failure because you're trying to map over something existing, as you thought. Not sure the best way to (programatically) determine what segments are mapped/available, but you can start by examining /proc/<pid>/maps for a number of 'typical' processes to see how your system allocates address ranges to programs and libraries. Address space randomization throws some extra wrinkles in there, as well...

这篇关于将多个数据数组映射到任意固定内存地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆