VirtualBox

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

Last change on this file since 94293 was 93115, checked in by vboxsync, 3 years ago

scm --update-copyright-year

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1/* $Id: memuserkernel-r0drv-linux.c 93115 2022-01-01 11:31:46Z vboxsync $ */
2/** @file
3 * IPRT - User & Kernel Memory, Ring-0 Driver, Linux.
4 */
5
6/*
7 * Copyright (C) 2009-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#include "the-linux-kernel.h"
32#include "internal/iprt.h"
33
34#include <iprt/mem.h>
35#include <iprt/errcore.h>
36
37
38RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst, RTR3PTR R3PtrSrc, size_t cb)
39{
40 IPRT_LINUX_SAVE_EFL_AC();
41 if (RT_LIKELY(copy_from_user(pvDst, (void *)R3PtrSrc, cb) == 0))
42 {
43 IPRT_LINUX_RESTORE_EFL_AC();
44 return VINF_SUCCESS;
45 }
46 IPRT_LINUX_RESTORE_EFL_AC();
47 return VERR_ACCESS_DENIED;
48}
49RT_EXPORT_SYMBOL(RTR0MemUserCopyFrom);
50
51
52RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst, void const *pvSrc, size_t cb)
53{
54 IPRT_LINUX_SAVE_EFL_AC();
55 if (RT_LIKELY(copy_to_user((void *)R3PtrDst, pvSrc, cb) == 0))
56 {
57 IPRT_LINUX_RESTORE_EFL_AC();
58 return VINF_SUCCESS;
59 }
60 IPRT_LINUX_RESTORE_EFL_AC();
61 return VERR_ACCESS_DENIED;
62}
63RT_EXPORT_SYMBOL(RTR0MemUserCopyTo);
64
65
66RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr)
67{
68 IPRT_LINUX_SAVE_EFL_AC();
69#if RTLNX_VER_MIN(5,0,0) || RTLNX_RHEL_MIN(8,1)
70 bool fRc = access_ok((void *)R3Ptr, 1);
71#else
72 bool fRc = access_ok(VERIFY_READ, (void *)R3Ptr, 1);
73#endif
74 IPRT_LINUX_RESTORE_EFL_AC();
75 return fRc;
76}
77RT_EXPORT_SYMBOL(RTR0MemUserIsValidAddr);
78
79
80RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv)
81{
82 /* Couldn't find a straight forward way of doing this... */
83#if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY)
84 return true; /* ?? */
85#elif defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
86 return (uintptr_t)pv >= PAGE_OFFSET;
87#else
88# error "PORT ME"
89#if RTLNX_VER_MIN(5,0,0) || RTLNX_RHEL_MIN(8,1)
90 return !access_ok(pv, 1);
91#else
92 return !access_ok(VERIFY_READ, pv, 1);
93#endif
94#endif
95}
96RT_EXPORT_SYMBOL(RTR0MemKernelIsValidAddr);
97
98
99RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void)
100{
101#if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY) /* ?? */
102 return false;
103#else
104 return true;
105#endif
106}
107RT_EXPORT_SYMBOL(RTR0MemAreKrnlAndUsrDifferent);
108
109
110/**
111 * Treats both source and destination as unsafe buffers.
112 */
113static int rtR0MemKernelCopyLnxWorker(void *pvDst, void const *pvSrc, size_t cb)
114{
115#if RTLNX_VER_MIN(2,5,55)
116/* _ASM_EXTABLE was introduced in 2.6.25 from what I can tell. Using #ifndef
117 here since it has to be a macro and you never know what someone might have
118 backported to an earlier kernel release. */
119# ifndef _ASM_EXTABLE
120# if ARCH_BITS == 32
121# define _ASM_EXTABLE(a_Instr, a_Resume) \
122 ".section __ex_table,\"a\"\n" \
123 ".balign 4\n" \
124 ".long " #a_Instr "\n" \
125 ".long " #a_Resume "\n" \
126 ".previous\n"
127# else
128# define _ASM_EXTABLE(a_Instr, a_Resume) \
129 ".section __ex_table,\"a\"\n" \
130 ".balign 8\n" \
131 ".quad " #a_Instr "\n" \
132 ".quad " #a_Resume "\n" \
133 ".previous\n"
134# endif
135# endif /* !_ASM_EXTABLE */
136 int rc;
137 IPRT_LINUX_SAVE_EFL_AC(); /* paranoia */
138 if (!cb)
139 return VINF_SUCCESS;
140
141 __asm__ __volatile__ ("cld\n"
142 "1:\n\t"
143 "rep; movsb\n"
144 "2:\n\t"
145 ".section .fixup,\"ax\"\n"
146 "3:\n\t"
147 "movl %4, %0\n\t"
148 "jmp 2b\n\t"
149 ".previous\n"
150 _ASM_EXTABLE(1b, 3b)
151 : "=r" (rc),
152 "=D" (pvDst),
153 "=S" (pvSrc),
154 "=c" (cb)
155 : "i" (VERR_ACCESS_DENIED),
156 "0" (VINF_SUCCESS),
157 "1" (pvDst),
158 "2" (pvSrc),
159 "3" (cb)
160 : "memory");
161 IPRT_LINUX_RESTORE_EFL_AC();
162 return rc;
163#else
164 return VERR_NOT_SUPPORTED;
165#endif
166}
167
168
169RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb)
170{
171 return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
172}
173RT_EXPORT_SYMBOL(RTR0MemKernelCopyFrom);
174
175
176RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb)
177{
178 return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
179}
180RT_EXPORT_SYMBOL(RTR0MemKernelCopyTo);
181
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