copying data from kernel mode to user mode

Hi, everybody!

I have a little question:

I have an NDIS driver, and I want to copy data from one of its internal
structures to a user’s application, that calls an ioctl command.

The internal structure is allocated in the following manner:
ExAllocatePool (NonPagedPool, size);

The copy process itself is straight-forward, since I copy
the data to the IRP->UserBuffer I get.
My questions are:

  1. How can I know that the pointer I get from the user is valid (i.e.: isn’t
    NULL,
    but merely garbage). I want to avoid from crashing the machine…:wink:
  2. Should I lock things in memory before the copy? What should I lock? The
    IRP? How?
  3. Can I assume that my internal structure is never swapped out?
  4. Can the address I get from the user be swapped before I get to write to
    it
    (thus creating a page fault)? How can I avoid it?

I’m quite sure that the answers to these questions are very simple,
I just don’t know the system too well. References to specific places
(i.e., not “see the DDK”) will be highly appreciated…

thanks in advance,

  • Barak

You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Barak,

The internal structure is allocated in the following manner:
ExAllocatePool (NonPagedPool, size);

The copy process itself is straight-forward, since I copy
the data to the IRP->UserBuffer I get.

Except of course that unless the IOCTL is defined using METHOD_NEITHER this
is the wrong place to look for the users data buffer, EVEN IF IT HAPPENS TO
WORK.

If on the other hand the IOCTL is defined using METHOD_NEITHER, then this is
indeed the output (going to the user) buffer location.

For more information see 13.3 Defining I/O Control Codes in the DDK.

My questions are:

  1. How can I know that the pointer I get from the user is
    valid (i.e.: isn’t
    NULL,
    but merely garbage). I want to avoid from crashing the
    machine…:wink:

For Irp->UserBuffer you don’t know you have to find out, see 16.10.2 Errors
in Referencing User-Space Addresses. If on the other hand your IOCTL is
defined using METHOD_BUFFERED then the OS has done all this checking for you
and put a SAFE pointer to the user output buffer at
Irp->AssociatedIrp.SystemBuffer.

  1. Should I lock things in memory before the copy? What
    should I lock? The
    IRP? How?

This depends on how the IOCTL is defined. (See above.) Typically private
IOCTLs are going to use METHOD_BUFFERED and then you don’t have to do
anything. You never ‘lock an Irp’. You might have to do something about
UserBuffer, but only if you were going to DMA in or out of it of the system.
If you are just going to copy data into user space from kernel space then
you just copy the data. You might want to put an exception handler around
the copy in case things go very wrong. See 16.10.2 Errors in Referencing
User-Space Addresses.

  1. Can I assume that my internal structure is never swapped out?

It is nonpaged pool so it is not pageable. NT does not have anything
resembling swapping, but non-paged pool is by definition not going to be
paged out.

  1. Can the address I get from the user be swapped before I
    get to write to
    it
    (thus creating a page fault)? How can I avoid it?

You don’t. The system handles page faults all on its own. As long as you are
in the correct process context and are running at less than DISPATCH_LEVEL
and as long as the user has given you a valid virtual address and length,
page faults are not an issue. Once again see 16.10.2 Errors in Referencing
User-Space Addresses. And once again most private IOCTLS are going to use
METHOD_BUFFERED, and then all of your concerns other than “where is the
user’s data buffer” are non-issues.

Mark Roddy
xxxxx@hollistech.com
www.hollistech.com
WindowsNT Windows 2000 Consulting Services


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Lookup “METHOD_BUFFERED” in the DDK Help files and read all of the topics
that are found.

Also, most good books on NT device driver programming explain this topic in
more detail.

Good luck,

Thomas F. Divine

PCAUSA - Toolkits & Resources For Network Software Developers
NDIS Protocol - NDIS Intermediate - TDI Client
http: - http:

----- Original Message -----
From: Barak Mandelovich
To: NT Developers Interest List
Sent: Tuesday, December 19, 2000 8:38 AM
Subject: [ntdev] copying data from kernel mode to user mode

> Hi, everybody!
>
> I have a little question:
>
> I have an NDIS driver, and I want to copy data from one of its internal
> structures to a user’s application, that calls an ioctl command.
>
> The internal structure is allocated in the following manner:
> ExAllocatePool (NonPagedPool, size);
>
> The copy process itself is straight-forward, since I copy
> the data to the IRP->UserBuffer I get.
> My questions are:
>
> 1. How can I know that the pointer I get from the user is valid (i.e.:
isn’t
> NULL,
> but merely garbage). I want to avoid from crashing the machine…:wink:
> 2. Should I lock things in memory before the copy? What should I lock? The
> IRP? How?
> 3. Can I assume that my internal structure is never swapped out?
> 4. Can the address I get from the user be swapped before I get to write to
> it
> (thus creating a page fault)? How can I avoid it?
>
> I’m quite sure that the answers to these questions are very simple,
> I just don’t know the system too well. References to specific places
> (i.e., not “see the DDK”) will be highly appreciated…
>
>
> thanks in advance,
>
> - Barak
>
> —
> You are currently subscribed to ntdev as: xxxxx@pcausa.com
> To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com</http:></http:>

