yuchongjike
驱动牛犊
驱动牛犊
  • 注册日期2010-07-08
  • 最后登录2016-01-09
  • 粉丝0
  • 关注0
  • 积分4分
  • 威望21点
  • 贡献值0点
  • 好评度0点
  • 原创分0分
  • 专家分0分
阅读:1496回复:0

物理内存访问异常

楼主#
更多 发布于:2010-07-08 16:25
刚写了一个访问硬件寄存器的NT驱动,在调试内存读写那一块时,总是提示:映射内存错误

_In_Mem_32(0x400)
0x400地址是串口1的内存地址

//--------------------------------------------------------------------------
//打开物理内存,并返回Handle
#pragma PAGEDCODE
HANDLE _OpenPhyMem()
{
    NTSTATUS status;
    HANDLE hMem;
    UNICODE_STRING MemDevName;
    OBJECT_ATTRIBUTES obj_attr;
    
    RtlInitUnicodeString(&MemDevName,L"\\Device\\PhysicalMemory");
    InitializeObjectAttributes(&obj_attr,&MemDevName,OBJ_CASE_INSENSITIVE,NULL,NULL);
    status = ZwOpenSection(&hMem,SECTION_ALL_ACCESS,&obj_attr);
    if(!NT_SUCCESS(status))
    {
        KdPrint(("Open memory device error\n"));
        return 0;
    }
    return hMem;
}
//--------------------------------------------------------------------------
//从内存读一个32位值
#pragma PAGEDCODE
ULONG _In_Mem_32(ULONG MemAddr)
{
    NTSTATUS status;
    ULONG Value = 0xFFFFFFFF;
    PHYSICAL_ADDRESS phyAddr;
    ULONG *pVirMemAddr;
    ULONG memSize = 0x0F;
    HANDLE hLocal;
    HANDLE hMem;

    hMem = _OpenPhyMem();
    if(!hMem)
    {
        KdPrint(("Can not open physical memory\n"));
        return Value;
    }

    hLocal = NtCurrentProcess();
    phyAddr.u.LowPart = MemAddr;
    phyAddr.u.HighPart = 0x00;
    status = ZwMapViewOfSection(hMem,hLocal,(VOID**)(&pVirMemAddr),0,memSize,&phyAddr,&memSize,ViewShare,0,PAGE_READWRITE | PAGE_NOCACHE);
    if(!NT_SUCCESS(status))
    {
        ZwClose(hMem);
        KdPrint(("Map physical memory eror:%X\n",status)); [color=#FF0000]//执行到这里报错了.............[/color]        return Value;
    }
    Value = pVirMemAddr[0];
    KdPrint(("Value=%X,Buf=%X%X\n",Value,pVirMemAddr[0],pVirMemAddr[1]));
    ZwUnmapViewOfSection(hLocal,pVirMemAddr);
    ZwClose(hMem);
    return Value;
}
游客

返回顶部