MmUnmapLockedPages(): +Support for MmBuildMdlForNonPagedPool() MDLs
[captive.git] / src / libcaptive / mm / mdl.c
1 /* $Id$
2  * reactos MDL emulation of libcaptive
3  * Copyright (C) 2002 Jan Kratochvil <project-captive@jankratochvil.net>
4  * 
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; exactly version 2 of June 1991 is required
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19
20 #include "config.h"
21
22 #include "reactos/ddk/mmfuncs.h"        /* self */
23 #include <glib/gmessages.h>
24
25
26 /**
27  * MmProbeAndLockPages:
28  * @Mdl: Pointer to the #MDL specification structure to probe and lock.
29  * %NULL value is forbidden.
30  * @AccessMode: Access at which to lock the pages. %KernelMode required by libcaptive.
31  * @Operation: One of %IoReadAccess, %IoWriteAccess or %IoModifyAccess.
32  *
33  * Probles the specified @AccessMode and locks those pages specified by @Mdl to memory
34  * with the desired @AccessMode.
35  * libcaptive does NOP here as it just sets the %MDL_PAGES_LOCKED flag.
36  */
37 VOID MmProbeAndLockPages(PMDL Mdl,KPROCESSOR_MODE AccessMode,LOCK_OPERATION Operation)
38 {
39         g_return_if_fail(Mdl!=NULL);
40         g_return_if_fail(AccessMode==KernelMode /* || AccessMode==UserMode */);
41
42         Mdl->MdlFlags|=MDL_PAGES_LOCKED;
43 }
44
45
46 /**
47  * MmUnlockPages:
48  * @Mdl: Pointer to the #MDL specification structure to unlock.
49  * %NULL value is forbidden.
50  *
51  * Unlock the pages specified by @Mdl from memory.
52  * libcaptive does NOP here as it just clears the %MDL_PAGES_LOCKED flag.
53  */
54 VOID MmUnlockPages(PMDL Mdl)
55 {
56         g_return_if_fail(Mdl!=NULL);
57
58         Mdl->MdlFlags&=~MDL_PAGES_LOCKED;
59 }
60
61
62 /**
63  * MmMapLockedPages:
64  * @Mdl: Pointer to the #MDL specification structure to map.
65  * %NULL value is forbidden.
66  * @AccessMode: Access at which to lock the pages. %KernelMode required by libcaptive.
67  *
68  * Leaves the pages at their original location as they are never moved anyway
69  * by libcaptive.
70  *
71  * Returns: Address of the mapped pages base.
72  * This offset does not neet to be %PAGE_SIZE aligned.
73  */
74 PVOID MmMapLockedPages(PMDL Mdl,KPROCESSOR_MODE AccessMode)
75 {
76         g_return_val_if_fail(Mdl!=NULL,NULL);
77         g_assert(Mdl->StartVa!=NULL);
78         g_return_val_if_fail(AccessMode==KernelMode /* || AccessMode==UserMode */ ,NULL);
79
80         if (Mdl->MdlFlags&MDL_MAPPED_TO_SYSTEM_VA) {
81                 /* already mapped */
82                 g_assert(Mdl->MappedSystemVa==Mdl->StartVa+Mdl->ByteOffset);
83                 }
84         else {
85                 /* new mapping */
86                 Mdl->MappedSystemVa=Mdl->StartVa+Mdl->ByteOffset;
87                 Mdl->MdlFlags|=MDL_MAPPED_TO_SYSTEM_VA;
88                 }
89
90         return Mdl->MappedSystemVa;
91 }
92
93
94 /**
95  * MmUnmapLockedPages:
96  * @BaseAddress: Address of the mapped pages base.
97  * This offset does not neet to be %PAGE_SIZE aligned.
98  * @Mdl: Pointer to the #MDL specification structure to unmap.
99  *
100  * Declares the specified @Mdl pages as unmapped. @BaseAddress
101  * is required to be previously returned by MmMapLockedPages().
102  * You must not call this function twice without MmMapLockedPages() between.
103  */
104 VOID MmUnmapLockedPages(PVOID BaseAddress,PMDL Mdl)
105 {
106         g_return_if_fail(BaseAddress!=NULL);
107         g_return_if_fail(Mdl!=NULL);
108         g_return_if_fail(BaseAddress==Mdl->MappedSystemVa);
109
110         /* No mapping is done for pages from MmBuildMdlForNonPagedPool(). */
111         if (Mdl->MdlFlags&MDL_SOURCE_IS_NONPAGED_POOL) {
112                 g_return_if_fail(!(Mdl->MdlFlags&MDL_MAPPED_TO_SYSTEM_VA));
113                 }
114         else {
115                 g_return_if_fail(Mdl->MdlFlags&MDL_MAPPED_TO_SYSTEM_VA);
116                 Mdl->MdlFlags&=~MDL_MAPPED_TO_SYSTEM_VA;
117                 }
118         Mdl->MappedSystemVa=NULL;
119 }
120
121 /**
122  * MmMapLockedPagesSpecifyCache:
123  * @Mdl: Pointer to the #MDL specification structure to map.
124  * %NULL value is forbidden.
125  * @AccessMode: Access at which to lock the pages. %KernelMode required by libcaptive.
126  * @CacheType: Suggested caching type. %MmNonCached, %MmCached or %MmWriteCombined required by libcaptive.
127  * @BaseAddress: Required block base address if @AccessMode is %UserMode. Ignored by libcaptive.
128  * @BugCheckOnFailure: Whether to call KeBugCheck() instead of returning %NULL. Ignored by libcaptive.
129  * @Priority: Suggested pages priority. %LowPagePriority, %NormalPagePriority or %HighPagePriority required by libcaptive.
130  *
131  * Leaves the pages at their original location as they are never moved anyway
132  * by libcaptive. This call is just a wrapper around MmMapLockedPages() in libcaptive.
133  *
134  * Returns: Address of the mapped pages base.
135  * This offset does not neet to be %PAGE_SIZE aligned.
136  */
137 PVOID MmMapLockedPagesSpecifyCache(IN PMDL Mdl,
138                 IN KPROCESSOR_MODE AccessMode,IN MEMORY_CACHING_TYPE CacheType,IN PVOID BaseAddress,
139                 IN ULONG BugCheckOnFailure,IN MM_PAGE_PRIORITY Priority)
140 {
141         g_return_val_if_fail(Mdl!=NULL,NULL);
142         g_return_val_if_fail(AccessMode==KernelMode /* || AccessMode==UserMode */ ,NULL);
143         g_return_val_if_fail(0
144                                         || CacheType==MmNonCached
145                                         || CacheType==MmCached
146                                         || CacheType==MmWriteCombined,
147                         NULL);
148         g_return_val_if_fail(0
149                                         || Priority==LowPagePriority
150                                         || Priority==NormalPagePriority
151                                         || Priority==HighPagePriority,
152                         NULL);
153
154         return MmMapLockedPages(Mdl,AccessMode);
155 }