^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Optimized memory copy routines.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2004 Randolph Chung <tausq@debian.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Copyright (C) 2013-2017 Helge Deller <deller@gmx.de>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * Portions derived from the GNU C Library
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #define get_user_space() (uaccess_kernel() ? 0 : mfsp(3))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define get_kernel_space() (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* Returns 0 for success, otherwise, returns number of bytes not transferred. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) extern unsigned long pa_memcpy(void *dst, const void *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) unsigned long len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) unsigned long raw_copy_to_user(void __user *dst, const void *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) mtsp(get_kernel_space(), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) mtsp(get_user_space(), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return pa_memcpy((void __force *)dst, src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) EXPORT_SYMBOL(raw_copy_to_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned long raw_copy_from_user(void *dst, const void __user *src,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) mtsp(get_user_space(), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) mtsp(get_kernel_space(), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) return pa_memcpy(dst, (void __force *)src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) EXPORT_SYMBOL(raw_copy_from_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) unsigned long raw_copy_in_user(void __user *dst, const void __user *src, unsigned long len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) mtsp(get_user_space(), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) mtsp(get_user_space(), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) return pa_memcpy((void __force *)dst, (void __force *)src, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) void * memcpy(void * dst,const void *src, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) mtsp(get_kernel_space(), 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) mtsp(get_kernel_space(), 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) pa_memcpy(dst, src, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) return dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) EXPORT_SYMBOL(raw_copy_in_user);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) EXPORT_SYMBOL(memcpy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) bool copy_from_kernel_nofault_allowed(const void *unsafe_src, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if ((unsigned long)unsafe_src < PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) /* check for I/O space F_EXTEND(0xfff00000) access as well? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }