uaccess_32.h 12 KB

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