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
18 _When_(fl
& FL_ZERO_MEMORY
, _Ret_opt_bytecount_(cjMemSize
))
19 _When_(!(fl
& FL_ZERO_MEMORY
), _Ret_opt_bytecap_(cjMemSize
))
20 __drv_allocatesMem(Mem
)
31 pvBaseAddress
= ExAllocatePoolWithTag((fl
& FL_NONPAGED_MEMORY
) ?
32 NonPagedPool
: PagedPool
,
36 if (pvBaseAddress
== NULL
)
39 if (fl
& FL_ZERO_MEMORY
)
40 RtlZeroMemory(pvBaseAddress
, cjMemSize
);
50 EngFreeMem(PVOID pvBaseAddress
)
52 /* Windows allows to pass NULL */
55 /* Use 0 as tag, which equals a call to ExFreePool */
56 ExFreePoolWithTag(pvBaseAddress
, 0);
64 _Ret_opt_bytecount_(cjMemSize
)
65 __drv_allocatesMem(UserMem
)
70 _In_ SIZE_T cjMemSize
,
73 PVOID pvBaseAddress
= NULL
;
76 Status
= ZwAllocateVirtualMemory(NtCurrentProcess(),
80 MEM_COMMIT
| MEM_RESERVE
,
83 if (!NT_SUCCESS(Status
))
88 /* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
89 //hSecure = EngSecureMem(pvBaseAddress, cj);
99 EngFreeUserMem(PVOID pvBaseAddress
)
103 ZwFreeVirtualMemory(NtCurrentProcess(),
108 /* TODO: Remove allocation info from AVL tree */
113 HackSecureVirtualMemory(
117 OUT PVOID
*SafeAddress
)
119 NTSTATUS Status
= STATUS_SUCCESS
;
121 LOCK_OPERATION Operation
;
123 if (ProbeMode
== PAGE_READONLY
) Operation
= IoReadAccess
;
124 else if (ProbeMode
== PAGE_READWRITE
) Operation
= IoModifyAccess
;
127 pmdl
= IoAllocateMdl(Address
, (ULONG
)Size
, FALSE
, TRUE
, NULL
);
135 MmProbeAndLockPages(pmdl
, UserMode
, Operation
);
137 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
139 Status
= _SEH2_GetExceptionCode();
143 if (!NT_SUCCESS(Status
))
149 *SafeAddress
= MmGetSystemAddressForMdlSafe(pmdl
, NormalPagePriority
);
163 HackUnsecureVirtualMemory(
164 IN PVOID SecureHandle
)
166 PMDL pmdl
= (PMDL
)SecureHandle
;
177 EngSecureMem(PVOID Address
, ULONG Length
)
182 ProbeForWrite(Address
, Length
, 1);
184 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
186 _SEH2_YIELD(return NULL
);
191 return MmSecureVirtualMemory(Address
, Length
, PAGE_READWRITE
);
196 EngSecureMemForRead(PVOID Address
, ULONG Length
)
200 volatile BYTE
*pjProbe
;
204 ProbeForRead(Address
, Length
, 1);
205 cPages
= ADDRESS_AND_SIZE_TO_SPAN_PAGES(Address
, Length
);
206 pjProbe
= ALIGN_DOWN_POINTER_BY(Address
, PAGE_SIZE
);
209 /* Do a read probe */
211 pjProbe
+= PAGE_SIZE
;
214 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
216 _SEH2_YIELD(return NULL
);
221 return MmSecureVirtualMemory(Address
, Length
, PAGE_READONLY
);
228 EngUnsecureMem(HANDLE Mem
)
230 if (Mem
== (HANDLE
)-1) return; // HACK!!!
231 MmUnsecureVirtualMemory((PVOID
) Mem
);