blob: dcfb69361408aef1cbd0853c4ebebf5c45adf1e5 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#ifndef __M68KNOMMU_UACCESS_H
3#define __M68KNOMMU_UACCESS_H
4
5/*
6 * User space memory access functions
7 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07008#include <linux/string.h>
9
10#include <asm/segment.h>
11
Linus Torvalds96d4f262019-01-03 18:57:57 -080012#define access_ok(addr,size) _access_ok((unsigned long)(addr),(size))
Linus Torvalds1da177e2005-04-16 15:20:36 -070013
Greg Ungerer10146802007-07-19 01:49:15 -070014/*
15 * It is not enough to just have access_ok check for a real RAM address.
16 * This would disallow the case of code/ro-data running XIP in flash/rom.
17 * Ideally we would check the possible flash ranges too, but that is
18 * currently not so easy.
19 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070020static inline int _access_ok(unsigned long addr, unsigned long size)
21{
Greg Ungerer10146802007-07-19 01:49:15 -070022 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -070023}
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 * These are the main single-value transfer routines. They automatically
27 * use the right size if we just have the right pointer type.
28 */
29
30#define put_user(x, ptr) \
31({ \
32 int __pu_err = 0; \
33 typeof(*(ptr)) __pu_val = (x); \
34 switch (sizeof (*(ptr))) { \
35 case 1: \
36 __put_user_asm(__pu_err, __pu_val, ptr, b); \
37 break; \
38 case 2: \
39 __put_user_asm(__pu_err, __pu_val, ptr, w); \
40 break; \
41 case 4: \
42 __put_user_asm(__pu_err, __pu_val, ptr, l); \
43 break; \
44 case 8: \
Luc Van Oostenryck9e2b6ed42020-05-29 21:02:18 +020045 memcpy((void __force *)ptr, &__pu_val, sizeof(*(ptr))); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 break; \
47 default: \
48 __pu_err = __put_user_bad(); \
49 break; \
50 } \
51 __pu_err; \
52})
53#define __put_user(x, ptr) put_user(x, ptr)
54
55extern int __put_user_bad(void);
56
57/*
58 * Tell gcc we read from memory instead of writing: this is because
59 * we do not write to any memory gcc knows about, so there are no
60 * aliasing issues.
61 */
62
Luc Van Oostenryckce3e8372020-05-29 21:02:17 +020063#define __ptr(x) ((unsigned long __user *)(x))
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#define __put_user_asm(err,x,ptr,bwl) \
66 __asm__ ("move" #bwl " %0,%1" \
67 : /* no outputs */ \
68 :"d" (x),"m" (*__ptr(ptr)) : "memory")
69
70#define get_user(x, ptr) \
71({ \
72 int __gu_err = 0; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 switch (sizeof(*(ptr))) { \
74 case 1: \
Greg Ungerer8044aad2020-01-17 16:28:19 +100075 __get_user_asm(__gu_err, x, ptr, b, "=d"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 break; \
77 case 2: \
Greg Ungerer8044aad2020-01-17 16:28:19 +100078 __get_user_asm(__gu_err, x, ptr, w, "=r"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 break; \
80 case 4: \
Greg Ungerer8044aad2020-01-17 16:28:19 +100081 __get_user_asm(__gu_err, x, ptr, l, "=r"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 break; \
Greg Ungerer8044aad2020-01-17 16:28:19 +100083 case 8: { \
84 union { \
85 u64 l; \
86 __typeof__(*(ptr)) t; \
87 } __gu_val; \
Luc Van Oostenryck9e2b6ed42020-05-29 21:02:18 +020088 memcpy(&__gu_val.l, (const void __force *)ptr, sizeof(__gu_val.l)); \
Greg Ungerer8044aad2020-01-17 16:28:19 +100089 (x) = __gu_val.t; \
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 break; \
Greg Ungerer8044aad2020-01-17 16:28:19 +100091 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 default: \
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 __gu_err = __get_user_bad(); \
94 break; \
95 } \
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 __gu_err; \
97})
98#define __get_user(x, ptr) get_user(x, ptr)
99
100extern int __get_user_bad(void);
101
Greg Ungerer1b0f06d2006-07-13 09:32:41 +1000102#define __get_user_asm(err,x,ptr,bwl,reg) \
103 __asm__ ("move" #bwl " %1,%0" \
104 : "=d" (x) \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 : "m" (*__ptr(ptr)))
106
Al Viro29be02e2017-03-21 08:50:56 -0400107static inline unsigned long
108raw_copy_from_user(void *to, const void __user *from, unsigned long n)
109{
110 memcpy(to, (__force const void *)from, n);
111 return 0;
112}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Al Viro29be02e2017-03-21 08:50:56 -0400114static inline unsigned long
115raw_copy_to_user(void __user *to, const void *from, unsigned long n)
116{
117 memcpy((__force void *)to, from, n);
118 return 0;
119}
120#define INLINE_COPY_FROM_USER
121#define INLINE_COPY_TO_USER
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123/*
124 * Copy a null terminated string from userspace.
125 */
126
127static inline long
128strncpy_from_user(char *dst, const char *src, long count)
129{
130 char *tmp;
131 strncpy(dst, src, count);
132 for (tmp = dst; *tmp && count > 0; tmp++, count--)
133 ;
134 return(tmp - dst); /* DAVIDM should we count a NUL ? check getname */
135}
136
137/*
138 * Return the size of a string (including the ending 0)
139 *
140 * Return 0 on exception, a value greater than N if too long
141 */
142static inline long strnlen_user(const char *src, long n)
143{
144 return(strlen(src) + 1); /* DAVIDM make safer */
145}
146
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147/*
148 * Zero Userspace
149 */
150
151static inline unsigned long
Matt Waddelfa2eae92007-10-23 14:37:54 +1000152__clear_user(void *to, unsigned long n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 memset(to, 0, n);
155 return 0;
156}
157
Matt Waddelfa2eae92007-10-23 14:37:54 +1000158#define clear_user(to,n) __clear_user(to,n)
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160#endif /* _M68KNOMMU_UACCESS_H */