VirtualBox

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

Last change on this file since 43974 was 41169, checked in by vboxsync, 12 years ago

IPRT,SUPDrv: RTR0MemKernelCopyFrom/To for safe kernel memory access in debug & tracing code..

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