Hi, Mark, and 10x for your reply!

I have some follow-up questions:
When I copy my buffer to the user, may I use memcpy()?

Now, let me be more specific:
I’m using the packet.sys sample from the DDK.
I changed it a bit, to store any frame that arrives
in a non-paged area. When a user does a DeviceIoControl()
to receive it (METHOD_BUFFERED, as far I as know, and checked the code),
I copy the buffer to the IRP.

I changed the code, to copy data to the Irp->AssociatedIrp.SystemBuffer
field, but then I always get a blue screen (exception not handled).

I did a __try-__catch around it, ( __catch(0) ), but I still get the
“exception unhandled” blue screen.

When I copy the data to Irp->UserBuffer – it works fine.

What am I doing wrong?
Can I be sure that the Irp->AssociatedIrp.SystemBuffer area is
at the size of the user’s buffer? How can I get the size of this
buffer from the IRP?

As you can see, these are all newbie’s questions… I think
that there’s something here I just don’t get… ;-(

One more things: The chapter numbers that you specified in your
previous message - to which DDK do they apply? the win2k DDK?
(Currently, only the NT4 DDK is installed on my machine :wink: )

thanks very very much in advance,

  • Barak

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, December 19, 2000 4:13 PM
To: NT Developers Interest List
Subject: [ntdev] RE: copying data from kernel mode to user mode

Barak,

The internal structure is allocated in the following manner:
ExAllocatePool (NonPagedPool, size);

The copy process itself is straight-forward, since I copy
the data to the IRP->UserBuffer I get.

Except of course that unless the IOCTL is defined using METHOD_NEITHER this
is the wrong place to look for the users data buffer, EVEN IF IT HAPPENS TO
WORK.

If on the other hand the IOCTL is defined using METHOD_NEITHER, then this is
indeed the output (going to the user) buffer location.

For more information see 13.3 Defining I/O Control Codes in the DDK.

My questions are:

  1. How can I know that the pointer I get from the user is
    valid (i.e.: isn’t
    NULL,
    but merely garbage). I want to avoid from crashing the
    machine…:wink:

For Irp->UserBuffer you don’t know you have to find out, see 16.10.2 Errors
in Referencing User-Space Addresses. If on the other hand your IOCTL is
defined using METHOD_BUFFERED then the OS has done all this checking for you
and put a SAFE pointer to the user output buffer at
Irp->AssociatedIrp.SystemBuffer.

  1. Should I lock things in memory before the copy? What
    should I lock? The
    IRP? How?

This depends on how the IOCTL is defined. (See above.) Typically private
IOCTLs are going to use METHOD_BUFFERED and then you don’t have to do
anything. You never ‘lock an Irp’. You might have to do something about
UserBuffer, but only if you were going to DMA in or out of it of the system.
If you are just going to copy data into user space from kernel space then
you just copy the data. You might want to put an exception handler around
the copy in case things go very wrong. See 16.10.2 Errors in Referencing
User-Space Addresses.

  1. Can I assume that my internal structure is never swapped out?

It is nonpaged pool so it is not pageable. NT does not have anything
resembling swapping, but non-paged pool is by definition not going to be
paged out.

  1. Can the address I get from the user be swapped before I
    get to write to
    it
    (thus creating a page fault)? How can I avoid it?

You don’t. The system handles page faults all on its own. As long as you are
in the correct process context and are running at less than DISPATCH_LEVEL
and as long as the user has given you a valid virtual address and length,
page faults are not an issue. Once again see 16.10.2 Errors in Referencing
User-Space Addresses. And once again most private IOCTLS are going to use
METHOD_BUFFERED, and then all of your concerns other than “where is the
user’s data buffer” are non-issues.

Mark Roddy
xxxxx@hollistech.com
www.hollistech.com
WindowsNT Windows 2000 Consulting Services


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Barak,

You need to be careful.

The as-is Packet.sys sample uses ReadFile and WriteFile to read and write
packets. It uses DO_DIRECT_IO for read and write; this is NOT the same as
METHOD_BUFFERED.

So, I presume that you have defined some new IOCTL codes to read and write
packets differently from the way Packet.sys was originally designed.

One question. Why did you decide to use DeviceIoControl instead of
ReadFile/WriteFile? Seems to work OK…

Anyway, if you are using DeviceIoControl the I/o method is speficied in the
IOCTL. See the CTL_CODE macro in the DDK devioctl.h file.

As I mentioned in a previous message, look up all references to
METHOD_BUFFERED in the DDK help file. They will show the way.

Aslo see the “IOCTL” sample on the Microsoft website. Go to:

http:

Look in the Windows NT 4.0 Samples for IOCTL.

There are other samples there that may also be of interest.

Some of these may be included with the Windows 2000 DDK, but I am not sure.

Good luck,

Thomas F. Divine

PCAUSA - Toolkits & Resources For Network Software Developers
NDIS Protocol - NDIS Intermediate - TDI Client
http: - http:

----- Original Message -----
From: Barak Mandelovich
To: NT Developers Interest List
Cc:
Sent: Tuesday, December 19, 2000 1:01 PM
Subject: [ntdev] RE: copying data from kernel mode to user mode

> Hi, Mark, and 10x for your reply!
>
> I have some follow-up questions:
> When I copy my buffer to the user, may I use memcpy()?
>
> Now, let me be more specific:
> I’m using the packet.sys sample from the DDK.
> I changed it a bit, to store any frame that arrives
> in a non-paged area. When a user does a DeviceIoControl()
> to receive it (METHOD_BUFFERED, as far I as know, and checked the code),
> I copy the buffer to the IRP.
>
> I changed the code, to copy data to the Irp->AssociatedIrp.SystemBuffer
> field, but then I always get a blue screen (exception not handled).
>
> I did a try- catch around it, ( __catch(0) ), but I still get the
> “exception unhandled” blue screen.
>
> When I copy the data to Irp->UserBuffer – it works fine.
>
> What am I doing wrong?
> Can I be sure that the Irp->AssociatedIrp.SystemBuffer area is
> at the size of the user’s buffer? How can I get the size of this
> buffer from the IRP?
>
> As you can see, these are all newbie’s questions… I think
> that there’s something here I just don’t get… ;-(
>
> One more things: The chapter numbers that you specified in your
> previous message - to which DDK do they apply? the win2k DDK?
> (Currently, only the NT4 DDK is installed on my machine :wink: )
>
>
> thanks very very much in advance,
>
> - Barak
>
>
>
> -----Original Message-----
> From: Roddy, Mark [mailto:xxxxx@stratus.com]
> Sent: Tuesday, December 19, 2000 4:13 PM
> To: NT Developers Interest List
> Subject: [ntdev] RE: copying data from kernel mode to user mode
>
>
>
> Barak,
> >
> > The internal structure is allocated in the following manner:
> > ExAllocatePool (NonPagedPool, size);
> >
> > The copy process itself is straight-forward, since I copy
> > the data to the IRP->UserBuffer I get.
>
> Except of course that unless the IOCTL is defined using METHOD_NEITHER
this
> is the wrong place to look for the users data buffer, EVEN IF IT HAPPENS
TO
> WORK.
>
> If on the other hand the IOCTL is defined using METHOD_NEITHER, then this
is
> indeed the output (going to the user) buffer location.
>
> For more information see 13.3 Defining I/O Control Codes in the DDK.
>
> > My questions are:
> >
> > 1. How can I know that the pointer I get from the user is
> > valid (i.e.: isn’t
> > NULL,
> > but merely garbage). I want to avoid from crashing the
> > machine…:wink:
>
> For Irp->UserBuffer you don’t know you have to find out, see 16.10.2
Errors
> in Referencing User-Space Addresses. If on the other hand your IOCTL is
> defined using METHOD_BUFFERED then the OS has done all this checking for
you
> and put a SAFE pointer to the user output buffer at
> Irp->AssociatedIrp.SystemBuffer.
>
> > 2. Should I lock things in memory before the copy? What
> > should I lock? The
> > IRP? How?
>
> This depends on how the IOCTL is defined. (See above.) Typically private
> IOCTLs are going to use METHOD_BUFFERED and then you don’t have to do
> anything. You never ‘lock an Irp’. You might have to do something about
> UserBuffer, but only if you were going to DMA in or out of it of the
system.
> If you are just going to copy data into user space from kernel space then
> you just copy the data. You might want to put an exception handler around
> the copy in case things go very wrong. See 16.10.2 Errors in Referencing
> User-Space Addresses.
>
> > 3. Can I assume that my internal structure is never swapped out?
>
> It is nonpaged pool so it is not pageable. NT does not have anything
> resembling swapping, but non-paged pool is by definition not going to be
> paged out.
>
> > 4. Can the address I get from the user be swapped before I
> > get to write to
> > it
> > (thus creating a page fault)? How can I avoid it?
> >
>
> You don’t. The system handles page faults all on its own. As long as you
are
> in the correct process context and are running at less than DISPATCH_LEVEL
> and as long as the user has given you a valid virtual address and length,
> page faults are not an issue. Once again see 16.10.2 Errors in Referencing
> User-Space Addresses. And once again most private IOCTLS are going to use
> METHOD_BUFFERED, and then all of your concerns other than “where is the
> user’s data buffer” are non-issues.
>
> Mark Roddy
> xxxxx@hollistech.com
> www.hollistech.com
> WindowsNT Windows 2000 Consulting Services
>
> —
> You are currently subscribed to ntdev as: xxxxx@pcausa.com
> To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com</http:></http:></http:>

Barak,

I have some follow-up questions:
When I copy my buffer to the user, may I use memcpy()?

No not legally. You should use RtlCopyMemory.

I changed the code, to copy data to the
Irp->AssociatedIrp.SystemBuffer
field, but then I always get a blue screen (exception not handled).

I did a __try-__catch around it, ( __catch(0) ), but I still get the
“exception unhandled” blue screen.

Well something is obviously wrong.

My earlier posting, on reflection, was less than crystal clear about the
exception handling. METHOD_BUFFERED never needs an exception handler, only
METHOD_NEITHER needs to use this technique. BTW, you can’t use try/catch you
have to use try/except, and the syntax is:

__try
{

}
__except(EXCEPTION_EXECUTE_HANDLER)
{
}

for C++ code or try {} except(EXCEPTION_EXECUTE_HANDLER){} for C code.

When I copy the data to Irp->UserBuffer – it works fine.

What am I doing wrong?

I have no idea, given that I can’t see your code. One guess is that you have
not specified an ‘output buffer’ in your invocation of DeviceControl from
user space, or you specified a length that is way too small.

Can I be sure that the Irp->AssociatedIrp.SystemBuffer area is
at the size of the user’s buffer? How can I get the size of this
buffer from the IRP?

You have to look at the IoStack parameters for the IOCTL to see how big the
output buffer is. My guess is that it is zero length. Read about
IoGetCurrentIrpStackLocation().

As you can see, these are all newbie’s questions… I think
that there’s something here I just don’t get… ;-(

The DDK has lots of source examples one or more of which most likely
actually processes a METHOD_BUFFERED IOCTL with an output buffer. Search the
sample drivers for something that does what you are trying to do.

One more things: The chapter numbers that you specified in your
previous message - to which DDK do they apply? the win2k DDK?
(Currently, only the NT4 DDK is installed on my machine :wink: )

W2K of course, get with the program :slight_smile:

Mark Roddy
xxxxx@hollistech.com
www.hollistech.com
WindowsNT Windows 2000 Consulting Services


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

----- Original Message -----
From: Roddy, Mark
To: NT Developers Interest List
Sent: Tuesday, December 19, 2000 2:18 PM
Subject: [ntdev] RE: copying data from kernel mode to user mode

>
> W2K of course, get with the program :slight_smile:
>

Mark,

I didn’t know you worked for Microsoft :slight_smile:

Only Microsoft operates in a world where Windows 95, Windows 98 and Windows
NT don’t exist and aren’t supported.

Thomas


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

>

>
> W2K of course, get with the program :slight_smile:
>

Mark,

I didn’t know you worked for Microsoft :slight_smile:

Only Microsoft operates in a world where Windows 95, Windows
98 and Windows
NT don’t exist and aren’t supported.

I don’t work for microsoft, but as one of the pilot fish swimming alongside
the great white shark, I do understand where my meals are coming from. The
better microsoft does, the better I do, its a symbiotic arrangement :slight_smile:

And finally, microsoft is not the first or only company to obsolete
products, particularly software products, in order to generate revenue. For
example, try getting an update/patch of Quicken2000 that actually works with
Windows2000. The update/patch is called Quicken2001 and it comes in its own
box with its own price tag and with the aggravation of my now having to also
upgrade my spouse to the new user interface as well.

But lest you think I am some sort of sycophant, have you tried that .Net
Studio (or whatever the .lousy .name .is) Beta? I did, and quite frankly it
massively sucked even for a microsoft beta, which is pretty hard to imagine.

Mark Roddy
xxxxx@hollistech.com
www.hollistech.com
WindowsNT Windows 2000 Consulting Services


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

FYI:The DDK Macro definition of RtlCopyMemory is:
#define RtlCopyMemory(Destination,Source,Length)
memcpy((Destination),(Source),(Length))

Regards,

Paul Bunn, UltraBac.com, 425-644-6000
Microsoft MVP - WindowsNT/2000
http://www.ultrabac.com

-----Original Message-----
From: Roddy, Mark [mailto:xxxxx@stratus.com]
Sent: Tuesday, December 19, 2000 11:19 AM
To: NT Developers Interest List
Subject: [ntdev] RE: copying data from kernel mode to user mode

Barak,

I have some follow-up questions:
When I copy my buffer to the user, may I use memcpy()?

No not legally. You should use RtlCopyMemory.


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Hmmm, but then some where someone still uses CPM, a 360 thrashes across the
floor, and someone stumbles and drops their card deck.

:slight_smile:

-----Original Message-----
From: Thomas F. Divine [mailto:xxxxx@pcausa.com]
Sent: Tuesday, December 19, 2000 11:25 AM
To: NT Developers Interest List
Subject: [ntdev] RE: copying data from kernel mode to user mode

----- Original Message -----
From: Roddy, Mark
To: NT Developers Interest List
Sent: Tuesday, December 19, 2000 2:18 PM
Subject: [ntdev] RE: copying data from kernel mode to user mode

>
> W2K of course, get with the program :slight_smile:
>

Mark,

I didn’t know you worked for Microsoft :slight_smile:

Only Microsoft operates in a world where Windows 95, Windows 98 and Windows
NT don’t exist and aren’t supported.

Thomas


You are currently subscribed to ntdev as: xxxxx@delphieng.com
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

http://support.microsoft.com/support/kb/articles/Q178/3/17.asp
http:

-Eliyas
http://support.microsoft.com/support/ddk/
http:</http:>

-----Original Message-----
From: Roddy, Mark
Sent: Tue 12/19/2000 11:18 AM
To: NT Developers Interest List
Cc:
Subject: [ntdev] RE: copying data from kernel mode to user mode

Barak,

>
> I have some follow-up questions:
> When I copy my buffer to the user, may I use memcpy()?
>

No not legally. You should use RtlCopyMemory.

>
> I changed the code, to copy data to the
> Irp->AssociatedIrp.SystemBuffer
> field, but then I always get a blue screen (exception not handled).
>
> I did a try- catch around it, ( catch(0) ), but I still get the
> “exception unhandled” blue screen.
>

Well something is obviously wrong.

My earlier posting, on reflection, was less than crystal clear about the
exception handling. METHOD_BUFFERED never needs an exception handler, only
METHOD_NEITHER needs to use this technique. BTW, you can’t use try/catch you
have to use try/except, and the syntax is:

try
{

}
except(EXCEPTION_EXECUTE_HANDLER)
{
}

for C++ code or try {} except(EXCEPTION_EXECUTE_HANDLER){} for C code.

> When I copy the data to Irp->UserBuffer – it works fine.
>
> What am I doing wrong?

I have no idea, given that I can’t see your code. One guess is that you have
not specified an ‘output buffer’ in your invocation of DeviceControl from
user space, or you specified a length that is way too small.

> Can I be sure that the Irp->AssociatedIrp.SystemBuffer area is
> at the size of the user’s buffer? How can I get the size of this
> buffer from the IRP?

You have to look at the IoStack parameters for the IOCTL to see how big the
output buffer is. My guess is that it is zero length. Read about
IoGetCurrentIrpStackLocation().

>
> As you can see, these are all newbie’s questions… I think
> that there’s something here I just don’t get… ;-(
>

The DDK has lots of source examples one or more of which most likely
actually processes a METHOD_BUFFERED IOCTL with an output buffer. Search the
sample drivers for something that does what you are trying to do.

> One more things: The chapter numbers that you specified in your
> previous message - to which DDK do they apply? the win2k DDK?
> (Currently, only the NT4 DDK is installed on my machine :wink: )
>

W2K of course, get with the program :slight_smile:

Mark Roddy
xxxxx@hollistech.com
www.hollistech.com
WindowsNT Windows 2000 Consulting Services


You are currently subscribed to ntdev as: xxxxx@microsoft.com
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

begin 600 winmail.dat
M>)^(BD50:0"``$```````!``$``0&gt;0!@(Y`0```````#H``$%@`,`<br>M#@```-`'#``3``P`(``9``(`/0$!"8`!`"$S,#0T-3=“0T0U0T4X,C1#
M0C!&035”,C-“.$(Q,$%”-P!!P$@@,#@-`'#``3``T`(``F``(`2P$!<br>M#8`$``(````"``(``0.0!@"&lt;&amp;.@$``.0`3S`W,^FG``1\`,4`!````<br>M#@#,-0U#,.0P```````#!I!\`,$`!````#@```#,`-0`U<br>M`#,`.0`P```````#`!E!`20!````;@```%L;@!T&amp;090!V%T
M(!2$4.@@&amp;,;P!P'D:0!N&amp;&lt;(!D&$=!A"``9@!R&`;0@<br>M&L90!R&X90!L";0!O`&amp;0`90`@`'0`;P`@`'4`<p>M9`!E``````!$X@$xxxxx@O!IP$“5L``0```$``````````@2L?I+ZC$!F=<br>M;@#=0]4@````!2;V1D&gt;2P@36%R:P!33510$UAMM'(:P!?%(;P!D&amp;0>0!',`=`!R`&amp;$`=`!U`',`+@!C`&amp;\`;0``````<br>M'P!:$84@!O&amp;09!Y"P(!-&amp;$<@!K'P`R0`$N
M30!A`'(`:P!?`%(`;P!D`&amp;0`&gt;0!``',`=`!R`&amp;$`=`!U`',`+@!C`&amp;\`<br>M;0```````@%&lt;``$<4TU44#I-05)+7U)/1$190%-44D%455,N0T]-<br>M``,`'4```````@%&gt;``$```!``````````($K'Z2^HQ`9G6X`W0$/5`(`````<br>M4F]D9'DL($UA<fl>M”@%,`30!4`%```````!\`:0`!````+@$T80!R&L7P!2&`
M9!D’D0!S'0<@!A'0=0!S"X8P!O&amp;T``````!\70!````&amp;```<br>M%(;P!D&0&gt;0L"``30!A’(:P```!\,T!````+@```$T80!R&amp;L
M7P!2&amp;\9!D’D0!S'0<@!A'0=0!S"X8P!O&amp;T```````(!7P!
M'%–5%Z34%22U]23T1$64!35%)!5%53+D-/30#!Y```````(!<br>M&lt;0!&amp;P:?-,\IH14,?PO42DB^;R@[E(CO4``=-X-``?'00
M#@```!.%0(!$&amp;4=@!E&amp;P;P!P&amp;4<@!S"``20!N’090!R&4<br>M<p>M``HM$P
_Q#%$5@A5![(1U0Y1P'=$-<r>M&amp;,\.,#4[$=(,8&amp;,4L)60S-I,18NE-"0BI&lt;#K*]9!G%/*HQ'C'&gt;@T<br>M%/``/"%$3T-465``12!(5$U,(%``54),24,@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&amp;-8@<br>M,C#_&amp;AKPN_>Y4TH4?0&N"<M)PD#R4?NVA/DT-CY]9'P&gt;\8$[T&gt;@&gt;6#&amp;L339#10&D;7R=[EM"1LQ!I
M>:T0>4#N;31P>(!.(&:@M(&%IOY.PW/\M?WMW/-!K\2TL,^M-S-(0!4R=L"RH%^\L#!_,8\RGSI,-3LQ<br>M+_Q&amp;3^D.XGU)R/@]38VXI_:#R(-M,\CS9&-S(^0390.XZ3#<@#Y00DR!
M;N!+455/5$4^OSU"WV<_\3;2&\VT5RNE!91%U$$GO3'$WH3Y!2%1-<br>M3#N?4Z@!\1Q!````'@```&amp;T90!S',80!G&amp;4+P!R&amp;88PX#(<br>M,@```````PF```````##8```````,WC_I_0```P#Q/PD$```#/T_Y0<br>M!`<!````;@```%L`;@!T`&amp;0`90!V`%T`(`!2`$4`.@`@`&amp;,`;P!P`'D`<br>M:0!N`&amp;&lt;`(`!D`&amp;$`=`!A`"9@!R`&`;0`@`&L`90!R`&X`90!L`“;0!O<br>M`&amp;0`90`@`'0`;P`@`'4`<p>M]!``````"P#U$``````+`/80`````!\`\Q`!````A@```%(`10`E`#,`00`@<br>M`%L`;@!T`&amp;0`90!V`%T`(`!2`$4`)0`S`$$`(`!C`&amp;\`&lt;`!Y`&amp;D`;@!G`"
M9`!A`'0`80`@`&8`<@!O`&T`(`!K`&4`<@!N`&4`;@`&amp;T`;P!D`&amp;4`(`!T<br>M`&amp;\`(`!U`',`90!R`";0!O`&0`90`N`$4`30!,```````”`4<0```#(`<br>M!C/553.V$]34-).W`];7-F=#ML/5)%1"U-4TM6BTS-30X`````@'Y/P$```!1`````````-RG0,C`0A`:M+D(`“LOX8(!````
M`````”]//4U)0U)/4T]&5"]/53U.3U)42$%-15))0T$O0TX]4D5#25!)14Y4
M4R]#3CTS-34S.3``````‘P#X/P$````:````10!L`&D`>0!A`’,`(`!9`&$`
M:P!U`&(``````!`.$`!````#@```#,`-0`U`#,`.0`P```````"`?L
`0<br>M`%$`````````W*=`R,!"$!JTN0@`*R_A@@$`````````+T\]34E#4D]33T94<br>M+T]5/4Y/4E1(04U%4DE#02]#3CU214-)4$E%3E13+T-./3,U-3,Y,``````?<br>M`/H_`0```!H```!%`&amp;P`:0!Y`&amp;$`<p>M```.````,P`U`#4`,P`Y`#```````$!S"4-6:^FG4"#!$U
,U`VK`
M`1`&@`!````$@```$D`4`!-`“X`3@!/`%0`10``````‘P`W$```!V````<br>M4@!%`#H`(`!;`&amp;X`=`!D`&amp;4`=@!=`"4@!%`#H`(`!C`&`<`!Y`&D`;@!G
M`"9`!A`'0`80`@`&amp;8`&lt;@!O`&amp;T`(`!K`&amp;4`&lt;@!N`&amp;4`;@`&T`;P!D`&4`
M(`!T`&`(`!U`’,`90!R`”;0!O`&amp;0`90``````'P`]$````*````4@!%
M`#H`(```````'P`=#@$```!N````6P!N`'0`9`!E`'8`70`@`%(`10`Z`”<br>M8P!O`'>0!I`&X`9P`@`&0`80!T`&$`(`!F`‘(`;P!M`“:P!E`'(`;@!E<br>M`&amp;P`(`!M`&amp;\`9`!E`"=`!O`”=0!S`&amp;4`&lt;@`@`&amp;T`;P!D`&amp;4``````!\`<br>M-1`!````G@```#P`,Q`#(`10`T`$0`.S`#4`-Y`#$`00`R`#0`.Y<br>M`$(`0@`S`$(`.0!!`$0`-P!&amp;`#.V`$8`1@`R`#0`.X`#4`.`!%`$<br>M&lt;@!E`&amp;0`+0!M`',`9P`M`#-0`N`’(`90!D`&T`;P!N`&0`+@!C`&`<@!P
M`"X`;0!I`&,`<@!O`',`;P!F`'0`+@!C`&`;0`^```````+`"D```````L`
M(P```````P`&$/*T"<#&lt;02P@```,`$!```````P`1$``````&gt;@0`0<br>M`&amp;4```!(5%10.B\O4U504$]25$U)0U)/4T]&amp;5$-/32]355!03U)4+TM"+T%2<br>M5$E#3$53+U$xxxxx@O,R\Q-T%34#Q(5%10.B\O4U504$]25$U)0U)/4T]&amp;5$-/<br>M32]355!03U)4+TM"+T%2``````(!?P`!````3P```#PP,3)%-$0X,S4T.3%!<br>M,C0X.4)",T(Y040W1C`X-D9&amp;,C0X.#4X14!R960M;7-G+3`U+G)E9&amp;UO;F0N<br>78V]R&lt;"YM:6-R;W-O9G0N8V]M/@;U@=
`
end


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

</t.>

</http:>


  1. AA_6?")X:(D0>44B=XK_><^,SXW?CN^/
    M_YB_EL^7W_>;?YR/G9Y7:"]L"_B.R##(+M"!O8G8SH30[TOD5^2; ↩︎

> 1. How can I know that the pointer I get from the user is valid (i.e.:
isn’t

NULL,
but merely garbage). I want to avoid from crashing the machine…:wink:

Do the copying under try/catch. Note that this is possible from
PASSIVE_LEVEL only.

  1. Should I lock things in memory before the copy? What should I lock? The
    IRP? How?

No. You are at PASSIVE_LEVEL during the copying - so, you can left the
buffer unlocked.

  1. Can the address I get from the user be swapped before I get to write to
    it
    (thus creating a page fault)? How can I avoid it?

Yes, but this fault is OK. You cannot touch user memory from > PASSIVE_LEVEL
because try/catch do not work from such a high IRQL.

Max


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

>

FYI:The DDK Macro definition of RtlCopyMemory is:
#define RtlCopyMemory(Destination,Source,Length)
memcpy((Destination),(Source),(Length))

Fine and dandy, but what happens if for W2k+1 it isn’t? Then I have to go
and figure out why my driver won’t even load. I am more than willing to use
undocumented interfaces WHEN THERE IS NO ALTERNATIVE, but I haven’t a clue
why I should do this when there is a documented supported interface.

Mark Roddy
Windows 2000/NT Consultant
Hollis Technology Solutions
www.hollistech.com


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com

Hello, Thomas and Mark (and all others too :slight_smile: )

I think I’m getting a clearer picture now, but there are
some issues that I still need some info about, but first,
please let me explain what I did:

I took the packet.sys driver, as is. Then, I’ve noticed that it
misses network data. This is because it has an internal “queue”
of “requests” for packets. I.e.: If the user did not do ReadFile(),
before a packet has arrived, the packet that was indicated
was simply dropped.

In order to overcome this problem, I built an internal queue,
that resides in a non-paged area. Then, I copy the data that is
indicated by the network card to this internal structure (using
a simple memcpy(), without any kind of lock whatsoever). I know
that there are Ndis functions for this, but memcpy seems to be much
faster. In addition, since most NICs use DMA – at the moment
of indication, the entire packet resides in memory, so the
indication is actually considered by me as “packet is ready”.
I don’t mind if this thing won’t work on devices that do not
use DMA.

When the user does a ReadFile(), the entire structure
is copied to the user buffer, again - without any locks.

This thing worked for quite a long time, but it started
to do problems on platforms running VLAN adapters (which
have an IM driver instead of a NIC driver. This IM driver
is “talking” to a special MAC driver, to simulate multiple
network cards).
I think that the problem is the “short cuts” I took when
coding this thing, so, here are my questions:

  1. Where should I use locking before copying?
  2. Which copy functions should I use?
  3. The BSOD appears with the “IRQ LESS OR EQUAL” error. What does this mean?
    How do I prevent this? In which IRQL do the Indication routines run?
    In which IRQL do the IRP_MJ_READ and IRP_MJ_WRITE work? Can I control it?
    What should I do if I run in a higher IRQL? How do I check it?

I do not expect to find a complete answer here (since I know you all have a
life :wink: ),
but some pointers to places I can search in, or some advice by the
gurus will help me a lot.

thanks in advance (and sorry for being lengthy),

  • Barak

-----Original Message-----
From: Thomas F. Divine [mailto:xxxxx@pcausa.com]
Sent: Tuesday, December 19, 2000 8:35 PM
To: NT Developers Interest List
Subject: [ntdev] RE: copying data from kernel mode to user mode

Barak,

You need to be careful.

The as-is Packet.sys sample uses ReadFile and WriteFile to read and write
packets. It uses DO_DIRECT_IO for read and write; this is NOT the same as
METHOD_BUFFERED.

So, I presume that you have defined some new IOCTL codes to read and write
packets differently from the way Packet.sys was originally designed.

One question. Why did you decide to use DeviceIoControl instead of
ReadFile/WriteFile? Seems to work OK…

Anyway, if you are using DeviceIoControl the I/o method is speficied in the
IOCTL. See the CTL_CODE macro in the DDK devioctl.h file.

As I mentioned in a previous message, look up all references to
METHOD_BUFFERED in the DDK help file. They will show the way.

Aslo see the “IOCTL” sample on the Microsoft website. Go to:

http:

Look in the Windows NT 4.0 Samples for IOCTL.

There are other samples there that may also be of interest.

Some of these may be included with the Windows 2000 DDK, but I am not sure.

Good luck,

Thomas F. Divine

PCAUSA - Toolkits & Resources For Network Software Developers
NDIS Protocol - NDIS Intermediate - TDI Client
http: - http:

----- Original Message -----
From: Barak Mandelovich
To: NT Developers Interest List
Cc:
Sent: Tuesday, December 19, 2000 1:01 PM
Subject: [ntdev] RE: copying data from kernel mode to user mode

> Hi, Mark, and 10x for your reply!
>
> I have some follow-up questions:
> When I copy my buffer to the user, may I use memcpy()?
>
> Now, let me be more specific:
> I’m using the packet.sys sample from the DDK.
> I changed it a bit, to store any frame that arrives
> in a non-paged area. When a user does a DeviceIoControl()
> to receive it (METHOD_BUFFERED, as far I as know, and checked the code),
> I copy the buffer to the IRP.
>
> I changed the code, to copy data to the Irp->AssociatedIrp.SystemBuffer
> field, but then I always get a blue screen (exception not handled).
>
> I did a try- catch around it, ( __catch(0) ), but I still get the
> “exception unhandled” blue screen.
>
> When I copy the data to Irp->UserBuffer – it works fine.
>
> What am I doing wrong?
> Can I be sure that the Irp->AssociatedIrp.SystemBuffer area is
> at the size of the user’s buffer? How can I get the size of this
> buffer from the IRP?
>
> As you can see, these are all newbie’s questions… I think
> that there’s something here I just don’t get… ;-(
>
> One more things: The chapter numbers that you specified in your
> previous message - to which DDK do they apply? the win2k DDK?
> (Currently, only the NT4 DDK is installed on my machine :wink: )
>
>
> thanks very very much in advance,
>
> - Barak
>
>
>
> -----Original Message-----
> From: Roddy, Mark [mailto:xxxxx@stratus.com]
> Sent: Tuesday, December 19, 2000 4:13 PM
> To: NT Developers Interest List
> Subject: [ntdev] RE: copying data from kernel mode to user mode
>
>
>
> Barak,
> >
> > The internal structure is allocated in the following manner:
> > ExAllocatePool (NonPagedPool, size);
> >
> > The copy process itself is straight-forward, since I copy
> > the data to the IRP->UserBuffer I get.
>
> Except of course that unless the IOCTL is defined using METHOD_NEITHER
this
> is the wrong place to look for the users data buffer, EVEN IF IT HAPPENS
TO
> WORK.
>
> If on the other hand the IOCTL is defined using METHOD_NEITHER, then this
is
> indeed the output (going to the user) buffer location.
>
> For more information see 13.3 Defining I/O Control Codes in the DDK.
>
> > My questions are:
> >
> > 1. How can I know that the pointer I get from the user is
> > valid (i.e.: isn’t
> > NULL,
> > but merely garbage). I want to avoid from crashing the
> > machine…:wink:
>
> For Irp->UserBuffer you don’t know you have to find out, see 16.10.2
Errors
> in Referencing User-Space Addresses. If on the other hand your IOCTL is
> defined using METHOD_BUFFERED then the OS has done all this checking for
you
> and put a SAFE pointer to the user output buffer at
> Irp->AssociatedIrp.SystemBuffer.
>
> > 2. Should I lock things in memory before the copy? What
> > should I lock? The
> > IRP? How?
>
> This depends on how the IOCTL is defined. (See above.) Typically private
> IOCTLs are going to use METHOD_BUFFERED and then you don’t have to do
> anything. You never ‘lock an Irp’. You might have to do something about
> UserBuffer, but only if you were going to DMA in or out of it of the
system.
> If you are just going to copy data into user space from kernel space then
> you just copy the data. You might want to put an exception handler around
> the copy in case things go very wrong. See 16.10.2 Errors in Referencing
> User-Space Addresses.
>
> > 3. Can I assume that my internal structure is never swapped out?
>
> It is nonpaged pool so it is not pageable. NT does not have anything
> resembling swapping, but non-paged pool is by definition not going to be
> paged out.
>
> > 4. Can the address I get from the user be swapped before I
> > get to write to
> > it
> > (thus creating a page fault)? How can I avoid it?
> >
>
> You don’t. The system handles page faults all on its own. As long as you
are
> in the correct process context and are running at less than DISPATCH_LEVEL
> and as long as the user has given you a valid virtual address and length,
> page faults are not an issue. Once again see 16.10.2 Errors in Referencing
> User-Space Addresses. And once again most private IOCTLS are going to use
> METHOD_BUFFERED, and then all of your concerns other than “where is the
> user’s data buffer” are non-issues.
>
> Mark Roddy
> xxxxx@hollistech.com
> www.hollistech.com
> WindowsNT Windows 2000 Consulting Services


You are currently subscribed to ntdev as: $subst(‘Recip.EmailAddr’)
To unsubscribe send a blank email to leave-ntdev-$subst(‘Recip.MemberIDChar’)@lists.osr.com</http:></http:></http:>