uaccess.h 14 KB

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