Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | #ifndef __M68KNOMMU_UACCESS_H |
| 3 | #define __M68KNOMMU_UACCESS_H |
| 4 | |
| 5 | /* |
| 6 | * User space memory access functions |
| 7 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include <linux/string.h> |
| 9 | |
| 10 | #include <asm/segment.h> |
| 11 | |
Linus Torvalds | 96d4f26 | 2019-01-03 18:57:57 -0800 | [diff] [blame] | 12 | #define access_ok(addr,size) _access_ok((unsigned long)(addr),(size)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Greg Ungerer | 1014680 | 2007-07-19 01:49:15 -0700 | [diff] [blame] | 14 | /* |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | static inline int _access_ok(unsigned long addr, unsigned long size) |
| 21 | { |
Greg Ungerer | 1014680 | 2007-07-19 01:49:15 -0700 | [diff] [blame] | 22 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | } |
| 24 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | * 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 Oostenryck | 9e2b6ed4 | 2020-05-29 21:02:18 +0200 | [diff] [blame] | 45 | memcpy((void __force *)ptr, &__pu_val, sizeof(*(ptr))); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | 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 | |
| 55 | extern 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 Oostenryck | ce3e837 | 2020-05-29 21:02:17 +0200 | [diff] [blame] | 63 | #define __ptr(x) ((unsigned long __user *)(x)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | switch (sizeof(*(ptr))) { \ |
| 74 | case 1: \ |
Greg Ungerer | 8044aad | 2020-01-17 16:28:19 +1000 | [diff] [blame] | 75 | __get_user_asm(__gu_err, x, ptr, b, "=d"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | break; \ |
| 77 | case 2: \ |
Greg Ungerer | 8044aad | 2020-01-17 16:28:19 +1000 | [diff] [blame] | 78 | __get_user_asm(__gu_err, x, ptr, w, "=r"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | break; \ |
| 80 | case 4: \ |
Greg Ungerer | 8044aad | 2020-01-17 16:28:19 +1000 | [diff] [blame] | 81 | __get_user_asm(__gu_err, x, ptr, l, "=r"); \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | break; \ |
Greg Ungerer | 8044aad | 2020-01-17 16:28:19 +1000 | [diff] [blame] | 83 | case 8: { \ |
| 84 | union { \ |
| 85 | u64 l; \ |
| 86 | __typeof__(*(ptr)) t; \ |
| 87 | } __gu_val; \ |
Luc Van Oostenryck | 9e2b6ed4 | 2020-05-29 21:02:18 +0200 | [diff] [blame] | 88 | memcpy(&__gu_val.l, (const void __force *)ptr, sizeof(__gu_val.l)); \ |
Greg Ungerer | 8044aad | 2020-01-17 16:28:19 +1000 | [diff] [blame] | 89 | (x) = __gu_val.t; \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | break; \ |
Greg Ungerer | 8044aad | 2020-01-17 16:28:19 +1000 | [diff] [blame] | 91 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | default: \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 93 | __gu_err = __get_user_bad(); \ |
| 94 | break; \ |
| 95 | } \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | __gu_err; \ |
| 97 | }) |
| 98 | #define __get_user(x, ptr) get_user(x, ptr) |
| 99 | |
| 100 | extern int __get_user_bad(void); |
| 101 | |
Greg Ungerer | 1b0f06d | 2006-07-13 09:32:41 +1000 | [diff] [blame] | 102 | #define __get_user_asm(err,x,ptr,bwl,reg) \ |
| 103 | __asm__ ("move" #bwl " %1,%0" \ |
| 104 | : "=d" (x) \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | : "m" (*__ptr(ptr))) |
| 106 | |
Al Viro | 29be02e | 2017-03-21 08:50:56 -0400 | [diff] [blame] | 107 | static inline unsigned long |
| 108 | raw_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
Al Viro | 29be02e | 2017-03-21 08:50:56 -0400 | [diff] [blame] | 114 | static inline unsigned long |
| 115 | raw_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 Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 123 | /* |
| 124 | * Copy a null terminated string from userspace. |
| 125 | */ |
| 126 | |
| 127 | static inline long |
| 128 | strncpy_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 | */ |
| 142 | static inline long strnlen_user(const char *src, long n) |
| 143 | { |
| 144 | return(strlen(src) + 1); /* DAVIDM make safer */ |
| 145 | } |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | /* |
| 148 | * Zero Userspace |
| 149 | */ |
| 150 | |
| 151 | static inline unsigned long |
Matt Waddel | fa2eae9 | 2007-10-23 14:37:54 +1000 | [diff] [blame] | 152 | __clear_user(void *to, unsigned long n) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | { |
| 154 | memset(to, 0, n); |
| 155 | return 0; |
| 156 | } |
| 157 | |
Matt Waddel | fa2eae9 | 2007-10-23 14:37:54 +1000 | [diff] [blame] | 158 | #define clear_user(to,n) __clear_user(to,n) |
| 159 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | #endif /* _M68KNOMMU_UACCESS_H */ |