uaccess_32.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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_H
  14. #define __ASM_SH_UACCESS_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. #define copy_to_user(to,from,n) ({ \
  275. void *__copy_to = (void *) (to); \
  276. __kernel_size_t __copy_size = (__kernel_size_t) (n); \
  277. __kernel_size_t __copy_res; \
  278. if(__copy_size && __access_ok((unsigned long)__copy_to, __copy_size)) { \
  279. __copy_res = __copy_user(__copy_to, (void *) (from), __copy_size); \
  280. } else __copy_res = __copy_size; \
  281. __copy_res; })
  282. #define copy_from_user(to,from,n) ({ \
  283. void *__copy_to = (void *) (to); \
  284. void *__copy_from = (void *) (from); \
  285. __kernel_size_t __copy_size = (__kernel_size_t) (n); \
  286. __kernel_size_t __copy_res; \
  287. if(__copy_size && __access_ok((unsigned long)__copy_from, __copy_size)) { \
  288. __copy_res = __copy_user(__copy_to, __copy_from, __copy_size); \
  289. } else __copy_res = __copy_size; \
  290. __copy_res; })
  291. static __always_inline unsigned long
  292. __copy_from_user(void *to, const void __user *from, unsigned long n)
  293. {
  294. return __copy_user(to, (__force void *)from, n);
  295. }
  296. static __always_inline unsigned long __must_check
  297. __copy_to_user(void __user *to, const void *from, unsigned long n)
  298. {
  299. return __copy_user((__force void *)to, from, n);
  300. }
  301. #define __copy_to_user_inatomic __copy_to_user
  302. #define __copy_from_user_inatomic __copy_from_user
  303. /*
  304. * Clear the area and return remaining number of bytes
  305. * (on failure. Usually it's 0.)
  306. */
  307. extern __kernel_size_t __clear_user(void *addr, __kernel_size_t size);
  308. #define clear_user(addr,n) ({ \
  309. void * __cl_addr = (addr); \
  310. unsigned long __cl_size = (n); \
  311. if (__cl_size && __access_ok(((unsigned long)(__cl_addr)), __cl_size)) \
  312. __cl_size = __clear_user(__cl_addr, __cl_size); \
  313. __cl_size; })
  314. static __inline__ int
  315. __strncpy_from_user(unsigned long __dest, unsigned long __user __src, int __count)
  316. {
  317. __kernel_size_t res;
  318. unsigned long __dummy, _d, _s, _c;
  319. __asm__ __volatile__(
  320. "9:\n"
  321. "mov.b @%2+, %1\n\t"
  322. "cmp/eq #0, %1\n\t"
  323. "bt/s 2f\n"
  324. "1:\n"
  325. "mov.b %1, @%3\n\t"
  326. "dt %4\n\t"
  327. "bf/s 9b\n\t"
  328. " add #1, %3\n\t"
  329. "2:\n\t"
  330. "sub %4, %0\n"
  331. "3:\n"
  332. ".section .fixup,\"ax\"\n"
  333. "4:\n\t"
  334. "mov.l 5f, %1\n\t"
  335. "jmp @%1\n\t"
  336. " mov %9, %0\n\t"
  337. ".balign 4\n"
  338. "5: .long 3b\n"
  339. ".previous\n"
  340. ".section __ex_table,\"a\"\n"
  341. " .balign 4\n"
  342. " .long 9b,4b\n"
  343. ".previous"
  344. : "=r" (res), "=&z" (__dummy), "=r" (_s), "=r" (_d), "=r"(_c)
  345. : "0" (__count), "2" (__src), "3" (__dest), "4" (__count),
  346. "i" (-EFAULT)
  347. : "memory", "t");
  348. return res;
  349. }
  350. /**
  351. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  352. * @dst: Destination address, in kernel space. This buffer must be at
  353. * least @count bytes long.
  354. * @src: Source address, in user space.
  355. * @count: Maximum number of bytes to copy, including the trailing NUL.
  356. *
  357. * Copies a NUL-terminated string from userspace to kernel space.
  358. *
  359. * On success, returns the length of the string (not including the trailing
  360. * NUL).
  361. *
  362. * If access to userspace fails, returns -EFAULT (some data may have been
  363. * copied).
  364. *
  365. * If @count is smaller than the length of the string, copies @count bytes
  366. * and returns @count.
  367. */
  368. #define strncpy_from_user(dest,src,count) ({ \
  369. unsigned long __sfu_src = (unsigned long) (src); \
  370. int __sfu_count = (int) (count); \
  371. long __sfu_res = -EFAULT; \
  372. if(__access_ok(__sfu_src, __sfu_count)) { \
  373. __sfu_res = __strncpy_from_user((unsigned long) (dest), __sfu_src, __sfu_count); \
  374. } __sfu_res; })
  375. /*
  376. * Return the size of a string (including the ending 0 even when we have
  377. * exceeded the maximum string length).
  378. */
  379. static __inline__ long __strnlen_user(const char __user *__s, long __n)
  380. {
  381. unsigned long res;
  382. unsigned long __dummy;
  383. __asm__ __volatile__(
  384. "1:\t"
  385. "mov.b @(%0,%3), %1\n\t"
  386. "cmp/eq %4, %0\n\t"
  387. "bt/s 2f\n\t"
  388. " add #1, %0\n\t"
  389. "tst %1, %1\n\t"
  390. "bf 1b\n\t"
  391. "2:\n"
  392. ".section .fixup,\"ax\"\n"
  393. "3:\n\t"
  394. "mov.l 4f, %1\n\t"
  395. "jmp @%1\n\t"
  396. " mov #0, %0\n"
  397. ".balign 4\n"
  398. "4: .long 2b\n"
  399. ".previous\n"
  400. ".section __ex_table,\"a\"\n"
  401. " .balign 4\n"
  402. " .long 1b,3b\n"
  403. ".previous"
  404. : "=z" (res), "=&r" (__dummy)
  405. : "0" (0), "r" (__s), "r" (__n)
  406. : "t");
  407. return res;
  408. }
  409. /**
  410. * strnlen_user: - Get the size of a string in user space.
  411. * @s: The string to measure.
  412. * @n: The maximum valid length
  413. *
  414. * Context: User context only. This function may sleep.
  415. *
  416. * Get the size of a NUL-terminated string in user space.
  417. *
  418. * Returns the size of the string INCLUDING the terminating NUL.
  419. * On exception, returns 0.
  420. * If the string is too long, returns a value greater than @n.
  421. */
  422. static __inline__ long strnlen_user(const char __user *s, long n)
  423. {
  424. if (!__addr_ok(s))
  425. return 0;
  426. else
  427. return __strnlen_user(s, n);
  428. }
  429. /**
  430. * strlen_user: - Get the size of a string in user space.
  431. * @str: The string to measure.
  432. *
  433. * Context: User context only. This function may sleep.
  434. *
  435. * Get the size of a NUL-terminated string in user space.
  436. *
  437. * Returns the size of the string INCLUDING the terminating NUL.
  438. * On exception, returns 0.
  439. *
  440. * If there is a limit on the length of a valid string, you may wish to
  441. * consider using strnlen_user() instead.
  442. */
  443. #define strlen_user(str) strnlen_user(str, ~0UL >> 1)
  444. /*
  445. * The exception table consists of pairs of addresses: the first is the
  446. * address of an instruction that is allowed to fault, and the second is
  447. * the address at which the program should continue. No registers are
  448. * modified, so it is entirely up to the continuation code to figure out
  449. * what to do.
  450. *
  451. * All the routines below use bits of fixup code that are out of line
  452. * with the main instruction path. This means when everything is well,
  453. * we don't even have to jump over them. Further, they do not intrude
  454. * on our cache or tlb entries.
  455. */
  456. struct exception_table_entry
  457. {
  458. unsigned long insn, fixup;
  459. };
  460. extern int fixup_exception(struct pt_regs *regs);
  461. #endif /* __ASM_SH_UACCESS_H */