Pageable kernel-mode drivers
By default, the kernel loader will load all driver executables and any global data that you may have defined in your driver into nonpaged memory. Therefore, if you want your driver to reside in nonpaged memory, there is nothing further you need to do besides compiling, linking, and loading the driver.
Furthermore, the kernel loads the entire driver executable (and any associated dynamic link libraries) all at once, before invoking any driver initialization routines. Although it may not make much sense to you at this time, after loading the executable into memory, the kernel loader closes the executable file, allowing a user to delete even the currently executing driver image.
It is possible to specify to the loader the portions of your driver that you wish to make pageable. This can be done by using the following compiler directive in your driver code:
•ifdef ALLOC_PRAGMA
•pragma alloc_text(PAGE, function_namel)
•pragma alloc_text(PAGE, function_name2)
// You can list additional functions at this point just as the two
// functions are listed above ...
•endif // ALLOC_PRAGMA
Be careful, though, that you never allow any routine that could possibly be invoked at a high IRQL to be paged out. File system drivers can never allow any code or data to be paged out that might be required to satisfy page fault requests from the NT Virtual Memory Manager.
It is also possible for a kernel-mode driver to determine at run-time whether certain sections of driver code and/or data should be paged out or locked into memory. To do this, the driver must perform the following actions:
(Note: The functions referenced in a pragma statement must he defined in the same compilation unit as the pragma.)
• To make a code section pageable, use the following compiler directive in your code:
#ifdef ALLOC_PRAGMA
#pragma alloc_text(PAGExxxx, function_namel)
#pragma alloc_text(PAGExxxx, function_name2)
#endif
where xxxx is an optional, four-character, unique identifier for the driver's pageable section.
• To make a data section pageable, use the following compiler directive in your code:
#ifdef ALLOC_PRAGMA
#pragma data_seg(PAGE)...
// Define your pageable data section module here.
#pragma data_seg() // Ends the pageable data section.
• Invoke MmLockPagableCodeSection() and MmLockPagableCodeSectionByHandle( ) to lock code sections that were marked as pageable in memory.
• Invoke MmLockPagableDataSectionf) and MmLockPagableDataSectionByHandle( ) to lock data sections that were marked as pageable.
• Invoke MmUnlockPagableImageSection() to unlock any code or datasection that may have been locked using the functions listed above.
There are two additional routines provided by the VMM that you should be aware of (and look up in the DDK documentation) if you wish to page out the entire driver or reset paging attributes back to their original settings:
MmPageEntireDriver()
This routine will make the entire driver pageable, overriding any section page attributes that were declared earlier using compiler directives.
MmResetDriverPaging()
This function will reset the paging attributes back to the initially declared attributes.
Finally, to automatically have the Memory Manager discard sections of code that you won't need once the driver has been initialized, use the following compiler directives:
•ifdef ALLOC_PRAGMA
•pragma alloc_text(INIT, DriverEntry)
•pragma alloc_text(INIT, functionl_called_by_driver_entry)
•endif // ALLOC_PRAGMA
Be careful to specify only those functions that can be safely discarded and will never again be required once the driver initialization has been completed.