rtuDataOperationAnalysis.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 1996, 1997, 1998, 1999, 2000, 03, 04 by Ralf Baechle
  7. * Copyright (C) 1999, 2000 Silicon Graphics, Inc.
  8. * Copyright (C) 2007 Maciej W. Rozycki
  9. */
  10. #ifndef _ASM_UACCESS_H
  11. #define _ASM_UACCESS_H
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/thread_info.h>
  15. /*
  16. * The fs value determines whether argument validity checking should be
  17. * performed or not. If get_fs() == USER_DS, checking is performed, with
  18. * get_fs() == KERNEL_DS, checking is bypassed.
  19. *
  20. * For historical reasons, these macros are grossly misnamed.
  21. */
  22. #ifdef CONFIG_32BIT
  23. #define __UA_LIMIT 0x80000000UL
  24. #define __UA_ADDR ".word"
  25. #define __UA_LA "la"
  26. #define __UA_ADDU "addu"
  27. #define __UA_t0 "$8"
  28. #define __UA_t1 "$9"
  29. #endif /* CONFIG_32BIT */
  30. #ifdef CONFIG_64BIT
  31. extern u64 __ua_limit;
  32. #define __UA_LIMIT __ua_limit
  33. #define __UA_ADDR ".dword"
  34. #define __UA_LA "dla"
  35. #define __UA_ADDU "daddu"
  36. #define __UA_t0 "$12"
  37. #define __UA_t1 "$13"
  38. #endif /* CONFIG_64BIT */
  39. /*
  40. * USER_DS is a bitmask that has the bits set that may not be set in a valid
  41. * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
  42. * the arithmetic we're doing only works if the limit is a power of two, so
  43. * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
  44. * address in this range it's the process's problem, not ours :-)
  45. */
  46. #define KERNEL_DS ((mm_segment_t) { 0UL })
  47. #define USER_DS ((mm_segment_t) { __UA_LIMIT })
  48. #define VERIFY_READ 0
  49. #define VERIFY_WRITE 1
  50. #define get_ds() (KERNEL_DS)
  51. #define get_fs() (current_thread_info()->addr_limit)
  52. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  53. #define segment_eq(a, b) ((a).seg == (b).seg)
  54. /*
  55. * Is a address valid? This does a straighforward calculation rather
  56. * than tests.
  57. *
  58. * Address valid if:
  59. * - "addr" doesn't have any high-bits set
  60. * - AND "size" doesn't have any high-bits set
  61. * - AND "addr+size" doesn't have any high-bits set
  62. * - OR we are in kernel mode.
  63. *
  64. * __ua_size() is a trick to avoid runtime checking of positive constant
  65. * sizes; for those we already know at compile time that the size is ok.
  66. */
  67. #define __ua_size(size) \
  68. ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
  69. /*
  70. * access_ok: - Checks if a user space pointer is valid
  71. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  72. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  73. * to write to a block, it is always safe to read from it.
  74. * @addr: User space pointer to start of block to check
  75. * @size: Size of block to check
  76. *
  77. * Context: User context only. This function may sleep.
  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. #define __access_mask get_fs().seg
  89. #define __access_ok(addr, size, mask) \
  90. ({ \
  91. unsigned long __addr = (unsigned long) (addr); \
  92. unsigned long __size = size; \
  93. unsigned long __mask = mask; \
  94. unsigned long __ok; \
  95. \
  96. __chk_user_ptr(addr); \
  97. __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
  98. __ua_size(__size))); \
  99. __ok == 0; \
  100. })
  101. #define access_ok(type, addr, size) \
  102. likely(__access_ok((addr), (size), __access_mask))
  103. /*
  104. * put_user: - Write a simple value into user space.
  105. * @x: Value to copy to user space.
  106. * @ptr: Destination address, in user space.
  107. *
  108. * Context: User context only. This function may sleep.
  109. *
  110. * This macro copies a single simple value from kernel space to user
  111. * space. It supports simple types like char and int, but not larger
  112. * data types like structures or arrays.
  113. *
  114. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  115. * to the result of dereferencing @ptr.
  116. *
  117. * Returns zero on success, or -EFAULT on error.
  118. */
  119. #define put_user(x,ptr) \
  120. __put_user_check((x), (ptr), sizeof(*(ptr)))
  121. /*
  122. * get_user: - Get a simple variable from user space.
  123. * @x: Variable to store result.
  124. * @ptr: Source address, in user space.
  125. *
  126. * Context: User context only. This function may sleep.
  127. *
  128. * This macro copies a single simple variable from user space to kernel
  129. * space. It supports simple types like char and int, but not larger
  130. * data types like structures or arrays.
  131. *
  132. * @ptr must have pointer-to-simple-variable type, and the result of
  133. * dereferencing @ptr must be assignable to @x without a cast.
  134. *
  135. * Returns zero on success, or -EFAULT on error.
  136. * On error, the variable @x is set to zero.
  137. */
  138. #define get_user(x,ptr) \
  139. __get_user_check((x), (ptr), sizeof(*(ptr)))
  140. /*
  141. * __put_user: - Write a simple value into user space, with less checking.
  142. * @x: Value to copy to user space.
  143. * @ptr: Destination address, in user space.
  144. *
  145. * Context: User context only. This function may sleep.
  146. *
  147. * This macro copies a single simple value from kernel space to user
  148. * space. It supports simple types like char and int, but not larger
  149. * data types like structures or arrays.
  150. *
  151. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  152. * to the result of dereferencing @ptr.
  153. *
  154. * Caller must check the pointer with access_ok() before calling this
  155. * function.
  156. *
  157. * Returns zero on success, or -EFAULT on error.
  158. */
  159. #define __put_user(x,ptr) \
  160. __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  161. /*
  162. * __get_user: - Get a simple variable from user space, with less checking.
  163. * @x: Variable to store result.
  164. * @ptr: Source address, in user space.
  165. *
  166. * Context: User context only. This function may sleep.
  167. *
  168. * This macro copies a single simple variable from user space to kernel
  169. * space. It supports simple types like char and int, but not larger
  170. * data types like structures or arrays.
  171. *
  172. * @ptr must have pointer-to-simple-variable type, and the result of
  173. * dereferencing @ptr must be assignable to @x without a cast.
  174. *
  175. * Caller must check the pointer with access_ok() before calling this
  176. * function.
  177. *
  178. * Returns zero on success, or -EFAULT on error.
  179. * On error, the variable @x is set to zero.
  180. */
  181. #define __get_user(x,ptr) \
  182. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  183. struct __large_struct { unsigned long buf[100]; };
  184. #define __m(x) (*(struct __large_struct __user *)(x))
  185. /*
  186. * Yuck. We need two variants, one for 64bit operation and one
  187. * for 32 bit mode and old iron.
  188. */
  189. #ifdef CONFIG_32BIT
  190. #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
  191. #endif
  192. #ifdef CONFIG_64BIT
  193. #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
  194. #endif
  195. extern void __get_user_unknown(void);
  196. #define __get_user_common(val, size, ptr) \
  197. do { \
  198. switch (size) { \
  199. case 1: __get_user_asm(val, "lb", ptr); break; \
  200. case 2: __get_user_asm(val, "lh", ptr); break; \
  201. case 4: __get_user_asm(val, "lw", ptr); break; \
  202. case 8: __GET_USER_DW(val, ptr); break; \
  203. default: __get_user_unknown(); break; \
  204. } \
  205. } while (0)
  206. #define __get_user_nocheck(x, ptr, size) \
  207. ({ \
  208. int __gu_err; \
  209. \
  210. __chk_user_ptr(ptr); \
  211. __get_user_common((x), size, ptr); \
  212. __gu_err; \
  213. })
  214. #define __get_user_check(x, ptr, size) \
  215. ({ \
  216. int __gu_err = -EFAULT; \
  217. const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
  218. \
  219. might_fault(); \
  220. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
  221. __get_user_common((x), size, __gu_ptr); \
  222. \
  223. __gu_err; \
  224. })
  225. #define __get_user_asm(val, insn, addr) \
  226. { \
  227. long __gu_tmp; \
  228. \
  229. __asm__ __volatile__( \
  230. "1: " insn " %1, %3 \n" \
  231. "2: \n" \
  232. " .section .fixup,\"ax\" \n" \
  233. "3: li %0, %4 \n" \
  234. " j 2b \n" \
  235. " .previous \n" \
  236. " .section __ex_table,\"a\" \n" \
  237. " "__UA_ADDR "\t1b, 3b \n" \
  238. " .previous \n" \
  239. : "=r" (__gu_err), "=r" (__gu_tmp) \
  240. : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
  241. \
  242. (val) = (__typeof__(*(addr))) __gu_tmp; \
  243. }
  244. /*
  245. * Get a long long 64 using 32 bit registers.
  246. */
  247. #define __get_user_asm_ll32(val, addr) \
  248. { \
  249. union { \
  250. unsigned long long l; \
  251. __typeof__(*(addr)) t; \
  252. } __gu_tmp; \
  253. \
  254. __asm__ __volatile__( \
  255. "1: lw %1, (%3) \n" \
  256. "2: lw %D1, 4(%3) \n" \
  257. "3: .section .fixup,\"ax\" \n" \
  258. "4: li %0, %4 \n" \
  259. " move %1, $0 \n" \
  260. " move %D1, $0 \n" \
  261. " j 3b \n" \
  262. " .previous \n" \
  263. " .section __ex_table,\"a\" \n" \
  264. " " __UA_ADDR " 1b, 4b \n" \
  265. " " __UA_ADDR " 2b, 4b \n" \
  266. " .previous \n" \
  267. : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
  268. : "0" (0), "r" (addr), "i" (-EFAULT)); \
  269. \
  270. (val) = __gu_tmp.t; \
  271. }
  272. /*
  273. * Yuck. We need two variants, one for 64bit operation and one
  274. * for 32 bit mode and old iron.
  275. */
  276. #ifdef CONFIG_32BIT
  277. #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
  278. #endif
  279. #ifdef CONFIG_64BIT
  280. #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
  281. #endif
  282. #define __put_user_nocheck(x, ptr, size) \
  283. ({ \
  284. __typeof__(*(ptr)) __pu_val; \
  285. int __pu_err = 0; \
  286. \
  287. __chk_user_ptr(ptr); \
  288. __pu_val = (x); \
  289. switch (size) { \
  290. case 1: __put_user_asm("sb", ptr); break; \
  291. case 2: __put_user_asm("sh", ptr); break; \
  292. case 4: __put_user_asm("sw", ptr); break; \
  293. case 8: __PUT_USER_DW(ptr); break; \
  294. default: __put_user_unknown(); break; \
  295. } \
  296. __pu_err; \
  297. })
  298. #define __put_user_check(x, ptr, size) \
  299. ({ \
  300. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  301. __typeof__(*(ptr)) __pu_val = (x); \
  302. int __pu_err = -EFAULT; \
  303. \
  304. might_fault(); \
  305. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
  306. switch (size) { \
  307. case 1: __put_user_asm("sb", __pu_addr); break; \
  308. case 2: __put_user_asm("sh", __pu_addr); break; \
  309. case 4: __put_user_asm("sw", __pu_addr); break; \
  310. case 8: __PUT_USER_DW(__pu_addr); break; \
  311. default: __put_user_unknown(); break; \
  312. } \
  313. } \
  314. __pu_err; \
  315. })
  316. #define __put_user_asm(insn, ptr) \
  317. { \
  318. __asm__ __volatile__( \
  319. "1: " insn " %z2, %3 # __put_user_asm\n" \
  320. "2: \n" \
  321. " .section .fixup,\"ax\" \n" \
  322. "3: li %0, %4 \n" \
  323. " j 2b \n" \
  324. " .previous \n" \
  325. " .section __ex_table,\"a\" \n" \
  326. " " __UA_ADDR " 1b, 3b \n" \
  327. " .previous \n" \
  328. : "=r" (__pu_err) \
  329. : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
  330. "i" (-EFAULT)); \
  331. }
  332. #define __put_user_asm_ll32(ptr) \
  333. { \
  334. __asm__ __volatile__( \
  335. "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
  336. "2: sw %D2, 4(%3) \n" \
  337. "3: \n" \
  338. " .section .fixup,\"ax\" \n" \
  339. "4: li %0, %4 \n" \
  340. " j 3b \n" \
  341. " .previous \n" \
  342. " .section __ex_table,\"a\" \n" \
  343. " " __UA_ADDR " 1b, 4b \n" \
  344. " " __UA_ADDR " 2b, 4b \n" \
  345. " .previous" \
  346. : "=r" (__pu_err) \
  347. : "0" (0), "r" (__pu_val), "r" (ptr), \
  348. "i" (-EFAULT)); \
  349. }
  350. extern void __put_user_unknown(void);
  351. /*
  352. * put_user_unaligned: - Write a simple value into user space.
  353. * @x: Value to copy to user space.
  354. * @ptr: Destination address, in user space.
  355. *
  356. * Context: User context only. This function may sleep.
  357. *
  358. * This macro copies a single simple value from kernel space to user
  359. * space. It supports simple types like char and int, but not larger
  360. * data types like structures or arrays.
  361. *
  362. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  363. * to the result of dereferencing @ptr.
  364. *
  365. * Returns zero on success, or -EFAULT on error.
  366. */
  367. #define put_user_unaligned(x,ptr) \
  368. __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
  369. /*
  370. * get_user_unaligned: - Get a simple variable from user space.
  371. * @x: Variable to store result.
  372. * @ptr: Source address, in user space.
  373. *
  374. * Context: User context only. This function may sleep.
  375. *
  376. * This macro copies a single simple variable from user space to kernel
  377. * space. It supports simple types like char and int, but not larger
  378. * data types like structures or arrays.
  379. *
  380. * @ptr must have pointer-to-simple-variable type, and the result of
  381. * dereferencing @ptr must be assignable to @x without a cast.
  382. *
  383. * Returns zero on success, or -EFAULT on error.
  384. * On error, the variable @x is set to zero.
  385. */
  386. #define get_user_unaligned(x,ptr) \
  387. __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
  388. /*
  389. * __put_user_unaligned: - Write a simple value into user space, with less checking.
  390. * @x: Value to copy to user space.
  391. * @ptr: Destination address, in user space.
  392. *
  393. * Context: User context only. This function may sleep.
  394. *
  395. * This macro copies a single simple value from kernel space to user
  396. * space. It supports simple types like char and int, but not larger
  397. * data types like structures or arrays.
  398. *
  399. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  400. * to the result of dereferencing @ptr.
  401. *
  402. * Caller must check the pointer with access_ok() before calling this
  403. * function.
  404. *
  405. * Returns zero on success, or -EFAULT on error.
  406. */
  407. #define __put_user_unaligned(x,ptr) \
  408. __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
  409. /*
  410. * __get_user_unaligned: - Get a simple variable from user space, with less checking.
  411. * @x: Variable to store result.
  412. * @ptr: Source address, in user space.
  413. *
  414. * Context: User context only. This function may sleep.
  415. *
  416. * This macro copies a single simple variable from user space to kernel
  417. * space. It supports simple types like char and int, but not larger
  418. * data types like structures or arrays.
  419. *
  420. * @ptr must have pointer-to-simple-variable type, and the result of
  421. * dereferencing @ptr must be assignable to @x without a cast.
  422. *
  423. * Caller must check the pointer with access_ok() before calling this
  424. * function.
  425. *
  426. * Returns zero on success, or -EFAULT on error.
  427. * On error, the variable @x is set to zero.
  428. */
  429. #define __get_user_unaligned(x,ptr) \
  430. __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
  431. /*
  432. * Yuck. We need two variants, one for 64bit operation and one
  433. * for 32 bit mode and old iron.
  434. */
  435. #ifdef CONFIG_32BIT
  436. #define __GET_USER_UNALIGNED_DW(val, ptr) \
  437. __get_user_unaligned_asm_ll32(val, ptr)
  438. #endif
  439. #ifdef CONFIG_64BIT
  440. #define __GET_USER_UNALIGNED_DW(val, ptr) \
  441. __get_user_unaligned_asm(val, "uld", ptr)
  442. #endif
  443. extern void __get_user_unaligned_unknown(void);
  444. #define __get_user_unaligned_common(val, size, ptr) \
  445. do { \
  446. switch (size) { \
  447. case 1: __get_user_asm(val, "lb", ptr); break; \
  448. case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
  449. case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
  450. case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
  451. default: __get_user_unaligned_unknown(); break; \
  452. } \
  453. } while (0)
  454. #define __get_user_unaligned_nocheck(x,ptr,size) \
  455. ({ \
  456. int __gu_err; \
  457. \
  458. __get_user_unaligned_common((x), size, ptr); \
  459. __gu_err; \
  460. })
  461. #define __get_user_unaligned_check(x,ptr,size) \
  462. ({ \
  463. int __gu_err = -EFAULT; \
  464. const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
  465. \
  466. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
  467. __get_user_unaligned_common((x), size, __gu_ptr); \
  468. \
  469. __gu_err; \
  470. })
  471. #define __get_user_unaligned_asm(val, insn, addr) \
  472. { \
  473. long __gu_tmp; \
  474. \
  475. __asm__ __volatile__( \
  476. "1: " insn " %1, %3 \n" \
  477. "2: \n" \
  478. " .section .fixup,\"ax\" \n" \
  479. "3: li %0, %4 \n" \
  480. " j 2b \n" \
  481. " .previous \n" \
  482. " .section __ex_table,\"a\" \n" \
  483. " "__UA_ADDR "\t1b, 3b \n" \