:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / ntoskrnl / mm / ncache.c
1 /* $Id$
2  *
3  * COPYRIGHT:       See COPYING in the top level directory
4  * PROJECT:         ReactOS kernel
5  * FILE:            ntoskrnl/mm/cont.c
6  * PURPOSE:         Manages non-cached memory
7  * PROGRAMMER:      David Welch (welch@cwcom.net)
8  * UPDATE HISTORY:
9  *                  Created 22/05/98
10  */
11
12 /* INCLUDES *****************************************************************/
13
14 #include <ddk/ntddk.h>
15 #include <internal/mm.h>
16 #include <internal/ps.h>
17
18 #define NDEBUG
19 #include <internal/debug.h>
20
21 /* FUNCTIONS *****************************************************************/
22
23
24 /**********************************************************************
25  * NAME                                                 EXPORTED
26  *      MmAllocateNonCachedMemory@4
27  *
28  * DESCRIPTION
29  *      Allocates a virtual address range of noncached and cache
30  *      aligned memory.
31  *      
32  * ARGUMENTS
33  *      NumberOfBytes
34  *              Size of region to allocate.
35  *              
36  * RETURN VALUE
37  *      The base address of the range on success;
38  *      NULL on failure.
39  *
40  * NOTE
41  *      Description taken from include/ddk/mmfuncs.h.
42  *      Code taken from ntoskrnl/mm/special.c.
43  *
44  * REVISIONS
45  *
46  */
47 PVOID STDCALL 
48 MmAllocateNonCachedMemory(IN ULONG NumberOfBytes)
49 {
50    PVOID Result;
51    MEMORY_AREA* marea;
52    NTSTATUS Status;
53    ULONG i;
54    ULONG Attributes;
55
56    MmLockAddressSpace(MmGetKernelAddressSpace());
57    Result = NULL;
58    Status = MmCreateMemoryArea (NULL,
59                                 MmGetKernelAddressSpace(),
60                                 MEMORY_AREA_NO_CACHE,
61                                 &Result,
62                                 NumberOfBytes,
63                                 0,
64                                 &marea,
65                                 FALSE);
66    MmUnlockAddressSpace(MmGetKernelAddressSpace());
67
68    if (!NT_SUCCESS(Status))
69      {
70         return (NULL);
71      }
72    Attributes = PAGE_READWRITE | PAGE_SYSTEM | PAGE_NOCACHE | 
73      PAGE_WRITETHROUGH;
74    for (i = 0; i <= (NumberOfBytes / PAGE_SIZE); i++)
75      {
76        PHYSICAL_ADDRESS NPage;
77
78        Status = MmRequestPageMemoryConsumer(MC_NPPOOL, TRUE, &NPage);
79        MmCreateVirtualMapping (NULL,
80                                Result + (i * PAGE_SIZE),
81                                Attributes,
82                                NPage,
83                                TRUE);
84      }
85    return ((PVOID)Result);
86 }
87
88 VOID STATIC
89 MmFreeNonCachedPage(PVOID Context, MEMORY_AREA* MemoryArea, PVOID Address, 
90                     PHYSICAL_ADDRESS PhysAddr, SWAPENTRY SwapEntry, 
91                     BOOLEAN Dirty)
92 {
93   assert(SwapEntry == 0);
94   if (PhysAddr.QuadPart != 0)
95     {
96       MmReleasePageMemoryConsumer(MC_NPPOOL, PhysAddr);
97     }
98 }
99
100 /**********************************************************************
101  * NAME                                                 EXPORTED
102  *      MmFreeNonCachedMemory@8
103  *
104  * DESCRIPTION
105  *      Releases a range of noncached memory allocated with 
106  *      MmAllocateNonCachedMemory.
107  *      
108  * ARGUMENTS
109  *      BaseAddress
110  *              Virtual address to be freed;
111  *              
112  *      NumberOfBytes
113  *              Size of the region to be freed.
114  *              
115  * RETURN VALUE
116  *      None.
117  *
118  * NOTE
119  *      Description taken from include/ddk/mmfuncs.h.
120  *      Code taken from ntoskrnl/mm/special.c.
121  *
122  * REVISIONS
123  *
124  */
125 VOID STDCALL MmFreeNonCachedMemory (IN PVOID BaseAddress,
126                                     IN ULONG NumberOfBytes)
127 {
128   MmFreeMemoryArea (MmGetKernelAddressSpace(),
129                     BaseAddress,
130                     NumberOfBytes,
131                     MmFreeNonCachedPage,
132                     NULL);
133 }
134
135
136 /* EOF */