Tech Support > Microsoft Windows > Drivers > question for AllocateCommonBuffer and How to send user mode memory data to kernel
question for AllocateCommonBuffer and How to send user mode memory data to kernel
Posted by paullee on March 4th, 2005


Hi all

A I have use AllocateCommonBuffer to allocate memory for
DMA use,I have four channel and need more then 20MB byte memory
,so I just want to ask which method is better ??

1 Allocate 20MB int one time then divide it up to use??
2 Allocate many time until 20 MB??


B I have to send the user data to kernel for DMA use,it will crash
down when this line run

RtlCopyMemory(pMPG->Message, (PULONG)
pLDI->DMABuffers[pMPG->DmaChannel].Virtual, pMPG->DataLen);

every time the bugcheck code is different,why??

The DMABuffers is allocate memory by AllocateCommonBuffer 。

AP use DevicIoCo

AP code

typedef struct _MPG_PARAM {
ULONG DmaChannel; // number of DMA Channel
ULONG DataLen
ULONG Message[1]; //ULONG *Message
} MPG_PARAM ,*PMPG_PARAM;

#define DATA_LEN (sizeof(MPG_PARAM)+1024*sizeof(unsigned char))

MpgParam = (MPG_PARAM*) malloc(DATA_LEN);

MpgParam.DmaChannel =0;
MpgParam.DataLen = 1024;

fread( MpgParam->Message ,sizeof(unsigned char),1024, FilePtr );

bResult = DeviceIoControl(hdevice, IOCTL_MPG_SEND_DATA, MpgParam,
DATA_LEN, MpgParam, DATA_LEN, &dwRet, NULL);


Kernel code :

KeAcquireSpinLock(&pLDI->DmaSpinLock, &oldIrql);

pMPG = (PMPG_PARAM) pIrp->AssociatedIrp.SystemBuffer;

RtlCopyMemory((PULONG)
pLDI->DMABuffers[(AUDIO_CHANNEL+pMPG->DmaChannel)].Virtual,pMPG->Message,pMPG->DataLen);

KeReleaseSpinLock(&pLDI->DmaSpinLock, oldIrql);


can anyone tell me how to send user mode memory data to kernel by
DeviceIoControl??

Thanks a lot

Posted by Stephan Wolf [MVP] on March 4th, 2005


On 3 Mar 2005 22:14:25 -0800, "paullee" <paullee@iei.com.tw> wrote:

Depends on whether the 20MB need to be physically contiguous. If yes,
then you have no choice but allocate all memory at once.

Otherwise, if you do not need all memory to be physically contiguous,
it is a much better idea to allocate smaller chunks of memory that sum
up to 20MB. This is better because physically contiguous memory is a
scarce system resource. Large blocks are even more scarce.

Not sure if I fully understand your bugcheck problem, but please make
sure you do not have your device do DMA transfers that outreach the
end of a *physical* memory block.

"How To Share Memory Between User Mode and Kernel Mode"
Microsoft Knowledge Base Article KB191840
http://support.microsoft.com/kb/q191840/


"Sharing Memory Between Drivers and Applications"
http://www.osronline.com/article.cfm?id=39
[Note that you first need to register yourself here at no cost.]

Stephan

Posted by paullee on March 10th, 2005


Thanks to Stephan ^^

I didn't need physically contiguous,I found it can be allocate 50MB
at once on the boot time,but it will fail where PC ready,even just
allocate 20MB only.

I have try to allocate it small and many times.


I am sorry,Some code is disappear, The code I have run very well
when I write console mode program, But it fail when I move to
windows MFC program,Every time the bugcheck code is different,
So I also didn't know what happen??

I will try to read this two article

Thanks a again

Best regards

Paul


Posted by Stephan Wolf [MVP] on March 10th, 2005


On 9 Mar 2005 18:30:31 -0800, "paullee" <paullee@iei.com.tw> wrote:

That's clear as memory gets more and more scattered the longer the
machine's uptime. Thus, chances are small to be able to allocate large
blocks of physically contiguous memory some time after system boot.

Stephan