uaccess_32.h 13 KB

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