+MmMapLockedPagesSpecifyCache()
[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(Mdl->MdlFlags&MDL_MAPPED_TO_SYSTEM_VA);
109         g_return_if_fail(BaseAddress!=Mdl->MappedSystemVa);
110
111         Mdl->MappedSystemVa=NULL;
112         Mdl->MdlFlags&=~MDL_MAPPED_TO_SYSTEM_VA;
113 }
114
115 /**
116  * MmMapLockedPagesSpecifyCache:
117  * @Mdl: Pointer to the #MDL specification structure to map.
118  * %NULL value is forbidden.
119  * @AccessMode: Access at which to lock the pages. %KernelMode required by libcaptive.
120  * @CacheType: Suggested caching type. %MmNonCached, %MmCached or %MmWriteCombined required by libcaptive.
121  * @BaseAddress: Required block base address if @AccessMode is %UserMode. Ignored by libcaptive.
122  * @BugCheckOnFailure: Whether to call KeBugCheck() instead of returning %NULL. Ignored by libcaptive.
123  * @Priority: Suggested pages priority. %LowPagePriority, %NormalPagePriority or %HighPagePriority required by libcaptive.
124  *
125  * Leaves the pages at their original location as they are never moved anyway
126  * by libcaptive. This call is just a wrapper around MmMapLockedPages() in libcaptive.
127  *
128  * Returns: Address of the mapped pages base.
129  * This offset does not neet to be %PAGE_SIZE aligned.
130  */
131 PVOID MmMapLockedPagesSpecifyCache(IN PMDL Mdl,
132                 IN KPROCESSOR_MODE AccessMode,IN MEMORY_CACHING_TYPE CacheType,IN PVOID BaseAddress,
133                 IN ULONG BugCheckOnFailure,IN MM_PAGE_PRIORITY Priority)
134 {
135         g_return_val_if_fail(Mdl!=NULL,NULL);
136         g_return_val_if_fail(AccessMode==KernelMode /* || AccessMode==UserMode */ ,NULL);
137         g_return_val_if_fail(0
138                                         || CacheType==MmNonCached
139                                         || CacheType==MmCached
140                                         || CacheType==MmWriteCombined,
141                         NULL);
142         g_return_val_if_fail(0
143                                         || Priority==LowPagePriority
144                                         || Priority==NormalPagePriority
145                                         || Priority==HighPagePriority,
146                         NULL);
147
148         return MmMapLockedPages(Mdl,AccessMode);
149 }