1 | /* $Id: memuserkernel-r0drv-linux.c 53427 2014-12-02 21:18:18Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - User & Kernel Memory, Ring-0 Driver, Linux.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2009-2012 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 |
|
---|
38 | RTR0DECL(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 | }
|
---|
44 | RT_EXPORT_SYMBOL(RTR0MemUserCopyFrom);
|
---|
45 |
|
---|
46 |
|
---|
47 | RTR0DECL(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 | }
|
---|
53 | RT_EXPORT_SYMBOL(RTR0MemUserCopyTo);
|
---|
54 |
|
---|
55 |
|
---|
56 | RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr)
|
---|
57 | {
|
---|
58 | return access_ok(VERIFY_READ, (void *)R3Ptr, 1);
|
---|
59 | }
|
---|
60 | RT_EXPORT_SYMBOL(RTR0MemUserIsValidAddr);
|
---|
61 |
|
---|
62 |
|
---|
63 | RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv)
|
---|
64 | {
|
---|
65 | /* Couldn't find a straight forward way of doing this... */
|
---|
66 | #if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY)
|
---|
67 | return true; /* ?? */
|
---|
68 | #elif defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
|
---|
69 | return (uintptr_t)pv >= PAGE_OFFSET;
|
---|
70 | #else
|
---|
71 | # error "PORT ME"
|
---|
72 | return !access_ok(VERIFY_READ, pv, 1);
|
---|
73 | #endif
|
---|
74 | }
|
---|
75 | RT_EXPORT_SYMBOL(RTR0MemKernelIsValidAddr);
|
---|
76 |
|
---|
77 |
|
---|
78 | RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void)
|
---|
79 | {
|
---|
80 | #if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY) /* ?? */
|
---|
81 | return false;
|
---|
82 | #else
|
---|
83 | return true;
|
---|
84 | #endif
|
---|
85 | }
|
---|
86 | RT_EXPORT_SYMBOL(RTR0MemAreKrnlAndUsrDifferent);
|
---|
87 |
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Treats both source and destination as unsafe buffers.
|
---|
91 | */
|
---|
92 | static int rtR0MemKernelCopyLnxWorker(void *pvDst, void const *pvSrc, size_t cb)
|
---|
93 | {
|
---|
94 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 55)
|
---|
95 | /* _ASM_EXTABLE was introduced in 2.6.25 from what I can tell. Using #ifndef
|
---|
96 | here since it has to be a macro and you never know what someone might have
|
---|
97 | backported to an earlier kernel release. */
|
---|
98 | # ifndef _ASM_EXTABLE
|
---|
99 | # if ARCH_BITS == 32
|
---|
100 | # define _ASM_EXTABLE(a_Instr, a_Resume) \
|
---|
101 | ".section __ex_table,\"a\"\n" \
|
---|
102 | ".balign 4\n" \
|
---|
103 | ".long " #a_Instr "\n" \
|
---|
104 | ".long " #a_Resume "\n" \
|
---|
105 | ".previous\n"
|
---|
106 | # else
|
---|
107 | # define _ASM_EXTABLE(a_Instr, a_Resume) \
|
---|
108 | ".section __ex_table,\"a\"\n" \
|
---|
109 | ".balign 8\n" \
|
---|
110 | ".quad " #a_Instr "\n" \
|
---|
111 | ".quad " #a_Resume "\n" \
|
---|
112 | ".previous\n"
|
---|
113 | # endif
|
---|
114 | # endif /* !_ASM_EXTABLE */
|
---|
115 | int rc;
|
---|
116 | if (!cb)
|
---|
117 | return VINF_SUCCESS;
|
---|
118 |
|
---|
119 | __asm__ __volatile__ ("cld\n"
|
---|
120 | "1:\n\t"
|
---|
121 | "rep; movsb\n"
|
---|
122 | "2:\n\t"
|
---|
123 | ".section .fixup,\"ax\"\n"
|
---|
124 | "3:\n\t"
|
---|
125 | "movl %4, %0\n"
|
---|
126 | ".previous\n"
|
---|
127 | _ASM_EXTABLE(1b, 3b)
|
---|
128 | : "=r" (rc),
|
---|
129 | "=D" (pvDst),
|
---|
130 | "=S" (pvSrc),
|
---|
131 | "=c" (cb)
|
---|
132 | : "i" (VERR_ACCESS_DENIED),
|
---|
133 | "0" (VINF_SUCCESS),
|
---|
134 | "1" (pvDst),
|
---|
135 | "2" (pvSrc),
|
---|
136 | "3" (cb)
|
---|
137 | : "memory");
|
---|
138 | return rc;
|
---|
139 | #else
|
---|
140 | return VERR_NOT_SUPPORTED;
|
---|
141 | #endif
|
---|
142 | }
|
---|
143 |
|
---|
144 |
|
---|
145 | RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst, void const *pvSrc, size_t cb)
|
---|
146 | {
|
---|
147 | return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
|
---|
148 | }
|
---|
149 | RT_EXPORT_SYMBOL(RTR0MemKernelCopyFrom);
|
---|
150 |
|
---|
151 |
|
---|
152 | RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst, void const *pvSrc, size_t cb)
|
---|
153 | {
|
---|
154 | return rtR0MemKernelCopyLnxWorker(pvDst, pvSrc, cb);
|
---|
155 | }
|
---|
156 | RT_EXPORT_SYMBOL(RTR0MemKernelCopyTo);
|
---|
157 |
|
---|