Creating an MDL from a set of physical pages

To communicate with Xen, I allocate (internally via a freelist) some
pages from the MMIO space of a virtual PCI adapter, and then ask Xen to
map certain information structures into those pages for me to work with.
These pages are probably contiguous in the MMIO space, so MmMapIoSpace
is probably going to work, but I like to avoid ‘probably’ whenever
possible.

Xen and Windows shouldn’t actually care if the pages are physically
contiguous, as long as they can be mapped to virtually contiguous space,
and based on what I know about MDL’s, I think that technically I should
be able to build an MDL from two non-contiguous pages via their PFN’s,
and then ask Windows to map those to a virtual base address for me.

Is that a supported scenario under Windows or am I stuck with
MmMapIoSpace?

Thanks

James

There is no interface in Windows to take a set of arbitrary physical pages
and create a MDL. The memory manager in Windows was designed with the
assumption that you would always start with virtual addresses, or by asking
the memory manager to allocate a set of physical pages for you, along with
the resulting MDL.


Jake Oshins
Hyper-V I/O Architect
Windows Kernel Team

This post implies no warranties and confers no rights.


“James Harper” wrote in message
news:xxxxx@ntdev…
> To communicate with Xen, I allocate (internally via a freelist) some
> pages from the MMIO space of a virtual PCI adapter, and then ask Xen to
> map certain information structures into those pages for me to work with.
> These pages are probably contiguous in the MMIO space, so MmMapIoSpace
> is probably going to work, but I like to avoid ‘probably’ whenever
> possible.
>
> Xen and Windows shouldn’t actually care if the pages are physically
> contiguous, as long as they can be mapped to virtually contiguous space,
> and based on what I know about MDL’s, I think that technically I should
> be able to build an MDL from two non-contiguous pages via their PFN’s,
> and then ask Windows to map those to a virtual base address for me.
>
> Is that a supported scenario under Windows or am I stuck with
> MmMapIoSpace?
>
> Thanks
>
> James
>

>be able to build an MDL from two non-contiguous pages via their PFN’s,

Try looking at AGP samples in the WDK to see how they do handle MDL tails.

Undocumented uncharted land though. Can fail.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com

>

There is no interface in Windows to take a set of arbitrary physical
pages
and create a MDL. The memory manager in Windows was designed with the
assumption that you would always start with virtual addresses, or by
asking
the memory manager to allocate a set of physical pages for you, along
with
the resulting MDL.

That’s the sort of definitive answer I was looking for, even if it’s not
the one I wanted to hear. Thanks.

James

> That’s the sort of definitive answer I was looking for,

Well, it is just so plainly obvious that it does not seem to need any confirmation - it is MM who owns all memory pages in the system, so that it is MM who grants you the right to use a given page. If you start using pages without MM’s knowledge, MM may use them while they are still used by you, which will immediately result in data corruption. Therefore, it is understandable that all MDL-related API expects you to obtain pages only from the MM.

If I got it right, in this context we are speaking about the memory that the OS itself is unaware of, so that this case is special. What you can try to do here is to build MDL on your own and subsequently map it. However, this is totally undocumented field…

Anton Bassov

> > That’s the sort of definitive answer I was looking for,

Well, it is just so plainly obvious that it does not seem to need any
confirmation - it is MM who owns all memory pages in the system, so
that
it is MM who grants you the right to use a given page. If you start
using
pages without MM’s knowledge, MM may use them while they are still
used by
you, which will immediately result in data corruption. Therefore, it
is
understandable that all MDL-related API expects you to obtain pages
only
from the MM.

If I got it right, in this context we are speaking about the memory
that
the OS itself is unaware of, so that this case is special. What you
can
try to do here is to build MDL on your own and subsequently map it.
However, this is totally undocumented field…

Correct. It is memory that exists within PCI space which the MM doesn’t
manage. MmMapIoSpace can map a contiguous area of PCI space to virtual
space, but I wanted to be more flexible. MmMapIoSpace will have to do
though.

Most of the things I want to do with this mmio space are going to need
only a single page, so for those it doesn’t matter. One routine though
will need up to 4 pages from this mmio space and the pages need to be
virtually contiguous (apart from MmMapIoSpace requiring them to be
physically contiguous there is no other requirement for that). I think I
will simply maintain a freelist of single physical pages and a freelist
of blocks of 4 physically contiguous pages, and allocate as necessary.
There is 1MByte of mmio space and at any given time my driver only needs
to use about 6 pages, and allocation only happens at startup and after a
suspend, migrate, or hibernation, so running into fragmentation problems
would be an extraordinary event. It would just suck if it did happen :slight_smile:

Thanks

James