我需要在运行Windows Embedded Compact 2013的平台上以用户模式访问物理内存。我发现了一篇文章。内存映射在内核模式驱动程序中完成,地址返回给用户模式程序。
在内核驱动程序中
BOOL BTN_IOControl(DWORD context, DWORD code, UCHAR *pInBuffer, DWORD inSize, UCHAR *pOutBuffer,DWORD outSize, DWORD *pOutSize)
{
PDWORD tValue = (PDWORD)pOutBuffer;
switch(code)
{
case IOCTL_MAP_MEMORY:
*tValue = GetVirtualAddress();
*pOutSize = 4;
break;
.....
}
GetVirtualMemory函数如下
LPOID GetVirtualAddress()
{
volatile DWORD sDevPhysAddr = 0xe000a000;
volatile DWORD dwSize = PAGE_SIZE;
LPVOID lpUserAddr;
volatile ULONG SourceSize;
volatile ULONG SourcePhys;
void* pvProcess = (void*)GetCallerVMProcessId();
SourcePhys = sDevPhysAddr & ~(PAGE_SIZE - 1);
SourceSize = PAGE_SIZE;
RETAILMSG(1, (L"Address: %08x Size: %08x.\r\n", SourcePhys, SourceSize));
lpUserAddr = (LPDWORD)VirtualAllocEx(pvProcess, 0, SourceSize, MEM_RESERVE, PAGE_NOACCESS);
if (lpUserAddr == NULL) {
RETAILMSG(1, (L"VirtualAllocEx failed. GetLastError %d.\r\n", GetLastError()));
return NULL;
}
if (!VirtualCopyEx(pvProcess, lpUserAddr, GetCurrentProcess(), (PVOID)
/*(*/SourcePhys/* >> 8)*/, SourceSize,
/*PAGE_PHYSICAL | */PAGE_READWRITE | PAGE_NOCACHE)) {
RETAILMSG(1, (L"VirtualCopyEx failed. Error %d.\r\n", GetLastError()));
return NULL;
}
RETAILMSG(1, (L"Before round up lpUserAddr=0x%x\r\n", lpUserAddr));
lpUserAddr = (LPVOID)((ULONG)lpUserAddr + (sDevPhysAddr & (PAGE_SIZE - 1)));
shared_mem = lpUserAddr;
RETAILMSG(1, (L"After round up gUserAddr=0x%x\r\n", shared_mem));
return lpUserAddr;
}
代码映射物理内存0xe000a000(zynq芯片中的GPIO基地址)。 VirtualCopyEx中的代码失败,错误为87(参数无效)。但代码使用基本物理地址0x81F00000(此地址由原始程序员使用)。我的问题是如何启用GPIO地址的映射。我也愿意尝试任何其他方法。
答案 0 :(得分:3)
经过长时间的斗争,我找到了答案。
DWORD GetVirtualAddress()
{
HANDLE hCurrentProcess;
HANDLE hCallerProcess;
DWORD pAddr;
PHYSICAL_ADDRESS pa;
hCurrentProcess = (HANDLE)GetCurrentProcess();
hCallerProcess = (HANDLE)GetDirectCallerProcessId();
pa.QuadPart = 0xe000a000;
pGpioRegs = MmMapIoSpace(pa, sizeof(XILINX_GPIO_REGS), FALSE);
if (!pGpioRegs)
RETAILMSG(1, (L"Cannot map the gpio register.\r\n"));
pAddr = (DWORD)VirtualAllocCopyEx(hCurrentProcess, hCallerProcess, pGpioRegs,
PAGE_SIZE, PAGE_READWRITE | PAGE_NOCACHE);
if (!pAddr)
RETAILMSG(1, (L"VirtualAllocCopy: Error: %d.\r\n", GetLastError()));
return pAddr;
}
这将返回给DeviceIoControl上的调用者调用映射到设备基址的物理地址的虚拟地址。