summaryrefslogblamecommitdiffstats
path: root/private/ntos/afd/buffer.c
blob: 11d6723601a26b995dc47af8c787323056d2a6a0 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651










































































































































































































































































































































































































































































































































































































































































                                                                                 
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    buffer.c

Abstract:

    This module contains routines for handling non-bufferring TDI
    providers.  The AFD interface assumes that bufferring will be done
    below AFD; if the TDI provider doesn't buffer, then AFD must.

Author:

    David Treadwell (davidtr)    21-Feb-1992

Revision History:

--*/

#include "afdp.h"

VOID
AfdInitializeBuffer (
    IN PAFD_BUFFER AfdBuffer,
    IN CLONG BufferDataSize,
    IN CLONG AddressSize
    );

#ifdef ALLOC_PRAGMA
#pragma alloc_text( PAGEAFD, AfdAllocateBuffer )
#pragma alloc_text( PAGEAFD, AfdCalculateBufferSize )
#pragma alloc_text( PAGEAFD, AfdInitializeBuffer )
#pragma alloc_text( PAGEAFD, AfdGetBuffer )
#pragma alloc_text( PAGEAFD, AfdGetBufferChain )
#pragma alloc_text( PAGEAFD, AfdReturnBuffer )
#pragma alloc_text( PAGEAFD, AfdReturnBufferChain )
#if DBG
#pragma alloc_text( PAGEAFD, AfdFreeBufferPool )
#endif
#endif


PVOID
AfdAllocateBuffer (
    IN POOL_TYPE PoolType,
    IN ULONG NumberOfBytes,
    IN ULONG Tag
    )

/*++

Routine Description:

    Used by the lookaside list allocation function to allocate a new
    AFD buffer structure.  The returned structure will be fully
    initialized.

Arguments:

    PoolType - passed to ExAllocatePoolWithTag.

    NumberOfBytes - the number of bytes required for the data buffer
        portion of the AFD buffer.

    Tag - passed to ExAllocatePoolWithTag.

Return Value:

    PVOID - a fully initialized PAFD_BUFFER, or NULL if the allocation
        attempt fails.

--*/

{
    PAFD_BUFFER afdBuffer;
    ULONG bytesRequired;

    //
    // The requested length must be the same as one of the standard
    // AFD buffer sizes.
    //

    ASSERT( NumberOfBytes == AfdSmallBufferSize ||
            NumberOfBytes == AfdMediumBufferSize ||
            NumberOfBytes == AfdLargeBufferSize );

    //
    // Determine how much data we'll actually need for the buffer.
    //

    bytesRequired = AfdCalculateBufferSize(
                        NumberOfBytes,
                        AfdStandardAddressLength
                        );

    //
    // Get nonpaged pool for the buffer.
    //

    afdBuffer = AFD_ALLOCATE_POOL( PoolType, bytesRequired, Tag );
    if ( afdBuffer == NULL ) {
        return NULL;
    }

    //
    // Initialize the buffer and return a pointer to it.
    //

    AfdInitializeBuffer( afdBuffer, NumberOfBytes, AfdStandardAddressLength );

    return afdBuffer;


} // AfdAllocateBuffer


CLONG
AfdCalculateBufferSize (
    IN CLONG BufferDataSize,
    IN CLONG AddressSize
    )

/*++

Routine Description:

    Determines the size of an AFD buffer structure given the amount of
    data that the buffer contains.

Arguments:

    BufferDataSize - data length of the buffer.

    AddressSize - length of address structure for the buffer.

Return Value:

    Number of bytes needed for an AFD_BUFFER structure for data of
    this size.

--*/

{
    CLONG irpSize;
    CLONG mdlSize;
    CLONG bufferSize;

    ASSERT( BufferDataSize != 0 );
    ASSERT( AfdCacheLineSize < 100 );

    //
    // Determine the sizes of the various components of an AFD_BUFFER
    // structure.  Note that these are all worst-case calculations--
    // actual sizes of the MDL and the buffer may be smaller.
    //

    irpSize = IoSizeOfIrp( AfdIrpStackSize ) + 8;
    bufferSize = BufferDataSize + AfdCacheLineSize;
    mdlSize = MmSizeOfMdl( (PVOID)(PAGE_SIZE-1), bufferSize );

    return ( (sizeof(AFD_BUFFER) + irpSize + mdlSize +
              AddressSize + bufferSize + 3) & ~3);

} // AfdCalculateBufferSize


PAFD_BUFFER
AfdGetBuffer (
    IN CLONG BufferDataSize,
    IN CLONG AddressSize
    )

