uaccess_32.h 13 KB

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