From: QuasiCodo on
I'm trying to manage my own virtual address space from my driver, but I
am having trouble when try to allocate, map and lock a subset of the VA
space. Here is what I am doing:

// Allocate our virtual address space
// Start at 1 TB and work our way down
va_space_size = MAX_VA_SPACE_SIZE;
do
{
va_space = MmAllocateMappingAddress (
pgallctr.va_space_size,
PGALLCTR_TAG);
if ( !va_space )
{
// Negoiate to smaller size
va_space_size >>= 1;
if ( va_space_size < MIN_VA_SPACE_SIZE )
{
error ("Cannot allocate VA space!\n");
goto Exit;
}
}
} while (!va_space);

>> I actually get 64 GB of VA space allocated -- which is plenty for my
>> purposes


>> I tried two different ways to allocate an MLD

// Allocate an MDL for the slot address
alloc_size = 128 * 1024 * 1024;
//mdl = IoAllocateMdl (ptr, alloc_size, FALSE, FALSE, NULL);
//if ( !mdl )
//{
// error ("IoAllcateMdl failed\n");
// goto Exit;
//}
low.QuadPart = 0;
high.QuadPart = 0xFFFFFFFFFFFFFFFF;
skip.QuadPart = PAGE_SIZE;
mdl = MmAllocatePagesForMdlEx (low, high, skip, alloc_size,
MmNonCached, 0);
if ( !mdl )
{
error ("MmAllocatePagesForMdlEx failed\n");
goto Exit;
}

>> I don't know if I need this following line

MmBuildMdlForNonPagedPool (mdl);

>> When I try to probe and lock, MmProbeAndLockPages() throws an
>> EXCEPTION_EXECUTION_HANDLER exception with status 0xC0000005,
>> which is an access violation.

__try
{
// Probe and lock pages
MmProbeAndLockPages (mdl, KernelMode, IoWriteAccess);
locked = TRUE;
}
__except(EXCEPTION_EXECUTE_HANDLER)
{
error ("MmProbeAndLockPages failed, status = 0x%x\n",
GetExceptionCode());
goto Exit;
}


I would appreciate any help or reference.

thx,

