memuserkernel-r0drv-nt.cpp revision 0b53618bc8b692bf8ed1499ba786f5959295c633
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/* $Id$ */
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass/** @file
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass * IPRT - User & Kernel Memory, Ring-0 Driver, NT.
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo */
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo/*
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * Copyright (C) 2009 Oracle Corporation
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo *
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * This file is part of VirtualBox Open Source Edition (OSE), as
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * available from http://www.virtualbox.org. This file is free software;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * you can redistribute it and/or modify it under the terms of the GNU
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * General Public License (GPL) as published by the Free Software
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * Foundation, in version 2 as it comes in the "COPYING" file of the
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo *
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * The contents of this file may alternatively be used under the terms
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * of the Common Development and Distribution License Version 1.0
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * VirtualBox OSE distribution, in which case the provisions of the
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * CDDL are applicable instead of those of the GPL.
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo *
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * You may elect to license modified versions of this file under the
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo * terms and conditions of either the GPL or the CDDL or both.
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo */
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo/*******************************************************************************
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo* Header Files *
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo*******************************************************************************/
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#include "the-nt-kernel.h"
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#include <iprt/mem.h>
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#include <iprt/err.h>
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric FerraiuoloRTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo{
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __try
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo ProbeForRead((PVOID)R3PtrSrc, cb, 1);
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo memcpy(pvDst, (void const *)R3PtrSrc, cb);
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __except(EXCEPTION_EXECUTE_HANDLER)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VERR_ACCESS_DENIED;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VINF_SUCCESS;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo}
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric FerraiuoloRTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo{
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __try
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo ProbeForWrite((PVOID)R3PtrDst, cb, 1);
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo memcpy((void *)R3PtrDst, pvSrc, cb);
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __except(EXCEPTION_EXECUTE_HANDLER)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VERR_ACCESS_DENIED;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VINF_SUCCESS;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo}
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric FerraiuoloRTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo{
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#ifdef IPRT_TARGET_NT4
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo /* Play safe+wrong... it used to be a constant, but in w2k+ is a variable. */
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return R3Ptr < _2G;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#else
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return R3Ptr <= (uintptr_t)MM_HIGHEST_USER_ADDRESS;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#endif
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo}
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric FerraiuoloRTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo{
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#ifdef IPRT_TARGET_NT4
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo /* Play safe+wrong... it used to be a constant, but in w2k+ is a variable. */
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return (uintptr_t)pv >= _2G;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#else
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return (uintptr_t)pv >= (uintptr_t)MM_SYSTEM_RANGE_START;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo#endif
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo}
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric FerraiuoloRTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo{
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return true;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo}
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric FerraiuoloRTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo{
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __try
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo memcpy(pvDst, pvSrc, cb);
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __except(EXCEPTION_EXECUTE_HANDLER)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VERR_ACCESS_DENIED;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VINF_SUCCESS;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo}
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric FerraiuoloRTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo{
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __try
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo memcpy(pvDst, pvSrc, cb);
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo __except(EXCEPTION_EXECUTE_HANDLER)
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo {
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VERR_ACCESS_DENIED;
76ca635d61eb3f9fb7c9d788a44fa8b1690aa138Dav Glass }
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo return VINF_SUCCESS;
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo}
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo
f6a69e9e28fcc3728269713204c6e84fbc5b6095Eric Ferraiuolo