uaccess.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. /*
  2. * arch/arm/include/asm/uaccess.h
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. */
  8. #ifndef _ASMARM_UACCESS_H
  9. #define _ASMARM_UACCESS_H
  10. /*
  11. * User space memory access functions
  12. */
  13. #include <linux/string.h>
  14. #include <linux/thread_info.h>
  15. #include <asm/errno.h>
  16. #include <asm/memory.h>
  17. #include <asm/domain.h>
  18. #include <asm/unified.h>
  19. #include <asm/compiler.h>
  20. #define VERIFY_READ 0
  21. #define VERIFY_WRITE 1
  22. /*
  23. * The exception table consists of pairs of addresses: the first is the
  24. * address of an instruction that is allowed to fault, and the second is
  25. * the address at which the program should continue. No registers are
  26. * modified, so it is entirely up to the continuation code to figure out
  27. * what to do.
  28. *
  29. * All the routines below use bits of fixup code that are out of line
  30. * with the main instruction path. This means when everything is well,
  31. * we don't even have to jump over them. Further, they do not intrude
  32. * on our cache or tlb entries.
  33. */
  34. struct exception_table_entry
  35. {
  36. unsigned long insn, fixup;
  37. };
  38. extern int fixup_exception(struct pt_regs *regs);
  39. /*
  40. * These two are intentionally not defined anywhere - if the kernel
  41. * code generates any references to them, that's a bug.
  42. */
  43. extern int __get_user_bad(void);
  44. extern int __put_user_bad(void);
  45. /*
  46. * Note that this is actually 0x1,0000,0000
  47. */
  48. #define KERNEL_DS 0x00000000
  49. #define get_ds() (KERNEL_DS)
  50. #ifdef CONFIG_MMU
  51. #define USER_DS TASK_SIZE
  52. #define get_fs() (current_thread_info()->addr_limit)
  53. static inline void set_fs(mm_segment_t fs)
  54. {
  55. current_thread_info()->addr_limit = fs;
  56. modify_domain(DOMAIN_KERNEL, fs ? DOMAIN_CLIENT : DOMAIN_MANAGER);
  57. }
  58. #define segment_eq(a,b) ((a) == (b))
  59. #define __addr_ok(addr) ({ \
  60. unsigned long flag; \
  61. __asm__("cmp %2, %0; movlo %0, #0" \
  62. : "=&r" (flag) \
  63. : "0" (current_thread_info()->addr_limit), "r" (addr) \
  64. : "cc"); \
  65. (flag == 0); })
  66. /* We use 33-bit arithmetic here... */
  67. #define __range_ok(addr,size) ({ \
  68. unsigned long flag, roksum; \
  69. __chk_user_ptr(addr); \
  70. __asm__("adds %1, %2, %3; sbcccs %1, %1, %0; movcc %0, #0" \
  71. : "=&r" (flag), "=&r" (roksum) \
  72. : "r" (addr), "Ir" (size), "0" (current_thread_info()->addr_limit) \
  73. : "cc"); \
  74. flag; })
  75. /*
  76. * Single-value transfer routines. They automatically use the right
  77. * size if we just have the right pointer type. Note that the functions
  78. * which read from user space (*get_*) need to take care not to leak
  79. * kernel data even if the calling code is buggy and fails to check
  80. * the return value. This means zeroing out the destination variable
  81. * or buffer on error. Normally this is done out of line by the
  82. * fixup code, but there are a few places where it intrudes on the
  83. * main code path. When we only write to user space, there is no
  84. * problem.
  85. */
  86. extern int __get_user_1(void *);
  87. extern int __get_user_2(void *);
  88. extern int __get_user_4(void *);
  89. extern int __get_user_lo8(void *);
  90. extern int __get_user_8(void *);
  91. #define __GUP_CLOBBER_1 "lr", "cc"
  92. #ifdef CONFIG_CPU_USE_DOMAINS
  93. #define __GUP_CLOBBER_2 "ip", "lr", "cc"
  94. #else
  95. #define __GUP_CLOBBER_2 "lr", "cc"
  96. #endif
  97. #define __GUP_CLOBBER_4 "lr", "cc"
  98. #define __GUP_CLOBBER_lo8 "lr", "cc"
  99. #define __GUP_CLOBBER_8 "lr", "cc"
  100. #define __get_user_x(__r2,__p,__e,__l,__s) \
  101. __asm__ __volatile__ ( \
  102. __asmeq("%0", "r0") __asmeq("%1", "r2") \
  103. __asmeq("%3", "r1") \
  104. "bl __get_user_" #__s \
  105. : "=&r" (__e), "=r" (__r2) \
  106. : "0" (__p), "r" (__l) \
  107. : __GUP_CLOBBER_##__s)
  108. /* narrowing a double-word get into a single 32bit word register: */
  109. #ifdef __ARMEB__
  110. #define __get_user_xb(__r2, __p, __e, __l, __s) \
  111. __get_user_x(__r2, __p, __e, __l, lo8)
  112. #else
  113. #define __get_user_xb __get_user_x
  114. #endif
  115. #define get_user(x,p) \
  116. ({ \
  117. unsigned long __limit = current_thread_info()->addr_limit - 1; \
  118. register const typeof(*(p)) __user *__p asm("r0") = (p);\
  119. register typeof(x) __r2 asm("r2"); \
  120. register unsigned long __l asm("r1") = __limit; \
  121. register int __e asm("r0"); \
  122. switch (sizeof(*(__p))) { \
  123. case 1: \
  124. __get_user_x(__r2, __p, __e, __l, 1); \
  125. break; \
  126. case 2: \
  127. __get_user_x(__r2, __p, __e, __l, 2); \
  128. break; \
  129. case 4: \
  130. __get_user_x(__r2, __p, __e, __l, 4); \
  131. break; \
  132. case 8: \
  133. if (sizeof((x)) < 8) \
  134. __get_user_xb(__r2, __p, __e, __l, 4); \
  135. else \
  136. __get_user_x(__r2, __p, __e, __l, 8); \
  137. break; \
  138. default: __e = __get_user_bad(); break; \
  139. } \
  140. x = (typeof(*(p))) __r2; \
  141. __e; \
  142. })
  143. extern int __put_user_1(void *, unsigned int);
  144. extern int __put_user_2(void *, unsigned int);
  145. extern int __put_user_4(void *, unsigned int);
  146. extern int __put_user_8(void *, unsigned long long);
  147. #define __put_user_x(__r2,__p,__e,__l,__s) \
  148. __asm__ __volatile__ ( \
  149. __asmeq("%0", "r0") __asmeq("%2", "r2") \
  150. __asmeq("%3", "r1") \
  151. "bl __put_user_" #__s \
  152. : "=&r" (__e) \
  153. : "0" (__p), "r" (__r2), "r" (__l) \
  154. : "ip", "lr", "cc")
  155. #define put_user(x,p) \
  156. ({ \
  157. unsigned long __limit = current_thread_info()->addr_limit - 1; \
  158. const typeof(*(p)) __user *__tmp_p = (p); \
  159. register const typeof(*(p)) __r2 asm("r2") = (x); \
  160. register const typeof(*(p)) __user *__p asm("r0") = __tmp_p; \
  161. register unsigned long __l asm("r1") = __limit; \
  162. register int __e asm("r0"); \
  163. switch (sizeof(*(__p))) { \
  164. case 1: \
  165. __put_user_x(__r2, __p, __e, __l, 1); \
  166. break; \
  167. case 2: \
  168. __put_user_x(__r2, __p, __e, __l, 2); \
  169. break; \
  170. case 4: \
  171. __put_user_x(__r2, __p, __e, __l, 4); \
  172. break; \
  173. case 8: \
  174. __put_user_x(__r2, __p, __e, __l, 8); \
  175. break; \
  176. default: __e = __put_user_bad(); break; \
  177. } \
  178. __e; \
  179. })
  180. #else /* CONFIG_MMU */
  181. /*
  182. * uClinux has only one addr space, so has simplified address limits.
  183. */
  184. #define USER_DS KERNEL_DS
  185. #define segment_eq(a,b) (1)
  186. #define __addr_ok(addr) (1)
  187. #define __range_ok(addr,size) (0)
  188. #define get_fs() (KERNEL_DS)
  189. static inline void set_fs(mm_segment_t fs)
  190. {
  191. }
  192. #define get_user(x,p) __get_user(x,p)
  193. #define put_user(x,p) __put_user(x,p)
  194. #endif /* CONFIG_MMU */
  195. #define access_ok(type,addr,size) (__range_ok(addr,size) == 0)
  196. /*
  197. * The "__xxx" versions of the user access functions do not verify the
  198. * address space - it must have been done previously with a separate
  199. * "access_ok()" call.
  200. *
  201. * The "xxx_error" versions set the third argument to EFAULT if an
  202. * error occurs, and leave it unchanged on success. Note that these
  203. * versions are void (ie, don't return a value as such).
  204. */
  205. #define __get_user(x,ptr) \
  206. ({ \
  207. long __gu_err = 0; \
  208. __get_user_err((x),(ptr),__gu_err); \
  209. __gu_err; \
  210. })
  211. #define __get_user_error(x,ptr,err) \
  212. ({ \
  213. __get_user_err((x),(ptr),err); \
  214. (void) 0; \
  215. })
  216. #define __get_user_err(x,ptr,err) \
  217. do { \
  218. unsigned long __gu_addr = (unsigned long)(ptr); \
  219. unsigned long __gu_val; \
  220. __chk_user_ptr(ptr); \
  221. switch (sizeof(*(ptr))) { \
  222. case 1: __get_user_asm_byte(__gu_val,__gu_addr,err); break; \
  223. case 2: __get_user_asm_half(__gu_val,__gu_addr,err); break; \
  224. case 4: __get_user_asm_word(__gu_val,__gu_addr,err); break; \
  225. default: (__gu_val) = __get_user_bad(); \
  226. } \
  227. (x) = (__typeof__(*(ptr)))__gu_val; \
  228. } while (0)
  229. #define __get_user_asm_byte(x,addr,err) \
  230. __asm__ __volatile__( \
  231. "1: " TUSER(ldrb) " %1,[%2],#0\n" \
  232. "2:\n" \
  233. " .pushsection .fixup,\"ax\"\n" \
  234. " .align 2\n" \
  235. "3: mov %0, %3\n" \
  236. " mov %1, #0\n" \
  237. " b 2b\n" \
  238. " .popsection\n" \
  239. " .pushsection __ex_table,\"a\"\n" \
  240. " .align 3\n" \
  241. " .long 1b, 3b\n" \
  242. " .popsection" \
  243. : "+r" (err), "=&r" (x) \
  244. : "r" (addr), "i" (-EFAULT) \
  245. : "cc")
  246. #ifndef __ARMEB__
  247. #define __get_user_asm_half(x,__gu_addr,err) \
  248. ({ \
  249. unsigned long __b1, __b2; \
  250. __get_user_asm_byte(__b1, __gu_addr, err); \
  251. __get_user_asm_byte(__b2, __gu_addr + 1, err); \
  252. (x) = __b1 | (__b2 << 8); \
  253. })
  254. #else
  255. #define __get_user_asm_half(x,__gu_addr,err) \
  256. ({ \
  257. unsigned long __b1, __b2; \
  258. __get_user_asm_byte(__b1, __gu_addr, err); \
  259. __get_user_asm_byte(__b2, __gu_addr + 1, err); \
  260. (x) = (__b1 << 8) | __b2; \
  261. })
  262. #endif
  263. #define __get_user_asm_word(x,addr,err) \
  264. __asm__ __volatile__( \
  265. "1: " TUSER(ldr) " %1,[%2],#0\n" \
  266. "2:\n" \
  267. " .pushsection .fixup,\"ax\"\n" \
  268. " .align 2\n" \
  269. "3: mov %0, %3\n" \
  270. " mov %1, #0\n" \
  271. " b 2b\n" \
  272. " .popsection\n" \
  273. " .pushsection __ex_table,\"a\"\n" \
  274. " .align 3\n" \
  275. " .long 1b, 3b\n" \
  276. " .popsection" \
  277. : "+r" (err), "=&r" (x) \
  278. : "r" (addr), "i" (-EFAULT) \
  279. : "cc")
  280. #define __put_user(x,ptr) \
  281. ({ \
  282. long __pu_err = 0; \
  283. __put_user_err((x),(ptr),__pu_err); \
  284. __pu_err; \
  285. })
  286. #define __put_user_error(x,ptr,err) \
  287. ({ \
  288. __put_user_err((x),(ptr),err); \
  289. (void) 0; \
  290. })
  291. #define __put_user_err(x,ptr,err) \
  292. do { \
  293. unsigned long __pu_addr = (unsigned long)(ptr); \
  294. __typeof__(*(ptr)) __pu_val = (x); \
  295. __chk_user_ptr(ptr); \
  296. switch (sizeof(*(ptr))) { \
  297. case 1: __put_user_asm_byte(__pu_val,__pu_addr,err); break; \
  298. case 2: __put_user_asm_half(__pu_val,__pu_addr,err); break; \
  299. case 4: __put_user_asm_word(__pu_val,__pu_addr,err); break; \
  300. case 8: __put_user_asm_dword(__pu_val,__pu_addr,err); break; \
  301. default: __put_user_bad(); \
  302. } \
  303. } while (0)
  304. #define __put_user_asm_byte(x,__pu_addr,err) \
  305. __asm__ __volatile__( \
  306. "1: " TUSER(strb) " %1,[%2],#0\n" \
  307. "2:\n" \
  308. " .pushsection .fixup,\"ax\"\n" \
  309. " .align 2\n" \
  310. "3: mov %0, %3\n" \
  311. " b 2b\n" \
  312. " .popsection\n" \
  313. " .pushsection __ex_table,\"a\"\n" \
  314. " .align 3\n" \
  315. " .long 1b, 3b\n" \
  316. " .popsection" \
  317. : "+r" (err) \
  318. : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
  319. : "cc")
  320. #ifndef __ARMEB__
  321. #define __put_user_asm_half(x,__pu_addr,err) \
  322. ({ \
  323. unsigned long __temp = (unsigned long)(x); \
  324. __put_user_asm_byte(__temp, __pu_addr, err); \
  325. __put_user_asm_byte(__temp >> 8, __pu_addr + 1, err); \
  326. })
  327. #else
  328. #define __put_user_asm_half(x,__pu_addr,err) \
  329. ({ \
  330. unsigned long __temp = (unsigned long)(x); \
  331. __put_user_asm_byte(__temp >> 8, __pu_addr, err); \
  332. __put_user_asm_byte(__temp, __pu_addr + 1, err); \
  333. })
  334. #endif
  335. #define __put_user_asm_word(x,__pu_addr,err) \
  336. __asm__ __volatile__( \
  337. "1: " TUSER(str) " %1,[%2],#0\n" \
  338. "2:\n" \
  339. " .pushsection .fixup,\"ax\"\n" \
  340. " .align 2\n" \
  341. "3: mov %0, %3\n" \
  342. " b 2b\n" \
  343. " .popsection\n" \
  344. " .pushsection __ex_table,\"a\"\n" \
  345. " .align 3\n" \
  346. " .long 1b, 3b\n" \
  347. " .popsection" \
  348. : "+r" (err) \
  349. : "r" (x), "r" (__pu_addr), "i" (-EFAULT) \
  350. : "cc")
  351. #ifndef __ARMEB__
  352. #define __reg_oper0 "%R2"
  353. #define __reg_oper1 "%Q2"
  354. #else
  355. #define __reg_oper0 "%Q2"
  356. #define __reg_oper1 "%R2"
  357. #endif
  358. #define __put_user_asm_dword(x,__pu_addr,err) \
  359. __asm__ __volatile__( \
  360. ARM( "1: " TUSER(str) " " __reg_oper1 ", [%1], #4\n" ) \
  361. ARM( "2: " TUSER(str) " " __reg_oper0 ", [%1]\n" ) \
  362. THUMB( "1: " TUSER(str) " " __reg_oper1 ", [%1]\n" ) \
  363. THUMB( "2: " TUSER(str) " " __reg_oper0 ", [%1, #4]\n" ) \
  364. "3:\n" \
  365. " .pushsection .fixup,\"ax\"\n" \
  366. " .align 2\n" \
  367. "4: mov %0, %3\n" \
  368. " b 3b\n" \
  369. " .popsection\n" \
  370. " .pushsection __ex_table,\"a\"\n" \
  371. " .align 3\n" \
  372. " .long 1b, 4b\n" \
  373. " .long 2b, 4b\n" \
  374. " .popsection" \
  375. : "+r" (err), "+r" (__pu_addr) \
  376. : "r" (x), "i" (-EFAULT) \
  377. : "cc")
  378. #ifdef CONFIG_MMU
  379. extern unsigned long __must_check __copy_from_user(void *to, const void __user *from, unsigned long n);
  380. extern unsigned long __must_check __copy_to_user(void __user *to, const void *from, unsigned long n);
  381. extern unsigned long __must_check __copy_to_user_std(void __user *to, const void *from, unsigned long n);
  382. extern unsigned long __must_check __clear_user(void __user *addr, unsigned long n);
  383. extern unsigned long __must_check __clear_user_std(void __user *addr, unsigned long n);
  384. #else
  385. #define __copy_from_user(to,from,n) (memcpy(to, (void __force *)from, n), 0)
  386. #define __copy_to_user(to,from,n) (memcpy((void __force *)to, from, n), 0)
  387. #define __clear_user(addr,n) (memset((void __force *)addr, 0, n), 0)
  388. #endif
  389. extern unsigned long __must_check __strncpy_from_user(char *to, const char __user *from, unsigned long count);
  390. extern unsigned long __must_check __strnlen_user(const char __user *s, long n);
  391. static inline unsigned long __must_check copy_from_user(void *to, const void __user *from, unsigned long n)
  392. {
  393. if (access_ok(VERIFY_READ, from, n))
  394. n = __copy_from_user(to, from, n);
  395. else /* security hole - plug it */
  396. memset(to, 0, n);
  397. return n;
  398. }
  399. static inline unsigned long __must_check copy_to_user(void __user *to, const void *from, unsigned long n)
  400. {
  401. if (access_ok(VERIFY_WRITE, to, n))
  402. n = __copy_to_user(to, from, n);
  403. return n;
  404. }
  405. #define __copy_to_user_inatomic __copy_to_user
  406. #define __copy_from_user_inatomic __copy_from_user
  407. static inline unsigned long __must_check clear_user(void __user *to, unsigned long n)
  408. {
  409. if (access_ok(VERIFY_WRITE, to, n))
  410. n = __clear_user(to, n);
  411. return n;
  412. }
  413. static inline long __must_check strncpy_from_user(char *dst, const char __user *src, long count)
  414. {
  415. long res = -EFAULT;
  416. if (access_ok(VERIFY_READ, src, 1))
  417. res = __strncpy_from_user(dst, src, count);
  418. return res;
  419. }
  420. #define strlen_user(s) strnlen_user(s, ~0UL >> 1)
  421. static inline long __must_check strnlen_user(const char __user *s, long n)
  422. {
  423. unsigned long res = 0;
  424. if (__addr_ok(s))
  425. res = __strnlen_user(s, n);
  426. return res;
  427. }
  428. #endif /* _ASMARM_UACCESS_H */