概述
目前接触嵌入式linux的东西,想在地址0x1f000010读写数据。我用的dev/mem,open,mmap,memcpy等实现的。代码大致如下:
void *init_addr = (void *)0x1f000010;
void *mem;
int fd;
fd = open ("/dev/mem", O_RDWR);
mem =(void *) mmap (init_addr, 20, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0
不知道这样写对不对啊?为什么mmap的返回值和init_addr不一样呢?mem=0x1f001000.那最后究竟是在地址0x1f001000还是0x1f000010进行的读写操作呢?
还有,void*指向的地址不是按4字节对齐的吗?为什么初始值是0xbb7ff4?
请高手指教!
|
如果需要精确指定map地址,需要MAP_FIXED,但这只是一个暗示或建议,有可能不成功:
1,以下复制了Linux man手册的部分内容:
SYNOPSIS
#include
void *mmap(void *addr, size_t len, int prot, int flags,
int fildes, off_t off);
The parameter flags provides other information about the handling of the mapped data.
The value of flags is the bitwise-inclusive OR of these options, defined in
:
Symbolic Constant Description
MAP_SHARED Changes are shared.
MAP_PRIVATE Changes are private.
MAP_FIXED Interpret addr exactly.
When MAP_FIXED is set in the flags argument, the implementation is informed that the
value of pa shall be addr, exactly. If MAP_FIXED is set, mmap() may return MAP_FAILED
and set errno to [EINVAL]. If a MAP_FIXED request is successful, the mapping estab-
lished by mmap() replaces any previous mappings for the processu2019 pages in the range
[pa,pa+len).
When MAP_FIXED is not set, the implementation uses addr in an implementation-defined
manner to arrive at pa. The pa so chosen shall be an area of the address space that the
implementation deems suitable for a mapping of len bytes to the file. All implementa-
tions interpret an addr value of 0 as granting the implementation complete freedom in
selecting pa, subject to constraints described below. A non-zero value of addr is taken
to be a suggestion of a process address near which the mapping should be placed. When
the implementation selects a value for pa, it never places a mapping at address 0, nor
does it replace any extant mapping.
2,以下引用UNPV2的共享内存相关部分:
For portability, MAP-FIXED should not be specified. If it is not specified, but addr is
not a null pointer, then it is implementation dependent as to what the implementation
does with addr. The nonnull value of addr is normally taken as a hint about where the
memory should be located. Portable code should specify addr as a null pointer and
should not specify MAP-FIXED.
最后
以上就是飘逸野狼为你收集整理的linux mmap读取新数据,mmap在指定地址读写数据__the_implementation_addr_MAP_FIXED_mapping__169IT.COM...的全部内容,希望文章能够帮你解决linux mmap读取新数据,mmap在指定地址读写数据__the_implementation_addr_MAP_FIXED_mapping__169IT.COM...所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复