blob: 94a9fe2702c2f4922418084b28c74c26b35edc6c [file] [log] [blame]
Thomas Gleixnerde6cc652019-05-27 08:55:02 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Optimized memory copy routines.
4 *
5 * Copyright (C) 2004 Randolph Chung <tausq@debian.org>
Helge Deller554bfec2017-03-29 21:41:05 +02006 * Copyright (C) 2013-2017 Helge Deller <deller@gmx.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Portions derived from the GNU C Library
9 * Copyright (C) 1991, 1997, 2003 Free Software Foundation, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
11
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/module.h>
13#include <linux/compiler.h>
Helge Dellerdb080f92013-10-09 23:47:03 +020014#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
Al Virodb68ce12017-03-20 21:08:07 -040016#define get_user_space() (uaccess_kernel() ? 0 : mfsp(3))
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#define get_kernel_space() (0)
18
Helge Deller5b879d72013-07-04 22:34:11 +020019/* Returns 0 for success, otherwise, returns number of bytes not transferred. */
Helge Deller554bfec2017-03-29 21:41:05 +020020extern unsigned long pa_memcpy(void *dst, const void *src,
21 unsigned long len);
Helge Deller5b879d72013-07-04 22:34:11 +020022
Al Virof64fd182017-03-24 23:11:26 -040023unsigned long raw_copy_to_user(void __user *dst, const void *src,
24 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070025{
26 mtsp(get_kernel_space(), 1);
27 mtsp(get_user_space(), 2);
28 return pa_memcpy((void __force *)dst, src, len);
29}
Al Virof64fd182017-03-24 23:11:26 -040030EXPORT_SYMBOL(raw_copy_to_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Al Virof64fd182017-03-24 23:11:26 -040032unsigned long raw_copy_from_user(void *dst, const void __user *src,
Helge Deller9e91db62016-10-06 09:07:30 +020033 unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
35 mtsp(get_user_space(), 1);
36 mtsp(get_kernel_space(), 2);
37 return pa_memcpy(dst, (void __force *)src, len);
38}
Al Virof64fd182017-03-24 23:11:26 -040039EXPORT_SYMBOL(raw_copy_from_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Al Virof64fd182017-03-24 23:11:26 -040041unsigned long raw_copy_in_user(void __user *dst, const void __user *src, unsigned long len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070042{
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
49void * 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 Virof64fd182017-03-24 23:11:26 -040057EXPORT_SYMBOL(raw_copy_in_user);
Linus Torvalds1da177e2005-04-16 15:20:36 -070058EXPORT_SYMBOL(memcpy);
Helge Dellerdb080f92013-10-09 23:47:03 +020059
Christoph Hellwig98a23602020-06-08 21:34:50 -070060bool probe_kernel_read_allowed(const void *unsafe_src, size_t size)
Helge Dellerdb080f92013-10-09 23:47:03 +020061{
Christoph Hellwigeab0c602020-06-08 21:34:27 -070062 if ((unsigned long)unsafe_src < PAGE_SIZE)
63 return false;
Helge Dellerdb080f92013-10-09 23:47:03 +020064 /* check for I/O space F_EXTEND(0xfff00000) access as well? */
Christoph Hellwigeab0c602020-06-08 21:34:27 -070065 return true;
Helge Dellerdb080f92013-10-09 23:47:03 +020066}