阅读:3410回复:5
请教User Mode和Kernel Mode的内存使用问题 ???
各位大侠,小弟在用户态使用内核态分配的内存时,感觉对内存操作太慢了,不知是什么原因。
我是使用函数MmAllocateContiguousMemory分配一块连续内存,使用函数MmGetPhysicalAddress获得物理地址,再使用函数ZwMapViewOfSection映射到一虚拟地址空间(用户态)。 |
|
沙发#
发布于:2002-04-10 09:17
把你的代码贴来看看。
ZwMapViewOfSection要section handle, 你咋得到的 ? |
|
板凳#
发布于:2002-04-10 18:41
好象是有这个问题.
|
|
地板#
发布于:2002-04-10 21:43
|
|
地下室#
发布于:2002-04-10 21:45
能解决吗?大家帮想想办法。
PVOID MapInUserSpace( PHYSICAL_ADDRESS AddrPhysical, ULONG Size ) { ULONG VirtualAddress; ULONG length; PVOID PhysicalMemorySection; HANDLE hPhysicalMemory; NTSTATUS status; LARGE_INTEGER ViewBase; UNICODE_STRING PhysicalMemory_Unicode; OBJECT_ATTRIBUTES ObjectAttributes; RtlInitUnicodeString( &PhysicalMemory_Unicode, L\"\\\\Device\\\\PhysicalMemory\" ); InitializeObjectAttributes( &ObjectAttributes, &PhysicalMemory_Unicode, OBJ_CASE_INSENSITIVE, (HANDLE) NULL, (PSECURITY_DESCRIPTOR) NULL ); status = ZwOpenSection( &hPhysicalMemory, SECTION_ALL_ACCESS, &ObjectAttributes ); if (!NT_SUCCESS(status)) { KdPrint((DBG_NAME \"ERROR - Unable to obtain a handle to the physical memory\\n\")); return NULL; } status = ObReferenceObjectByHandle( hPhysicalMemory, SECTION_ALL_ACCESS, (POBJECT_TYPE) NULL, KernelMode, &PhysicalMemorySection, (POBJECT_HANDLE_INFORMATION)NULL ); if (!NT_SUCCESS(status)) { KdPrint((DBG_NAME \"ERROR - Unable to reference object by handle\\n\")); ZwClose( hPhysicalMemory ); return NULL; } // Let the OS pick an address VirtualAddress = (ULONG) NULL; // Initialize view base that will receive the physical mapped address ViewBase = AddrPhysical; length = Size; // Map the section status = ZwMapViewOfSection( hPhysicalMemory, (HANDLE) -1, &(PVOID)VirtualAddress, 0L, length, &ViewBase, &length, ViewShare, 0, PAGE_READWRITE | PAGE_NOCACHE ); if (!NT_SUCCESS(status)) { KdPrint((DBG_NAME \"ERROR - Unable to map view of section, status = 0x%x\\n\", status)); ZwClose( hPhysicalMemory ); return NULL; } /* Mapping the section above rounded the physical address down to the nearest 64K boundary. Now return a virtual address that sits where we want by adding in the offset from the beginning of the section. */ VirtualAddress += (ULONG)(AddrPhysical.QuadPart - ViewBase.QuadPart); return (PVOID)VirtualAddress; } |
|
5楼#
发布于:2002-04-11 13:20
shenhd,
look at here : http://www.driverdevelop.com/forum/viewthread.php?tid=710 a big help ! i have settled this problem. |
|