1 /* $Id: memuserkernel-r0drv-linux.c $ */
3 * IPRT - User & Kernel Memory, Ring-0 Driver, Linux.
7 * Copyright (C) 2009-2016 Oracle Corporation
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.
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.
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.
28 /*********************************************************************************************************************************
30 *********************************************************************************************************************************/
31 #include "the-linux-kernel.h"
32 #include "internal/iprt.h"
38 RTR0DECL(int) RTR0MemUserCopyFrom(void *pvDst
, RTR3PTR R3PtrSrc
, size_t cb
)
40 IPRT_LINUX_SAVE_EFL_AC();
41 if (RT_LIKELY(copy_from_user(pvDst
, (void *)R3PtrSrc
, cb
) == 0))
43 IPRT_LINUX_RESTORE_EFL_AC();
46 IPRT_LINUX_RESTORE_EFL_AC();
47 return VERR_ACCESS_DENIED
;
49 RT_EXPORT_SYMBOL(RTR0MemUserCopyFrom
);
52 RTR0DECL(int) RTR0MemUserCopyTo(RTR3PTR R3PtrDst
, void const *pvSrc
, size_t cb
)
54 IPRT_LINUX_SAVE_EFL_AC();
55 if (RT_LIKELY(copy_to_user((void *)R3PtrDst
, pvSrc
, cb
) == 0))
57 IPRT_LINUX_RESTORE_EFL_AC();
60 IPRT_LINUX_RESTORE_EFL_AC();
61 return VERR_ACCESS_DENIED
;
63 RT_EXPORT_SYMBOL(RTR0MemUserCopyTo
);
66 RTR0DECL(bool) RTR0MemUserIsValidAddr(RTR3PTR R3Ptr
)
68 IPRT_LINUX_SAVE_EFL_AC();
69 bool fRc
= access_ok(VERIFY_READ
, (void *)R3Ptr
, 1);
70 IPRT_LINUX_RESTORE_EFL_AC();
73 RT_EXPORT_SYMBOL(RTR0MemUserIsValidAddr
);
76 RTR0DECL(bool) RTR0MemKernelIsValidAddr(void *pv
)
78 /* Couldn't find a straight forward way of doing this... */
79 #if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY)
81 #elif defined(RT_ARCH_X86) || defined(RT_ARCH_AMD64)
82 return (uintptr_t)pv
>= PAGE_OFFSET
;
85 return !access_ok(VERIFY_READ
, pv
, 1);
88 RT_EXPORT_SYMBOL(RTR0MemKernelIsValidAddr
);
91 RTR0DECL(bool) RTR0MemAreKrnlAndUsrDifferent(void)
93 #if defined(RT_ARCH_X86) && defined(CONFIG_X86_HIGH_ENTRY) /* ?? */
99 RT_EXPORT_SYMBOL(RTR0MemAreKrnlAndUsrDifferent
);
103 * Treats both source and destination as unsafe buffers.
105 static int rtR0MemKernelCopyLnxWorker(void *pvDst
, void const *pvSrc
, size_t cb
)
107 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2, 5, 55)
108 /* _ASM_EXTABLE was introduced in 2.6.25 from what I can tell. Using #ifndef
109 here since it has to be a macro and you never know what someone might have
110 backported to an earlier kernel release. */
111 # ifndef _ASM_EXTABLE
113 # define _ASM_EXTABLE(a_Instr, a_Resume) \
114 ".section __ex_table,\"a\"\n" \
116 ".long " #a_Instr "\n" \
117 ".long " #a_Resume "\n" \
120 # define _ASM_EXTABLE(a_Instr, a_Resume) \
121 ".section __ex_table,\"a\"\n" \
123 ".quad " #a_Instr "\n" \
124 ".quad " #a_Resume "\n" \
127 # endif /* !_ASM_EXTABLE */
129 IPRT_LINUX_SAVE_EFL_AC(); /* paranoia */
133 __asm__
__volatile__ ("cld\n"
137 ".section .fixup,\"ax\"\n"
146 : "i" (VERR_ACCESS_DENIED
),
152 IPRT_LINUX_RESTORE_EFL_AC();
155 return VERR_NOT_SUPPORTED
;
160 RTR0DECL(int) RTR0MemKernelCopyFrom(void *pvDst
, void const *pvSrc
, size_t cb
)
162 return rtR0MemKernelCopyLnxWorker(pvDst
, pvSrc
, cb
);
164 RT_EXPORT_SYMBOL(RTR0MemKernelCopyFrom
);
167 RTR0DECL(int) RTR0MemKernelCopyTo(void *pvDst
, void const *pvSrc
, size_t cb
)
169 return rtR0MemKernelCopyLnxWorker(pvDst
, pvSrc
, cb
);
171 RT_EXPORT_SYMBOL(RTR0MemKernelCopyTo
);