VirtualBox

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

Last change on this file was 100191, checked in by vboxsync, 12 months ago

*: Some of the easy build fixes for linux.arm64 not warranting their own commit, bugref:10457

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: memuserkernel-r0drv-linux.c 100191 2023-06-16 08:04:11Z vboxsync $ */
2/** @file
3 * IPRT - User & Kernel Memory, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2009-2023 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) || defined(RT_ARCH_ARM64) || defined(RT_ARCH_ARM32)
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#if defined(RT_ARCH_AMD64) || defined(RT_ARCH_X86)
121/**
122 * Treats both source and destination as unsafe buffers.
123 */
124static int rtR0MemKernelCopyLnxWorker(void *pvDst, void const *pvSrc, size_t cb)
125{
126# if RTLNX_VER_MIN(2,5,55)
127/* _ASM_EXTABLE was introduced in 2.6.25 from what I can tell. Using #ifndef
128 here since it has to be a macro and you never know what someone might have
129 backported to an earlier kernel release. */
130# ifndef _ASM_EXTABLE
131# if ARCH_BITS == 32
132# define _ASM_EXTABLE(a_Instr, a_Resume) \
133 ".section __ex_table,\"a\"\n" \
134 ".balign 4\n" \
135 ".long " #a_Instr "\n" \
136 ".long " #a_Resume "\n" \
137 ".previous\n"
138# else
139# define _ASM_EXTABLE(a_Instr, a_Resume) \
140 ".section __ex_table,\"a\"\n" \
141 ".balign 8\n" \
142 ".quad " #a_Instr "\n" \
143 ".quad " #a_Resume "\n" \
144 ".previous\n"
145# endif
146# endif /* !_ASM_EXTABLE */
147 int rc;
148 IPRT_LINUX_SAVE_EFL_AC(); /* paranoia */
149 if (!cb)
150 return VINF_SUCCESS;
151
152 __asm__ __volatile__ ("cld\n"
153 "1:\n\t"
154 "rep; movsb\n"
155 "2:\n\t"
156 ".section .fixup,\"ax\"\n"
157 "3:\n\t"
158 "movl %4, %0\n\t"
159 "jmp 2b\n\t"
160 ".previous\n"
161 _ASM_EXTABLE(1b, 3b)
162 : "=r" (rc),
163 "=D" (pvDst),
164 "=S" (pvSrc),
165 "=c" (cb)
166 : "i" (VERR_ACCESS_DENIED),
167 "0" (VINF_SUCCESS),
168 "1" (pvDst),
169 "2" (pvSrc),
170 "3" (cb)
171 : "memory");
172 IPRT_LINUX_RESTORE_EFL_AC();
173 return rc;
174# else
175 return VERR_NOT_SUPPORTED;
176# endif
177}
178
179
180RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb)
181{
182 return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
183}
184RT_EXPORT_SYMBOL(RTR0MemKernelCopyFrom);
185
186
187RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb)
188{
189 return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
190}
191RT_EXPORT_SYMBOL(RTR0MemKernelCopyTo);
192#endif /* !RT_ARCH_AMD64 && !RT_ARCH_X86 */
193
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use