uaccess.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /*
  2. * include/asm-xtensa/uaccess.h
  3. *
  4. * User space memory access functions
  5. *
  6. * These routines provide basic accessing functions to the user memory
  7. * space for the kernel. This header file provides functions such as:
  8. *
  9. * This file is subject to the terms and conditions of the GNU General Public
  10. * License. See the file "COPYING" in the main directory of this archive
  11. * for more details.
  12. *
  13. * Copyright (C) 2001 - 2005 Tensilica Inc.
  14. */
  15. #ifndef _XTENSA_UACCESS_H
  16. #define _XTENSA_UACCESS_H
  17. #include <linux/errno.h>
  18. #include <linux/prefetch.h>
  19. #define VERIFY_READ 0
  20. #define VERIFY_WRITE 1
  21. #ifdef __ASSEMBLY__
  22. #include <asm/current.h>
  23. #include <asm/asm-offsets.h>
  24. #include <asm/processor.h>
  25. #include <asm/types.h>
  26. /*
  27. * These assembly macros mirror the C macros that follow below. They
  28. * should always have identical functionality. See
  29. * arch/xtensa/kernel/sys.S for usage.
  30. */
  31. #define KERNEL_DS 0
  32. #define USER_DS 1
  33. #define get_ds (KERNEL_DS)
  34. /*
  35. * get_fs reads current->thread.current_ds into a register.
  36. * On Entry:
  37. * <ad> anything
  38. * <sp> stack
  39. * On Exit:
  40. * <ad> contains current->thread.current_ds
  41. */
  42. .macro get_fs ad, sp
  43. GET_CURRENT(\ad,\sp)
  44. l32i \ad, \ad, THREAD_CURRENT_DS
  45. .endm
  46. /*
  47. * set_fs sets current->thread.current_ds to some value.
  48. * On Entry:
  49. * <at> anything (temp register)
  50. * <av> value to write
  51. * <sp> stack
  52. * On Exit:
  53. * <at> destroyed (actually, current)
  54. * <av> preserved, value to write
  55. */
  56. .macro set_fs at, av, sp
  57. GET_CURRENT(\at,\sp)
  58. s32i \av, \at, THREAD_CURRENT_DS
  59. .endm
  60. /*
  61. * kernel_ok determines whether we should bypass addr/size checking.
  62. * See the equivalent C-macro version below for clarity.
  63. * On success, kernel_ok branches to a label indicated by parameter
  64. * <success>. This implies that the macro falls through to the next
  65. * insruction on an error.
  66. *
  67. * Note that while this macro can be used independently, we designed
  68. * in for optimal use in the access_ok macro below (i.e., we fall
  69. * through on error).
  70. *
  71. * On Entry:
  72. * <at> anything (temp register)
  73. * <success> label to branch to on success; implies
  74. * fall-through macro on error
  75. * <sp> stack pointer
  76. * On Exit:
  77. * <at> destroyed (actually, current->thread.current_ds)
  78. */
  79. #if ((KERNEL_DS != 0) || (USER_DS == 0))
  80. # error Assembly macro kernel_ok fails
  81. #endif
  82. .macro kernel_ok at, sp, success
  83. get_fs \at, \sp
  84. beqz \at, \success
  85. .endm
  86. /*
  87. * user_ok determines whether the access to user-space memory is allowed.
  88. * See the equivalent C-macro version below for clarity.
  89. *
  90. * On error, user_ok branches to a label indicated by parameter
  91. * <error>. This implies that the macro falls through to the next
  92. * instruction on success.
  93. *
  94. * Note that while this macro can be used independently, we designed
  95. * in for optimal use in the access_ok macro below (i.e., we fall
  96. * through on success).
  97. *
  98. * On Entry:
  99. * <aa> register containing memory address
  100. * <as> register containing memory size
  101. * <at> temp register
  102. * <error> label to branch to on error; implies fall-through
  103. * macro on success
  104. * On Exit:
  105. * <aa> preserved
  106. * <as> preserved
  107. * <at> destroyed (actually, (TASK_SIZE + 1 - size))
  108. */
  109. .macro user_ok aa, as, at, error
  110. movi \at, __XTENSA_UL_CONST(TASK_SIZE)
  111. bgeu \as, \at, \error
  112. sub \at, \at, \as
  113. bgeu \aa, \at, \error
  114. .endm
  115. /*
  116. * access_ok determines whether a memory access is allowed. See the
  117. * equivalent C-macro version below for clarity.
  118. *
  119. * On error, access_ok branches to a label indicated by parameter
  120. * <error>. This implies that the macro falls through to the next
  121. * instruction on success.
  122. *
  123. * Note that we assume success is the common case, and we optimize the
  124. * branch fall-through case on success.
  125. *
  126. * On Entry:
  127. * <aa> register containing memory address
  128. * <as> register containing memory size
  129. * <at> temp register
  130. * <sp>
  131. * <error> label to branch to on error; implies fall-through
  132. * macro on success
  133. * On Exit:
  134. * <aa> preserved
  135. * <as> preserved
  136. * <at> destroyed
  137. */
  138. .macro access_ok aa, as, at, sp, error
  139. kernel_ok \at, \sp, .Laccess_ok_\@
  140. user_ok \aa, \as, \at, \error
  141. .Laccess_ok_\@:
  142. .endm
  143. #else /* __ASSEMBLY__ not defined */
  144. #include <linux/sched.h>
  145. #include <asm/types.h>
  146. /*
  147. * The fs value determines whether argument validity checking should
  148. * be performed or not. If get_fs() == USER_DS, checking is
  149. * performed, with get_fs() == KERNEL_DS, checking is bypassed.
  150. *
  151. * For historical reasons (Data Segment Register?), these macros are
  152. * grossly misnamed.
  153. */
  154. #define KERNEL_DS ((mm_segment_t) { 0 })
  155. #define USER_DS ((mm_segment_t) { 1 })
  156. #define get_ds() (KERNEL_DS)
  157. #define get_fs() (current->thread.current_ds)
  158. #define set_fs(val) (current->thread.current_ds = (val))
  159. #define segment_eq(a,b) ((a).seg == (b).seg)
  160. #define __kernel_ok (segment_eq(get_fs(), KERNEL_DS))
  161. #define __user_ok(addr,size) (((size) <= TASK_SIZE)&&((addr) <= TASK_SIZE-(size)))
  162. #define __access_ok(addr,size) (__kernel_ok || __user_ok((addr),(size)))
  163. #define access_ok(type,addr,size) __access_ok((unsigned long)(addr),(size))
  164. /*
  165. * These are the main single-value transfer routines. They
  166. * automatically use the right size if we just have the right pointer
  167. * type.
  168. *
  169. * This gets kind of ugly. We want to return _two_ values in
  170. * "get_user()" and yet we don't want to do any pointers, because that
  171. * is too much of a performance impact. Thus we have a few rather ugly
  172. * macros here, and hide all the uglyness from the user.
  173. *
  174. * Careful to not
  175. * (a) re-use the arguments for side effects (sizeof is ok)
  176. * (b) require any knowledge of processes at this stage
  177. */
  178. #define put_user(x,ptr) __put_user_check((x),(ptr),sizeof(*(ptr)))
  179. #define get_user(x,ptr) __get_user_check((x),(ptr),sizeof(*(ptr)))
  180. /*
  181. * The "__xxx" versions of the user access functions are versions that
  182. * do not verify the address space, that must have been done previously
  183. * with a separate "access_ok()" call (this is used when we do multiple
  184. * accesses to the same area of user memory).
  185. */
  186. #define __put_user(x,ptr) __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
  187. #define __get_user(x,ptr) __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  188. extern long __put_user_bad(void);
  189. #define __put_user_nocheck(x,ptr,size) \
  190. ({ \
  191. long __pu_err; \
  192. __put_user_size((x),(ptr),(size),__pu_err); \
  193. __pu_err; \
  194. })
  195. #define __put_user_check(x,ptr,size) \
  196. ({ \
  197. long __pu_err = -EFAULT; \
  198. __typeof__(*(ptr)) *__pu_addr = (ptr); \
  199. if (access_ok(VERIFY_WRITE,__pu_addr,size)) \
  200. __put_user_size((x),__pu_addr,(size),__pu_err); \
  201. __pu_err; \
  202. })
  203. #define __put_user_size(x,ptr,size,retval) \
  204. do { \
  205. int __cb; \
  206. retval = 0; \
  207. switch (size) { \
  208. case 1: __put_user_asm(x,ptr,retval,1,"s8i",__cb); break; \
  209. case 2: __put_user_asm(x,ptr,retval,2,"s16i",__cb); break; \
  210. case 4: __put_user_asm(x,ptr,retval,4,"s32i",__cb); break; \
  211. case 8: { \
  212. __typeof__(*ptr) __v64 = x; \
  213. retval = __copy_to_user(ptr,&__v64,8); \
  214. break; \
  215. } \
  216. default: __put_user_bad(); \
  217. } \
  218. } while (0)
  219. /*
  220. * Consider a case of a user single load/store would cause both an
  221. * unaligned exception and an MMU-related exception (unaligned
  222. * exceptions happen first):
  223. *
  224. * User code passes a bad variable ptr to a system call.
  225. * Kernel tries to access the variable.
  226. * Unaligned exception occurs.
  227. * Unaligned exception handler tries to make aligned accesses.
  228. * Double exception occurs for MMU-related cause (e.g., page not mapped).
  229. * do_page_fault() thinks the fault address belongs to the kernel, not the
  230. * user, and panics.
  231. *
  232. * The kernel currently prohibits user unaligned accesses. We use the
  233. * __check_align_* macros to check for unaligned addresses before
  234. * accessing user space so we don't crash the kernel. Both
  235. * __put_user_asm and __get_user_asm use these alignment macros, so
  236. * macro-specific labels such as 0f, 1f, %0, %2, and %3 must stay in
  237. * sync.
  238. */
  239. #define __check_align_1 ""
  240. #define __check_align_2 \
  241. " _bbci.l %3, 0, 1f \n" \
  242. " movi %0, %4 \n" \
  243. " _j 2f \n"
  244. #define __check_align_4 \
  245. " _bbsi.l %3, 0, 0f \n" \
  246. " _bbci.l %3, 1, 1f \n" \
  247. "0: movi %0, %4 \n" \
  248. " _j 2f \n"
  249. /*
  250. * We don't tell gcc that we are accessing memory, but this is OK
  251. * because we do not write to any memory gcc knows about, so there
  252. * are no aliasing issues.
  253. *
  254. * WARNING: If you modify this macro at all, verify that the
  255. * __check_align_* macros still work.
  256. */
  257. #define __put_user_asm(x, addr, err, align, insn, cb) \
  258. __asm__ __volatile__( \
  259. __check_align_##align \
  260. "1: "insn" %2, %3, 0 \n" \
  261. "2: \n" \
  262. " .section .fixup,\"ax\" \n" \
  263. " .align 4 \n" \
  264. "4: \n" \
  265. " .long 2b \n" \
  266. "5: \n" \
  267. " l32r %1, 4b \n" \
  268. " movi %0, %4 \n" \
  269. " jx %1 \n" \
  270. " .previous \n" \
  271. " .section __ex_table,\"a\" \n" \
  272. " .long 1b, 5b \n" \
  273. " .previous" \
  274. :"=r" (err), "=r" (cb) \
  275. :"r" ((int)(x)), "r" (addr), "i" (-EFAULT), "0" (err))
  276. #define __get_user_nocheck(x,ptr,size) \
  277. ({ \
  278. long __gu_err, __gu_val; \
  279. __get_user_size(__gu_val,(ptr),(size),__gu_err); \
  280. (x) = (__typeof__(*(ptr)))__gu_val; \
  281. __gu_err; \
  282. })
  283. #define __get_user_check(x,ptr,size) \
  284. ({ \
  285. long __gu_err = -EFAULT, __gu_val = 0; \
  286. const __typeof__(*(ptr)) *__gu_addr = (ptr); \
  287. if (access_ok(VERIFY_READ,__gu_addr,size)) \
  288. __get_user_size(__gu_val,__gu_addr,(size),__gu_err); \
  289. (x) = (__typeof__(*(ptr)))__gu_val; \
  290. __gu_err; \
  291. })
  292. extern long __get_user_bad(void);
  293. #define __get_user_size(x,ptr,size,retval) \
  294. do { \
  295. int __cb; \
  296. retval = 0; \
  297. switch (size) { \
  298. case 1: __get_user_asm(x,ptr,retval,1,"l8ui",__cb); break; \
  299. case 2: __get_user_asm(x,ptr,retval,2,"l16ui",__cb); break; \
  300. case 4: __get_user_asm(x,ptr,retval,4,"l32i",__cb); break; \
  301. case 8: retval = __copy_from_user(&x,ptr,8); break; \
  302. default: (x) = __get_user_bad(); \
  303. } \
  304. } while (0)
  305. /*
  306. * WARNING: If you modify this macro at all, verify that the
  307. * __check_align_* macros still work.
  308. */
  309. #define __get_user_asm(x, addr, err, align, insn, cb) \
  310. __asm__ __volatile__( \
  311. __check_align_##align \
  312. "1: "insn" %2, %3, 0 \n" \
  313. "2: \n" \
  314. " .section .fixup,\"ax\" \n" \
  315. " .align 4 \n" \
  316. "4: \n" \
  317. " .long 2b \n" \
  318. "5: \n" \
  319. " l32r %1, 4b \n" \
  320. " movi %2, 0 \n" \
  321. " movi %0, %4 \n" \
  322. " jx %1 \n" \
  323. " .previous \n" \
  324. " .section __ex_table,\"a\" \n" \
  325. " .long 1b, 5b \n" \
  326. " .previous" \
  327. :"=r" (err), "=r" (cb), "=r" (x) \
  328. :"r" (addr), "i" (-EFAULT), "0" (err))
  329. /*
  330. * Copy to/from user space
  331. */
  332. /*
  333. * We use a generic, arbitrary-sized copy subroutine. The Xtensa
  334. * architecture would cause heavy code bloat if we tried to inline
  335. * these functions and provide __constant_copy_* equivalents like the
  336. * i386 versions. __xtensa_copy_user is quite efficient. See the
  337. * .fixup section of __xtensa_copy_user for a discussion on the
  338. * X_zeroing equivalents for Xtensa.
  339. */
  340. extern unsigned __xtensa_copy_user(void *to, const void *from, unsigned n);
  341. #define __copy_user(to,from,size) __xtensa_copy_user(to,from,size)
  342. static inline unsigned long
  343. __generic_copy_from_user_nocheck(void *to, const void *from, unsigned long n)
  344. {
  345. return __copy_user(to,from,n);
  346. }
  347. static inline unsigned long
  348. __generic_copy_to_user_nocheck(void *to, const void *from, unsigned long n)
  349. {
  350. return __copy_user(to,from,n);
  351. }
  352. static inline unsigned long
  353. __generic_copy_to_user(void *to, const void *from, unsigned long n)
  354. {
  355. prefetch(from);
  356. if (access_ok(VERIFY_WRITE, to, n))
  357. return __copy_user(to,from,n);
  358. return n;
  359. }
  360. static inline unsigned long
  361. __generic_copy_from_user(void *to, const void *from, unsigned long n)
  362. {
  363. prefetchw(to);
  364. if (access_ok(VERIFY_READ, from, n))
  365. return __copy_user(to,from,n);
  366. else
  367. memset(to, 0, n);
  368. return n;
  369. }
  370. #define copy_to_user(to,from,n) __generic_copy_to_user((to),(from),(n))
  371. #define copy_from_user(to,from,n) __generic_copy_from_user((to),(from),(n))
  372. #define __copy_to_user(to,from,n) __generic_copy_to_user_nocheck((to),(from),(n))
  373. #define __copy_from_user(to,from,n) __generic_copy_from_user_nocheck((to),(from),(n))
  374. #define __copy_to_user_inatomic __copy_to_user
  375. #define __copy_from_user_inatomic __copy_from_user
  376. /*
  377. * We need to return the number of bytes not cleared. Our memset()
  378. * returns zero if a problem occurs while accessing user-space memory.
  379. * In that event, return no memory cleared. Otherwise, zero for
  380. * success.
  381. */
  382. static inline unsigned long
  383. __xtensa_clear_user(void *addr, unsigned long size)
  384. {
  385. if ( ! memset(addr, 0, size) )
  386. return size;
  387. return 0;
  388. }
  389. static inline unsigned long
  390. clear_user(void *addr, unsigned long size)
  391. {
  392. if (access_ok(VERIFY_WRITE, addr, size))
  393. return __xtensa_clear_user(addr, size);
  394. return size ? -EFAULT : 0;
  395. }
  396. #define __clear_user __xtensa_clear_user
  397. extern long __strncpy_user(char *, const char *, long);
  398. #define __strncpy_from_user __strncpy_user
  399. static inline long
  400. strncpy_from_user(char *dst, const char *src, long count)
  401. {
  402. if (access_ok(VERIFY_READ, src, 1))
  403. return __strncpy_from_user(dst, src, count);
  404. return -EFAULT;
  405. }
  406. #define strlen_user(str) strnlen_user((str), TASK_SIZE - 1)
  407. /*
  408. * Return the size of a string (including the ending 0!)
  409. */
  410. extern long __strnlen_user(const char *, long);
  411. static inline long strnlen_user(const char *str, long len)
  412. {
  413. unsigned long top = __kernel_ok ? ~0UL : TASK_SIZE - 1;
  414. if ((unsigned long)str > top)
  415. return 0;
  416. return __strnlen_user(str, len);
  417. }
  418. struct exception_table_entry
  419. {
  420. unsigned long insn, fixup;
  421. };
  422. /* Returns 0 if exception not found and fixup.unit otherwise. */
  423. extern unsigned long search_exception_table(unsigned long addr);
  424. extern void sort_exception_table(void);
  425. /* Returns the new pc */
  426. #define fixup_exception(map_reg, fixup_unit, pc) \
  427. ({ \
  428. fixup_unit; \
  429. })
  430. #endif /* __ASSEMBLY__ */
  431. #endif /* _XTENSA_UACCESS_H */