uaccess.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. /*
  2. * Copyright 2010 Tilera Corporation. All Rights Reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation, version 2.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  11. * NON INFRINGEMENT. See the GNU General Public License for
  12. * more details.
  13. */
  14. #ifndef _ASM_TILE_UACCESS_H
  15. #define _ASM_TILE_UACCESS_H
  16. /*
  17. * User space memory access functions
  18. */
  19. #include <linux/sched.h>
  20. #include <linux/mm.h>
  21. #include <asm-generic/uaccess-unaligned.h>
  22. #include <asm/processor.h>
  23. #include <asm/page.h>
  24. #define VERIFY_READ 0
  25. #define VERIFY_WRITE 1
  26. /*
  27. * The fs value determines whether argument validity checking should be
  28. * performed or not. If get_fs() == USER_DS, checking is performed, with
  29. * get_fs() == KERNEL_DS, checking is bypassed.
  30. *
  31. * For historical reasons, these macros are grossly misnamed.
  32. */
  33. #define MAKE_MM_SEG(a) ((mm_segment_t) { (a) })
  34. #define KERNEL_DS MAKE_MM_SEG(-1UL)
  35. #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
  36. #define get_ds() (KERNEL_DS)
  37. #define get_fs() (current_thread_info()->addr_limit)
  38. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  39. #define segment_eq(a, b) ((a).seg == (b).seg)
  40. #ifndef __tilegx__
  41. /*
  42. * We could allow mapping all 16 MB at 0xfc000000, but we set up a
  43. * special hack in arch_setup_additional_pages() to auto-create a mapping
  44. * for the first 16 KB, and it would seem strange to have different
  45. * user-accessible semantics for memory at 0xfc000000 and above 0xfc004000.
  46. */
  47. static inline int is_arch_mappable_range(unsigned long addr,
  48. unsigned long size)
  49. {
  50. return (addr >= MEM_USER_INTRPT &&
  51. addr < (MEM_USER_INTRPT + INTRPT_SIZE) &&
  52. size <= (MEM_USER_INTRPT + INTRPT_SIZE) - addr);
  53. }
  54. #define is_arch_mappable_range is_arch_mappable_range
  55. #else
  56. #define is_arch_mappable_range(addr, size) 0
  57. #endif
  58. /*
  59. * Test whether a block of memory is a valid user space address.
  60. * Returns 0 if the range is valid, nonzero otherwise.
  61. */
  62. int __range_ok(unsigned long addr, unsigned long size);
  63. /**
  64. * access_ok: - Checks if a user space pointer is valid
  65. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  66. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  67. * to write to a block, it is always safe to read from it.
  68. * @addr: User space pointer to start of block to check
  69. * @size: Size of block to check
  70. *
  71. * Context: User context only. This function may sleep.
  72. *
  73. * Checks if a pointer to a block of memory in user space is valid.
  74. *
  75. * Returns true (nonzero) if the memory block may be valid, false (zero)
  76. * if it is definitely invalid.
  77. *
  78. * Note that, depending on architecture, this function probably just
  79. * checks that the pointer is in the user space range - after calling
  80. * this function, memory access functions may still return -EFAULT.
  81. */
  82. #define access_ok(type, addr, size) ({ \
  83. __chk_user_ptr(addr); \
  84. likely(__range_ok((unsigned long)(addr), (size)) == 0); \
  85. })
  86. /*
  87. * The exception table consists of pairs of addresses: the first is the
  88. * address of an instruction that is allowed to fault, and the second is
  89. * the address at which the program should continue. No registers are
  90. * modified, so it is entirely up to the continuation code to figure out
  91. * what to do.
  92. *
  93. * All the routines below use bits of fixup code that are out of line
  94. * with the main instruction path. This means when everything is well,
  95. * we don't even have to jump over them. Further, they do not intrude
  96. * on our cache or tlb entries.
  97. */
  98. struct exception_table_entry {
  99. unsigned long insn, fixup;
  100. };
  101. extern int fixup_exception(struct pt_regs *regs);
  102. /*
  103. * We return the __get_user_N function results in a structure,
  104. * thus in r0 and r1. If "err" is zero, "val" is the result
  105. * of the read; otherwise, "err" is -EFAULT.
  106. *
  107. * We rarely need 8-byte values on a 32-bit architecture, but
  108. * we size the structure to accommodate. In practice, for the
  109. * the smaller reads, we can zero the high word for free, and
  110. * the caller will ignore it by virtue of casting anyway.
  111. */
  112. struct __get_user {
  113. unsigned long long val;
  114. int err;
  115. };
  116. /*
  117. * FIXME: we should express these as inline extended assembler, since
  118. * they're fundamentally just a variable dereference and some
  119. * supporting exception_table gunk. Note that (a la i386) we can
  120. * extend the copy_to_user and copy_from_user routines to call into
  121. * such extended assembler routines, though we will have to use a
  122. * different return code in that case (1, 2, or 4, rather than -EFAULT).
  123. */
  124. extern struct __get_user __get_user_1(const void __user *);
  125. extern struct __get_user __get_user_2(const void __user *);
  126. extern struct __get_user __get_user_4(const void __user *);
  127. extern struct __get_user __get_user_8(const void __user *);
  128. extern int __put_user_1(long, void __user *);
  129. extern int __put_user_2(long, void __user *);
  130. extern int __put_user_4(long, void __user *);
  131. extern int __put_user_8(long long, void __user *);
  132. /* Unimplemented routines to cause linker failures */
  133. extern struct __get_user __get_user_bad(void);
  134. extern int __put_user_bad(void);
  135. /*
  136. * Careful: we have to cast the result to the type of the pointer
  137. * for sign reasons.
  138. */
  139. /**
  140. * __get_user: - Get a simple variable from user space, with less checking.
  141. * @x: Variable to store result.
  142. * @ptr: Source address, in user space.
  143. *
  144. * Context: User context only. This function may sleep.
  145. *
  146. * This macro copies a single simple variable from user space to kernel
  147. * space. It supports simple types like char and int, but not larger
  148. * data types like structures or arrays.
  149. *
  150. * @ptr must have pointer-to-simple-variable type, and the result of
  151. * dereferencing @ptr must be assignable to @x without a cast.
  152. *
  153. * Returns zero on success, or -EFAULT on error.
  154. * On error, the variable @x is set to zero.
  155. *
  156. * Caller must check the pointer with access_ok() before calling this
  157. * function.
  158. */
  159. #define __get_user(x, ptr) \
  160. ({ struct __get_user __ret; \
  161. __typeof__(*(ptr)) const __user *__gu_addr = (ptr); \
  162. __chk_user_ptr(__gu_addr); \
  163. switch (sizeof(*(__gu_addr))) { \
  164. case 1: \
  165. __ret = __get_user_1(__gu_addr); \
  166. break; \
  167. case 2: \
  168. __ret = __get_user_2(__gu_addr); \
  169. break; \
  170. case 4: \
  171. __ret = __get_user_4(__gu_addr); \
  172. break; \
  173. case 8: \
  174. __ret = __get_user_8(__gu_addr); \
  175. break; \
  176. default: \
  177. __ret = __get_user_bad(); \
  178. break; \
  179. } \
  180. (x) = (__typeof__(*__gu_addr)) (__typeof__(*__gu_addr - *__gu_addr)) \
  181. __ret.val; \
  182. __ret.err; \
  183. })
  184. /**
  185. * __put_user: - Write a simple value into user space, with less checking.
  186. * @x: Value to copy to user space.
  187. * @ptr: Destination address, in user space.
  188. *
  189. * Context: User context only. This function may sleep.
  190. *
  191. * This macro copies a single simple value from kernel space to user
  192. * space. It supports simple types like char and int, but not larger
  193. * data types like structures or arrays.
  194. *
  195. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  196. * to the result of dereferencing @ptr.
  197. *
  198. * Caller must check the pointer with access_ok() before calling this
  199. * function.
  200. *
  201. * Returns zero on success, or -EFAULT on error.
  202. *
  203. * Implementation note: The "case 8" logic of casting to the type of
  204. * the result of subtracting the value from itself is basically a way
  205. * of keeping all integer types the same, but casting any pointers to
  206. * ptrdiff_t, i.e. also an integer type. This way there are no
  207. * questionable casts seen by the compiler on an ILP32 platform.
  208. */
  209. #define __put_user(x, ptr) \
  210. ({ \
  211. int __pu_err = 0; \
  212. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  213. typeof(*__pu_addr) __pu_val = (x); \
  214. __chk_user_ptr(__pu_addr); \
  215. switch (sizeof(__pu_val)) { \
  216. case 1: \
  217. __pu_err = __put_user_1((long)__pu_val, __pu_addr); \
  218. break; \
  219. case 2: \
  220. __pu_err = __put_user_2((long)__pu_val, __pu_addr); \
  221. break; \
  222. case 4: \
  223. __pu_err = __put_user_4((long)__pu_val, __pu_addr); \
  224. break; \
  225. case 8: \
  226. __pu_err = \
  227. __put_user_8((__typeof__(__pu_val - __pu_val))__pu_val,\
  228. __pu_addr); \
  229. break; \
  230. default: \
  231. __pu_err = __put_user_bad(); \
  232. break; \
  233. } \
  234. __pu_err; \
  235. })
  236. /*
  237. * The versions of get_user and put_user without initial underscores
  238. * check the address of their arguments to make sure they are not
  239. * in kernel space.
  240. */
  241. #define put_user(x, ptr) \
  242. ({ \
  243. __typeof__(*(ptr)) __user *__Pu_addr = (ptr); \
  244. access_ok(VERIFY_WRITE, (__Pu_addr), sizeof(*(__Pu_addr))) ? \
  245. __put_user((x), (__Pu_addr)) : \
  246. -EFAULT; \
  247. })
  248. #define get_user(x, ptr) \
  249. ({ \
  250. __typeof__(*(ptr)) const __user *__Gu_addr = (ptr); \
  251. access_ok(VERIFY_READ, (__Gu_addr), sizeof(*(__Gu_addr))) ? \
  252. __get_user((x), (__Gu_addr)) : \
  253. ((x) = 0, -EFAULT); \
  254. })
  255. /**
  256. * __copy_to_user() - copy data into user space, with less checking.
  257. * @to: Destination address, in user space.
  258. * @from: Source address, in kernel space.
  259. * @n: Number of bytes to copy.
  260. *
  261. * Context: User context only. This function may sleep.
  262. *
  263. * Copy data from kernel space to user space. Caller must check
  264. * the specified block with access_ok() before calling this function.
  265. *
  266. * Returns number of bytes that could not be copied.
  267. * On success, this will be zero.
  268. *
  269. * An alternate version - __copy_to_user_inatomic() - is designed
  270. * to be called from atomic context, typically bracketed by calls
  271. * to pagefault_disable() and pagefault_enable().
  272. */
  273. extern unsigned long __must_check __copy_to_user_inatomic(
  274. void __user *to, const void *from, unsigned long n);
  275. static inline unsigned long __must_check
  276. __copy_to_user(void __user *to, const void *from, unsigned long n)
  277. {
  278. might_fault();
  279. return __copy_to_user_inatomic(to, from, n);
  280. }
  281. static inline unsigned long __must_check
  282. copy_to_user(void __user *to, const void *from, unsigned long n)
  283. {
  284. if (access_ok(VERIFY_WRITE, to, n))
  285. n = __copy_to_user(to, from, n);
  286. return n;
  287. }
  288. /**
  289. * __copy_from_user() - copy data from user space, with less checking.
  290. * @to: Destination address, in kernel space.
  291. * @from: Source address, in user space.
  292. * @n: Number of bytes to copy.
  293. *
  294. * Context: User context only. This function may sleep.
  295. *
  296. * Copy data from user space to kernel space. Caller must check
  297. * the specified block with access_ok() before calling this function.
  298. *
  299. * Returns number of bytes that could not be copied.
  300. * On success, this will be zero.
  301. *
  302. * If some data could not be copied, this function will pad the copied
  303. * data to the requested size using zero bytes.
  304. *
  305. * An alternate version - __copy_from_user_inatomic() - is designed
  306. * to be called from atomic context, typically bracketed by calls
  307. * to pagefault_disable() and pagefault_enable(). This version
  308. * does *NOT* pad with zeros.
  309. */
  310. extern unsigned long __must_check __copy_from_user_inatomic(
  311. void *to, const void __user *from, unsigned long n);
  312. extern unsigned long __must_check __copy_from_user_zeroing(
  313. void *to, const void __user *from, unsigned long n);
  314. static inline unsigned long __must_check
  315. __copy_from_user(void *to, const void __user *from, unsigned long n)
  316. {
  317. might_fault();
  318. return __copy_from_user_zeroing(to, from, n);
  319. }
  320. static inline unsigned long __must_check
  321. _copy_from_user(void *to, const void __user *from, unsigned long n)
  322. {
  323. if (access_ok(VERIFY_READ, from, n))
  324. n = __copy_from_user(to, from, n);
  325. else
  326. memset(to, 0, n);
  327. return n;
  328. }
  329. #ifdef CONFIG_DEBUG_COPY_FROM_USER
  330. extern void copy_from_user_overflow(void)
  331. __compiletime_warning("copy_from_user() size is not provably correct");
  332. static inline unsigned long __must_check copy_from_user(void *to,
  333. const void __user *from,
  334. unsigned long n)
  335. {
  336. int sz = __compiletime_object_size(to);
  337. if (likely(sz == -1 || sz >= n))
  338. n = _copy_from_user(to, from, n);
  339. else
  340. copy_from_user_overflow();
  341. return n;
  342. }
  343. #else
  344. #define copy_from_user _copy_from_user
  345. #endif
  346. #ifdef __tilegx__
  347. /**
  348. * __copy_in_user() - copy data within user space, with less checking.
  349. * @to: Destination address, in user space.
  350. * @from: Source address, in kernel space.
  351. * @n: Number of bytes to copy.
  352. *
  353. * Context: User context only. This function may sleep.
  354. *
  355. * Copy data from user space to user space. Caller must check
  356. * the specified blocks with access_ok() before calling this function.
  357. *
  358. * Returns number of bytes that could not be copied.
  359. * On success, this will be zero.
  360. */
  361. extern unsigned long __copy_in_user_inatomic(
  362. void __user *to, const void __user *from, unsigned long n);
  363. static inline unsigned long __must_check
  364. __copy_in_user(void __user *to, const void __user *from, unsigned long n)
  365. {
  366. might_sleep();
  367. return __copy_in_user_inatomic(to, from, n);
  368. }
  369. static inline unsigned long __must_check
  370. copy_in_user(void __user *to, const void __user *from, unsigned long n)
  371. {
  372. if (access_ok(VERIFY_WRITE, to, n) && access_ok(VERIFY_READ, from, n))
  373. n = __copy_in_user(to, from, n);
  374. return n;
  375. }
  376. #endif
  377. /**
  378. * strlen_user: - Get the size of a string in user space.
  379. * @str: The string to measure.
  380. *
  381. * Context: User context only. This function may sleep.
  382. *
  383. * Get the size of a NUL-terminated string in user space.
  384. *
  385. * Returns the size of the string INCLUDING the terminating NUL.
  386. * On exception, returns 0.
  387. *
  388. * If there is a limit on the length of a valid string, you may wish to
  389. * consider using strnlen_user() instead.
  390. */
  391. extern long strnlen_user_asm(const char __user *str, long n);
  392. static inline long __must_check strnlen_user(const char __user *str, long n)
  393. {
  394. might_fault();
  395. return strnlen_user_asm(str, n);
  396. }
  397. #define strlen_user(str) strnlen_user(str, LONG_MAX)
  398. /**
  399. * strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  400. * @dst: Destination address, in kernel space. This buffer must be at
  401. * least @count bytes long.
  402. * @src: Source address, in user space.
  403. * @count: Maximum number of bytes to copy, including the trailing NUL.
  404. *
  405. * Copies a NUL-terminated string from userspace to kernel space.
  406. * Caller must check the specified block with access_ok() before calling
  407. * this function.
  408. *
  409. * On success, returns the length of the string (not including the trailing
  410. * NUL).
  411. *
  412. * If access to userspace fails, returns -EFAULT (some data may have been
  413. * copied).
  414. *
  415. * If @count is smaller than the length of the string, copies @count bytes
  416. * and returns @count.
  417. */
  418. extern long strncpy_from_user_asm(char *dst, const char __user *src, long);
  419. static inline long __must_check __strncpy_from_user(
  420. char *dst, const char __user *src, long count)
  421. {
  422. might_fault();
  423. return strncpy_from_user_asm(dst, src, count);
  424. }
  425. static inline long __must_check strncpy_from_user(
  426. char *dst, const char __user *src, long count)
  427. {
  428. if (access_ok(VERIFY_READ, src, 1))
  429. return __strncpy_from_user(dst, src, count);
  430. return -EFAULT;
  431. }
  432. /**
  433. * clear_user: - Zero a block of memory in user space.
  434. * @mem: Destination address, in user space.
  435. * @len: Number of bytes to zero.
  436. *
  437. * Zero a block of memory in user space.
  438. *
  439. * Returns number of bytes that could not be cleared.
  440. * On success, this will be zero.
  441. */
  442. extern unsigned long clear_user_asm(void __user *mem, unsigned long len);
  443. static inline unsigned long __must_check __clear_user(
  444. void __user *mem, unsigned long len)
  445. {
  446. might_fault();
  447. return clear_user_asm(mem, len);
  448. }
  449. static inline unsigned long __must_check clear_user(
  450. void __user *mem, unsigned long len)
  451. {
  452. if (access_ok(VERIFY_WRITE, mem, len))
  453. return __clear_user(mem, len);
  454. return len;
  455. }
  456. /**
  457. * flush_user: - Flush a block of memory in user space from cache.
  458. * @mem: Destination address, in user space.
  459. * @len: Number of bytes to flush.
  460. *
  461. * Returns number of bytes that could not be flushed.
  462. * On success, this will be zero.
  463. */
  464. extern unsigned long flush_user_asm(void __user *mem, unsigned long len);
  465. static inline unsigned long __must_check __flush_user(
  466. void __user *mem, unsigned long len)
  467. {
  468. int retval;
  469. might_fault();
  470. retval = flush_user_asm(mem, len);
  471. mb_incoherent();
  472. return retval;
  473. }
  474. static inline unsigned long __must_check flush_user(
  475. void __user *mem, unsigned long len)
  476. {
  477. if (access_ok(VERIFY_WRITE, mem, len))
  478. return __flush_user(mem, len);
  479. return len;
  480. }
  481. /**
  482. * inv_user: - Invalidate a block of memory in user space from cache.
  483. * @mem: Destination address, in user space.
  484. * @len: Number of bytes to invalidate.
  485. *
  486. * Returns number of bytes that could not be invalidated.
  487. * On success, this will be zero.
  488. *
  489. * Note that on Tile64, the "inv" operation is in fact a
  490. * "flush and invalidate", so cache write-backs will occur prior
  491. * to the cache being marked invalid.
  492. */
  493. extern unsigned long inv_user_asm(void __user *mem, unsigned long len);
  494. static inline unsigned long __must_check __inv_user(
  495. void __user *mem, unsigned long len)
  496. {
  497. int retval;
  498. might_fault();
  499. retval = inv_user_asm(mem, len);
  500. mb_incoherent();
  501. return retval;
  502. }
  503. static inline unsigned long __must_check inv_user(
  504. void __user *mem, unsigned long len)
  505. {
  506. if (access_ok(VERIFY_WRITE, mem, len))
  507. return __inv_user(mem, len);
  508. return len;
  509. }
  510. /**
  511. * finv_user: - Flush-inval a block of memory in user space from cache.
  512. * @mem: Destination address, in user space.
  513. * @len: Number of bytes to invalidate.
  514. *
  515. * Returns number of bytes that could not be flush-invalidated.
  516. * On success, this will be zero.
  517. */
  518. extern unsigned long finv_user_asm(void __user *mem, unsigned long len);
  519. static inline unsigned long __must_check __finv_user(
  520. void __user *mem, unsigned long len)
  521. {
  522. int retval;
  523. might_fault();
  524. retval = finv_user_asm(mem, len);
  525. mb_incoherent();
  526. return retval;
  527. }
  528. static inline unsigned long __must_check finv_user(
  529. void __user *mem, unsigned long len)
  530. {
  531. if (access_ok(VERIFY_WRITE, mem, len))
  532. return __finv_user(mem, len);
  533. return len;
  534. }
  535. #endif /* _ASM_TILE_UACCESS_H */