Thomas Gleixner | de6cc65 | 2019-05-27 08:55:02 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Optimized memory copy routines. |
| 4 | * |
| 5 | * Copyright (C) 2004 Randolph Chung <tausq@debian.org> |
Helge Deller | 554bfec | 2017-03-29 21:41:05 +0200 | [diff] [blame] | 6 | * Copyright (C) 2013-2017 Helge Deller <deller@gmx.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Portions derived from the GNU C Library |
| 9 | * Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
| 11 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> |
| 13 | #include <linux/compiler.h> |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 14 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | |
Al Viro | db68ce1 | 2017-03-20 21:08:07 -0400 | [diff] [blame] | 16 | #define get_user_space() (uaccess_kernel() ? 0 : mfsp(3)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | #define get_kernel_space() (0) |
| 18 | |
Helge Deller | 5b879d7 | 2013-07-04 22:34:11 +0200 | [diff] [blame] | 19 | /* Returns 0 for success, otherwise, returns number of bytes not transferred. */ |
Helge Deller | 554bfec | 2017-03-29 21:41:05 +0200 | [diff] [blame] | 20 | extern unsigned long pa_memcpy(void *dst, const void *src, |
| 21 | unsigned long len); |
Helge Deller | 5b879d7 | 2013-07-04 22:34:11 +0200 | [diff] [blame] | 22 | |
Al Viro | f64fd18 | 2017-03-24 23:11:26 -0400 | [diff] [blame] | 23 | unsigned long raw_copy_to_user(void __user *dst, const void *src, |
| 24 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | { |
| 26 | mtsp(get_kernel_space(), 1); |
| 27 | mtsp(get_user_space(), 2); |
| 28 | return pa_memcpy((void __force *)dst, src, len); |
| 29 | } |
Al Viro | f64fd18 | 2017-03-24 23:11:26 -0400 | [diff] [blame] | 30 | EXPORT_SYMBOL(raw_copy_to_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
Al Viro | f64fd18 | 2017-03-24 23:11:26 -0400 | [diff] [blame] | 32 | unsigned long raw_copy_from_user(void *dst, const void __user *src, |
Helge Deller | 9e91db6 | 2016-10-06 09:07:30 +0200 | [diff] [blame] | 33 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | { |
| 35 | mtsp(get_user_space(), 1); |
| 36 | mtsp(get_kernel_space(), 2); |
| 37 | return pa_memcpy(dst, (void __force *)src, len); |
| 38 | } |
Al Viro | f64fd18 | 2017-03-24 23:11:26 -0400 | [diff] [blame] | 39 | EXPORT_SYMBOL(raw_copy_from_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Al Viro | f64fd18 | 2017-03-24 23:11:26 -0400 | [diff] [blame] | 41 | unsigned long raw_copy_in_user(void __user *dst, const void __user *src, unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | { |
| 43 | mtsp(get_user_space(), 1); |
| 44 | mtsp(get_user_space(), 2); |
| 45 | return pa_memcpy((void __force *)dst, (void __force *)src, len); |
| 46 | } |
| 47 | |
| 48 | |
| 49 | void * memcpy(void * dst,const void *src, size_t count) |
| 50 | { |
| 51 | mtsp(get_kernel_space(), 1); |
| 52 | mtsp(get_kernel_space(), 2); |
| 53 | pa_memcpy(dst, src, count); |
| 54 | return dst; |
| 55 | } |
| 56 | |
Al Viro | f64fd18 | 2017-03-24 23:11:26 -0400 | [diff] [blame] | 57 | EXPORT_SYMBOL(raw_copy_in_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | EXPORT_SYMBOL(memcpy); |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 59 | |
Christoph Hellwig | 98a2360 | 2020-06-08 21:34:50 -0700 | [diff] [blame^] | 60 | bool probe_kernel_read_allowed(const void *unsafe_src, size_t size) |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 61 | { |
Christoph Hellwig | eab0c60 | 2020-06-08 21:34:27 -0700 | [diff] [blame] | 62 | if ((unsigned long)unsafe_src < PAGE_SIZE) |
| 63 | return false; |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 64 | /* check for I/O space F_EXTEND(0xfff00000) access as well? */ |
Christoph Hellwig | eab0c60 | 2020-06-08 21:34:27 -0700 | [diff] [blame] | 65 | return true; |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 66 | } |