阅读:1496回复:0
物理内存访问异常
刚写了一个访问硬件寄存器的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; } |
|