((&->

From: Scott Noone on
"QuasiCodo" <QuasiCodo_nospam_(a)Yahoo.com> wrote in message
news:uDcSYo$ULHA.5716(a)TK2MSFTNGP02.phx.gbl...
> I actually get 64 GB of VA space allocated -- which is plenty for my
> purposes

What, may I ask, are you trying to do exactly?

> I don't know if I need this following line
> MmBuildMdlForNonPagedPool (mdl);

No, you don't.

>When I try to probe and lock, MmProbeAndLockPages() throws an
>EXCEPTION_EXECUTION_HANDLER exception with status 0xC0000005,
>which is an access violation.

You don't need to probe and lock either, though it probably shouldn't raise.
Have you tried this with a checked kernel/hal and Verifier running?

You also might really want to describe the problem you're trying to solve,
there might be a better answer.

-scott

--
Scott Noone
Consulting Associate
OSR Open Systems Resources, Inc.
http://www.osronline.com
http://twitter.com/analyzev



"QuasiCodo" <QuasiCodo_nospam_(a)Yahoo.com> wrote in message
news:uDcSYo$ULHA.5716(a)TK2MSFTNGP02.phx.gbl...
> I'm trying to manage my own virtual address space from my driver, but I am
> having trouble when try to allocate, map and lock a subset of the VA
> space. Here is what I am doing:
>
> // Allocate our virtual address space
> // Start at 1 TB and work our way down va_space_size =
> MAX_VA_SPACE_SIZE;
> do
> {
> va_space = MmAllocateMappingAddress (
> pgallctr.va_space_size,
> PGALLCTR_TAG);
> if ( !va_space )
> {
> // Negoiate to smaller size
> va_space_size >>= 1;
> if ( va_space_size < MIN_VA_SPACE_SIZE )
> {
> error ("Cannot allocate VA space!\n");
> goto Exit;
> }
> }
> } while (!va_space);
>
> >> I actually get 64 GB of VA space allocated -- which is plenty for my
> >> purposes
>
>
> >> I tried two different ways to allocate an MLD
>
> // Allocate an MDL for the slot address
> alloc_size = 128 * 1024 * 1024;
> //mdl = IoAllocateMdl (ptr, alloc_size, FALSE, FALSE, NULL);
> //if ( !mdl )
> //{
> // error ("IoAllcateMdl failed\n");
> // goto Exit;
> //}
> low.QuadPart = 0;
> high.QuadPart = 0xFFFFFFFFFFFFFFFF;
> skip.QuadPart = PAGE_SIZE;
> mdl = MmAllocatePagesForMdlEx (low, high, skip, alloc_size,
> MmNonCached, 0);
> if ( !mdl )
> {
> error ("MmAllocatePagesForMdlEx failed\n");
> goto Exit;
> }
>
> >> I don't know if I need this following line
>
> MmBuildMdlForNonPagedPool (mdl);
>
> >> When I try to probe and lock, MmProbeAndLockPages() throws an
> >> EXCEPTION_EXECUTION_HANDLER exception with status 0xC0000005,
> >> which is an access violation.
>
> __try
> {
> // Probe and lock pages
> MmProbeAndLockPages (mdl, KernelMode, IoWriteAccess);
> locked = TRUE;
> }
> __except(EXCEPTION_EXECUTE_HANDLER)
> {
> error ("MmProbeAndLockPages failed, status = 0x%x\n",
> GetExceptionCode());
> goto Exit;
> }
>
>
> I would appreciate any help or reference.
>
> thx,
>
> ((&->
>

From: QuasiCodo on
On 9/14/2010 7:51 AM, Scott Noone wrote:
> "QuasiCodo" <QuasiCodo_nospam_(a)Yahoo.com> wrote in message
> news:uDcSYo$ULHA.5716(a)TK2MSFTNGP02.phx.gbl...
>> I actually get 64 GB of VA space allocated -- which is plenty for my
>> purposes
>
> What, may I ask, are you trying to do exactly?

Let me describe the problem. Under WS03, we are using
ExAllocatePoolWithTag()/ExFreePoolWithTag() to allocate/free pieces of a
data structure one page at a time. Over time the data structure in
memory grows. The problem is that when we try to deallocate the pages
belonging to the data structure, it takes a long, long time
(approximately 8 ms per ExFreePoolWithTag() call). For example, when we
have over 10 GB of memory allocated (slightly less than 3 million
pages), it takes over 7 hours just to free the memory. YIKES!

So we thought we would make things more efficient by allocating memory
chunks from a large VA space from which we allocate and free pages.
Once we are done with a chunk, we can free it with less overhead than
ExPoolFree().


>
>> I don't know if I need this following line
>> MmBuildMdlForNonPagedPool (mdl);
>
> No, you don't.

thx -- I didn't think I needed this, but there is no good examples in
the DDK on using these routines.

>
>> When I try to probe and lock, MmProbeAndLockPages() throws an
>> EXCEPTION_EXECUTION_HANDLER exception with status 0xC0000005,
>> which is an access violation.
>
> You don't need to probe and lock either, though it probably shouldn't
> raise. Have you tried this with a checked kernel/hal and Verifier running?

Not yet. I'll try verifier, but I'm not sure it will show me much.

I think the issue is that the pages I want to be allocated to the memory
chunk are not being assigned to the MDL in MmAllocatePagesForMdlEx(). I
guess there is fundamental concept I am missing. I need to allocate the
memory and then get it mapped into my VA, but there is nothing in the
descriptions of MmAllocateMappingAddress() and
MmMapLockedPagesWithReservedMapping() that tell me how to do it.
However, the MSDN weblink for MmMapLockedPagesWithReservedMapping()
indicates that I have to call IoAllocateMdl() and MmProbeAndLockPages()
before caling MmMapLockedPagesWithReservedMapping().

> You also might really want to describe the problem you're trying to
> solve, there might be a better answer.
>
> -scott
>