0
votes

I'm currently playing around with windows kernel driver in order to better understand windows internals. As a Toy Project, I wrote a kernel driver whose role is to allocate memory which can be shared between process.

An application can ask the driver to create a memory buffer of any size. The driver then create this buffer in KernelSpace with MmAllocatePagesForMdl, and then map this buffer in User mode with MmMapLockedPagesSpecifyCache. The resulting pointer is given back to the application which can directly write in it like in any ordinary buffer. Then another application can ask the driver to access this memory in order to read it (or even to write) ; the driver just has to call MmMapLockedPagesSpecifyCache on the already existing buffer in the context of the new process. Everything is working very good so far.

After this small succes, I wanted to create a bigger buffer in kernel space, and I hit a wall. An MDL can only manage up to "4Go - PAGE_SIZE".

My first idea was to create several MDL using MmAllocatePagesForMdl until I fulfill the size request, chain the MDL using the Next pointer, and then return the pointer to User Space using MmMapLockedPagesSpecifyCache. But MmMapLockedPagesSpecifyCache doesn't work with chained MDL, it only map in user space the first MDL.

Up to now, I haven't found a way to return more than 4Go of continuous virtual memory from kernel space in user space. The allocation in Kernel Space is not a problem since I use memory paged so the physical memory does not have to be continuous, but I can't find how to map those in continuous virtual memory to be used in User Space.

So am I being to greedy and it's impossible ? Or did I miss something in order to do it ?

For information, it's a 64 bit only driver and 64 bit applications, so no 32 bit limits here.

1

1 Answers

1
votes

So after looking everywhere, it's not possible using MDL. To have more than 4Go, I must create section in my driver. It's the equivalent of CreateFileMapping in User Mode. But I didn't want to rely on SharedMemory since I had some lock during memory allocation/mapping.

And then Alex gave me a wonderful advice on osr forum in order to solve my initial problem ; use CreateFileMapping with SEC_LARGE_PAGES option. Memory lock have disappeared and I'm as fast as with my driver, without all the problems which might be caused by such a development.