uaccess.h 14 KB

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