来源于QNX IDE
实质就是将物理内存
physical映射到程序中,我们通过指针来取这个物理地址的内容;
这样做的好处是:大家可以共用这个物理地址的内容。
1、mmap_device_memory()函数
Map a device’s physical memory into a process’s address space
实质:将物理内存映射到程序中指针
#include <sys/mman.h>
void * mmap_device_memory( void * addr,
size_t len,
int prot,
int flags,
uint64_t physical );
Arguments:
1)addr
NULL, or a pointer to where you want to map the object in the calling process’s address space.
//可以写为0
2)len
The number of bytes you want to map into the caller’s address space. It can’t be 0.
//例如可以写为:0x1000,写一个很大的数2^12 ==4096
3)prot
The access capabilities that you want to use for the memory region being mapped. You can use a combination of at least the following protection bits, as defined in <sys/mman.h>:
访问内存的权限功能。
- PROT_EXEC — the region can be executed.
-
PROT_NOCACHE — disable the caching of the region (e.g. to access dual-ported memory).
//禁用区域缓存
- PROT_NONE — the region can’t be accessed.
- PROT_READ — the region can be read.
- PROT_WRITE — the region can be written.
4)flags
Specifies further information about handling the mapped region. You can use the following flag:
//可以置为0
-
MAP_FIXED — map the object to the address specified by
addr
. If this area is already mapped, the call changes the existing mapping of the area.A memory area being mapped with MAP_FIXED is first unmapped by the system using the same memory area. See
munmap()
for details. -
Use MAP_FIXED with caution. Not all memory models support it. In general, you should assume that you can MAP_FIXED only at an address (and size) that a call to
mmap()
without MAP_FIXED returned.
-
A memory area being mapped with MAP_FIXED is first unmapped by the system using the same memory area. See
munmap()
for details.
This function already uses
MAP_SHARED
ORed with
MAP_PHYS
(see
mmap()
for a description of these flags).
5)physical
The physical address of the memory to map into the caller’s address space.
//物理地址,例如:0x80000000
Returns:
The address of the mapped-in object, or MAP_FAILED if an error occurs (
errno
is set). // 返回值与参数1 addr 应该是一样的,所以,addr置0了。
例子:
/* Map in the physical memory; 0xb8000 is text mode VGA video memory */
ptr = mmap_device_memory( 0, len, PROT_READ|PROT_WRITE|PROT_NOCACHE, 0, 0xb8000 );
if ( ptr == MAP_FAILED ) {
perror( "mmap_device_memory for physical address 0xb8000 failed" );
exit( EXIT_FAILURE );
}