xref: /openbmc/linux/arch/hexagon/include/asm/uaccess.h (revision 4e1a33b1)
1 /*
2  * User memory access support for Hexagon
3  *
4  * Copyright (c) 2010-2011, The Linux Foundation. All rights reserved.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 and
8  * only version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18  * 02110-1301, USA.
19  */
20 
21 #ifndef _ASM_UACCESS_H
22 #define _ASM_UACCESS_H
23 /*
24  * User space memory access functions
25  */
26 #include <linux/sched.h>
27 #include <linux/mm.h>
28 #include <asm/segment.h>
29 #include <asm/sections.h>
30 
31 /*
32  * access_ok: - Checks if a user space pointer is valid
33  * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE.  Note that
34  *        %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
35  *        to write to a block, it is always safe to read from it.
36  * @addr: User space pointer to start of block to check
37  * @size: Size of block to check
38  *
39  * Context: User context only. This function may sleep if pagefaults are
40  *          enabled.
41  *
42  * Checks if a pointer to a block of memory in user space is valid.
43  *
44  * Returns true (nonzero) if the memory block *may* be valid, false (zero)
45  * if it is definitely invalid.
46  *
47  * User address space in Hexagon, like x86, goes to 0xbfffffff, so the
48  * simple MSB-based tests used by MIPS won't work.  Some further
49  * optimization is probably possible here, but for now, keep it
50  * reasonably simple and not *too* slow.  After all, we've got the
51  * MMU for backup.
52  */
53 #define VERIFY_READ     0
54 #define VERIFY_WRITE    1
55 
56 #define __access_ok(addr, size) \
57 	((get_fs().seg == KERNEL_DS.seg) || \
58 	(((unsigned long)addr < get_fs().seg) && \
59 	  (unsigned long)size < (get_fs().seg - (unsigned long)addr)))
60 
61 /*
62  * When a kernel-mode page fault is taken, the faulting instruction
63  * address is checked against a table of exception_table_entries.
64  * Each entry is a tuple of the address of an instruction that may
65  * be authorized to fault, and the address at which execution should
66  * be resumed instead of the faulting instruction, so as to effect
67  * a workaround.
68  */
69 
70 /*  Assembly somewhat optimized copy routines  */
71 unsigned long __copy_from_user_hexagon(void *to, const void __user *from,
72 				     unsigned long n);
73 unsigned long __copy_to_user_hexagon(void __user *to, const void *from,
74 				   unsigned long n);
75 
76 #define __copy_from_user(to, from, n) __copy_from_user_hexagon(to, from, n)
77 #define __copy_to_user(to, from, n) __copy_to_user_hexagon(to, from, n)
78 
79 /*
80  * XXX todo: some additonal performance gain is possible by
81  * implementing __copy_to/from_user_inatomic, which is much
82  * like __copy_to/from_user, but performs slightly less checking.
83  */
84 
85 __kernel_size_t __clear_user_hexagon(void __user *dest, unsigned long count);
86 #define __clear_user(a, s) __clear_user_hexagon((a), (s))
87 
88 #define __strncpy_from_user(dst, src, n) hexagon_strncpy_from_user(dst, src, n)
89 
90 /*  get around the ifndef in asm-generic/uaccess.h  */
91 #define __strnlen_user __strnlen_user
92 
93 extern long __strnlen_user(const char __user *src, long n);
94 
95 static inline long hexagon_strncpy_from_user(char *dst, const char __user *src,
96 					     long n);
97 
98 #include <asm-generic/uaccess.h>
99 
100 /*  Todo:  an actual accelerated version of this.  */
101 static inline long hexagon_strncpy_from_user(char *dst, const char __user *src,
102 					     long n)
103 {
104 	long res = __strnlen_user(src, n);
105 
106 	if (unlikely(!res))
107 		return -EFAULT;
108 
109 	if (res > n) {
110 		copy_from_user(dst, src, n);
111 		return n;
112 	} else {
113 		copy_from_user(dst, src, res);
114 		return res-1;
115 	}
116 }
117 
118 #endif
119