/*++

Routine Description:

    Obtains a buffer of the appropriate size for the caller.  Uses
    the preallocated buffers if possible, or else allocates a new buffer
    structure if required.

Arguments:

    BufferDataSize - the size of the data buffer that goes along with the
        buffer structure.

    AddressSize - size of the address field required for the buffer.

Return Value:

    PAFD_BUFFER - a pointer to an AFD_BUFFER structure, or NULL if one
        was not available or could not be allocated.

--*/

{
    PAFD_BUFFER afdBuffer;
    CLONG bufferSize;
    PLIST_ENTRY listEntry;
    PNPAGED_LOOKASIDE_LIST lookasideList;

    //
    // If possible, allocate the buffer from one of the lookaside lists.
    //

    if ( AddressSize <= AfdStandardAddressLength &&
             BufferDataSize <= AfdLargeBufferSize ) {

        if ( BufferDataSize <= AfdSmallBufferSize ) {

            lookasideList = &AfdLookasideLists->SmallBufferList;
            BufferDataSize = AfdSmallBufferSize;

        } else if ( BufferDataSize <= AfdMediumBufferSize ) {

            lookasideList = &AfdLookasideLists->MediumBufferList;
            BufferDataSize = AfdMediumBufferSize;

        } else {

            lookasideList = &AfdLookasideLists->LargeBufferList;
            BufferDataSize = AfdLargeBufferSize;
        }

        afdBuffer = ExAllocateFromNPagedLookasideList( lookasideList );
#if DBG
        if ( afdBuffer != NULL ) {

            RtlGetCallersAddress(
                &afdBuffer->Caller,
                &afdBuffer->CallersCaller
                );
        }
#endif

        return afdBuffer;

    }

    //
    // Couldn't find an appropriate buffer that was preallocated.
    // Allocate one manually.  If the buffer size requested was
    // zero bytes, give them four bytes.  This is because some of
    // the routines like MmSizeOfMdl() cannot handle getting passed
    // in a length of zero.
    //
    // !!! It would be good to ROUND_TO_PAGES for this allocation
    //     if appropriate, then use entire buffer size.
    //

    if ( BufferDataSize == 0 ) {
        BufferDataSize = sizeof(ULONG);
    }

    bufferSize = AfdCalculateBufferSize( BufferDataSize, AddressSize );

    afdBuffer = AFD_ALLOCATE_POOL(
                    NonPagedPool,
                    bufferSize,
                    AFD_DATA_BUFFER_POOL_TAG
                    );

    if ( afdBuffer == NULL ) {
        return NULL;
    }

    //
    // Initialize the AFD buffer structure and return it.
    //

    AfdInitializeBuffer( afdBuffer, BufferDataSize, AddressSize );

    return afdBuffer;

} // AfdGetBuffer


PAFD_BUFFER
AfdGetBufferChain (
    IN CLONG BufferDataSize
    )
{
    PAFD_BUFFER afdBuffer;
    PAFD_BUFFER bufferChain;
    PAFD_BUFFER *bufferChainTarget;
    PMDL mdlChain;
    PMDL *mdlChainTarget;
    CLONG currentBufferSize;
#if DBG
    CLONG totalChainLength = BufferDataSize;
#endif

    //
    // Sanity check.
    //

    ASSERT( BufferDataSize > AfdBufferLengthForOnePage );

    //
    // Setup so we know how to cleanup.
    //

    bufferChain = NULL;
    mdlChain = NULL;
    bufferChainTarget = &bufferChain;
    mdlChainTarget = &mdlChain;

    //
    // Loop, acquiring & chaining the buffers.
    //

    while ( BufferDataSize > 0 ) {

        //
        // Acquire a new buffer.  If this fails, we're toast.
        //

        currentBufferSize = max( BufferDataSize, AfdBufferLengthForOnePage );

        afdBuffer = AfdGetBuffer( currentBufferSize, 0 );

        if ( afdBuffer == NULL ) {

            break;

        }

        //
        // Chain it on.
        //

        *bufferChainTarget = afdBuffer;
        bufferChainTarget = &afdBuffer->NextBuffer;

        *mdlChainTarget = afdBuffer->Mdl;
        mdlChainTarget = &afdBuffer->Mdl->Next;

        BufferDataSize -= currentBufferSize;

    }

    if ( BufferDataSize == 0 ) {

        ASSERT( bufferChain != NULL );
        ASSERT( afdBuffer != NULL );
        ASSERT( currentBufferSize > 0 );
        ASSERT( currentBufferSize <= AfdBufferLengthForOnePage );

        //
        // Set the byte count in the final MDL in the chain.
        //

        afdBuffer->Mdl->ByteCount = currentBufferSize;
        SET_CHAIN_LENGTH( afdBuffer, totalChainLength );

        return bufferChain;

    }

    //
    //  Error, time to cleanup.
    //

    while( bufferChain != NULL ) {

        afdBuffer = bufferChain->NextBuffer;

        bufferChain->Mdl->Next = NULL;
        bufferChain->NextBuffer = NULL;
        RESET_CHAIN_LENGTH( bufferChain );
        AfdReturnBuffer( bufferChain );

        bufferChain = afdBuffer;

    }

    return NULL;

} // AfdGetBufferChain


