summaryrefslogtreecommitdiffstats
path: root/private/ntos/mm/vadtree.c
blob: b5d214534d874c7ef97cfd0ff8eb3447d0ebbdfc (plain) (blame)
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
/*++

Copyright (c) 1989  Microsoft Corporation

Module Name:

    vadtree.c

Abstract:

    This module contains the routine to manipulate the virtual address
    descriptor tree.

Author:

    Lou Perazzoli (loup) 19-May-1989

Environment:

    Kernel mode only, working set mutex held, APC's disabled.

Revision History:

--*/

#include "mi.h"


VOID
MiInsertVad (
    IN PMMVAD Vad
    )

/*++

Routine Description:

    This function inserts a virtual address descriptor into the tree and
    reorders the splay tree as appropriate.

Arguments:

    Vad - Supplies a pointer to a virtual address descriptor


Return Value:

    None - An exception is raised if quota is exceeded.

--*/

{
    PMMADDRESS_NODE *Root;
    PEPROCESS CurrentProcess;
    ULONG RealCharge;
    ULONG PageCharge = 0;
    ULONG PagedQuotaCharged = 0;
    ULONG FirstPage;
    ULONG LastPage;
    ULONG PagedPoolCharge;
    ULONG ChargedPageFileQuota = FALSE;

    ASSERT (Vad->EndingVa > Vad->StartingVa);

    CurrentProcess = PsGetCurrentProcess();
    //ASSERT (KeReadStateMutant (&CurrentProcess->WorkingSetLock) == 0);

    //
    // Commit charge of MAX_COMMIT means don't charge quota.
    //

    if (Vad->u.VadFlags.CommitCharge != MM_MAX_COMMIT) {

        //
        // Charge quota for the nonpaged pool for the VAD.  This is
        // done here rather than by using ExAllocatePoolWithQuota
        // so the process object is not referenced by the quota charge.
        //

        PsChargePoolQuota (CurrentProcess, NonPagedPool, sizeof(MMVAD));

        try {

            //
            // Charge quota for the prototype PTEs if this is a mapped view.
            //

            if ((Vad->u.VadFlags.PrivateMemory == 0) &&
                (Vad->ControlArea != NULL)) {
                PagedPoolCharge =
                  ((ULONG)Vad->EndingVa - (ULONG)Vad->StartingVa) >>
                                                      (PAGE_SHIFT - PTE_SHIFT);
                PsChargePoolQuota (CurrentProcess, PagedPool, PagedPoolCharge);
                PagedQuotaCharged = PagedPoolCharge;
            }

            //
            // Add in the charge for page table pages.
            //

            FirstPage = MiGetPdeOffset (Vad->StartingVa);
            LastPage = MiGetPdeOffset (Vad->EndingVa);

            while (FirstPage <= LastPage) {

                if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageTables,
                                   FirstPage)) {
                    PageCharge += 1;
                }
                FirstPage += 1;
            }

            RealCharge = Vad->u.VadFlags.CommitCharge + PageCharge;

            if (RealCharge != 0) {

                MiChargePageFileQuota (RealCharge, CurrentProcess);
                ChargedPageFileQuota = TRUE;

#if 0 //commented out so page file quota is meaningful.
                if (Vad->u.VadFlags.PrivateMemory == 0) {

                    if ((Vad->ControlArea->FilePointer == NULL) &&
                        (Vad->u.VadFlags.PhysicalMapping == 0)) {

                        //
                        // Don't charge commitment for the page file space
                        // occupied by a page file section.  This will be
                        // charged as the shared memory is committed.
                        //

                        RealCharge -= BYTES_TO_PAGES ((ULONG)Vad->EndingVa -
                                                       (ULONG)Vad->StartingVa);
                    }
                }
#endif //0
                MiChargeCommitment (RealCharge, CurrentProcess);
                CurrentProcess->CommitCharge += RealCharge;
            }
        } except (EXCEPTION_EXECUTE_HANDLER) {

            //
            // Return any quotas charged thus far.
            //

            PsReturnPoolQuota (CurrentProcess, NonPagedPool, sizeof(MMVAD));

            if (PagedQuotaCharged != 0) {
                PsReturnPoolQuota (CurrentProcess, PagedPool, PagedPoolCharge);
            }

            if (ChargedPageFileQuota) {

                MiReturnPageFileQuota (RealCharge,
                                       CurrentProcess);
            }

            ExRaiseStatus (GetExceptionCode());
        }

        if (PageCharge != 0) {

            //
            // Since the committment was successful, charge the page
            // table pages.
            //

            FirstPage = MiGetPdeOffset (Vad->StartingVa);

            while (FirstPage <= LastPage) {

                if (!MI_CHECK_BIT (MmWorkingSetList->CommittedPageTables,
                                   FirstPage)) {
                    MI_SET_BIT (MmWorkingSetList->CommittedPageTables,
                                FirstPage);
                    MmWorkingSetList->NumberOfCommittedPageTables += 1;
                    ASSERT (MmWorkingSetList->NumberOfCommittedPageTables <
                                                                 PDE_PER_PAGE);
                }
                FirstPage += 1;
            }
        }
    }

    Root = (PMMADDRESS_NODE *)&CurrentProcess->VadRoot;

    //
    // Set the hint field in the process to this Vad.
    //

    CurrentProcess->VadHint = Vad;

    if (CurrentProcess->VadFreeHint != NULL) {
        if (((ULONG)((PMMVAD)CurrentProcess->VadFreeHint)->EndingVa + X64K) >=
                (ULONG)Vad->StartingVa) {
            CurrentProcess->VadFreeHint = Vad;
        }
    }

    MiInsertNode ( (PMMADDRESS_NODE)Vad, Root);
    return;
}

VOID
MiRemoveVad (
    IN PMMVAD Vad
    )

/*++

Routine Description:

    This function removes a virtual address descriptor from the tree and
    reorders the splay tree as appropriate.  If any quota or commitment
    was charged by the VAD (as indicated by the CommitCharge field) it
    is released.

Arguments:

    Vad - Supplies a pointer to a virtual address descriptor.

Return Value:

    None.

--*/

{
    PMMADDRESS_NODE *Root;
    PEPROCESS CurrentProcess;
    ULONG RealCharge;
    PLIST_ENTRY Next;
    PMMSECURE_ENTRY Entry;

    CurrentProcess = PsGetCurrentProcess();


    //
    // Commit charge of MAX_COMMIT means don't charge quota.
    //

    if (Vad->u.VadFlags.CommitCharge != MM_MAX_COMMIT) {

        //
        // Return the quota charge to the process.
        //

        PsReturnPoolQuota (CurrentProcess, NonPagedPool, sizeof(MMVAD));

        if ((Vad->u.VadFlags.PrivateMemory == 0) &&
            (Vad->ControlArea != NULL)) {
            PsReturnPoolQuota (CurrentProcess,
                               PagedPool,
                     ((ULONG)Vad->EndingVa - (ULONG)Vad->StartingVa) >> (PAGE_SHIFT - PTE_SHIFT));
        }

        RealCharge = Vad->u.VadFlags.CommitCharge;

        if (RealCharge != 0) {

            MiReturnPageFileQuota (RealCharge, CurrentProcess);

            if ((Vad->u.VadFlags.PrivateMemory == 0) &&
                (Vad->ControlArea != NULL)) {

#if 0 //commented out so page file quota is meaningful.
                if (Vad->ControlArea->FilePointer == NULL) {

                    //
                    // Don't release commitment for the page file space
                    // occupied by a page file section.  This will be charged
                    // as the shared memory is committed.
                    //

                    RealCharge -= BYTES_TO_PAGES ((ULONG)Vad->EndingVa -
                                                   (ULONG)Vad->StartingVa);
                }
#endif
            }

            MiReturnCommitment (RealCharge);
            CurrentProcess->CommitCharge -= RealCharge;
        }
    }

    if (Vad == CurrentProcess->VadFreeHint) {
        CurrentProcess->VadFreeHint = MiGetPreviousVad (Vad);
    }

    Root = (PMMADDRESS_NODE *)&CurrentProcess->VadRoot;

    MiRemoveNode ( (PMMADDRESS_NODE)Vad, Root);

    if (Vad->u.VadFlags.NoChange) {
        if (Vad->u2.VadFlags2.MultipleSecured) {

           //
           // Free the oustanding pool allocations.
           //

            Next = Vad->u3.List.Flink;
            do {
                Entry = CONTAINING_RECORD( Next,
                                           MMSECURE_ENTRY,
                                           List);

                Next = Entry->List.Flink;
                ExFreePool (Entry);
            } while (Next != &Vad->u3.List);
        }
    }

    //
    // If the VadHint was the removed Vad, change the Hint.

    if (CurrentProcess->VadHint == Vad) {
        CurrentProcess->VadHint = CurrentProcess->VadRoot;
    }

    return;
}

