阅读:1535回复:3
内存共享--救命阿!
我用DriverWorks开发驱动,想在内核和应用之间共享内存,先在内核分配内存块,供DMA缓存数据,应用程序取得该内存的首指针,再利用这个指针读出该块的数据。现在初步的实验是回传指针,生成框架之后,我只改了kernel的READ()和app的doRead()函数,kernel里的READ()函数如下:
NTSTATUS Pointer2Device::Read(KIrp I) { t << "Entering Pointer2Device::Read, " << I << EOL; KMemory Mem(I.Mdl()); //应用程序的MDL; PULONG pBuffer = (PULONG) Mem.MapToSystemSpace(); ULONG BufferSize = 128; PVOID pImageMemory = ExAllocatePoolWithTag(NonPagedPool, BufferSize, 'MpaM'); if(!pImageMemory) { t << "status = STATUS_UNSUCCESSFUL" << EOL; } KMemory ImageMdl = KMemory(pImageMemory,BufferSize); PUCHAR pp = (PUCHAR)ImageMdl.VirtualAddress(); t << "ImageMdl.VirtualAddress = " << (ULONG)pp << EOL; for(int i = 0; i < 16; i++) //写数0、2、4、6... *pp++ = 2 * i; if(NT_SUCCESS(ImageMdl.LockPages(IoReadAccess,UserMode))) //锁定 { t << "lockpages sucessed" << EOL; } else { t << "lockpages unsucessed" << EOL; } ImageMdl.SetPageArray(); PVOID m_UserSpace = (PVOID)ImageMdl.MapToUserSpace(); t << "ImageMdl.PointerOfUserSpace = " << (ULONG)m_UserSpace << EOL; *pBuffer = (ULONG)(m_UserSpace); I.Information() = 4; return I.PnpComplete(this, STATUS_SUCCESS); } 应用程序doRead()如下: void doRead(int n) { unsigned long *buf; ULONG nRead; ULONG i; buf = (unsigned long *)malloc(n); if (buf == NULL) { printf("Failed to allocate buffer for read"); Exit(1); } // Read data from driver printf("Reading from device - "); ReadFile(hDevice, buf, n, &nRead, NULL); printf("%d bytes read from device (%d requested).n", nRead, n); // Print what was read printf("the pointer form kernel is :n"); ULONG pToKernelBuffer; pToKernelBuffer = *buf; printf("pToKernelBuffer = %xn",pToKernelBuffer); for(i = 0; i < 16; i++) { printf("%u ",*(unsigned char *)(pToKernelBuffer++)); } printf("n"); free(buf); } doRead()能够读到m_UserSpace = (PVOID)ImageMdl.MapToUserSpace()的指针,也能够把写入缓冲区的0、2、4、6、、、读出来。但DriverMonitor锁定缓冲页时显示lockpages unsucessed,那我没有锁定缓冲区就MapToUserSpace是不是有错误呢.而最严重的是如果我把生成MDL的程序放到其他例程,例如把PVOID pImageMemory = ExAllocatePoolWithTag(NonPagedPool, BufferSize, 'MpaM'); KMemory ImageMdl = KMemory(pImageMemory,BufferSize)放到onstartdivece ()时就会篮平(该在那里生成呢?onstartdevice()还是driverentry()),我肯定是要在其他地方生存MDL,所以很郁闷,请指点下这关于内核分配内存块、回传指针的编程,谢谢!告诉我怎么样给分,多多都给! 另外如果READ()函数这样生成MDL时显示能够生成MDL,但是执行MapToUserSpace会篮平。 pImageMemory = ExAllocatePoolWithTag(NonPagedPool, 128, 'MpaM'); if(!pImageMemory) { status = STATUS_UNSUCCESSFUL; return status; } int MdlSize = KMemory::RequiredMdlStorage(pImageMemory, 128); p = (PMDL) new (NonPagedPool) char[MdlSize]; if ( p == NULL) { ExFreePool(pImageMemory); pImageMemory = NULL; status = STATUS_UNSUCCESSFUL; return status; } ImageMdl = KMemory(pImageMemory,128,p); if ((PMDL)ImageMdl == NULL) { ExFreePool(pImageMemory); delete p; pImageMemory = NULL; status = STATUS_UNSUCCESSFUL; return status; } t << "ImageMdl = KMemory(pImageMemory,128,p) successn"; PUCHAR pp = (PUCHAR)ImageMdl.VirtualAddress(); t << "ImageMdl.VirtualAddress = " << (ULONG)pp << EOL; for(int i = 0; i < 16; i++) //写数0、2、4、6... *pp++ = 2 * i; if(NT_SUCCESS(ImageMdl.LockPages(IoReadAccess,UserMode))) //锁定 { t << "lockpages sucessed" << EOL; } else { t << "lockpages unsucessed" << EOL; } ImageMdl.SetPageArray(); PVOID m_UserSpace = (PVOID)ImageMdl.MapToUserSpace(); t << "ImageMdl.PointerOfUserSpace = " << (ULONG)m_UserSpace << EOL; *pBuffer = (ULONG)(m_UserSpace); I.Information() = 4; return I.PnpComplete(this, STATUS_SUCCESS); } 谢谢! [编辑 - 7/1/04 by chylml] |
|
沙发#
发布于:2004-07-01 16:57
ImageMdl.LockPages(IoReadAccess,UserMode)?把内核的内存锁到应用层?
|
|
|
板凳#
发布于:2004-07-01 18:22
to楼上:是啊,我是想锁定,再回传指针给app啊。你的意思是怎么样啊。
|
|
地板#
发布于:2004-07-02 09:52
这样的问题我至少回答了3次,在论坛中搜索一下
|
|
|