Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Optimized memory copy routines. |
| 3 | * |
| 4 | * Copyright (C) 2004 Randolph Chung <tausq@debian.org> |
Helge Deller | 554bfec | 2017-03-29 21:41:05 +0200 | [diff] [blame^] | 5 | * Copyright (C) 2013-2017 Helge Deller <deller@gmx.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2, or (at your option) |
| 10 | * any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program; if not, write to the Free Software |
| 19 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 20 | * |
| 21 | * Portions derived from the GNU C Library |
| 22 | * Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc. |
| 23 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/module.h> |
| 27 | #include <linux/compiler.h> |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 28 | #include <linux/uaccess.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
| 30 | DECLARE_PER_CPU(struct exception_data, exception_data); |
| 31 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #define get_user_space() (segment_eq(get_fs(), KERNEL_DS) ? 0 : mfsp(3)) |
| 33 | #define get_kernel_space() (0) |
| 34 | |
Helge Deller | 5b879d7 | 2013-07-04 22:34:11 +0200 | [diff] [blame] | 35 | /* Returns 0 for success, otherwise, returns number of bytes not transferred. */ |
Helge Deller | 554bfec | 2017-03-29 21:41:05 +0200 | [diff] [blame^] | 36 | extern unsigned long pa_memcpy(void *dst, const void *src, |
| 37 | unsigned long len); |
Helge Deller | 5b879d7 | 2013-07-04 22:34:11 +0200 | [diff] [blame] | 38 | |
Helge Deller | 9e91db6 | 2016-10-06 09:07:30 +0200 | [diff] [blame] | 39 | unsigned long __copy_to_user(void __user *dst, const void *src, |
| 40 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | { |
| 42 | mtsp(get_kernel_space(), 1); |
| 43 | mtsp(get_user_space(), 2); |
| 44 | return pa_memcpy((void __force *)dst, src, len); |
| 45 | } |
Helge Deller | 9e91db6 | 2016-10-06 09:07:30 +0200 | [diff] [blame] | 46 | EXPORT_SYMBOL(__copy_to_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | |
Helge Deller | 9e91db6 | 2016-10-06 09:07:30 +0200 | [diff] [blame] | 48 | unsigned long __copy_from_user(void *dst, const void __user *src, |
| 49 | unsigned long len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | { |
| 51 | mtsp(get_user_space(), 1); |
| 52 | mtsp(get_kernel_space(), 2); |
| 53 | return pa_memcpy(dst, (void __force *)src, len); |
| 54 | } |
Helge Deller | 9e91db6 | 2016-10-06 09:07:30 +0200 | [diff] [blame] | 55 | EXPORT_SYMBOL(__copy_from_user); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | unsigned long copy_in_user(void __user *dst, const void __user *src, unsigned long len) |
| 58 | { |
| 59 | mtsp(get_user_space(), 1); |
| 60 | mtsp(get_user_space(), 2); |
| 61 | return pa_memcpy((void __force *)dst, (void __force *)src, len); |
| 62 | } |
| 63 | |
| 64 | |
| 65 | void * memcpy(void * dst,const void *src, size_t count) |
| 66 | { |
| 67 | mtsp(get_kernel_space(), 1); |
| 68 | mtsp(get_kernel_space(), 2); |
| 69 | pa_memcpy(dst, src, count); |
| 70 | return dst; |
| 71 | } |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | EXPORT_SYMBOL(copy_in_user); |
| 74 | EXPORT_SYMBOL(memcpy); |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 75 | |
| 76 | long probe_kernel_read(void *dst, const void *src, size_t size) |
| 77 | { |
| 78 | unsigned long addr = (unsigned long)src; |
| 79 | |
Helge Deller | 964f413 | 2013-11-12 21:01:24 +0100 | [diff] [blame] | 80 | if (addr < PAGE_SIZE) |
Helge Deller | db080f9 | 2013-10-09 23:47:03 +0200 | [diff] [blame] | 81 | return -EFAULT; |
| 82 | |
| 83 | /* check for I/O space F_EXTEND(0xfff00000) access as well? */ |
| 84 | |
| 85 | return __probe_kernel_read(dst, src, size); |
| 86 | } |