uaccess.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _ASM_M32R_UACCESS_H
  3. #define _ASM_M32R_UACCESS_H
  4. /*
  5. * linux/include/asm-m32r/uaccess.h
  6. *
  7. * M32R version.
  8. * Copyright (C) 2004, 2006 Hirokazu Takata <takata at linux-m32r.org>
  9. */
  10. /*
  11. * User space memory access functions
  12. */
  13. #include <asm/page.h>
  14. #include <asm/setup.h>
  15. #include <linux/prefetch.h>
  16. /*
  17. * The fs value determines whether argument validity checking should be
  18. * performed or not. If get_fs() == USER_DS, checking is performed, with
  19. * get_fs() == KERNEL_DS, checking is bypassed.
  20. *
  21. * For historical reasons, these macros are grossly misnamed.
  22. */
  23. #define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  24. #ifdef CONFIG_MMU
  25. #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
  26. #define USER_DS MAKE_MM_SEG(PAGE_OFFSET)
  27. #define get_ds() (KERNEL_DS)
  28. #define get_fs() (current_thread_info()->addr_limit)
  29. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  30. #else /* not CONFIG_MMU */
  31. #define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
  32. #define USER_DS MAKE_MM_SEG(0xFFFFFFFF)
  33. #define get_ds() (KERNEL_DS)
  34. static inline mm_segment_t get_fs(void)
  35. {
  36. return USER_DS;
  37. }
  38. static inline void set_fs(mm_segment_t s)
  39. {
  40. }
  41. #endif /* not CONFIG_MMU */
  42. #define segment_eq(a, b) ((a).seg == (b).seg)
  43. #define __addr_ok(addr) \
  44. ((unsigned long)(addr) < (current_thread_info()->addr_limit.seg))
  45. /*
  46. * Test whether a block of memory is a valid user space address.
  47. * Returns 0 if the range is valid, nonzero otherwise.
  48. *
  49. * This is equivalent to the following test:
  50. * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
  51. *
  52. * This needs 33-bit arithmetic. We have a carry...
  53. */
  54. #define __range_ok(addr, size) ({ \
  55. unsigned long flag, roksum; \
  56. __chk_user_ptr(addr); \
  57. asm ( \
  58. " cmpu %1, %1 ; clear cbit\n" \
  59. " addx %1, %3 ; set cbit if overflow\n" \
  60. " subx %0, %0\n" \
  61. " cmpu %4, %1\n" \
  62. " subx %0, %5\n" \
  63. : "=&r" (flag), "=r" (roksum) \
  64. : "1" (addr), "r" ((int)(size)), \
  65. "r" (current_thread_info()->addr_limit.seg), "r" (0) \
  66. : "cbit" ); \
  67. flag; })
  68. /**
  69. * access_ok: - Checks if a user space pointer is valid
  70. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  71. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  72. * to write to a block, it is always safe to read from it.
  73. * @addr: User space pointer to start of block to check
  74. * @size: Size of block to check
  75. *
  76. * Context: User context only. This function may sleep if pagefaults are
  77. * enabled.
  78. *
  79. * Checks if a pointer to a block of memory in user space is valid.
  80. *
  81. * Returns true (nonzero) if the memory block may be valid, false (zero)
  82. * if it is definitely invalid.
  83. *
  84. * Note that, depending on architecture, this function probably just
  85. * checks that the pointer is in the user space range - after calling
  86. * this function, memory access functions may still return -EFAULT.
  87. */
  88. #ifdef CONFIG_MMU
  89. #define access_ok(type, addr, size) (likely(__range_ok(addr, size) == 0))
  90. #else
  91. static inline int access_ok(int type, const void *addr, unsigned long size)
  92. {
  93. unsigned long val = (unsigned long)addr;
  94. return ((val >= memory_start) && ((val + size) < memory_end));
  95. }
  96. #endif /* CONFIG_MMU */
  97. #include <asm/extable.h>
  98. /*
  99. * These are the main single-value transfer routines. They automatically
  100. * use the right size if we just have the right pointer type.
  101. *
  102. * This gets kind of ugly. We want to return _two_ values in "get_user()"
  103. * and yet we don't want to do any pointers, because that is too much
  104. * of a performance impact. Thus we have a few rather ugly macros here,
  105. * and hide all the uglyness from the user.
  106. *
  107. * The "__xxx" versions of the user access functions are versions that
  108. * do not verify the address space, that must have been done previously
  109. * with a separate "access_ok()" call (this is used when we do multiple
  110. * accesses to the same area of user memory).
  111. */
  112. /* Careful: we have to cast the result to the type of the pointer for sign
  113. reasons */
  114. /**
  115. * get_user: - Get a simple variable from user space.
  116. * @x: Variable to store result.
  117. * @ptr: Source address, in user space.
  118. *
  119. * Context: User context only. This function may sleep if pagefaults are
  120. * enabled.
  121. *
  122. * This macro copies a single simple variable from user space to kernel
  123. * space. It supports simple types like char and int, but not larger
  124. * data types like structures or arrays.
  125. *
  126. * @ptr must have pointer-to-simple-variable type, and the result of
  127. * dereferencing @ptr must be assignable to @x without a cast.
  128. *
  129. * Returns zero on success, or -EFAULT on error.
  130. * On error, the variable @x is set to zero.
  131. */
  132. #define get_user(x, ptr) \
  133. __get_user_check((x), (ptr), sizeof(*(ptr)))
  134. /**
  135. * put_user: - Write a simple value into user space.
  136. * @x: Value to copy to user space.
  137. * @ptr: Destination address, in user space.
  138. *
  139. * Context: User context only. This function may sleep if pagefaults are
  140. * enabled.
  141. *
  142. * This macro copies a single simple value from kernel space to user
  143. * space. It supports simple types like char and int, but not larger
  144. * data types like structures or arrays.
  145. *
  146. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  147. * to the result of dereferencing @ptr.
  148. *
  149. * Returns zero on success, or -EFAULT on error.
  150. */
  151. #define put_user(x, ptr) \
  152. __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  153. /**
  154. * __get_user: - Get a simple variable from user space, with less checking.
  155. * @x: Variable to store result.
  156. * @ptr: Source address, in user space.
  157. *
  158. * Context: User context only. This function may sleep if pagefaults are
  159. * enabled.
  160. *
  161. * This macro copies a single simple variable from user space to kernel
  162. * space. It supports simple types like char and int, but not larger
  163. * data types like structures or arrays.
  164. *
  165. * @ptr must have pointer-to-simple-variable type, and the result of
  166. * dereferencing @ptr must be assignable to @x without a cast.
  167. *
  168. * Caller must check the pointer with access_ok() before calling this
  169. * function.
  170. *
  171. * Returns zero on success, or -EFAULT on error.
  172. * On error, the variable @x is set to zero.
  173. */
  174. #define __get_user(x, ptr) \
  175. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  176. #define __get_user_nocheck(x, ptr, size) \
  177. ({ \
  178. long __gu_err = 0; \
  179. unsigned long __gu_val = 0; \
  180. might_fault(); \
  181. __get_user_size(__gu_val, (ptr), (size), __gu_err); \
  182. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  183. __gu_err; \
  184. })
  185. #define __get_user_check(x, ptr, size) \
  186. ({ \
  187. long __gu_err = -EFAULT; \
  188. unsigned long __gu_val = 0; \
  189. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  190. might_fault(); \
  191. if (access_ok(VERIFY_READ, __gu_addr, size)) \
  192. __get_user_size(__gu_val, __gu_addr, (size), __gu_err); \
  193. (x) = (__force __typeof__(*(ptr)))__gu_val; \
  194. __gu_err; \
  195. })
  196. extern long __get_user_bad(void);
  197. #define __get_user_size(x, ptr, size, retval) \
  198. do { \
  199. retval = 0; \
  200. __chk_user_ptr(ptr); \
  201. switch (size) { \
  202. case 1: __get_user_asm(x, ptr, retval, "ub"); break; \
  203. case 2: __get_user_asm(x, ptr, retval, "uh"); break; \
  204. case 4: __get_user_asm(x, ptr, retval, ""); break; \
  205. default: (x) = __get_user_bad(); \
  206. } \
  207. } while (0)
  208. #define __get_user_asm(x, addr, err, itype) \
  209. __asm__ __volatile__( \
  210. " .fillinsn\n" \
  211. "1: ld"itype" %1,@%2\n" \
  212. " .fillinsn\n" \
  213. "2:\n" \
  214. ".section .fixup,\"ax\"\n" \
  215. " .balign 4\n" \
  216. "3: ldi %0,%3\n" \
  217. " seth r14,#high(2b)\n" \
  218. " or3 r14,r14,#low(2b)\n" \
  219. " jmp r14\n" \
  220. ".previous\n" \
  221. ".section __ex_table,\"a\"\n" \
  222. " .balign 4\n" \
  223. " .long 1b,3b\n" \
  224. ".previous" \
  225. : "=&r" (err), "=&r" (x) \
  226. : "r" (addr), "i" (-EFAULT), "0" (err) \
  227. : "r14", "memory")
  228. /**
  229. * __put_user: - Write a simple value into user space, with less checking.
  230. * @x: Value to copy to user space.
  231. * @ptr: Destination address, in user space.
  232. *
  233. * Context: User context only. This function may sleep if pagefaults are
  234. * enabled.
  235. *
  236. * This macro copies a single simple value from kernel space to user
  237. * space. It supports simple types like char and int, but not larger
  238. * data types like structures or arrays.
  239. *
  240. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  241. * to the result of dereferencing @ptr.
  242. *
  243. * Caller must check the pointer with access_ok() before calling this
  244. * function.
  245. *
  246. * Returns zero on success, or -EFAULT on error.
  247. */
  248. #define __put_user(x, ptr) \
  249. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  250. #define __put_user_nocheck(x, ptr, size) \
  251. ({ \
  252. long __pu_err; \
  253. might_fault(); \
  254. __put_user_size((x), (ptr), (size), __pu_err); \
  255. __pu_err; \
  256. })
  257. #define __put_user_check(x, ptr, size) \
  258. ({ \
  259. long __pu_err = -EFAULT; \
  260. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  261. might_fault(); \
  262. if (access_ok(VERIFY_WRITE, __pu_addr, size)) \
  263. __put_user_size((x), __pu_addr, (size), __pu_err); \
  264. __pu_err; \
  265. })
  266. #if defined(__LITTLE_ENDIAN__)
  267. #define __put_user_u64(x, addr, err) \
  268. __asm__ __volatile__( \
  269. " .fillinsn\n" \
  270. "1: st %L1,@%2\n" \
  271. " .fillinsn\n" \
  272. "2: st %H1,@(4,%2)\n" \
  273. " .fillinsn\n" \
  274. "3:\n" \
  275. ".section .fixup,\"ax\"\n" \
  276. " .balign 4\n" \
  277. "4: ldi %0,%3\n" \
  278. " seth r14,#high(3b)\n" \
  279. " or3 r14,r14,#low(3b)\n" \
  280. " jmp r14\n" \
  281. ".previous\n" \
  282. ".section __ex_table,\"a\"\n" \
  283. " .balign 4\n" \
  284. " .long 1b,4b\n" \
  285. " .long 2b,4b\n" \
  286. ".previous" \
  287. : "=&r" (err) \
  288. : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
  289. : "r14", "memory")
  290. #elif defined(__BIG_ENDIAN__)
  291. #define __put_user_u64(x, addr, err) \
  292. __asm__ __volatile__( \
  293. " .fillinsn\n" \
  294. "1: st %H1,@%2\n" \
  295. " .fillinsn\n" \
  296. "2: st %L1,@(4,%2)\n" \
  297. " .fillinsn\n" \
  298. "3:\n" \
  299. ".section .fixup,\"ax\"\n" \
  300. " .balign 4\n" \
  301. "4: ldi %0,%3\n" \
  302. " seth r14,#high(3b)\n" \
  303. " or3 r14,r14,#low(3b)\n" \
  304. " jmp r14\n" \
  305. ".previous\n" \
  306. ".section __ex_table,\"a\"\n" \
  307. " .balign 4\n" \
  308. " .long 1b,4b\n" \
  309. " .long 2b,4b\n" \
  310. ".previous" \
  311. : "=&r" (err) \
  312. : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
  313. : "r14", "memory")
  314. #else
  315. #error no endian defined
  316. #endif
  317. extern void __put_user_bad(void);
  318. #define __put_user_size(x, ptr, size, retval) \
  319. do { \
  320. retval = 0; \
  321. __chk_user_ptr(ptr); \
  322. switch (size) { \
  323. case 1: __put_user_asm(x, ptr, retval, "b"); break; \
  324. case 2: __put_user_asm(x, ptr, retval, "h"); break; \
  325. case 4: __put_user_asm(x, ptr, retval, ""); break; \
  326. case 8: __put_user_u64((__typeof__(*ptr))(x), ptr, retval); break;\
  327. default: __put_user_bad(); \
  328. } \
  329. } while (0)
  330. struct __large_struct { unsigned long buf[100]; };
  331. #define __m(x) (*(struct __large_struct *)(x))
  332. /*
  333. * Tell gcc we read from memory instead of writing: this is because
  334. * we do not write to any memory gcc knows about, so there are no
  335. * aliasing issues.
  336. */
  337. #define __put_user_asm(x, addr, err, itype) \
  338. __asm__ __volatile__( \
  339. " .fillinsn\n" \
  340. "1: st"itype" %1,@%2\n" \
  341. " .fillinsn\n" \
  342. "2:\n" \
  343. ".section .fixup,\"ax\"\n" \
  344. " .balign 4\n" \
  345. "3: ldi %0,%3\n" \
  346. " seth r14,#high(2b)\n" \
  347. " or3 r14,r14,#low(2b)\n" \
  348. " jmp r14\n" \
  349. ".previous\n" \
  350. ".section __ex_table,\"a\"\n" \
  351. " .balign 4\n" \
  352. " .long 1b,3b\n" \
  353. ".previous" \
  354. : "=&r" (err) \
  355. : "r" (x), "r" (addr), "i" (-EFAULT), "0" (err) \
  356. : "r14", "memory")
  357. /*
  358. * Here we special-case 1, 2 and 4-byte copy_*_user invocations. On a fault
  359. * we return the initial request size (1, 2 or 4), as copy_*_user should do.
  360. * If a store crosses a page boundary and gets a fault, the m32r will not write
  361. * anything, so this is accurate.
  362. */
  363. /*
  364. * Copy To/From Userspace
  365. */
  366. /* Generic arbitrary sized copy. */
  367. /* Return the number of bytes NOT copied. */
  368. #define __copy_user(to, from, size) \
  369. do { \
  370. unsigned long __dst, __src, __c; \
  371. __asm__ __volatile__ ( \
  372. " mv r14, %0\n" \
  373. " or r14, %1\n" \
  374. " beq %0, %1, 9f\n" \
  375. " beqz %2, 9f\n" \
  376. " and3 r14, r14, #3\n" \
  377. " bnez r14, 2f\n" \
  378. " and3 %2, %2, #3\n" \
  379. " beqz %3, 2f\n" \
  380. " addi %0, #-4 ; word_copy \n" \
  381. " .fillinsn\n" \
  382. "0: ld r14, @%1+\n" \
  383. " addi %3, #-1\n" \
  384. " .fillinsn\n" \
  385. "1: st r14, @+%0\n" \
  386. " bnez %3, 0b\n" \
  387. " beqz %2, 9f\n" \
  388. " addi %0, #4\n" \
  389. " .fillinsn\n" \
  390. "2: ldb r14, @%1 ; byte_copy \n" \
  391. " .fillinsn\n" \
  392. "3: stb r14, @%0\n" \
  393. " addi %1, #1\n" \
  394. " addi %2, #-1\n" \
  395. " addi %0, #1\n" \
  396. " bnez %2, 2b\n" \
  397. " .fillinsn\n" \
  398. "9:\n" \
  399. ".section .fixup,\"ax\"\n" \
  400. " .balign 4\n" \
  401. "5: addi %3, #1\n" \
  402. " addi %1, #-4\n" \
  403. " .fillinsn\n" \
  404. "6: slli %3, #2\n" \
  405. " add %2, %3\n" \
  406. " addi %0, #4\n" \
  407. " .fillinsn\n" \
  408. "7: seth r14, #high(9b)\n" \
  409. " or3 r14, r14, #low(9b)\n" \
  410. " jmp r14\n" \
  411. ".previous\n" \
  412. ".section __ex_table,\"a\"\n" \
  413. " .balign 4\n" \
  414. " .long 0b,6b\n" \
  415. " .long 1b,5b\n" \
  416. " .long 2b,9b\n" \
  417. " .long 3b,9b\n" \
  418. ".previous\n" \
  419. : "=&r" (__dst), "=&r" (__src), "=&r" (size), \
  420. "=&r" (__c) \
  421. : "0" (to), "1" (from), "2" (size), "3" (size / 4) \
  422. : "r14", "memory"); \
  423. } while (0)
  424. /* We let the __ versions of copy_from/to_user inline, because they're often
  425. * used in fast paths and have only a small space overhead.
  426. */
  427. static inline unsigned long
  428. raw_copy_from_user(void *to, const void __user *from, unsigned long n)
  429. {
  430. prefetchw(to);
  431. __copy_user(to, from, n);
  432. return n;
  433. }
  434. static inline unsigned long
  435. raw_copy_to_user(void __user *to, const void *from, unsigned long n)
  436. {
  437. prefetch(from);
  438. __copy_user(to, from, n);
  439. return n;
  440. }
  441. long __must_check strncpy_from_user(char *dst, const char __user *src,
  442. long count);
  443. /**
  444. * __clear_user: - Zero a block of memory in user space, with less checking.
  445. * @to: Destination address, in user space.
  446. * @n: Number of bytes to zero.
  447. *
  448. * Zero a block of memory in user space. Caller must check
  449. * the specified block with access_ok() before calling this function.
  450. *
  451. * Returns number of bytes that could not be cleared.
  452. * On success, this will be zero.
  453. */
  454. unsigned long __clear_user(void __user *mem, unsigned long len);
  455. /**
  456. * clear_user: - Zero a block of memory in user space.
  457. * @to: Destination address, in user space.
  458. * @n: Number of bytes to zero.
  459. *
  460. * Zero a block of memory in user space. Caller must check
  461. * the specified block with access_ok() before calling this function.
  462. *
  463. * Returns number of bytes that could not be cleared.
  464. * On success, this will be zero.
  465. */
  466. unsigned long clear_user(void __user *mem, unsigned long len);
  467. long strnlen_user(const char __user *str, long n);
  468. #endif /* _ASM_M32R_UACCESS_H */