PMMVAD
FASTCALL
MiLocateAddress (
    IN PVOID VirtualAddress
    )

/*++

Routine Description:

    The function locates the virtual address descriptor which describes
    a given address.

Arguments:

    VirtualAddress - Supplies the virtual address to locate a descriptor
                     for.

Return Value:

    Returns a pointer to the virtual address descriptor which contains
    the supplied virtual address or NULL if none was located.

--*/

{
    PMMVAD FoundVad;
    PEPROCESS CurrentProcess;

    CurrentProcess = PsGetCurrentProcess();

    //ASSERT (KeReadStateMutant (&CurrentProcess->WorkingSetLock) == 0);

    if (CurrentProcess->VadHint == NULL) {
        return NULL;
    }

    if ((VirtualAddress >= ((PMMADDRESS_NODE)CurrentProcess->VadHint)->StartingVa) &&
        (VirtualAddress <= ((PMMADDRESS_NODE)CurrentProcess->VadHint)->EndingVa)) {

        return (PMMVAD)CurrentProcess->VadHint;
    }

    FoundVad = (PMMVAD)MiLocateAddressInTree ( VirtualAddress,
                   (PMMADDRESS_NODE *)&(CurrentProcess->VadRoot));

    if (FoundVad != NULL) {
        CurrentProcess->VadHint = (PVOID)FoundVad;
    }
    return FoundVad;
}

PVOID
MiFindEmptyAddressRange (
    IN ULONG SizeOfRange,
    IN ULONG Alignment,
    IN ULONG QuickCheck
    )

/*++

Routine Description:

    The function examines the virtual address descriptors to locate
    an unused range of the specified size and returns the starting
    address of the range.

Arguments:

    SizeOfRange - Supplies the size in bytes of the range to locate.

    Alignment - Supplies the alignment for the address.  Must be
                 a power of 2 and greater than the page_size.

    QuickCheck - Supplies a zero if a quick check for free memory
                 after the VadFreeHint exists, non-zero if checking
                 should start at the lowest address.

Return Value:

    Returns the starting address of a suitable range.

--*/

{
    PMMVAD NextVad;
    PMMVAD FreeHint;
    PEPROCESS CurrentProcess;

    CurrentProcess = PsGetCurrentProcess();
    //ASSERT (KeReadStateMutant (&CurrentProcess->WorkingSetLock) == 0);

    FreeHint = CurrentProcess->VadFreeHint;
    if ((QuickCheck == 0) && (FreeHint != NULL)) {
        NextVad = MiGetNextVad (FreeHint);
        if (NextVad == NULL) {

            if (SizeOfRange <
                (((ULONG)MM_HIGHEST_USER_ADDRESS + 1) -
                         (ULONG)MI_ROUND_TO_SIZE(FreeHint->EndingVa, Alignment))) {
                return (PMMADDRESS_NODE)MI_ROUND_TO_SIZE(FreeHint->EndingVa,
                                                         Alignment);
            }
        } else {

            if (SizeOfRange <
                ((ULONG)NextVad->StartingVa -
                         (ULONG)MI_ROUND_TO_SIZE(FreeHint->EndingVa, Alignment))) {

                //
                // Check to ensure that the ending address aligned upwards
                // is not greater than the starting address.
                //

                if ((ULONG)NextVad->StartingVa >
                        (ULONG)MI_ROUND_TO_SIZE(FreeHint->EndingVa,Alignment)) {
                    return (PMMADDRESS_NODE)MI_ROUND_TO_SIZE(FreeHint->EndingVa,
                                                           Alignment);
                }
            }
        }
    }

    return (PMMVAD)MiFindEmptyAddressRangeInTree (
                   SizeOfRange,
                   Alignment,
                   (PMMADDRESS_NODE)(CurrentProcess->VadRoot),
                   (PMMADDRESS_NODE *)&CurrentProcess->VadFreeHint);

}

#if DBG
VOID
VadTreeWalk (
    PMMVAD Start
    )

{
    Start;
    NodeTreeWalk ( (PMMADDRESS_NODE)(PsGetCurrentProcess()->VadRoot));
    return;
}
#endif //DBG