VirtualBox

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

Last change on this file since 80712 was 80712, checked in by vboxsync, 5 years ago

Additions/linux: ticketref:18917: VBox 6.0.10 GAs fail to compile on Red Hat/CentOS/OL 7.7; also Red Hat 8.1 Beta

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