VOID
AfdReturnBuffer (
    IN PAFD_BUFFER AfdBuffer
    )

/*++

Routine Description:

    Returns an AFD buffer to the appropriate global list, or frees
    it if necessary.

Arguments:

    AfdBuffer - points to the AFD_BUFFER structure to return or free.

Return Value:

    None.

--*/

{
    PNPAGED_LOOKASIDE_LIST lookasideList;

    //
    // Most of the AFD buffer must be zeroed when returning the buffer.
    //

    ASSERT( AfdBuffer->DataOffset == 0 );
    ASSERT( !AfdBuffer->ExpeditedData );
    ASSERT( AfdBuffer->TdiInputInfo.UserDataLength == 0 );
    ASSERT( AfdBuffer->TdiInputInfo.UserData == NULL );
    ASSERT( AfdBuffer->TdiInputInfo.OptionsLength == 0 );
    ASSERT( AfdBuffer->TdiInputInfo.Options == NULL );
    ASSERT( AfdBuffer->TdiInputInfo.RemoteAddressLength == 0 );
    ASSERT( AfdBuffer->TdiInputInfo.RemoteAddress == NULL );
    ASSERT( AfdBuffer->TdiOutputInfo.UserDataLength == 0 );
    ASSERT( AfdBuffer->TdiOutputInfo.UserData == NULL );
    ASSERT( AfdBuffer->TdiOutputInfo.OptionsLength == 0 );
    ASSERT( AfdBuffer->TdiOutputInfo.Options == NULL );
    ASSERT( AfdBuffer->TdiOutputInfo.RemoteAddressLength == 0 );
    ASSERT( AfdBuffer->TdiOutputInfo.RemoteAddress == NULL );

    ASSERT( AfdBuffer->Mdl->ByteCount == AfdBuffer->BufferLength );
    ASSERT( AfdBuffer->Mdl->Next == NULL );
    ASSERT( AfdBuffer->FileObject == NULL );
    ASSERT( AfdBuffer->NextBuffer == NULL );
    ASSERT( AfdBuffer->TotalChainLength == AfdBuffer->BufferLength );

#if DBG
    AfdBuffer->Caller = NULL;
    AfdBuffer->CallersCaller = NULL;
#endif

    //
    // If appropriate, return the buffer to one of the AFD buffer
    // lookaside lists.
    //

    if ( AfdBuffer->AllocatedAddressLength == AfdStandardAddressLength &&
             AfdBuffer->BufferLength <= AfdLargeBufferSize ) {

        if ( AfdBuffer->BufferLength == AfdSmallBufferSize ) {

            lookasideList = &AfdLookasideLists->SmallBufferList;

        } else if ( AfdBuffer->BufferLength == AfdMediumBufferSize ) {

            lookasideList = &AfdLookasideLists->MediumBufferList;

        } else {

            ASSERT( AfdBuffer->BufferLength == AfdLargeBufferSize );
            lookasideList = &AfdLookasideLists->LargeBufferList;
        }

        ExFreeToNPagedLookasideList( lookasideList, AfdBuffer );

        return;
    }

    //
    // The buffer was not from a lookaside list allocation, so just free
    // the pool we used for it.
    //

    AFD_FREE_POOL(
        AfdBuffer,
        AFD_DATA_BUFFER_POOL_TAG
        );

    return;

} // AfdReturnBuffer

VOID
AfdReturnBufferChain (
    IN PAFD_BUFFER AfdBuffer
    )

/*++

Routine Description:

    Returns an AFD buffer chain to the appropriate global list, or frees
    it if necessary.

Arguments:

    AfdBuffer - points to the AFD_BUFFER structure to return or free.

Return Value:

    None.

--*/

{
    PAFD_BUFFER nextBuffer;

    while ( AfdBuffer != NULL ) {

        nextBuffer = AfdBuffer->NextBuffer;

        AfdBuffer->NextBuffer = NULL;
        AfdBuffer->Mdl->Next = NULL;
        AfdBuffer->Mdl->ByteCount = AfdBuffer->BufferLength;
        RESET_CHAIN_LENGTH( AfdBuffer );
        AfdReturnBuffer( AfdBuffer );

        AfdBuffer = nextBuffer;

    }

} // AfdReturnBufferChain


VOID
AfdInitializeBuffer (
    IN PAFD_BUFFER AfdBuffer,
    IN CLONG BufferDataSize,
    IN CLONG AddressSize
    )

/*++

Routine Description:

    Initializes an AFD buffer.  Sets up fields in the actual AFD_BUFFER
    structure and initializes the IRP and MDL associated with the
    buffer.  This routine assumes that the caller has properly allocated
    sufficient space for all this.

Arguments:

    AfdBuffer - points to the AFD_BUFFER structure to initialize.

    BufferDataSize - the size of the data buffer that goes along with the
        buffer structure.

    AddressSize - the size of data allocated for the address buffer.

    ListHead - the global list this buffer belongs to, or NULL if it
        doesn't belong on any list.  This routine does NOT place the
        buffer structure on the list.

Return Value:

    None.

--*/

{
    CLONG irpSize;
    CLONG mdlSize;

    //
    // Initialize the IRP pointer and the IRP itself.
    //

    AfdBuffer->Irp = (PIRP)(( ((ULONG)(AfdBuffer + 1)) + 7) & ~7);
    irpSize = IoSizeOfIrp( AfdIrpStackSize );

    IoInitializeIrp( AfdBuffer->Irp, (USHORT)irpSize, AfdIrpStackSize );

    //
    // Set up the MDL pointer but don't build it yet.  We have to wait
    // until after the data buffer is built to build the MDL.
    //

    mdlSize = MmSizeOfMdl( (PVOID)(PAGE_SIZE-1), BufferDataSize );

    AfdBuffer->Mdl = (PMDL)( (PCHAR)AfdBuffer->Irp + irpSize );

    //
    // Set up the address buffer pointer.
    //

    AfdBuffer->SourceAddress = (PCHAR)AfdBuffer->Mdl + mdlSize;
    AfdBuffer->AllocatedAddressLength = (USHORT)AddressSize;

    //
    // Initialize the TDI information structures.
    //

    RtlZeroMemory( &AfdBuffer->TdiInputInfo, sizeof(AfdBuffer->TdiInputInfo) );
    RtlZeroMemory( &AfdBuffer->TdiOutputInfo, sizeof(AfdBuffer->TdiOutputInfo) );

    //
    // Set up the data buffer pointer and length.  Note that the buffer
    // MUST begin on a cache line boundary so that we can use the fast
    // copy routines like RtlCopyMemory on the buffer.
    //

    AfdBuffer->Buffer = (PVOID)
        ( ( (ULONG)AfdBuffer->SourceAddress + AddressSize +
                AfdCacheLineSize - 1 ) & ~(AfdCacheLineSize - 1) );

    AfdBuffer->BufferLength = BufferDataSize;
    RESET_CHAIN_LENGTH( AfdBuffer );

    //
    // Now build the MDL and set up a pointer to the MDL in the IRP.
    //

    MmInitializeMdl( AfdBuffer->Mdl, AfdBuffer->Buffer, BufferDataSize );
    MmBuildMdlForNonPagedPool( AfdBuffer->Mdl );

    AfdBuffer->Irp->MdlAddress = AfdBuffer->Mdl;
    AfdBuffer->DataOffset = 0;
    AfdBuffer->ExpeditedData = FALSE;
    AfdBuffer->PartialMessage = FALSE;
    AfdBuffer->FileObject = NULL;

    //
    // By default, buffers are not part of a chain.
    //

    AfdBuffer->NextBuffer = NULL;


#if DBG
    AfdBuffer->BufferListEntry.Flink = (PVOID)0xE0E1E2E3;
    AfdBuffer->BufferListEntry.Blink = (PVOID)0xE4E5E6E7;
    AfdBuffer->Caller = NULL;
    AfdBuffer->CallersCaller = NULL;
#endif

} // AfdInitializeBuffer


#if DBG
VOID
NTAPI
AfdFreeBufferPool(
    IN PVOID Block
    )
{

    AFD_FREE_POOL(
        Block,
        AFD_DATA_BUFFER_POOL_TAG
        );

} // AfdFreeBufferPool
#endif