VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/linux/memuserkernel-r0drv-linux.c@ 96715

Last change on this file since 96715 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.8 KB
Line 
1/* $Id: memuserkernel-r0drv-linux.c 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 * IPRT - User & Kernel Memory, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2009-2022 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-linux-kernel.h"
42#include "internal/iprt.h"
43
44#include <iprt/mem.h>
45#include <iprt/errcore.h>
46
47
48RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb)
49{
50 IPRT_LINUX_SAVE_EFL_AC();
51 if (RT_LIKELY(copy_from_user(pvDst, (void *)R3PtrSrc, cb) == 0))
52 {
53 IPRT_LINUX_RESTORE_EFL_AC();
54 return VINF_SUCCESS;
55 }
56 IPRT_LINUX_RESTORE_EFL_AC();
57 return VERR_ACCESS_DENIED;
58}
59RT_EXPORT_SYMBOL(RTR0MemUserCopyFrom);
60
61
62RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb)
63{
64 IPRT_LINUX_SAVE_EFL_AC();
65 if (RT_LIKELY(copy_to_user((void *)R3PtrDst, pvSrc, cb) == 0))
66 {
67 IPRT_LINUX_RESTORE_EFL_AC();
68 return VINF_SUCCESS;
69 }
70 IPRT_LINUX_RESTORE_EFL_AC();
71 return VERR_ACCESS_DENIED;
72}
73RT_EXPORT_SYMBOL(RTR0MemUserCopyTo);
74
75
76RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr)
77{
78 IPRT_LINUX_SAVE_EFL_AC();
79#if RTLNX_VER_MIN(5,0,0) || RTLNX_RHEL_MIN(8,1)
80 bool fRc = access_ok((void *)R3Ptr, 1);
81#else
82 bool fRc = access_ok(VERIFY_READ, (void *)R3Ptr, 1);
83#endif
84 IPRT_LINUX_RESTORE_EFL_AC();
85 return fRc;
86}
87RT_EXPORT_SYMBOL(RTR0MemUserIsValidAddr);
88
89
90RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv)
91{
92 /* Couldn't find a straight forward way of doing this... */
93#if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY)
94 return true; /* ?? */
95#elif defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
96 return (uintptr_t)pv >= PAGE_OFFSET;
97#else
98# error "PORT ME"
99#if RTLNX_VER_MIN(5,0,0) || RTLNX_RHEL_MIN(8,1)
100 return !access_ok(pv, 1);
101#else
102 return !access_ok(VERIFY_READ, pv, 1);
103#endif
104#endif
105}
106RT_EXPORT_SYMBOL(RTR0MemKernelIsValidAddr);
107
108
109RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void)
110{
111#if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY) /* ?? */
112 return false;
113#else
114 return true;
115#endif
116}
117RT_EXPORT_SYMBOL(RTR0MemAreKrnlAndUsrDifferent);
118
119
120/**
121 * Treats both source and destination as unsafe buffers.
122 */
123static int rtR0MemKernelCopyLnxWorker(void *pvDst, void const *pvSrc, size_t cb)
124{
125#if RTLNX_VER_MIN(2,5,55)
126/* _ASM_EXTABLE was introduced in 2.6.25 from what I can tell. Using #ifndef
127 here since it has to be a macro and you never know what someone might have
128 backported to an earlier kernel release. */
129# ifndef _ASM_EXTABLE
130# if ARCH_BITS == 32
131# define _ASM_EXTABLE(a_Instr, a_Resume) \
132 ".section __ex_table,\"a\"\n" \
133 ".balign 4\n" \
134 ".long " #a_Instr "\n" \
135 ".long " #a_Resume "\n" \
136 ".previous\n"
137# else
138# define _ASM_EXTABLE(a_Instr, a_Resume) \
139 ".section __ex_table,\"a\"\n" \
140 ".balign 8\n" \
141 ".quad " #a_Instr "\n" \
142 ".quad " #a_Resume "\n" \
143 ".previous\n"
144# endif
145# endif /* !_ASM_EXTABLE */
146 int rc;
147 IPRT_LINUX_SAVE_EFL_AC(); /* paranoia */
148 if (!cb)
149 return VINF_SUCCESS;
150
151 __asm__ __volatile__ ("cld\n"
152 "1:\n\t"
153 "rep; movsb\n"
154 "2:\n\t"
155 ".section .fixup,\"ax\"\n"
156 "3:\n\t"
157 "movl %4, %0\n\t"
158 "jmp 2b\n\t"
159 ".previous\n"
160 _ASM_EXTABLE(1b, 3b)
161 : "=r" (rc),
162 "=D" (pvDst),
163 "=S" (pvSrc),
164 "=c" (cb)
165 : "i" (VERR_ACCESS_DENIED),
166 "0" (VINF_SUCCESS),
167 "1" (pvDst),
168 "2" (pvSrc),
169 "3" (cb)
170 : "memory");
171 IPRT_LINUX_RESTORE_EFL_AC();
172 return rc;
173#else
174 return VERR_NOT_SUPPORTED;
175#endif
176}
177
178
179RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb)
180{
181 return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
182}
183RT_EXPORT_SYMBOL(RTR0MemKernelCopyFrom);
184
185
186RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb)
187{
188 return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
189}
190RT_EXPORT_SYMBOL(RTR0MemKernelCopyTo);
191
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