kuangnuzhiren@163.com wrote:
So can I get a conclusion : if my code is paged out to the disk, when it is needed to execute, the code can be paged back, then my code is pageable, otherwise it is nonpageable?
The problem is that there are two different concepts involved here, and
there is a tendency to be a little loose with the terminology.
At compile time, you can use compiler directives to tell the compiler
whether a particular function is allowed to be paged out or not. In
that sense, a function is either pageable or non-pageable. Similarly,
when you call ExAllocatePool, you can specify whether the memory you get
back is allowed to be paged out or not. Same concept.
When your code runs, the current IRQL determines whether your code can
HANDLE a page fault or not. If you are running at PASSIVE_LEVEL, and
your code touches a piece of memory that is paged out (code or data),
then everything still works fine. Your thread gets suspended until the
page fault is handled and the code or data is brought back in, at which
point you continue running.
But if you are running at a raised IRQL, like DISPATCH_LEVEL, and your
code touches a piece of memory that is paged out, you get a blue screen
(either IRQL_NOT_LESS_OR_EQUAL or PAGE_FAULT_IN_NONPAGED_AREA).
One of the things that makes this tricky is that pageable code is not
necessarily paged out. So, you can be at a raised IRQL and call into a
function that is marked at pageable, and it might work just fine, most
of the time. Sooner or later, however, the stars will be in
misalignment so that your function really is paged out, and you’ll get a
BSOD. Another tricky thing is that it isn’t always obvious what leads
to a raised IRQL. If you grab a spinlock, you are at a raised IRQL. If
you are in an IRP completion routine, you are at a raised IRQL.
So, here’s the takeaway. For each function and data item, you need to
ask yourself, “is there a possibility that I will need to call this
function or access this piece of data at a raised IRQL?” If the answer
is “yes”, then you must make that function or data item non-pageable.
Otherwise, you can make it pageable (although it isn’t strictly
necessary to do so).
–
Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.