VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/nt/memuserkernel-r0drv-nt.cpp@ 106061

Last change on this file since 106061 was 106061, checked in by vboxsync, 3 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.6 KB
Line 
1/* $Id: memuserkernel-r0drv-nt.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * IPRT - User & Kernel Memory, Ring-0 Driver, NT.
4 */
5
6/*
7 * Copyright (C) 2009-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * The contents of this file may alternatively be used under the terms
26 * of the Common Development and Distribution License Version 1.0
27 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
28 * in the VirtualBox distribution, in which case the provisions of the
29 * CDDL are applicable instead of those of the GPL.
30 *
31 * You may elect to license modified versions of this file under the
32 * terms and conditions of either the GPL or the CDDL or both.
33 *
34 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
35 */
36
37
38/*********************************************************************************************************************************
39* Header Files *
40*********************************************************************************************************************************/
41#include "the-nt-kernel.h"
42
43#include <iprt/mem.h>
44#include <iprt/errcore.h>
45
46#include "internal-r0drv-nt.h"
47
48
49RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb)
50{
51 __try
52 {
53 ProbeForRead((PVOID)R3PtrSrc, cb, 1);
54 memcpy(pvDst, (void const *)R3PtrSrc, cb);
55 }
56 __except(EXCEPTION_EXECUTE_HANDLER)
57 {
58 return VERR_ACCESS_DENIED;
59 }
60 return VINF_SUCCESS;
61}
62
63
64RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb)
65{
66 __try
67 {
68 ProbeForWrite((PVOID)R3PtrDst, cb, 1);
69 memcpy((void *)R3PtrDst, pvSrc, cb);
70 }
71 __except(EXCEPTION_EXECUTE_HANDLER)
72 {
73 return VERR_ACCESS_DENIED;
74 }
75 return VINF_SUCCESS;
76}
77
78
79RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr)
80{
81#ifdef IPRT_TARGET_NT4
82 uintptr_t const uLast = g_puRtMmHighestUserAddress ? *g_puRtMmHighestUserAddress : ~(uintptr_t)0 / 2;
83#else
84 uintptr_t const uLast = (uintptr_t)MM_HIGHEST_USER_ADDRESS;
85#endif
86 return R3Ptr <= uLast;
87}
88
89
90RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv)
91{
92#ifdef IPRT_TARGET_NT4
93 uintptr_t const uFirst = g_puRtMmSystemRangeStart ? *g_puRtMmSystemRangeStart : ~(uintptr_t)0 / 2 + 1;
94#else
95 uintptr_t const uFirst = (uintptr_t)MM_SYSTEM_RANGE_START;
96#endif
97 return (uintptr_t)pv >= uFirst;
98}
99
100
101RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void)
102{
103 return true;
104}
105
106
107RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb)
108{
109 if (!RTR0MemKernelIsValidAddr((void *)pvSrc))
110 return VERR_ACCESS_DENIED;
111
112 uint8_t *pbDst = (uint8_t *)pvDst;
113 uint8_t const *pbSrc = (uint8_t const *)pvSrc;
114
115#if 0
116 /*
117 * The try+except stuff does not work for kernel addresses.
118 */
119 __try
120 {
121 while (cb-- > 0)
122 *pbDst++ = *pbSrc++;
123 }
124 __except(EXCEPTION_EXECUTE_HANDLER)
125 {
126 return VERR_ACCESS_DENIED;
127 }
128#else
129 /*
130 * This is the best I can come up with for now: Work page-by-page using MmIsAddressValid.
131 */
132 while (cb > 0)
133 {
134 if (!MmIsAddressValid((PVOID)pbSrc))
135 return VERR_ACCESS_DENIED;
136
137 size_t cbToCopy = (uintptr_t)pbSrc & PAGE_OFFSET_MASK;
138 if (cbToCopy > cb)
139 cbToCopy = cb;
140 cb -= cbToCopy;
141
142 __try /* doesn't work, but can't hurt, right? */
143 {
144 while (cbToCopy-- > 0)
145 *pbDst++ = *pbSrc++;
146 }
147 __except(EXCEPTION_EXECUTE_HANDLER)
148 {
149 return VERR_ACCESS_DENIED;
150 }
151 }
152#endif
153 return VINF_SUCCESS;
154}
155
156
157RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb)
158{
159 if (!RTR0MemKernelIsValidAddr(pvDst))
160 return VERR_ACCESS_DENIED;
161#if 0
162 uint8_t *pbDst = (uint8_t *)pvDst;
163 uint8_t const *pbSrc = (uint8_t const *)pvSrc;
164# if 0
165 /*
166 * The try+except stuff does not work for kernel addresses.
167 */
168 __try
169 {
170 while (cb-- > 0)
171 *pbDst++ = *pbSrc++;
172 }
173 __except(EXCEPTION_EXECUTE_HANDLER)
174 {
175 return VERR_ACCESS_DENIED;
176 }
177
178# else
179 /*
180 * This is the best I can come up with for now: Work page-by-page using MmIsAddressValid.
181 * Note! MmIsAddressValid does not indicate that it's writable, so we're a bit buggered if it isn't...
182 */
183 while (cb > 0)
184 {
185 if (!MmIsAddressValid((PVOID)pbSrc))
186 return VERR_ACCESS_DENIED;
187
188 size_t cbToCopy = (uintptr_t)pbSrc & PAGE_OFFSET_MASK;
189 if (cbToCopy > cb)
190 cbToCopy = cb;
191 cb -= cbToCopy;
192
193 __try /* doesn't work, but can't hurt, right? */
194 {
195 while (cbToCopy-- > 0)
196 *pbDst++ = *pbSrc++;
197 }
198 __except(EXCEPTION_EXECUTE_HANDLER)
199 {
200 return VERR_ACCESS_DENIED;
201 }
202 }
203# endif
204 return VINF_SUCCESS;
205#else
206 RT_NOREF(pvDst, pvSrc, cb);
207 return VERR_NOT_SUPPORTED;
208#endif
209}
210
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette