当前位置: 技术问答>linux和unix
mmap在指定地址读写数据
来源: 互联网 发布时间:2016-04-08
本文导语: 目前接触嵌入式linux的东西,想在地址0x1f000010读写数据。我用的dev/mem,open,mmap,memcpy等实现的。代码大致如下: void *init_addr = (void *)0x1f000010; void *mem; int fd; fd = open ("/dev/mem", O_RDWR); mem =(void *) ...
目前接触嵌入式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?
请高手指教!
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.
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.
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。