linux mmap读取新数据,mmap在指定地址读写数据__the_implementation_addr_MAP_FIXED_mapping__169IT.COM...

本文探讨了在嵌入式Linux中使用/dev/mem和mmap函数对地址0x1f000010进行读写操作的过程,涉及MAP_SHARED和MAP_FIXED标志的区别,以及内存对齐问题。作者还解析了Linux手册中关于内存映射的官方指导,并提供了UNPV2共享内存的参考内容。
摘要由CSDN通过智能技术生成

目前接触嵌入式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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值