在
X86和ARM架构的CPU中,wince访问系统内存的方法随程序所属模式层次的不同而有所区别.
1.在系统内核模式下(kernel mode),在OAL层访问,只需要在OEMAddressTable 中做静态的虚实地址映射就可以了.例如X86架构的映射表格式如下:
; OEMAddressTable defines the mapping between Physical and Virtual Address // 定义4GB的虚拟地址和512MB存储的映射关系
; o MUST be in a READONLY Section
; o First Entry MUST be RAM, mapping from 0x80000000 -> 0x00000000
; o each entry is of the format ( VA, PA, cbSize )
; o cbSize must be multiple of 4M
; o last entry must be (0, 0, 0)
; o must have at least one non-zero entry
; RAM 0x80000000 -> 0x00000000, size 64M //把物理地址为0x00000000映射到虚拟地址为 0x80000000 处
dd 80000000h, 0, 04000000h
; FLASH and other memory, if any
; dd FlashVA, FlashPA, FlashSize
; Last entry, all zeros
dd 0 0 0
2.在驱动或应用程序(user mode)中访问RAM,既可以通过OEMAddressTable+VirtualCopy方式,也可以直接用MmMapIoSpace函数建立物理地址到当前进程虚拟地址的映射关系.
经过OEMAddressTable,实现的只是CPU物理地址到OS内核层虚拟地址的一次映射,如果需要在普通的应用程序中访问内存,还要再用VirtuaAlloc+VirtualCopy做一个内核到当前进程的二次映射(有一种情况例外,就是你的OS被配置成Full Kernel Mode,这时任何应用程序都可以访问OS内核地址).
简单说明几个关键函数:
VirtualAlloc用于在当前进程的虚拟地址空间中保留或者提交空间,在保留时以64KB为单位,提交时以4KB为单位。其函数原型为
1.在系统内核模式下(kernel mode),在OAL层访问,只需要在OEMAddressTable 中做静态的虚实地址映射就可以了.例如X86架构的映射表格式如下:
; OEMAddressTable defines the mapping between Physical and Virtual Address // 定义4GB的虚拟地址和512MB存储的映射关系
; o MUST be in a READONLY Section
; o First Entry MUST be RAM, mapping from 0x80000000 -> 0x00000000
; o each entry is of the format ( VA, PA, cbSize )
; o cbSize must be multiple of 4M
; o last entry must be (0, 0, 0)
; o must have at least one non-zero entry
; RAM 0x80000000 -> 0x00000000, size 64M //把物理地址为0x00000000映射到虚拟地址为 0x80000000 处
dd 80000000h, 0, 04000000h
; FLASH and other memory, if any
; dd FlashVA, FlashPA, FlashSize
; Last entry, all zeros
dd 0 0 0
2.在驱动或应用程序(user mode)中访问RAM,既可以通过OEMAddressTable+VirtualCopy方式,也可以直接用MmMapIoSpace函数建立物理地址到当前进程虚拟地址的映射关系.
经过OEMAddressTable,实现的只是CPU物理地址到OS内核层虚拟地址的一次映射,如果需要在普通的应用程序中访问内存,还要再用VirtuaAlloc+VirtualCopy做一个内核到当前进程的二次映射(有一种情况例外,就是你的OS被配置成Full Kernel Mode,这时任何应用程序都可以访问OS内核地址).
简单说明几个关键函数:
VirtualAlloc用于在当前进程的虚拟地址空间中保留或者提交空间,在保留时以64KB为单位,提交时以4KB为单位。其函数原型为
LPVOID VirtualAlloc(
LPVOID
lpAddress, // 分配虚拟地址的起始指针
DWORD
dwSize, // 大小,以字节为单位
DWORD
flAllocationType, // 类型,设为
MEM_RESERVE
DWORD
flProtect // 存取保护,设为
PAGE_NOACCESS
);
VirtualCopy 用来绑定物理地址到静态映射虚拟地址:
BOOL VirtualCopy(
LPVOID
lpvDest, // 虚拟目的地址指针,接受
VirtualAlloc的返回值
LPVOID
lpvSrc, // 源物理地址指针
DWORD
cbSize, // 大小必须与虚拟地址相同
DWORD
fdwProtect // 存取保护类型
);
这里需要注意的是
fdwProtect 参数。如果是驱动程序访问,需要设置为
PAGE_NOCACHE
,以访问无缓存段虚拟地址。如果映射的物理地址范围在
0x1FFFFFFF
之上,必须使用
PAGE_PHYSICAL
,此时必须把
lpvSrc 右移八位,实现地址对齐。(这是由内核中 VirtualCopy 的实现决定的,在那个函数中会判断如果是 PAGE_PHYSICAL 就将 PHYSADDR 左移 8 位移回来,源代码位于 private/winceos/coreos/nk/kernel 目录下的 virtmem.c中的
DoVirtualCopy )
MmMapIoSpace 用来把物理地址直接映射到与进程无关的虚拟地址上。函数原型为
PVOID MmMapIoSpace(
PHYSICAL_ADDRESS
PhysicalAddress,
ULONG
NumberOfBytes,
BOOLEAN
CacheEnable
);
一个使用 VirtualAlloc+Copy 的例子:把物理地址为 0x10000000 的单元映射到虚拟地址空间中。
#include <windows.h>
#define PHYSADDR ((PVOID)0x10000000)
// PHYSADDR is the physical address of the peripheral
// registers
#define SIZE (4800*4)
LPVOID lpv;
BOOL bRet;
lpv = VirtualAlloc(0, SIZE, MEM_RESERVE, PAGE_NOACCESS);
// For a user mode driver, always leave the first
// parameter 0 and use only the flags MEM_RESERVE
// and PAGE_NOACCESS Check the return value: lpv == 0
// is an error
printf(TEXT("VirtualAlloc reservation @%8.8lx/r/n"), lpv);
bRet = VirtualCopy(lpv, PHYSADDR>>8, SIZE, PAGE_READWRITE | PAGE_NOCACHE | PAGE_PHYSICAL);
// The lpv parameter is the virtual address returned
// by VirtualAlloc().
// Always use PAGE_NOCACHE */
// Check the return value: bRet ==0 is an error */
printf(TEXT("VirtualCopy returned: %d/r/n"), bRet);
// At this point lpv is a virtual address which maps
// the I/O registers
// at PHYSADDR for SIZE bytes */