uaccess.h 18 KB

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