uaccess_32.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * User space memory access functions
  3. *
  4. * Copyright (C) 1999, 2002 Niibe Yutaka
  5. * Copyright (C) 2003 - 2008 Paul Mundt
  6. *
  7. * Based on:
  8. * MIPS implementation version 1.15 by
  9. * Copyright (C) 1996, 1997, 1998 by Ralf Baechle
  10. * and i386 version.
  11. */
  12. #ifndef __ASM_SH_UACCESS_32_H
  13. #define __ASM_SH_UACCESS_32_H
  14. #include <linux/errno.h>
  15. #include <linux/sched.h>
  16. #include <asm/segment.h>
  17. #define VERIFY_READ 0
  18. #define VERIFY_WRITE 1
  19. #define __addr_ok(addr) \
  20. ((unsigned long __force)(addr) < current_thread_info()->addr_limit.seg)
  21. /*
  22. * __access_ok: Check if address with size is OK or not.
  23. *
  24. * Uhhuh, this needs 33-bit arithmetic. We have a carry..
  25. *
  26. * sum := addr + size; carry? --> flag = true;
  27. * if (sum >= addr_limit) flag = true;
  28. */
  29. #define __access_ok(addr, size) \
  30. (__addr_ok((addr) + (size)))
  31. #define access_ok(type, addr, size) \
  32. (__chk_user_ptr(addr), \
  33. __access_ok((unsigned long __force)(addr), (size)))
  34. /*
  35. * Uh, these should become the main single-value transfer routines ...
  36. * They automatically use the right size if we just have the right
  37. * pointer type ...
  38. *
  39. * As SuperH uses the same address space for kernel and user data, we
  40. * can just do these as direct assignments.
  41. *
  42. * Careful to not
  43. * (a) re-use the arguments for side effects (sizeof is ok)
  44. * (b) require any knowledge of processes at this stage
  45. */
  46. #define put_user(x,ptr) __put_user_check((x), (ptr), sizeof(*(ptr)))
  47. #define get_user(x,ptr) __get_user_check((x), (ptr), sizeof(*(ptr)))
  48. /*
  49. * The "__xxx" versions do not do address space checking, useful when
  50. * doing multiple accesses to the same area (the user has to do the
  51. * checks by hand with "access_ok()")
  52. */
  53. #define __put_user(x,ptr) __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  54. #define __get_user(x,ptr) __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  55. struct __large_struct { unsigned long buf[100]; };
  56. #define __m(x) (*(struct __large_struct __user *)(x))
  57. #define __get_user_size(x,ptr,size,retval) \
  58. do { \
  59. retval = 0; \
  60. switch (size) { \
  61. case 1: \
  62. __get_user_asm(x, ptr, retval, "b"); \
  63. break; \
  64. case 2: \
  65. __get_user_asm(x, ptr, retval, "w"); \
  66. break; \
  67. case 4: \
  68. __get_user_asm(x, ptr, retval, "l"); \
  69. break; \
  70. default: \
  71. __get_user_unknown(); \
  72. break; \
  73. } \
  74. } while (0)
  75. #define __get_user_nocheck(x,ptr,size) \
  76. ({ \
  77. long __gu_err; \
  78. unsigned long __gu_val; \
  79. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  80. __chk_user_ptr(ptr); \
  81. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  82. (x) = (__typeof__(*(ptr)))__gu_val; \
  83. __gu_err; \
  84. })
  85. #define __get_user_check(x,ptr,size) \
  86. ({ \
  87. long __gu_err = -EFAULT; \
  88. unsigned long __gu_val = 0; \
  89. const __typeof__(*(ptr)) *__gu_addr = (ptr); \
  90. if (likely(access_ok(VERIFY_READ, __gu_addr, (size)))) \
  91. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  92. (x) = (__typeof__(*(ptr)))__gu_val; \
  93. __gu_err; \
  94. })
  95. #define __get_user_asm(x, addr, err, insn) \
  96. ({ \
  97. __asm__ __volatile__( \
  98. "1:\n\t" \
  99. "mov." insn " %2, %1\n\t" \
  100. "2:\n" \
  101. ".section .fixup,\"ax\"\n" \
  102. "3:\n\t" \
  103. "mov #0, %1\n\t" \
  104. "mov.l 4f, %0\n\t" \
  105. "jmp @%0\n\t" \
  106. " mov %3, %0\n\t" \
  107. ".balign 4\n" \
  108. "4: .long 2b\n\t" \
  109. ".previous\n" \
  110. ".section __ex_table,\"a\"\n\t" \
  111. ".long 1b, 3b\n\t" \
  112. ".previous" \
  113. :"=&r" (err), "=&r" (x) \
  114. :"m" (__m(addr)), "i" (-EFAULT), "0" (err)); })
  115. extern void __get_user_unknown(void);
  116. #define __put_user_size(x,ptr,size,retval) \
  117. do { \
  118. retval = 0; \
  119. switch (size) { \
  120. case 1: \
  121. __put_user_asm(x, ptr, retval, "b"); \
  122. break; \
  123. case 2: \
  124. __put_user_asm(x, ptr, retval, "w"); \
  125. break; \
  126. case 4: \
  127. __put_user_asm(x, ptr, retval, "l"); \
  128. break; \
  129. case 8: \
  130. __put_user_u64(x, ptr, retval); \
  131. break; \
  132. default: \
  133. __put_user_unknown(); \
  134. } \
  135. } while (0)
  136. #define __put_user_nocheck(x,ptr,size) \
  137. ({ \
  138. long __pu_err; \
  139. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  140. __chk_user_ptr(ptr); \
  141. __put_user_size((x), __pu_addr, (size), __pu_err); \
  142. __pu_err; \
  143. })
  144. #define __put_user_check(x,ptr,size) \
  145. ({ \
  146. long __pu_err = -EFAULT; \
  147. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  148. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
  149. __put_user_size((x), __pu_addr, (size), \
  150. __pu_err); \
  151. __pu_err; \
  152. })
  153. #define __put_user_asm(x, addr, err, insn) \
  154. ({ \
  155. __asm__ __volatile__( \
  156. "1:\n\t" \
  157. "mov." insn " %1, %2\n\t" \
  158. "2:\n" \
  159. ".section .fixup,\"ax\"\n" \
  160. "3:\n\t" \
  161. "mov.l 4f, %0\n\t" \
  162. "jmp @%0\n\t" \
  163. " mov %3, %0\n\t" \
  164. ".balign 4\n" \
  165. "4: .long 2b\n\t" \
  166. ".previous\n" \
  167. ".section __ex_table,\"a\"\n\t" \
  168. ".long 1b, 3b\n\t" \
  169. ".previous" \
  170. :"=&r" (err) \
  171. :"r" (x), "m" (__m(addr)), "i" (-EFAULT), "0" (err) \
  172. :"memory"); })
  173. #if defined(CONFIG_CPU_LITTLE_ENDIAN)
  174. #define __put_user_u64(val,addr,retval) \
  175. ({ \
  176. __asm__ __volatile__( \
  177. "1:\n\t" \
  178. "mov.l %R1,%2\n\t" \
  179. "mov.l %S1,%T2\n\t" \
  180. "2:\n" \
  181. ".section .fixup,\"ax\"\n" \
  182. "3:\n\t" \
  183. "mov.l 4f,%0\n\t" \
  184. "jmp @%0\n\t" \
  185. " mov %3,%0\n\t" \
  186. ".balign 4\n" \
  187. "4: .long 2b\n\t" \
  188. ".previous\n" \
  189. ".section __ex_table,\"a\"\n\t" \
  190. ".long 1b, 3b\n\t" \
  191. ".previous" \
  192. : "=r" (retval) \
  193. : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
  194. : "memory"); })
  195. #else
  196. #define __put_user_u64(val,addr,retval) \
  197. ({ \
  198. __asm__ __volatile__( \
  199. "1:\n\t" \
  200. "mov.l %S1,%2\n\t" \
  201. "mov.l %R1,%T2\n\t" \
  202. "2:\n" \
  203. ".section .fixup,\"ax\"\n" \
  204. "3:\n\t" \
  205. "mov.l 4f,%0\n\t" \
  206. "jmp @%0\n\t" \
  207. " mov %3,%0\n\t" \
  208. ".balign 4\n" \
  209. "4: .long 2b\n\t" \
  210. ".previous\n" \
  211. ".section __ex_table,\"a\"\n\t" \
  212. ".long 1b, 3b\n\t" \
  213. ".previous" \
  214. : "=r" (retval) \
  215. : "r" (val), "m" (__m(addr)), "i" (-EFAULT), "0" (retval) \
  216. : "memory"); })
  217. #endif
  218. extern void __put_user_unknown(void);
  219. /* Generic arbitrary sized copy. */
  220. /* Return the number of bytes NOT copied */
  221. __kernel_size_t __copy_user(void *to, const void *from, __kernel_size_t n);
  222. static __always_inline unsigned long
  223. __copy_from_user(void *to, const void __user *from, unsigned long n)
  224. {
  225. return __copy_user(to, (__force void *)from, n);
  226. }
  227. static __always_inline unsigned long __must_check
  228. __copy_to_user(void __user *to, const void *from, unsigned long n)
  229. {
  230. return __copy_user((__force void *)to, from, n);
  231. }
  232. #define __copy_to_user_inatomic __copy_to_user
  233. #define __copy_from_user_inatomic __copy_from_user
  234. /*
  235. * Clear the area and return remaining number of bytes
  236. * (on failure. Usually it's 0.)
  237. */
  238. extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
  239. #define clear_user(addr,n) ({ \
  240. void * __cl_addr = (addr); \
  241. unsigned long __cl_size = (n); \
  242. if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
  243. __cl_size = __clear_user(__cl_addr, __cl_size); \
  244. __cl_size; })
  245. static __inline__ int
  246. __strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count)
  247. {
  248. __kernel_size_t res;
  249. unsigned long __dummy, _d, _s, _c;
  250. __asm__ __volatile__(
  251. "9:\n"
  252. "mov.b @%2+, %1\n\t"
  253. "cmp/eq #0, %1\n\t"
  254. "bt/s 2f\n"
  255. "1:\n"
  256. "mov.b %1, @%3\n\t"
  257. "dt %4\n\t"
  258. "bf/s 9b\n\t"
  259. " add #1, %3\n\t"
  260. "2:\n\t"
  261. "sub %4, %0\n"
  262. "3:\n"
  263. ".section .fixup,\"ax\"\n"
  264. "4:\n\t"
  265. "mov.l 5f, %1\n\t"
  266. "jmp @%1\n\t"
  267. " mov %9, %0\n\t"
  268. ".balign 4\n"
  269. "5: .long 3b\n"
  270. ".previous\n"
  271. ".section __ex_table,\"a\"\n"
  272. " .balign 4\n"
  273. " .long 9b,4b\n"
  274. ".previous"
  275. : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c)
  276. : "0" (__count), "2" (__src), "3" (__dest), "4" (__count),
  277. "i" (-EFAULT)
  278. : "memory", "t");
  279. return res;
  280. }
  281. /**
  282. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  283. * @dst: Destination address, in kernel space. This buffer must be at
  284. * least @count bytes long.
  285. * @src: Source address, in user space.
  286. * @count: Maximum number of bytes to copy, including the trailing NUL.
  287. *
  288. * Copies a NUL-terminated string from userspace to kernel space.
  289. *
  290. * On success, returns the length of the string (not including the trailing
  291. * NUL).
  292. *
  293. * If access to userspace fails, returns -EFAULT (some data may have been
  294. * copied).
  295. *
  296. * If @count is smaller than the length of the string, copies @count bytes
  297. * and returns @count.
  298. */
  299. #define strncpy_from_user(dest,src,count) ({ \
  300. unsigned long __sfu_src = (unsigned long) (src); \
  301. int __sfu_count = (int) (count); \
  302. long __sfu_res = -EFAULT; \
  303. if(__access_ok(__sfu_src, __sfu_count)) { \
  304. __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
  305. } __sfu_res; })
  306. /*
  307. * Return the size of a string (including the ending 0 even when we have
  308. * exceeded the maximum string length).
  309. */
  310. static __inline__ long __strnlen_user(const char __user *__s, long __n)
  311. {
  312. unsigned long res;
  313. unsigned long __dummy;
  314. __asm__ __volatile__(
  315. "1:\t"
  316. "mov.b @(%0,%3), %1\n\t"
  317. "cmp/eq %4, %0\n\t"
  318. "bt/s 2f\n\t"
  319. " add #1, %0\n\t"
  320. "tst %1, %1\n\t"
  321. "bf 1b\n\t"
  322. "2:\n"
  323. ".section .fixup,\"ax\"\n"
  324. "3:\n\t"
  325. "mov.l 4f, %1\n\t"
  326. "jmp @%1\n\t"
  327. " mov #0, %0\n"
  328. ".balign 4\n"
  329. "4: .long 2b\n"
  330. ".previous\n"
  331. ".section __ex_table,\"a\"\n"
  332. " .balign 4\n"
  333. " .long 1b,3b\n"
  334. ".previous"
  335. : "=z" (res), "=&r" (__dummy)
  336. : "0" (0), "r" (__s), "r" (__n)
  337. : "t");
  338. return res;
  339. }
  340. /**
  341. * strnlen_user: - Get the size of a string in user space.
  342. * @s: The string to measure.
  343. * @n: The maximum valid length
  344. *
  345. * Context: User context only. This function may sleep.
  346. *
  347. * Get the size of a NUL-terminated string in user space.
  348. *
  349. * Returns the size of the string INCLUDING the terminating NUL.
  350. * On exception, returns 0.
  351. * If the string is too long, returns a value greater than @n.
  352. */
  353. static __inline__ long strnlen_user(const char __user *s, long n)
  354. {
  355. if (!__addr_ok(s))
  356. return 0;
  357. else
  358. return __strnlen_user(s, n);
  359. }
  360. /**
  361. * strlen_user: - Get the size of a string in user space.
  362. * @str: The string to measure.
  363. *
  364. * Context: User context only. This function may sleep.
  365. *
  366. * Get the size of a NUL-terminated string in user space.
  367. *
  368. * Returns the size of the string INCLUDING the terminating NUL.
  369. * On exception, returns 0.
  370. *
  371. * If there is a limit on the length of a valid string, you may wish to
  372. * consider using strnlen_user() instead.
  373. */
  374. #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
  375. /*
  376. * The exception table consists of pairs of addresses: the first is the
  377. * address of an instruction that is allowed to fault, and the second is
  378. * the address at which the program should continue. No registers are
  379. * modified, so it is entirely up to the continuation code to figure out
  380. * what to do.
  381. *
  382. * All the routines below use bits of fixup code that are out of line
  383. * with the main instruction path. This means when everything is well,
  384. * we don't even have to jump over them. Further, they do not intrude
  385. * on our cache or tlb entries.
  386. */
  387. struct exception_table_entry
  388. {
  389. unsigned long insn, fixup;
  390. };
  391. extern int fixup_exception(struct pt_regs *regs);
  392. #endif /* __ASM_SH_UACCESS_32_H */