How to make PCI BAR addresses usable in the user mode?

I try to make it work, but …

The following is the code:

MyStartDevice(…)
{

case CmResourceTypeMemory:

port = resource->u.Memory.Start;
nports = resource->u.Memory.Length;
needmap = TRUE;
index = GetBarIndex(rawresource->u.Port.Start, &pciRegs);

if (index != (U32)(-1))
{
pdExt->PtrKernelBar[index] = (PU32) MmMapIoSpace(port,
nports, MmNonCached);
if (!pdExtx->PtrKernelBar[index])
return STATUS_NO_MEMORY;

// Get a MDL. The MDL will be used to map into user space
pMdl = IoAllocateMdl(pdExt->PtrKernelBar[index], nports,
FALSE, FALSE, NULL);
// Check if the MDL allocation succeeded
if (pMdl == NULL) {
DbgPrint(“my43: [StartDevice]No mdl available for BAR%i!\n”, index);
return (STATUS_INSUFFICIENT_RESOURCES);
}

// Build the MDL
MmBuildMdlForNonPagedPool(pMdl);

// Map into user space
pdExt->UserVirtual[index] = MmMapLockedPagesSpecifyCache(pMdl, UserMode,
MmNonCached, NULL, FALSE, HighPagePriority);

// Check if the mapping succeeded
if (pdExt->UserVirtual[index] == NULL) {
IoFreeMdl(pMdl);
DbgPrint(“my43: [StartDevice]No usr address available for BAR%i mdl
!\n”, index);
return (STATUS_INSUFFICIENT_RESOURCES);
}

}
break;

When I check “pdExt->UserVirtual[index]”, it is 0xC0000 or 0xD0000,
depending which BAR is. I pass the address through the ioctl command to the
user mode application. Of course, the application can not use it. It
appears to me 0xc0000 or 0xD0000 is like an offset to some base address
instaed of the address itself. Do I miss something? The platform is XP Pro
on Intel Pentium PC.

Thanks

AH

Simple … you don’t.

You have a driver that gets the BAR information. Instead of trying to do
something that is inherently dangerous to the rest of the system, do the IO
in the driver, where it should be done in the first place. You have an
upside down design that will not work.


Gary G. Little

“Ta H.” wrote in message news:xxxxx@ntdev…
>I try to make it work, but …
>
> The following is the code:
>
> MyStartDevice(…)
> {
> …
> case CmResourceTypeMemory:
>
> port = resource->u.Memory.Start;
> nports = resource->u.Memory.Length;
> needmap = TRUE;
> index = GetBarIndex(rawresource->u.Port.Start, &pciRegs);
>
> if (index != (U32)(-1))
> {
> pdExt->PtrKernelBar[index] = (PU32) MmMapIoSpace(port,
> nports, MmNonCached);
> if (!pdExtx->PtrKernelBar[index])
> return STATUS_NO_MEMORY;
>
> // Get a MDL. The MDL will be used to map into user space
> pMdl = IoAllocateMdl(pdExt->PtrKernelBar[index], nports,
> FALSE, FALSE, NULL);
> // Check if the MDL allocation succeeded
> if (pMdl == NULL) {
> DbgPrint(“my43: [StartDevice]No mdl available for BAR%i!\n”, index);
> return (STATUS_INSUFFICIENT_RESOURCES);
> }
>
> // Build the MDL
> MmBuildMdlForNonPagedPool(pMdl);
>
> // Map into user space
> pdExt->UserVirtual[index] = MmMapLockedPagesSpecifyCache(pMdl, UserMode,
> MmNonCached, NULL, FALSE, HighPagePriority);
>
> // Check if the mapping succeeded
> if (pdExt->UserVirtual[index] == NULL) {
> IoFreeMdl(pMdl);
> DbgPrint(“my43: [StartDevice]No usr address available for BAR%i mdl
> !\n”, index);
> return (STATUS_INSUFFICIENT_RESOURCES);
> }
>
> }
> break;
> …
>
> When I check “pdExt->UserVirtual[index]”, it is 0xC0000 or 0xD0000,
> depending which BAR is. I pass the address through the ioctl command to
> the user mode application. Of course, the application can not use it. It
> appears to me 0xc0000 or 0xD0000 is like an offset to some base address
> instaed of the address itself. Do I miss something? The platform is XP
> Pro on Intel Pentium PC.
>
> Thanks
>
> AH
>
>
>

Hmm…

What I want to accomplish is to let the user application to directly
communicate the chip FW via the shared memory that is mapped to one of the
BARs. Before we used the ioctl command through the driver to do that.

Do you mean Windows XP on purpose blocks people from doing what I intend to
do?

AH

From: “Gary G. Little”
>Reply-To: “Windows System Software Devs Interest List”
>
>To: “Windows System Software Devs Interest List”
>Subject: Re:[ntdev] How to make PCI BAR addresses usable in the user mode?
>Date: Wed, 26 Oct 2005 22:31:32 -0500
>
>Simple … you don’t.
>
>You have a driver that gets the BAR information. Instead of trying to do
>something that is inherently dangerous to the rest of the system, do the IO
>in the driver, where it should be done in the first place. You have an
>upside down design that will not work.
>
>–
>Gary G. Little
>
>“Ta H.” wrote in message news:xxxxx@ntdev…
> >I try to make it work, but …
> >
> > The following is the code:
> >
> > MyStartDevice(…)
> > {
> > …
> > case CmResourceTypeMemory:
> >
> > port = resource->u.Memory.Start;
> > nports = resource->u.Memory.Length;
> > needmap = TRUE;
> > index = GetBarIndex(rawresource->u.Port.Start, &pciRegs);
> >
> > if (index != (U32)(-1))
> > {
> > pdExt->PtrKernelBar[index] = (PU32) MmMapIoSpace(port,
> > nports, MmNonCached);
> > if (!pdExtx->PtrKernelBar[index])
> > return STATUS_NO_MEMORY;
> >
> > // Get a MDL. The MDL will be used to map into user space
> > pMdl = IoAllocateMdl(pdExt->PtrKernelBar[index], nports,
> > FALSE, FALSE, NULL);
> > // Check if the MDL allocation succeeded
> > if (pMdl == NULL) {
> > DbgPrint(“my43: [StartDevice]No mdl available for BAR%i!\n”,
>index);
> > return (STATUS_INSUFFICIENT_RESOURCES);
> > }
> >
> > // Build the MDL
> > MmBuildMdlForNonPagedPool(pMdl);
> >
> > // Map into user space
> > pdExt->UserVirtual[index] = MmMapLockedPagesSpecifyCache(pMdl, UserMode,
> > MmNonCached, NULL, FALSE, HighPagePriority);
> >
> > // Check if the mapping succeeded
> > if (pdExt->UserVirtual[index] == NULL) {
> > IoFreeMdl(pMdl);
> > DbgPrint(“my43: [StartDevice]No usr address available for BAR%i mdl
> > !\n”, index);
> > return (STATUS_INSUFFICIENT_RESOURCES);
> > }
> >
> > }
> > break;
> > …
> >
> > When I check “pdExt->UserVirtual[index]”, it is 0xC0000 or 0xD0000,
> > depending which BAR is. I pass the address through the ioctl command to
> > the user mode application. Of course, the application can not use it.
>It
> > appears to me 0xc0000 or 0xD0000 is like an offset to some base address
> > instaed of the address itself. Do I miss something? The platform is XP
> > Pro on Intel Pentium PC.
> >
> > Thanks
> >
> > AH
> >
> >
> >
>
>
>
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to xxxxx@lists.osr.com

> What I want to accomplish is to let the user application to directly

communicate the chip FW via the shared memory that is mapped to one of the
BARs. Before we used the ioctl command through the driver to do that.

Bad idea. Let the app send IOCTLs and the driver to the hardware access in
DispatchIoctl path.

Shared memory is bad. Nearly generally is bad. The reason is that there are no
possibilities of enforcing any rules of accessing set, while the set of IOCTLs
can enforce rules. Shared memory can be just filled with junk, sometimes
crashing the OS.

Do you mean Windows XP on purpose blocks people from doing what I intend

At least it makes this thing harder, in favour of better things.

Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

No, I meant that the Windows operating system, NT through Longhorn/Vista,
restricts IO to kernel mode. Note that there is no means, short of a kernel
mode component, to even acquire IO resources such as port addresses and
interrupt vectors. In WDM or PnP your driver resources are presented via the
IRP_MN_START_DRIVER, which is ONLY sent to a kernel mode driver. That should
be a really big hint to anyone thinking they can read/write to any IO port
they want, from their BSOD-Special application.

In almost every case where application level IO has been proposed it is
either an attempt to kludge a DOS application into NT, or a very very bad
design the really needs to be re-thought in the first place. Well, there is
always the case of the pointy haired boss that can’t find his or her ass
with both hands that thinks he, or she, knows something about low level
architecture because they did this in DOS 15 years ago. The kernel is the
best place to handle this.

The personal opinion of
Gary G. Little

“Ta H.” wrote in message news:xxxxx@ntdev…
> Hmm…
>
> What I want to accomplish is to let the user application to directly
> communicate the chip FW via the shared memory that is mapped to one of the
> BARs. Before we used the ioctl command through the driver to do that.
>
> Do you mean Windows XP on purpose blocks people from doing what I intend
> to do?
>
> AH
>
>
>>From: “Gary G. Little”
>>Reply-To: “Windows System Software Devs Interest List”
>>
>>To: “Windows System Software Devs Interest List”
>>Subject: Re:[ntdev] How to make PCI BAR addresses usable in the user mode?
>>Date: Wed, 26 Oct 2005 22:31:32 -0500
>>
>>Simple … you don’t.
>>
>>You have a driver that gets the BAR information. Instead of trying to do
>>something that is inherently dangerous to the rest of the system, do the
>>IO
>>in the driver, where it should be done in the first place. You have an
>>upside down design that will not work.
>>
>>–
>>Gary G. Little
>>
>>“Ta H.” wrote in message news:xxxxx@ntdev…
>> >I try to make it work, but …
>> >
>> > The following is the code:
>> >
>> > MyStartDevice(…)
>> > {
>> > …
>> > case CmResourceTypeMemory:
>> >
>> > port = resource->u.Memory.Start;
>> > nports = resource->u.Memory.Length;
>> > needmap = TRUE;
>> > index = GetBarIndex(rawresource->u.Port.Start, &pciRegs);
>> >
>> > if (index != (U32)(-1))
>> > {
>> > pdExt->PtrKernelBar[index] = (PU32) MmMapIoSpace(port,
>> > nports, MmNonCached);
>> > if (!pdExtx->PtrKernelBar[index])
>> > return STATUS_NO_MEMORY;
>> >
>> > // Get a MDL. The MDL will be used to map into user
>> > space
>> > pMdl = IoAllocateMdl(pdExt->PtrKernelBar[index], nports,
>> > FALSE, FALSE, NULL);
>> > // Check if the MDL allocation succeeded
>> > if (pMdl == NULL) {
>> > DbgPrint(“my43: [StartDevice]No mdl available for BAR%i!\n”,
>>index);
>> > return (STATUS_INSUFFICIENT_RESOURCES);
>> > }
>> >
>> > // Build the MDL
>> > MmBuildMdlForNonPagedPool(pMdl);
>> >
>> > // Map into user space
>> > pdExt->UserVirtual[index] = MmMapLockedPagesSpecifyCache(pMdl,
>> > UserMode,
>> > MmNonCached, NULL, FALSE, HighPagePriority);
>> >
>> > // Check if the mapping succeeded
>> > if (pdExt->UserVirtual[index] == NULL) {
>> > IoFreeMdl(pMdl);
>> > DbgPrint(“my43: [StartDevice]No usr address available for BAR%i mdl
>> > !\n”, index);
>> > return (STATUS_INSUFFICIENT_RESOURCES);
>> > }
>> >
>> > }
>> > break;
>> > …
>> >
>> > When I check “pdExt->UserVirtual[index]”, it is 0xC0000 or 0xD0000,
>> > depending which BAR is. I pass the address through the ioctl command
>> > to
>> > the user mode application. Of course, the application can not use it.
>>It
>> > appears to me 0xc0000 or 0xD0000 is like an offset to some base address
>> > instaed of the address itself. Do I miss something? The platform is
>> > XP
>> > Pro on Intel Pentium PC.
>> >
>> > Thanks
>> >
>> > AH
>> >
>> >
>> >
>>
>>
>>
>>—
>>Questions? First check the Kernel Driver FAQ at
>>http://www.osronline.com/article.cfm?id=256
>>
>>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>>To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>

Hi, all,

I apologize for not being clear at first, but I did explian my intention
afterwards. Hopefully I am not being rude when I try to seek help here.

Russel,

I will send you another offline email to you.

Thanks

AH

From: Russell Poffenberger
>Reply-To: “Windows System Software Devs Interest List”
>
>To: “Windows System Software Devs Interest List”
>Subject: Re:[ntdev] Re:How to make PCI BAR addresses usable in the user
>mode?
>Date: Thu, 27 Oct 2005 07:16:15 -0700
>
>Don,
>
>Thanks for a reasonable answer. The only responses he got at first was
>“this is an upside down design”, or “you don’t do this”. If the first
>responses would have been “Please describe what you are trying to do so we
>can help you with a proper design”, it would have been a completely
>different story.
>
>At 06:57 AM 10/27/2005, you wrote:
>>You shouldn’t be flamed. While I am one of the first people to speak out
>>against this for a general purpose driver, like all rules there are
>>exceptions. I was involved in a project that did this for high
>>performance
>>video, there are times this is needed, but they should be done in
>>essentially embedded systems.
>>
>>The problem with this thread is the OP, has not explained why he feels a
>>need to do this. It could be valid, or not.
>
>Russ Poffenberger
>Credence Systems Corp.
>xxxxx@credence.com
>
>
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to xxxxx@lists.osr.com

Actually, you haven’t told us why, just what. Does your device really need
this level of performance between the device and the application, to subvert
the security and reliability of the system? Is this in a controlled
environment where the above problems are not likely to impact the
unsuspecting user?


Don Burn (MVP, Windows DDK)
Windows 2k/XP/2k3 Filesystem and Driver Consulting
Remove StopSpam from the email to reply

“Ta H.” wrote in message news:xxxxx@ntdev…
> Hi, all,
>
> I apologize for not being clear at first, but I did explian my intention
> afterwards. Hopefully I am not being rude when I try to seek help here.
>
> Russel,
>
> I will send you another offline email to you.
>
> Thanks
>
> AH
>
>>From: Russell Poffenberger
>>Reply-To: “Windows System Software Devs Interest List”
>>
>>To: “Windows System Software Devs Interest List”
>>Subject: Re:[ntdev] Re:How to make PCI BAR addresses usable in the user
>>mode?
>>Date: Thu, 27 Oct 2005 07:16:15 -0700
>>
>>Don,
>>
>>Thanks for a reasonable answer. The only responses he got at first was
>>“this is an upside down design”, or “you don’t do this”. If the first
>>responses would have been “Please describe what you are trying to do so we
>>can help you with a proper design”, it would have been a completely
>>different story.
>>
>>At 06:57 AM 10/27/2005, you wrote:
>>>You shouldn’t be flamed. While I am one of the first people to speak out
>>>against this for a general purpose driver, like all rules there are
>>>exceptions. I was involved in a project that did this for high
>>>performance
>>>video, there are times this is needed, but they should be done in
>>>essentially embedded systems.
>>>
>>>The problem with this thread is the OP, has not explained why he feels a
>>>need to do this. It could be valid, or not.
>>
>>Russ Poffenberger
>>Credence Systems Corp.
>>xxxxx@credence.com
>>
>>
>>—
>>Questions? First check the Kernel Driver FAQ at
>>http://www.osronline.com/article.cfm?id=256
>>
>>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>>To unsubscribe send a blank email to xxxxx@lists.osr.com
>
>
>

Don,

My plaform is like a PC (Intel MB/XP Pro) but is an embedded box. So you
can say it is a controlled environment. My application needs to communicate
to the FW as quickly as possible. I have used the ioctl way, but the
performance is not good enough. Hope I have made myself clear enough.

Regards,

AH

From: “Don Burn”
>Reply-To: “Windows System Software Devs Interest List”
>
>To: “Windows System Software Devs Interest List”
>Subject: Re:[ntdev] Re:Re:How to make PCI BAR addresses usable in the user
>mode?
>Date: Thu, 27 Oct 2005 11:57:10 -0400
>
>Actually, you haven’t told us why, just what. Does your device really need
>this level of performance between the device and the application, to
>subvert
>the security and reliability of the system? Is this in a controlled
>environment where the above problems are not likely to impact the
>unsuspecting user?
>
>
>–
>Don Burn (MVP, Windows DDK)
>Windows 2k/XP/2k3 Filesystem and Driver Consulting
>Remove StopSpam from the email to reply
>
>
>
>“Ta H.” wrote in message news:xxxxx@ntdev…
> > Hi, all,
> >
> > I apologize for not being clear at first, but I did explian my intention
> > afterwards. Hopefully I am not being rude when I try to seek help here.
> >
> > Russel,
> >
> > I will send you another offline email to you.
> >
> > Thanks
> >
> > AH
> >
> >>From: Russell Poffenberger
> >>Reply-To: “Windows System Software Devs Interest List”
> >>
> >>To: “Windows System Software Devs Interest List”
> >>Subject: Re:[ntdev] Re:How to make PCI BAR addresses usable in the user
> >>mode?
> >>Date: Thu, 27 Oct 2005 07:16:15 -0700
> >>
> >>Don,
> >>
> >>Thanks for a reasonable answer. The only responses he got at first was
> >>“this is an upside down design”, or “you don’t do this”. If the first
> >>responses would have been “Please describe what you are trying to do so
>we
> >>can help you with a proper design”, it would have been a completely
> >>different story.
> >>
> >>At 06:57 AM 10/27/2005, you wrote:
> >>>You shouldn’t be flamed. While I am one of the first people to speak
>out
> >>>against this for a general purpose driver, like all rules there are
> >>>exceptions. I was involved in a project that did this for high
> >>>performance
> >>>video, there are times this is needed, but they should be done in
> >>>essentially embedded systems.
> >>>
> >>>The problem with this thread is the OP, has not explained why he feels
>a
> >>>need to do this. It could be valid, or not.
> >>
> >>Russ Poffenberger
> >>Credence Systems Corp.
> >>xxxxx@credence.com
> >>
> >>
> >>—
> >>Questions? First check the Kernel Driver FAQ at
> >>http://www.osronline.com/article.cfm?id=256
> >>
> >>You are currently subscribed to ntdev as: xxxxx@hotmail.com
> >>To unsubscribe send a blank email to xxxxx@lists.osr.com
> >
> >
> >
>
>
>
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to xxxxx@lists.osr.com

Ta H. wrote:

MyStartDevice(…)
{

case CmResourceTypeMemory:

port = resource->u.Memory.Start;
nports = resource->u.Memory.Length;
needmap = TRUE;
index = GetBarIndex(rawresource->u.Port.Start, &pciRegs);

if (index != (U32)(-1))
{
pdExt->PtrKernelBar[index] = (PU32) MmMapIoSpace(port,
nports, MmNonCached);
if (!pdExtx->PtrKernelBar[index])
return STATUS_NO_MEMORY;

// Get a MDL. The MDL will be used to map into user space
pMdl = IoAllocateMdl(pdExt->PtrKernelBar[index],
nports, FALSE, FALSE, NULL);
// Check if the MDL allocation succeeded
if (pMdl == NULL) {
DbgPrint(“my43: [StartDevice]No mdl available for BAR%i!\n”,
index);
return (STATUS_INSUFFICIENT_RESOURCES);
}

// Build the MDL
MmBuildMdlForNonPagedPool(pMdl);

// Map into user space
pdExt->UserVirtual[index] = MmMapLockedPagesSpecifyCache(pMdl,
UserMode, MmNonCached, NULL, FALSE, HighPagePriority);

// Check if the mapping succeeded
if (pdExt->UserVirtual[index] == NULL) {
IoFreeMdl(pMdl);
DbgPrint(“my43: [StartDevice]No usr address available for
BAR%i mdl !\n”, index);
return (STATUS_INSUFFICIENT_RESOURCES);
}

}
break;

When I check “pdExt->UserVirtual[index]”, it is 0xC0000 or 0xD0000,
depending which BAR is. I pass the address through the ioctl command
to the user mode application. Of course, the application can not use
it. It appears to me 0xc0000 or 0xD0000 is like an offset to some
base address instaed of the address itself. Do I miss something? The
platform is XP Pro on Intel Pentium PC.

The problem with this SPECIFIC code is that you are doing the mapping
during your StartDevice handler. Remember that every process has its
own, unique user-mode memory space. MmMapLockedPages and friends map
the memory into THE CURRENT PROCESS. StartDevice runs as part of a
system process. So later, when some application does an ioctl to fetch
the address, you are passing it a user-mode address that is only valid
in some other process.

You need to do the mapping during the ioctl. Remember to have an unmap
ioctl as well, and to clean up in your Close handler.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

> In almost every case where application level IO has been proposed it is

either an attempt to kludge a DOS application into NT, or a very very bad
design the really needs to be re-thought in the first place.

I think “it depends” fits here. DirectX maps video memory into application
virtual addresses. I’m not a video driver expert, but my understanding was
passing things like 3D drawing commands directly through a mapped hardware
interface was a desirable architecture. I’m currently scratching my head
understanding the implications of the Windows Sockets direct interface,
whose goal (among other things) is to bypass kernel transition to do network
I/O. It seems like there is a big trend TOWARD accessing hardware directly
from user mode.

  • Jan

Yes but the video frame buffer is a very special case and it includes
hardware support for optimizing memory writes specific to video
applications. Generally the requirement for direct to app hardware access is
in fact a result of bad design and when we manage to get the OP to tell us
the details of his very bad design, that becomes obvious.

If we just leap to the conclusion that its bad design, consider it a
challenge to the OP to demonstrate that it isn’t.

=====================
Mark Roddy DDK MVP
Windows 2003/XP/2000 Consulting
Hollis Technology Solutions 603-321-1032
www.hollistech.com

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Jan Bottorff
Sent: Thursday, October 27, 2005 4:04 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Re:How to make PCI BAR addresses usable
in the user mode?

> In almost every case where application level IO has been
proposed it
> is either an attempt to kludge a DOS application into NT, or a very
> very bad design the really needs to be re-thought in the
first place.

I think “it depends” fits here. DirectX maps video memory
into application virtual addresses. I’m not a video driver
expert, but my understanding was passing things like 3D
drawing commands directly through a mapped hardware interface
was a desirable architecture. I’m currently scratching my
head understanding the implications of the Windows Sockets
direct interface, whose goal (among other things) is to
bypass kernel transition to do network I/O. It seems like
there is a big trend TOWARD accessing hardware directly from
user mode.

  • Jan

Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as:
xxxxx@hollistech.com To unsubscribe send a blank email to
xxxxx@lists.osr.com

Mark Roddy wrote:

Yes but the video frame buffer is a very special case and it includes
hardware support for optimizing memory writes specific to video
applications. Generally the requirement for direct to app hardware access is
in fact a result of bad design and when we manage to get the OP to tell us
the details of his very bad design, that becomes obvious.

Further, if I may throw my own useless opinion into the melee, the only
way this “bad design” is really dangerous is when the exposed area
includes some kind of registers. If a user-mode application can write
to user-mode memory and cause a device to explode, or trigger an
interrupt storm, or wedge a bus and cause the machine to hang, that’s a
bad design. But when a user-mode application is handed a piece of
device memory that is nothing more than a buffer of data, there is no
danger to system stability.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

In the case of computer graphics, “direct rendering” is not only
an option but probably a good one. Graphics is a very difficult
area and much of it doesn’t fit the kernel-side straightjacket
very well. What I find it to be a bad design is to concentrate
graphics programming in the kernel side: the driver should be
little more than a conduit between the hardware and the
user-side library, and the OS should be, well, transparent to
the whole thing: none of their business. A very good thing to
implement is a strict partitioning of hardware functions: bus
interfaces and memory management belong to the driver, but
rendering registers belong to the user-side library.

On the other side, where there’s a will there’s a way: I worked
for a few years on an OpenGL system that did a lot of floating
point computations on the kernel side, and it did work fine.
Showing that in the end there’s no canon: do what’s needed,
period, and don’t listen to the background noise.

Alberto.

----- Original Message -----
From: “Tim Roberts”
To: “Windows System Software Devs Interest List”

Sent: Thursday, October 27, 2005 8:30 PM
Subject: Re: [ntdev] Re:How to make PCI BAR addresses usable in
the user mode?

> Mark Roddy wrote:
>
>>Yes but the video frame buffer is a very special case and it
>>includes
>>hardware support for optimizing memory writes specific to
>>video
>>applications. Generally the requirement for direct to app
>>hardware access is
>>in fact a result of bad design and when we manage to get the
>>OP to tell us
>>the details of his very bad design, that becomes obvious.
>
> Further, if I may throw my own useless opinion into the melee,
> the only way this “bad design” is really dangerous is when the
> exposed area includes some kind of registers. If a user-mode
> application can write to user-mode memory and cause a device
> to explode, or trigger an interrupt storm, or wedge a bus and
> cause the machine to hang, that’s a bad design. But when a
> user-mode application is handed a piece of device memory that
> is nothing more than a buffer of data, there is no danger to
> system stability.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@ieee.org
> To unsubscribe send a blank email to
> xxxxx@lists.osr.com

It’s called “direct rendering”. Google it up! Works beautifully.
You can try reading Mark Kilgard et. al. 's 1995 (yes, ten years
ago) paper at

http://citeseer.ist.psu.edu/kilgard95system.html

or you can go to some of the sites that have a lot of stuff
about it, for example,

http://www.cs.nmsu.edu/~joshagam/archive/cs574/3-dri.html

and much more. Yet, it works!

Alberto.

----- Original Message -----
From: “Jan Bottorff”
To: “Windows System Software Devs Interest List”

Sent: Thursday, October 27, 2005 4:04 PM
Subject: RE: [ntdev] Re:How to make PCI BAR addresses usable in
the user mode?

>> In almost every case where application level IO has been
>> proposed it is
>> either an attempt to kludge a DOS application into NT, or a
>> very very bad
>> design the really needs to be re-thought in the first place.
>
> I think “it depends” fits here. DirectX maps video memory into
> application
> virtual addresses. I’m not a video driver expert, but my
> understanding was
> passing things like 3D drawing commands directly through a
> mapped hardware
> interface was a desirable architecture. I’m currently
> scratching my head
> understanding the implications of the Windows Sockets direct
> interface,
> whose goal (among other things) is to bypass kernel transition
> to do network
> I/O. It seems like there is a big trend TOWARD accessing
> hardware directly
> from user mode.
>
> - Jan
>
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as: xxxxx@ieee.org
> To unsubscribe send a blank email to
> xxxxx@lists.osr.com

Thanks to Tim Roberts and Russell Poffenberger, they pointed me to the right
direction to correct my mistake. Now the whole thing works beautifully, and
I am leaving it to run overnight. My customer is also testing it now
overseas, and they are very happy with the flexibility and performance
provided by the new driver. I really appreciate you guys’ help!

AH

From: Tim Roberts
>Reply-To: “Windows System Software Devs Interest List”
>
>To: “Windows System Software Devs Interest List”
>Subject: Re: [ntdev] How to make PCI BAR addresses usable in the user mode?
>Date: Thu, 27 Oct 2005 09:40:59 -0700
>
>Ta H. wrote:
>
>>MyStartDevice(…)
>>{
>>…
>> case CmResourceTypeMemory:
>>
>> port = resource->u.Memory.Start;
>> nports = resource->u.Memory.Length;
>> needmap = TRUE;
>> index = GetBarIndex(rawresource->u.Port.Start, &pciRegs);
>>
>> if (index != (U32)(-1))
>> {
>> pdExt->PtrKernelBar[index] = (PU32) MmMapIoSpace(port,
>>nports, MmNonCached);
>> if (!pdExtx->PtrKernelBar[index])
>> return STATUS_NO_MEMORY;
>>
>> // Get a MDL. The MDL will be used to map into user space
>> pMdl = IoAllocateMdl(pdExt->PtrKernelBar[index], nports,
>>FALSE, FALSE, NULL);
>> // Check if the MDL allocation succeeded
>> if (pMdl == NULL) {
>> DbgPrint(“my43: [StartDevice]No mdl available for BAR%i!\n”,
>>index);
>> return (STATUS_INSUFFICIENT_RESOURCES);
>> }
>>
>> // Build the MDL
>> MmBuildMdlForNonPagedPool(pMdl);
>>
>> // Map into user space
>> pdExt->UserVirtual[index] = MmMapLockedPagesSpecifyCache(pMdl,
>>UserMode, MmNonCached, NULL, FALSE, HighPagePriority);
>>
>> // Check if the mapping succeeded
>> if (pdExt->UserVirtual[index] == NULL) {
>> IoFreeMdl(pMdl);
>> DbgPrint(“my43: [StartDevice]No usr address available for BAR%i
>>mdl !\n”, index);
>> return (STATUS_INSUFFICIENT_RESOURCES);
>> }
>>
>> }
>> break;
>>…
>>
>>When I check “pdExt->UserVirtual[index]”, it is 0xC0000 or 0xD0000,
>>depending which BAR is. I pass the address through the ioctl command to
>>the user mode application. Of course, the application can not use it. It
>>appears to me 0xc0000 or 0xD0000 is like an offset to some base address
>>instaed of the address itself. Do I miss something? The platform is XP
>>Pro on Intel Pentium PC.
>
>
>The problem with this SPECIFIC code is that you are doing the mapping
>during your StartDevice handler. Remember that every process has its own,
>unique user-mode memory space. MmMapLockedPages and friends map the memory
>into THE CURRENT PROCESS. StartDevice runs as part of a system process.
>So later, when some application does an ioctl to fetch the address, you
>are passing it a user-mode address that is only valid in some other
>process.
>
>You need to do the mapping during the ioctl. Remember to have an unmap
>ioctl as well, and to clean up in your Close handler.
>
>–
>Tim Roberts, xxxxx@probo.com
>Providenza & Boekelheide, Inc.
>
>
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to xxxxx@lists.osr.com

Direct access to video memory and/or video HW from UM
is considered “Legacy”. (Sorry Tim, I am using the
word you don’t like:)) It exists because of the design
of graphics subsystem on pre-LH systems. It actually
causes a lot of troubles in modern video H/W. One
simple example is that UM app is accessing video
memory directly while miniport is trying to adjust the
video memory clock or core clock for some reasons.

LDDM solves this problem nicely. D3D apps calls
usermode disply driver to draw primitive. UM driver
allocates vendor-specific command buffer, fill it up
and call D3d runtime to present and flush buffer. D3D
runtime passes CB to DirectX Graphics
kernel(dxgkrnl.sys). Miniport then builds paging
buffer and submits command(with fence) to GPU. GPU
executes command and DMA. dxgkrnl can also sechudle
commands. It’s expected that the next generation GPU
would support HW scheduling and context switching…
Well, you guys get me interested in video drivers
again.

I’d suggest OP seriously consider Arlie Davis’
suggestion on batching the I/O if all possible.


Calvin Guan (Windows DDK MVP)
NetXtreme Longhorn Miniport Prime
Broadcom Corp. www.broadcom.com

— Mark Roddy wrote:

> Yes but the video frame buffer is a very special
> case and it includes
> hardware support for optimizing memory writes
> specific to video
> applications. Generally the requirement for direct
> to app hardware access is
> in fact a result of bad design and when we manage to
> get the OP to tell us
> the details of his very bad design, that becomes
> obvious.
>
> If we just leap to the conclusion that its bad
> design, consider it a
> challenge to the OP to demonstrate that it isn’t.
>
> =====================
> Mark Roddy DDK MVP
> Windows 2003/XP/2000 Consulting
> Hollis Technology Solutions 603-321-1032
> www.hollistech.com
>
> > -----Original Message-----
> > From: xxxxx@lists.osr.com
> > [mailto:xxxxx@lists.osr.com] On
> Behalf Of Jan Bottorff
> > Sent: Thursday, October 27, 2005 4:04 PM
> > To: Windows System Software Devs Interest List
> > Subject: RE: [ntdev] Re:How to make PCI BAR
> addresses usable
> > in the user mode?
> >
> > > In almost every case where application level IO
> has been
> > proposed it
> > > is either an attempt to kludge a DOS application
> into NT, or a very
> > > very bad design the really needs to be
> re-thought in the
> > first place.
> >
> > I think “it depends” fits here. DirectX maps video
> memory
> > into application virtual addresses. I’m not a
> video driver
> > expert, but my understanding was passing things
> like 3D
> > drawing commands directly through a mapped
> hardware interface
> > was a desirable architecture. I’m currently
> scratching my
> > head understanding the implications of the Windows
> Sockets
> > direct interface, whose goal (among other things)
> is to
> > bypass kernel transition to do network I/O. It
> seems like
> > there is a big trend TOWARD accessing hardware
> directly from
> > user mode.
> >
> > - Jan
> >
> >
> >
> >
> > —
> > Questions? First check the Kernel Driver FAQ at
> > http://www.osronline.com/article.cfm?id=256
> >
> > You are currently subscribed to ntdev as:
> > xxxxx@hollistech.com To unsubscribe send a blank
> email to
> > xxxxx@lists.osr.com
> >
>
>
>
> —
> Questions? First check the Kernel Driver FAQ at
> http://www.osronline.com/article.cfm?id=256
>
> You are currently subscribed to ntdev as:
> xxxxx@yahoo.ca
> To unsubscribe send a blank email to
> xxxxx@lists.osr.com
>

__________________________________________________________
Find your next car at http://autos.yahoo.ca

Hi all,

Now I find out that I can only match one page 4k worth of memory, though in
the PCI configuration space it is declared 16K.

Any idea?

Thanks.

AH

From: Russell Poffenberger
>Reply-To: “Windows System Software Devs Interest List”
>
>To: “Windows System Software Devs Interest List”
>Subject: Re: [ntdev] How to make PCI BAR addresses usable in the user
>mode?
>Date: Fri, 28 Oct 2005 07:27:26 -0700
>
>Hi Arthur,
>
>Glad I could be of help.
>
>Sometimes we just need to do what we need to do.
>
>Later,
>
>At 10:25 PM 10/27/2005, you wrote:
>>Thanks to Tim Roberts and Russell Poffenberger, they pointed me to the
>>right direction to correct my mistake. Now the whole thing works
>>beautifully, and I am leaving it to run overnight. My customer is also
>>testing it now overseas, and they are very happy with the flexibility and
>>performance provided by the new driver. I really appreciate you guys’
>>help!
>
>Russ Poffenberger
>Credence Systems Corp.
>xxxxx@credence.com
>
>
>—
>Questions? First check the Kernel Driver FAQ at
>http://www.osronline.com/article.cfm?id=256
>
>You are currently subscribed to ntdev as: xxxxx@hotmail.com
>To unsubscribe send a blank email to xxxxx@lists.osr.com