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

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

get total physical memory

James_HarperJames_Harper Member Posts: 1,615
What is the correct way to get the total amount of physical memory (in pages, or at least not rounded to mb) on a windows system from a driver?

Thanks

James

Comments

  • Daniel_TerhellDaniel_Terhell Member Posts: 1,349
    >What is the correct way to get the total amount of physical memory (in
    >pages, or at least not rounded to mb) on a >windows system from a driver?

    Your driver is not allowed to know. You need to use MmQuerySystemSize and
    the contract is to be happy with either small, medium or large. Otherwise,
    write a usermode service that communicates with your driver just and only
    for this purpose.

    //Daniel
  • James_HarperJames_Harper Member Posts: 1,615
    >
    > >What is the correct way to get the total amount of physical memory (in
    > >pages, or at least not rounded to mb) on a >windows system from a driver?
    >
    > Your driver is not allowed to know. You need to use MmQuerySystemSize
    > and
    > the contract is to be happy with either small, medium or large. Otherwise,
    > write a usermode service that communicates with your driver just and only
    > for this purpose.
    >

    I was concerned that this might be the case...

    Under Xen it's possible to boot with (say) 4GB memory but only 1GB populated with actual physical memory. I need to allocate the other 3GB of memory so that windows can't use them, before Windows notices. Xen handles populated pages of memory on demand so in my example 4GB/1GB case it doesn't matter which 1GB memory Windows uses, as long as it doesn't use more (The additional pages can be populated later if more memory should be given to the VM, thus varying the amount of memory allocated to the VM based on current demand)

    I know the number of pages of memory Xen has given me, but I also need to know the amount of physical memory in the system so I can allocate the different between the two.

    Task manager seems to know though, so it can't be that big a secret!

    James
  • Daniel_TerhellDaniel_Terhell Member Posts: 1,349
    NtQuerySystemInformation with SystemFullMemoryInformation class but that's
    undocumented.

    You can boot Windows with the REMOVEMEM parameter to make only a limited
    amount of RAM available. Type
    BCDEDIT /? TYPES OSLOADER
    for more info.

    //Daniel



    "James Harper" wrote in message
    news:xxxxx@ntdev...
    > I was concerned that this might be the case...

    Under Xen it's possible to boot with (say) 4GB memory but only 1GB populated
    with actual physical memory. I need to allocate the other 3GB of memory so
    that windows can't use them, before Windows notices. Xen handles populated
    pages of memory on demand so in my example 4GB/1GB case it doesn't matter
    which 1GB memory Windows uses, as long as it doesn't use more (The
    additional pages can be populated later if more memory should be given to
    the VM, thus varying the amount of memory allocated to the VM based on
    current demand)

    I know the number of pages of memory Xen has given me, but I also need to
    know the amount of physical memory in the system so I can allocate the
    different between the two.

    Task manager seems to know though, so it can't be that big a secret!

    James
  • Don_BurnDon_Burn Member - All Emails Posts: 1,649
    I haven't tried this with Win8 but for every system prior to Win8 you
    could get the memory through the registry key:

    \REGISTRY\MACHINE\HARDWARE\RESOURCEMAP\System Resources\Physical Memory

    I've used this to get the physical memory since NT3.5.1 days, and for a
    lot of this time Microsoft has documented it in knowledge base articles.


    Don Burn
    Windows Filesystem and Driver Consulting
    Website: http://www.windrvr.com
    Blog: http://msmvps.com/blogs/WinDrvr







    "James Harper" <xxxxx@bendigoit.com.au> wrote in message
    news:xxxxx@ntdev:

    > What is the correct way to get the total amount of physical memory (in pages, or at least not rounded to mb) on a windows system from a driver?
    >
    > Thanks
    >
    > James
  • GPGP Member Posts: 69
    > I haven't tried this with Win8 but for every system prior to Win8 you
    > could get the memory through the registry key:
    >
    > \REGISTRY\MACHINE\HARDWARE\RESOURCEMAP\System Resources\Physical Memory

    Which is of type REG_RESOURCE_LIST. It's binary layout is documented here:

    [CM_RESOURCE_LIST structure (Windows Drivers)]
    http://msdn.microsoft.com/en-us/library/windows/hardware/ff541994(v=vs.85).aspx


    GP
  • Paul_DurrantPaul_Durrant Member Posts: 45
    I think you can also use MmGetPhysicalMemoryRanges() rather than
    having to use the registry.

    Paul

    On 12 February 2013 13:12, <xxxxx@world-direct.at> wrote:
    >> I haven't tried this with Win8 but for every system prior to Win8 you
    >> could get the memory through the registry key:
    >>
    >> \REGISTRY\MACHINE\HARDWARE\RESOURCEMAP\System Resources\Physical Memory
    >
    > Which is of type REG_RESOURCE_LIST. It's binary layout is documented here:
    >
    > [CM_RESOURCE_LIST structure (Windows Drivers)]
    > http://msdn.microsoft.com/en-us/library/windows/hardware/ff541994(v=vs.85).aspx
    >
    >
    > GP
    >
    > ---
    > NTDEV is sponsored by OSR
    >
    > OSR is HIRING!! See http://www.osr.com/careers
    >
    > For our schedule of WDF, WDM, debugging and other seminars visit:
    > http://www.osr.com/seminars
    >
    > To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer



    --
    Paul Durrant
    http://www.linkedin.com/in/pdurrant
  • Don_BurnDon_Burn Member - All Emails Posts: 1,649
    Both MmGetPhysicalMemoryRanges and the ZwQuerySystemInformation approach
    are undocumented, In fact MmGetPhysicalMemoryRanges the documentation
    specifically states don't use it in your driver. When there are
    alternatives why go there?


    Don Burn
    Windows Filesystem and Driver Consulting
    Website: http://www.windrvr.com
    Blog: http://msmvps.com/blogs/WinDrvr




    "Paul Durrant" <xxxxx@gmail.com> wrote in message news:xxxxx@ntdev:

    > I think you can also use MmGetPhysicalMemoryRanges() rather than
    > having to use the registry.
    >
    > Paul
    >
    > On 12 February 2013 13:12, <xxxxx@world-direct.at> wrote:
    > >> I haven't tried this with Win8 but for every system prior to Win8 you
    > >> could get the memory through the registry key:
    > >>
    > >> \REGISTRY\MACHINE\HARDWARE\RESOURCEMAP\System Resources\Physical Memory
    > >
    > > Which is of type REG_RESOURCE_LIST. It's binary layout is documented here:
    > >
    > > [CM_RESOURCE_LIST structure (Windows Drivers)]
    > > http://msdn.microsoft.com/en-us/library/windows/hardware/ff541994(v=vs.85).aspx
    > >
    > >
    > > GP
    > >
    > > ---
    > > NTDEV is sponsored by OSR
    > >
    > > OSR is HIRING!! See http://www.osr.com/careers
    > >
    > > For our schedule of WDF, WDM, debugging and other seminars visit:
    > > http://www.osr.com/seminars
    > >
    > > To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer
    >
    >
    >
    > --
    > Paul Durrant
    > http://www.linkedin.com/in/pdurrant
  • OSR_Community_UserOSR_Community_User Member Posts: 110,217
    On 2/12/2013 9:15 AM, Don Burn wrote:
    > Both MmGetPhysicalMemoryRanges and the ZwQuerySystemInformation approach
    > are undocumented, In fact MmGetPhysicalMemoryRanges the documentation
    > specifically states don't use it in your driver. When there are
    > alternatives why go there?
    >
    >

    The registry key is written during system initialization and will not
    reflect hot-swap memory that is added or removed subsequent to that
    point. Also, it will not reflect any memory pages that subsequently
    have been removed from the system, e.g. due to ECC parity errors.
    ZwQuerySystemInformation is probably the least bad of the "bad" options
    available. Many people who use MmGetPhysicalMemoryRanges forget that it
    allocates memory that will leak if not deallocated when no longer needed.

    If you really need to reserve 3 GiB of memory the best approach would be
    to remove this memory from the physical memory map before Windows loads.
    Trucrypt reserves memory in this way, for example (or so I am told).
    What would be the problem with allocating the sparse pages on demand and
    returning an error if not enough memory is available? Low memory
    resources is a common scenario that most if not all commercial operating
    systems are designed to handle.
  • James_HarperJames_Harper Member Posts: 1,615
    >
    > On 2/12/2013 9:15 AM, Don Burn wrote:
    > > Both MmGetPhysicalMemoryRanges and the ZwQuerySystemInformation
    > approach
    > > are undocumented, In fact MmGetPhysicalMemoryRanges the
    > documentation
    > > specifically states don't use it in your driver. When there are
    > > alternatives why go there?
    > >
    > >
    >
    > The registry key is written during system initialization and will not
    > reflect hot-swap memory that is added or removed subsequent to that
    > point. Also, it will not reflect any memory pages that subsequently
    > have been removed from the system, e.g. due to ECC parity errors.
    > ZwQuerySystemInformation is probably the least bad of the "bad" options
    > available. Many people who use MmGetPhysicalMemoryRanges forget that
    > it allocates memory that will leak if not deallocated when no longer needed.

    Thanks for the tip.

    > If you really need to reserve 3 GiB of memory the best approach would be
    > to remove this memory from the physical memory map before Windows
    > loads.
    > Trucrypt reserves memory in this way, for example (or so I am told).
    > What would be the problem with allocating the sparse pages on demand and
    > returning an error if not enough memory is available? Low memory
    > resources is a common scenario that most if not all commercial operating
    > systems are designed to handle.
    >

    What I need to do is allocate 3GB of memory, and be able to free it and reallocate it in response to demand management requests from Xen. This "memory ballooning" allows (eg) 10 VM's to allocate 8GB memory each on a system with 32GB, but initially boot up with only 2GB populated, and adjust the memory on each VM based on demand. Making the memory invisible doesn't allow for this.

    James
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
Writing WDF Drivers 25 Feb 2019 OSR Seminar Space
Developing Minifilters 8 April 2019 OSR Seminar Space