get total physical memory

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

>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

>

>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

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

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” 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

> 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

I think you can also use MmGetPhysicalMemoryRanges() rather than
having to use the registry.

Paul

On 12 February 2013 13:12, 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

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” 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, 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

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.

>

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