ReactOS  0.4.14-dev-293-g2b39b42
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  */
21 ENGAPI
22 PVOID
25  _In_ ULONG fl,
28 {
29  PVOID pvBaseAddress;
30 
31  pvBaseAddress = ExAllocatePoolWithTag((fl & FL_NONPAGED_MEMORY) ?
33  cjMemSize,
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  */
48 VOID
50 EngFreeMem(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)
66 ENGAPI
67 PVOID
69 EngAllocUserMem(
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  */
97 VOID
99 EngFreeUserMem(PVOID pvBaseAddress)
100 {
101  SIZE_T cjSize = 0;
102 
103  ZwFreeVirtualMemory(NtCurrentProcess(),
104  &pvBaseAddress,
105  &cjSize,
106  MEM_RELEASE);
107 
108  /* TODO: Remove allocation info from AVL tree */
109 }
110 
111 PVOID
112 APIENTRY
114  IN PVOID Address,
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 
133  _SEH2_TRY
134  {
136  }
138  {
140  }
141  _SEH2_END
142 
143  if (!NT_SUCCESS(Status))
144  {
145  IoFreeMdl(pmdl);
146  return NULL;
147  }
148 
149  *SafeAddress = MmGetSystemAddressForMdlSafe(pmdl, NormalPagePriority);
150 
151  if(!*SafeAddress)
152  {
153  MmUnlockPages(pmdl);
154  IoFreeMdl(pmdl);
155  return NULL;
156  }
157 
158  return pmdl;
159 }
160 
161 VOID
162 APIENTRY
164  IN PVOID SecureHandle)
165 {
166  PMDL pmdl = (PMDL)SecureHandle;
167 
168  MmUnlockPages(pmdl);
169  IoFreeMdl(pmdl);
170 }
171 
172 /*
173  * @implemented
174  */
175 HANDLE
176 APIENTRY
178 {
179  {// HACK!!!
180  _SEH2_TRY
181  {
183  }
185  {
186  _SEH2_YIELD(return NULL);
187  }
188  _SEH2_END;
189  return (HANDLE)-1;
190  }
192 }
193 
194 HANDLE
195 APIENTRY
197 {
198  {// HACK!!!
199  ULONG cPages;
200  volatile BYTE *pjProbe;
201 
202  _SEH2_TRY
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 */
#define IN
Definition: typedefs.h:38
#define _Must_inspect_result_
Definition: no_sal2.h:314
#define TRUE
Definition: types.h:120
VOID NTAPI RtlZeroMemory(PVOID Destination, SIZE_T Length)
Definition: mem.c:155
struct png_info_def **typedef void(__cdecl typeof(png_destroy_read_struct))(struct png_struct_def **
Definition: typeof.h:49
#define ENGAPI
Definition: winddi.h:48
LONG NTSTATUS
Definition: precomp.h:26
_Must_inspect_result_ _In_ _In_ ULONG ProbeMode
Definition: mmfuncs.h:562
VOID NTAPI MmUnlockPages(IN PMDL Mdl)
Definition: mdlsup.c:1439
#define MmGetSystemAddressForMdlSafe(_Mdl, _Priority)
PVOID PMDL
Definition: usb.h:39
#define MEM_COMMIT
Definition: nt_native.h:1313
VOID APIENTRY EngUnsecureMem(HANDLE Mem)
Definition: mem.c:228
VOID NTAPI ProbeForWrite(IN PVOID Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:143
_SEH2_TRY
Definition: create.c:4250
VOID APIENTRY EngFreeUserMem(PVOID pvBaseAddress)
Definition: mem.c:99
_Must_inspect_result_ _In_ ULONG cjMemSize
Definition: winddi.h:1381
_In_ FLONG fl
Definition: winddi.h:1279
#define FL_NONPAGED_MEMORY
Definition: winddi.h:1359
#define MEM_RESERVE
Definition: nt_native.h:1314
#define EXCEPTION_EXECUTE_HANDLER
Definition: excpt.h:85
smooth NULL
Definition: ftsmooth.c:416
VOID APIENTRY HackUnsecureVirtualMemory(IN PVOID SecureHandle)
Definition: mem.c:163
static WCHAR Address[46]
Definition: ping.c:68
#define ADDRESS_AND_SIZE_TO_SPAN_PAGES(_Va, _Size)
#define NtCurrentProcess()
Definition: nt_native.h:1657
#define __drv_allocatesMem(kind)
Definition: driverspecs.h:239
#define _SEH2_YIELD(STMT_)
Definition: pseh2_64.h:8
PVOID APIENTRY HackSecureVirtualMemory(IN PVOID Address, IN SIZE_T Size, IN ULONG ProbeMode, OUT PVOID *SafeAddress)
Definition: mem.c:113
#define NT_SUCCESS(StatCode)
Definition: apphelp.c:32
VOID APIENTRY EngFreeMem(PVOID pvBaseAddress)
Definition: mem.c:50
PVOID NTAPI MmSecureVirtualMemory(IN PVOID Address, IN SIZE_T Length, IN ULONG Mode)
Definition: virtual.c:2669
_In_ ULONG _In_ ULONG _In_ ULONG Length
Definition: ntddpcm.h:101
VOID NTAPI IoFreeMdl(PMDL Mdl)
Definition: iomdl.c:146
#define ExAllocatePoolWithTag(hernya, size, tag)
Definition: env_spec_w32.h:350
void * EngAllocMem(int zero, unsigned long size, int tag=0)
Definition: polytest.cpp:70
VOID NTAPI ProbeForRead(IN CONST VOID *Address, IN SIZE_T Length, IN ULONG Alignment)
Definition: exintrin.c:102
#define FL_ZERO_MEMORY
Definition: polytest.cpp:58
_Must_inspect_result_ _Ret_opt_bytecount_(cjMemSize)
Definition: mem.c:64
unsigned char BYTE
Definition: mem.h:68
HANDLE APIENTRY EngSecureMem(PVOID Address, ULONG Length)
Definition: mem.c:177
#define PAGE_SIZE
Definition: env_spec_w32.h:49
HANDLE APIENTRY EngSecureMemForRead(PVOID Address, ULONG Length)
Definition: mem.c:196
IN PVOID IN PVOID IN USHORT IN USHORT Size
Definition: pci.h:359
Status
Definition: gdiplustypes.h:24
PMDL NTAPI IoAllocateMdl(IN PVOID VirtualAddress, IN ULONG Length, IN BOOLEAN SecondaryBuffer, IN BOOLEAN ChargeQuota, IN PIRP Irp)
Definition: iomdl.c:22
#define _In_
Definition: no_sal2.h:204
_In_ ULONG _In_ ULONG ulTag
Definition: winddi.h:3941
ULONG_PTR SIZE_T
Definition: typedefs.h:78
_SEH2_END
Definition: create.c:4424
VOID NTAPI MmProbeAndLockPages(IN PMDL Mdl, IN KPROCESSOR_MODE AccessMode, IN LOCK_OPERATION Operation)
Definition: mdlsup.c:935
enum _LOCK_OPERATION LOCK_OPERATION
_Must_inspect_result_ _When_(fl &FL_ZERO_MEMORY, _Ret_opt_bytecount_(cjMemSize)) _When_(!(fl &FL_ZERO_MEMORY)
#define PAGE_READONLY
Definition: compat.h:127
#define MEM_RELEASE
Definition: nt_native.h:1316
#define OUT
Definition: typedefs.h:39
unsigned int ULONG
Definition: retypes.h:1
_In_ ULONG cjSize
Definition: winddi.h:3634
_In_ FLT_SET_CONTEXT_OPERATION Operation
Definition: fltkernel.h:1468
#define _SEH2_EXCEPT(...)
Definition: pseh2_64.h:6
#define ExFreePoolWithTag(_P, _T)
Definition: module.h:1099
#define _SEH2_GetExceptionCode()
Definition: pseh2_64.h:12
return STATUS_SUCCESS
Definition: btrfs.c:2966
#define ALIGN_DOWN_POINTER_BY(ptr, align)
Definition: umtypes.h:82
#define APIENTRY
Definition: api.h:79
VOID NTAPI MmUnsecureVirtualMemory(IN PVOID SecureMem)
Definition: virtual.c:2682
#define PAGE_READWRITE
Definition: nt_native.h:1304
_Must_inspect_result_ _Ret_opt_bytecap_(cjMemSize)
Definition: mem.c:19