2

I need to access physical memory in user mode for a platform running Windows Embedded Compact 2013. I found a article which does that. The memory mapping is done in kernel mode driver and the address is returned to the user mode program.

In the kernel driver

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;
           .....
}

The function GetVirtualMemory is as follows

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;
 }

The code is mapping physical memory 0xe000a000 (GPIO base address in zynq chip). The code fails in VirtualCopyEx with the error 87 (Invalid parameter). But the code works with base physical address at 0x81F00000 (this address was used by the original programmer). My question is how to enable the mapping for GPIO address. I am also willing to try any other method possible.

1 Answer 1

3

After long struggle, I found the answer.

 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;
}

This will return to the caller on DeviceIoControl call a virtual address mapped to the physical address of the device base address.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.