uaccess.h 14 KB

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