Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV
Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


How does one define #pragma alloc_text(X,Y)

AlbertAlbert Member - All Emails Posts: 442

This doesn't seem to work in C++ files, so how does one define these?
Also, what happens If these aren't defined, and then a function defines PAGED_CODE()?

Comments

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,605

    It certainly does work in C++ files, but you have to declare the functions as extern "C".

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,605

    BTW, what the PAGED_CODE macro does is assert that the IRQL is at PASSIVE_LEVEL. It neither knows nor cares in which section the code actually lives.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • AlbertAlbert Member - All Emails Posts: 442

    @Tim_Roberts said:
    BTW, what the PAGED_CODE macro does is assert that the IRQL is at PASSIVE_LEVEL. It neither knows nor cares in which section the code actually lives.

    Yes, but without the #pragma, it makes no sense to add it as SDV wont be able to validate?

  • AlbertAlbert Member - All Emails Posts: 442

    @Tim_Roberts said:
    BTW, what the PAGED_CODE macro does is assert that the IRQL is at PASSIVE_LEVEL. It neither knows nor cares in which section the code actually lives.

    If I have c++ classes, what is the best way to mark some functions as pagable/non-pagable? extern "C" won't work for member methods.

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,605

    Leave them all non-pageable. It's not worth the trouble.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • Doron_HolanDoron_Holan Member - All Emails Posts: 10,519
    You can use declspec code_seg to put c++ functions and classes into a PAGEable section. See https://docs.microsoft.com/en-us/cpp/cpp/code-seg-declspec?view=vs-2019
    d
  • AlbertAlbert Member - All Emails Posts: 442

    @Tim_Roberts said:
    Leave them all non-pageable. It's not worth the trouble.

    So the loader will by default make every section non-paged for a driver?

  • Tim_RobertsTim_Roberts Member - All Emails Posts: 13,605

    So the loader will by default make every section non-paged for a driver?

    The linker directives will make the INIT section discardable, and the PAGE section pageable. If there's no alloc_text, the function goes in the CODE section, which is not pageable.

    I'll get in trouble for saying it, but unless your driver is in the megabytes, it isn't worth the trouble.

    Tim Roberts, [email protected]
    Providenza & Boekelheide, Inc.

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 8,050
    edited September 16

    I'll get in trouble for saying it, but unless your driver is in the megabytes, it isn't worth the trouble.

    You won’t get in any trouble from me. I absolutely, and emphatically, agree.

    With the possible exception of PagedPool, and except for some very special cases, paging any kernel mode code (and much data) in the 21st Century is a mistake. For driver devs, it’s just asking for extra complications with zero return for your effort.

    There is one, small, special-case exception to all the above: That is the use of MmPageEntireDriver, MmLockPageableCodeSecrion, and friends on systems with limited memory, for device instances that are commonly enumerated but never used (because In Windows we load drivers when devices are discovered, not when the device is first opened, because of how we create Device Objects, etc).

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

  • AlbertAlbert Member - All Emails Posts: 442

    @Peter_Viscarola_(OSR) said:

    I'll get in trouble for saying it, but unless your driver is in the megabytes, it isn't worth the trouble.

    You won’t get in any trouble from me. I absolutely, and emphatically, agree.

    With the possible exception of PagedPool, and except for some very special cases, paging any kernel mode code (and much data) in the 21st Century is a mistake. For driver devs, it’s just asking for extra complications with zero return for your effort.

    There is one, small, special-case exception to all the above: That is the use of MmPageEntireDriver, MmLockPageableCodeSecrion, and friends on systems with limited memory, for device instances that are commonly enumerated but never used (because In Windows we load drivers when devices are discovered, not when the device is first opened, because of how we create Device Objects, etc).

    Peter

    @Tim_Roberts said:

    So the loader will by default make every section non-paged for a driver?

    The linker directives will make the INIT section discardable, and the PAGE section pageable. If there's no alloc_text, the function goes in the CODE section, which is not pageable.

    I'll get in trouble for saying it, but unless your driver is in the megabytes, it isn't worth the trouble.

    Thanks the both of you. Followup question:
    The NPP size has increased over the years, if one has to write a driver backward compatible to win7, where the NPP size is much smaller, then does it matter?
    Also, does the NPP pool from where ExAllocatePoolWithTag get memory the same as where the loader gets memory to load the drivers? I guess I am trying to ask if loading drivers as non-pagable will deplete the pool where data can be allocated from.

  • Peter_Viscarola_(OSR)Peter_Viscarola_(OSR) Administrator Posts: 8,050

    No. Drivers are not loaded into the non-paged pool. Non-paged pool is not synonymous with non-paged memory. Nonpaged pool is, rather, a specific subset of the non-paged memory on the system used for “scratch” storage.

    Peter

    Peter Viscarola
    OSR
    @OSRDrivers

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Internals & Software Drivers 30 Nov 2020 LIVE ONLINE
Writing WDF Drivers 7 Dec 2020 LIVE ONLINE
Developing Minifilters Early 2021 LIVE ONLINE