[YAROTOWS] Reintegrate the branch. For a brighter future.
[reactos.git] / reactos / subsystems / win32 / win32k / eng / mem.c
index ea821b0..1fb81b3 100644 (file)
@@ -12,9 +12,9 @@
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
- *  You should have received a copy of the GNU General Public License
- *  along with this program; if not, write to the Free Software
- *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 /* $Id$
  *
  *                 3/7/1999: Created
  */
 
-#include <w32k.h>
+#include <win32k.h>
 
 #define NDEBUG
 #include <debug.h>
 
-typedef struct _USERMEMHEADER
-  {
-  ULONG Tag;
-  ULONG MemSize;
-  }
-USERMEMHEADER, *PUSERMEMHEADER;
-
 /*
  * @implemented
  */
-PVOID STDCALL
+PVOID APIENTRY
 EngAllocMem(ULONG Flags,
            ULONG MemSize,
            ULONG Tag)
@@ -62,7 +55,7 @@ EngAllocMem(ULONG Flags,
 /*
  * @implemented
  */
-VOID STDCALL
+VOID APIENTRY
 EngFreeMem(PVOID Mem)
 {
   ExFreePool(Mem);
@@ -71,56 +64,121 @@ EngFreeMem(PVOID Mem)
 /*
  * @implemented
  */
-PVOID STDCALL
-EngAllocUserMem(ULONG cj, ULONG Tag)
+PVOID APIENTRY
+EngAllocUserMem(SIZE_T cj, ULONG Tag)
 {
   PVOID NewMem = NULL;
   NTSTATUS Status;
-  ULONG MemSize = sizeof(USERMEMHEADER) + cj;
-  PUSERMEMHEADER Header;
+  SIZE_T MemSize = cj;
 
-  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &NewMem, 0, &MemSize, MEM_COMMIT, PAGE_READWRITE);
+  Status = ZwAllocateVirtualMemory(NtCurrentProcess(), &NewMem, 0, &MemSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
 
   if (! NT_SUCCESS(Status))
     {
       return NULL;
     }
 
-  Header = (PUSERMEMHEADER) NewMem;
-  Header->Tag = Tag;
-  Header->MemSize = cj;
+  /* TODO: Add allocation info to AVL tree (stored inside W32PROCESS structure) */
+  //hSecure = EngSecureMem(NewMem, cj);
 
-  return (PVOID)(Header + 1);
+  return NewMem;
 }
 
 /*
  * @implemented
  */
-VOID STDCALL
+VOID APIENTRY
 EngFreeUserMem(PVOID pv)
 {
-  PUSERMEMHEADER Header = ((PUSERMEMHEADER) pv) - 1;
-  ULONG MemSize = sizeof(USERMEMHEADER) + Header->MemSize;
+  PVOID BaseAddress = pv;
+  SIZE_T MemSize = 0;
+
+  ZwFreeVirtualMemory(NtCurrentProcess(), &BaseAddress, &MemSize, MEM_RELEASE);
+
+  /* TODO: Remove allocation info from AVL tree */
+}
+
+
+
+PVOID
+APIENTRY
+HackSecureVirtualMemory(
+       IN PVOID Address,
+       IN SIZE_T Size,
+       IN ULONG ProbeMode,
+       OUT PVOID *SafeAddress)
+{
+       NTSTATUS Status = STATUS_SUCCESS;
+       PMDL mdl;
+       LOCK_OPERATION Operation;
+
+       if (ProbeMode == PAGE_READONLY) Operation = IoReadAccess;
+       else if (ProbeMode == PAGE_READWRITE) Operation = IoModifyAccess;
+       else return NULL;
+
+       mdl = IoAllocateMdl(Address, Size, FALSE, TRUE, NULL);
+       if (mdl == NULL)
+       {
+               return NULL;
+       }
+
+       _SEH2_TRY
+       {
+               MmProbeAndLockPages(mdl, UserMode, Operation);
+       }
+       _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER)
+       {
+               Status = _SEH2_GetExceptionCode();
+       }
+       _SEH2_END
+
+       if (!NT_SUCCESS(Status))
+       {
+               IoFreeMdl(mdl);
+               return NULL;
+       }
+
+       *SafeAddress = MmGetSystemAddressForMdlSafe(mdl, NormalPagePriority);
+
+       if(!*SafeAddress)
+       {
+               MmUnlockPages(mdl);
+               IoFreeMdl(mdl);
+               return NULL;           
+       }
+
+       return mdl;
+}
+
+VOID
+APIENTRY
+HackUnsecureVirtualMemory(
+       IN PVOID  SecureHandle)
+{
+       PMDL mdl = (PMDL)SecureHandle;
 
-  ZwFreeVirtualMemory(NtCurrentProcess(), (PVOID *) &Header, &MemSize, MEM_DECOMMIT);
+       MmUnlockPages(mdl);
+       IoFreeMdl(mdl);  
 }
 
 /*
  * @implemented
  */
-HANDLE STDCALL
+HANDLE APIENTRY
 EngSecureMem(PVOID Address, ULONG Length)
 {
+    return (HANDLE)-1; // HACK!!!
   return MmSecureVirtualMemory(Address, Length, PAGE_READWRITE);
 }
 
 /*
  * @implemented
  */
-VOID STDCALL
+VOID APIENTRY
 EngUnsecureMem(HANDLE Mem)
 {
-  return MmUnsecureVirtualMemory((PVOID) Mem);
+    if (Mem == (HANDLE)-1) return;  // HACK!!!
+  MmUnsecureVirtualMemory((PVOID) Mem);
 }
 
 /* EOF */