ReactOS 0.4.16-dev-340-g0540c21
mem.c
Go to the documentation of this file.
1/*
2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT: ReactOS kernel
4 * PURPOSE: GDI Driver Memory Management Functions
5 * FILE: win32ss/gdi/eng/mem.c
6 * PROGRAMER: Jason Filby
7 */
8
9#include <win32k.h>
10
11#define NDEBUG
12#include <debug.h>
13
14/*
15 * @implemented
16 */
28{
29 PVOID pvBaseAddress;
30
31 pvBaseAddress = ExAllocatePoolWithTag((fl & FL_NONPAGED_MEMORY) ?
34 ulTag);
35
36 if (pvBaseAddress == NULL)
37 return NULL;
38
39 if (fl & FL_ZERO_MEMORY)
40 RtlZeroMemory(pvBaseAddress, cjMemSize);
41
42 return pvBaseAddress;
43}
44
45/*
46 * @implemented
47 */
48VOID
50EngFreeMem(PVOID pvBaseAddress)
51{
52 /* Windows allows to pass NULL */
53 if (pvBaseAddress)
54 {
55 /* Use 0 as tag, which equals a call to ExFreePool */
56 ExFreePoolWithTag(pvBaseAddress, 0);
57 }
58}
59
60/*
61 * @implemented
62 */
65__drv_allocatesMem(UserMem)
69EngAllocUserMem(
72{
73 PVOID pvBaseAddress = NULL;
75
76 Status = ZwAllocateVirtualMemory(NtCurrentProcess(),
77 &pvBaseAddress,
78 0,
79 &cjMemSize,
82
83 if (!NT_SUCCESS(Status))
84 {
85 return NULL;
86 }
87
88 /* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
89 //hSecure = EngSecureMem(pvBaseAddress, cj);
90
91 return pvBaseAddress;
92}
93
94/*
95 * @implemented
96 */
97VOID
99EngFreeUserMem(PVOID pvBaseAddress)
100{
101 SIZE_T cjSize = 0;
102
103 ZwFreeVirtualMemory(NtCurrentProcess(),
104 &pvBaseAddress,
105 &cjSize,
107
108 /* TODO: Remove allocation info from AVL tree */
109}
110
111PVOID
115 IN SIZE_T Size,
117 OUT PVOID *SafeAddress)
118{
120 PMDL pmdl;
122
125 else return NULL;
126
128 if (pmdl == NULL)
129 {
130 return NULL;
131 }
132
134 {
136 }
138 {
140 }
142
143 if (!NT_SUCCESS(Status))
144 {
145 IoFreeMdl(pmdl);
146 return NULL;
147 }
148
150
151 if(!*SafeAddress)
152 {
153 MmUnlockPages(pmdl);
154 IoFreeMdl(pmdl);
155 return NULL;
156 }
157
158 return pmdl;
159}
160
161VOID
164 IN PVOID SecureHandle)
165{
166 PMDL pmdl = (PMDL)SecureHandle;
167
168 MmUnlockPages(pmdl);
169 IoFreeMdl(pmdl);
170}
171
172/*
173 * @implemented
174 */
175HANDLE
178{
179 {// HACK!!!
181 {
183 }
185 {
186 _SEH2_YIELD(return NULL);
187 }
188 _SEH2_END;
189 return (HANDLE)-1;
190 }
192}
193
194HANDLE
197{
198 {// HACK!!!
199 ULONG cPages;
200 volatile BYTE *pjProbe;
201
203 {
207 while(cPages--)
208 {
209 /* Do a read probe */
210 (void)*pjProbe;
211 pjProbe += PAGE_SIZE;
212 }
213 }
215 {
216 _SEH2_YIELD(return NULL);
217 }
218 _SEH2_END;
219 return (HANDLE)-1;
220 }
222}
223
224/*
225 * @implemented
226 */
229{
230 if (Mem == (HANDLE)-1) return; // HACK!!!
232}
233
234/* EOF */
LONG NTSTATUS
Definition: precomp.h:26
#define NULL
Definition: types.h:112
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:33
#define APIENTRY
Definition: api.h:79
#define PAGE_READONLY
Definition: compat.h:138
#define __drv_allocatesMem(kind)
Definition: driverspecs.h:257
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
#define PAGE_SIZE
Definition: env_spec_w32.h:49
#define NonPagedPool
Definition: env_spec_w32.h:307
#define PagedPool
Definition: env_spec_w32.h:308
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
FP_OP Operation
Definition: fpcontrol.c:150
#define IoFreeMdl
Definition: fxmdl.h:89
#define IoAllocateMdl
Definition: fxmdl.h:88
Status
Definition: gdiplustypes.h:25
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:90
VOID NTAPI MmProbeAndLockPages(IN PMDL Mdl, IN KPROCESSOR_MODE AccessMode, IN LOCK_OPERATION Operation)
Definition: mdlsup.c:931
VOID NTAPI MmUnlockPages(IN PMDL Mdl)
Definition: mdlsup.c:1435
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1109
@ NormalPagePriority
Definition: imports.h:54
#define _Ret_opt_bytecap_(size)
Definition: ms_sal.h:1227
#define _Ret_opt_bytecount_(size)
Definition: ms_sal.h:1252
#define UserMode
Definition: asm.h:35
#define _Must_inspect_result_
Definition: no_sal2.h:62
#define _In_
Definition: no_sal2.h:158
#define _When_(c, a)
Definition: no_sal2.h:38
#define PAGE_READWRITE
Definition: nt_native.h:1304
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define MEM_RESERVE
Definition: nt_native.h:1314
#define MEM_RELEASE
Definition: nt_native.h:1316
#define MEM_COMMIT
Definition: nt_native.h:1313
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:102
VOID NTAPI MmUnsecureVirtualMemory(IN PVOID SecureMem)
Definition: virtual.c:2807
PVOID NTAPI MmSecureVirtualMemory(IN PVOID Address, IN SIZE_T Length, IN ULONG Mode)
Definition: virtual.c:2794
static WCHAR Address[46]
Definition: ping.c:68
#define EngFreeMem
Definition: polytest.cpp:56
#define FL_ZERO_MEMORY
Definition: polytest.cpp:58
void * EngAllocMem(int zero, unsigned long size, int tag=0)
Definition: polytest.cpp:70
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:165
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:66
#define _SEH2_END
Definition: pseh2_64.h:155
#define _SEH2_TRY
Definition: pseh2_64.h:55
#define _SEH2_YIELD(__stmt)
Definition: pseh2_64.h:168
#define STATUS_SUCCESS
Definition: shellext.h:65
ULONG_PTR SIZE_T
Definition: typedefs.h:80
#define RtlZeroMemory(Destination, Length)
Definition: typedefs.h:262
#define IN
Definition: typedefs.h:39
uint32_t ULONG
Definition: typedefs.h:59
#define OUT
Definition: typedefs.h:40
#define ALIGN_DOWN_POINTER_BY(ptr, align)
Definition: umtypes.h:82
PVOID PMDL
Definition: usb.h:39
_Must_inspect_result_ _In_ WDFDEVICE _In_ PWDF_DEVICE_PROPERTY_DATA _In_ DEVPROPTYPE _In_ ULONG Size
Definition: wdfdevice.h:4533
HANDLE APIENTRY EngSecureMem(PVOID Address, ULONG Length)
Definition: mem.c:177
VOID APIENTRY EngUnsecureMem(HANDLE Mem)
Definition: mem.c:228
PVOID APIENTRY HackSecureVirtualMemory(IN PVOID Address, IN SIZE_T Size, IN ULONG ProbeMode, OUT PVOID *SafeAddress)
Definition: mem.c:113
VOID APIENTRY HackUnsecureVirtualMemory(IN PVOID SecureHandle)
Definition: mem.c:163
HANDLE APIENTRY EngSecureMemForRead(PVOID Address, ULONG Length)
Definition: mem.c:196
VOID APIENTRY EngFreeUserMem(PVOID pvBaseAddress)
Definition: mem.c:99
_In_ FLONG fl
Definition: winddi.h:1279
_In_ ULONG _In_ ULONG ulTag
Definition: winddi.h:3942
_Must_inspect_result_ _In_ ULONG cjMemSize
Definition: winddi.h:1381
#define ENGAPI
Definition: winddi.h:48
_In_ ULONG cjSize
Definition: winddi.h:3634
#define FL_NONPAGED_MEMORY
Definition: winddi.h:1359
enum _LOCK_OPERATION LOCK_OPERATION
@ IoReadAccess
Definition: ketypes.h:863
@ IoModifyAccess
Definition: ketypes.h:865
#define MmGetSystemAddressForMdlSafe(_Mdl, _Priority)
_Must_inspect_result_ _In_ _In_ ULONG ProbeMode
Definition: mmfuncs.h:561
#define ADDRESS_AND_SIZE_TO_SPAN_PAGES(_Va, _Size)
unsigned char BYTE
Definition: xxhash.c:193