uaccess.h 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  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. #define __UA_LIMIT (- TASK_SIZE)
  32. #define __UA_ADDR ".dword"
  33. #define __UA_LA "dla"
  34. #define __UA_ADDU "daddu"
  35. #define __UA_t0 "$12"
  36. #define __UA_t1 "$13"
  37. #endif /* CONFIG_64BIT */
  38. /*
  39. * USER_DS is a bitmask that has the bits set that may not be set in a valid
  40. * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
  41. * the arithmetic we're doing only works if the limit is a power of two, so
  42. * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
  43. * address in this range it's the process's problem, not ours :-)
  44. */
  45. #define KERNEL_DS ((mm_segment_t) { 0UL })
  46. #define USER_DS ((mm_segment_t) { __UA_LIMIT })
  47. #define VERIFY_READ 0
  48. #define VERIFY_WRITE 1
  49. #define get_ds() (KERNEL_DS)
  50. #define get_fs() (current_thread_info()->addr_limit)
  51. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  52. #define segment_eq(a, b) ((a).seg == (b).seg)
  53. /*
  54. * Is a address valid? This does a straighforward calculation rather
  55. * than tests.
  56. *
  57. * Address valid if:
  58. * - "addr" doesn't have any high-bits set
  59. * - AND "size" doesn't have any high-bits set
  60. * - AND "addr+size" doesn't have any high-bits set
  61. * - OR we are in kernel mode.
  62. *
  63. * __ua_size() is a trick to avoid runtime checking of positive constant
  64. * sizes; for those we already know at compile time that the size is ok.
  65. */
  66. #define __ua_size(size) \
  67. ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
  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.
  77. *
  78. * Checks if a pointer to a block of memory in user space is valid.
  79. *
  80. * Returns true (nonzero) if the memory block may be valid, false (zero)
  81. * if it is definitely invalid.
  82. *
  83. * Note that, depending on architecture, this function probably just
  84. * checks that the pointer is in the user space range - after calling
  85. * this function, memory access functions may still return -EFAULT.
  86. */
  87. #define __access_mask get_fs().seg
  88. #define __access_ok(addr, size, mask) \
  89. (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
  90. #define access_ok(type, addr, size) \
  91. likely(__access_ok((unsigned long)(addr), (size), __access_mask))
  92. /*
  93. * put_user: - Write a simple value into user space.
  94. * @x: Value to copy to user space.
  95. * @ptr: Destination address, in user space.
  96. *
  97. * Context: User context only. This function may sleep.
  98. *
  99. * This macro copies a single simple value from kernel space to user
  100. * space. It supports simple types like char and int, but not larger
  101. * data types like structures or arrays.
  102. *
  103. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  104. * to the result of dereferencing @ptr.
  105. *
  106. * Returns zero on success, or -EFAULT on error.
  107. */
  108. #define put_user(x,ptr) \
  109. __put_user_check((x), (ptr), sizeof(*(ptr)))
  110. /*
  111. * get_user: - Get a simple variable from user space.
  112. * @x: Variable to store result.
  113. * @ptr: Source address, in user space.
  114. *
  115. * Context: User context only. This function may sleep.
  116. *
  117. * This macro copies a single simple variable from user space to kernel
  118. * space. It supports simple types like char and int, but not larger
  119. * data types like structures or arrays.
  120. *
  121. * @ptr must have pointer-to-simple-variable type, and the result of
  122. * dereferencing @ptr must be assignable to @x without a cast.
  123. *
  124. * Returns zero on success, or -EFAULT on error.
  125. * On error, the variable @x is set to zero.
  126. */
  127. #define get_user(x,ptr) \
  128. __get_user_check((x), (ptr), sizeof(*(ptr)))
  129. /*
  130. * __put_user: - Write a simple value into user space, with less checking.
  131. * @x: Value to copy to user space.
  132. * @ptr: Destination address, in user space.
  133. *
  134. * Context: User context only. This function may sleep.
  135. *
  136. * This macro copies a single simple value from kernel space to user
  137. * space. It supports simple types like char and int, but not larger
  138. * data types like structures or arrays.
  139. *
  140. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  141. * to the result of dereferencing @ptr.
  142. *
  143. * Caller must check the pointer with access_ok() before calling this
  144. * function.
  145. *
  146. * Returns zero on success, or -EFAULT on error.
  147. */
  148. #define __put_user(x,ptr) \
  149. __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  150. /*
  151. * __get_user: - Get a simple variable from user space, with less checking.
  152. * @x: Variable to store result.
  153. * @ptr: Source address, in user space.
  154. *
  155. * Context: User context only. This function may sleep.
  156. *
  157. * This macro copies a single simple variable from user space to kernel
  158. * space. It supports simple types like char and int, but not larger
  159. * data types like structures or arrays.
  160. *
  161. * @ptr must have pointer-to-simple-variable type, and the result of
  162. * dereferencing @ptr must be assignable to @x without a cast.
  163. *
  164. * Caller must check the pointer with access_ok() before calling this
  165. * function.
  166. *
  167. * Returns zero on success, or -EFAULT on error.
  168. * On error, the variable @x is set to zero.
  169. */
  170. #define __get_user(x,ptr) \
  171. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  172. struct __large_struct { unsigned long buf[100]; };
  173. #define __m(x) (*(struct __large_struct __user *)(x))
  174. /*
  175. * Yuck. We need two variants, one for 64bit operation and one
  176. * for 32 bit mode and old iron.
  177. */
  178. #ifdef CONFIG_32BIT
  179. #define __GET_USER_DW(val, ptr) __get_user_asm_ll32(val, ptr)
  180. #endif
  181. #ifdef CONFIG_64BIT
  182. #define __GET_USER_DW(val, ptr) __get_user_asm(val, "ld", ptr)
  183. #endif
  184. extern void __get_user_unknown(void);
  185. #define __get_user_common(val, size, ptr) \
  186. do { \
  187. switch (size) { \
  188. case 1: __get_user_asm(val, "lb", ptr); break; \
  189. case 2: __get_user_asm(val, "lh", ptr); break; \
  190. case 4: __get_user_asm(val, "lw", ptr); break; \
  191. case 8: __GET_USER_DW(val, ptr); break; \
  192. default: __get_user_unknown(); break; \
  193. } \
  194. } while (0)
  195. #define __get_user_nocheck(x, ptr, size) \
  196. ({ \
  197. int __gu_err; \
  198. \
  199. __get_user_common((x), size, ptr); \
  200. __gu_err; \
  201. })
  202. #define __get_user_check(x, ptr, size) \
  203. ({ \
  204. int __gu_err = -EFAULT; \
  205. const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
  206. \
  207. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
  208. __get_user_common((x), size, __gu_ptr); \
  209. \
  210. __gu_err; \
  211. })
  212. #define __get_user_asm(val, insn, addr) \
  213. { \
  214. long __gu_tmp; \
  215. \
  216. __asm__ __volatile__( \
  217. "1: " insn " %1, %3 \n" \
  218. "2: \n" \
  219. " .section .fixup,\"ax\" \n" \
  220. "3: li %0, %4 \n" \
  221. " j 2b \n" \
  222. " .previous \n" \
  223. " .section __ex_table,\"a\" \n" \
  224. " "__UA_ADDR "\t1b, 3b \n" \
  225. " .previous \n" \
  226. : "=r" (__gu_err), "=r" (__gu_tmp) \
  227. : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
  228. \
  229. (val) = (__typeof__(*(addr))) __gu_tmp; \
  230. }
  231. /*
  232. * Get a long long 64 using 32 bit registers.
  233. */
  234. #define __get_user_asm_ll32(val, addr) \
  235. { \
  236. union { \
  237. unsigned long long l; \
  238. __typeof__(*(addr)) t; \
  239. } __gu_tmp; \
  240. \
  241. __asm__ __volatile__( \
  242. "1: lw %1, (%3) \n" \
  243. "2: lw %D1, 4(%3) \n" \
  244. "3: .section .fixup,\"ax\" \n" \
  245. "4: li %0, %4 \n" \
  246. " move %1, $0 \n" \
  247. " move %D1, $0 \n" \
  248. " j 3b \n" \
  249. " .previous \n" \
  250. " .section __ex_table,\"a\" \n" \
  251. " " __UA_ADDR " 1b, 4b \n" \
  252. " " __UA_ADDR " 2b, 4b \n" \
  253. " .previous \n" \
  254. : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
  255. : "0" (0), "r" (addr), "i" (-EFAULT)); \
  256. \
  257. (val) = __gu_tmp.t; \
  258. }
  259. /*
  260. * Yuck. We need two variants, one for 64bit operation and one
  261. * for 32 bit mode and old iron.
  262. */
  263. #ifdef CONFIG_32BIT
  264. #define __PUT_USER_DW(ptr) __put_user_asm_ll32(ptr)
  265. #endif
  266. #ifdef CONFIG_64BIT
  267. #define __PUT_USER_DW(ptr) __put_user_asm("sd", ptr)
  268. #endif
  269. #define __put_user_nocheck(x, ptr, size) \
  270. ({ \
  271. __typeof__(*(ptr)) __pu_val; \
  272. int __pu_err = 0; \
  273. \
  274. __pu_val = (x); \
  275. switch (size) { \
  276. case 1: __put_user_asm("sb", ptr); break; \
  277. case 2: __put_user_asm("sh", ptr); break; \
  278. case 4: __put_user_asm("sw", ptr); break; \
  279. case 8: __PUT_USER_DW(ptr); break; \
  280. default: __put_user_unknown(); break; \
  281. } \
  282. __pu_err; \
  283. })
  284. #define __put_user_check(x, ptr, size) \
  285. ({ \
  286. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  287. __typeof__(*(ptr)) __pu_val = (x); \
  288. int __pu_err = -EFAULT; \
  289. \
  290. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
  291. switch (size) { \
  292. case 1: __put_user_asm("sb", __pu_addr); break; \
  293. case 2: __put_user_asm("sh", __pu_addr); break; \
  294. case 4: __put_user_asm("sw", __pu_addr); break; \
  295. case 8: __PUT_USER_DW(__pu_addr); break; \
  296. default: __put_user_unknown(); break; \
  297. } \
  298. } \
  299. __pu_err; \
  300. })
  301. #define __put_user_asm(insn, ptr) \
  302. { \
  303. __asm__ __volatile__( \
  304. "1: " insn " %z2, %3 # __put_user_asm\n" \
  305. "2: \n" \
  306. " .section .fixup,\"ax\" \n" \
  307. "3: li %0, %4 \n" \
  308. " j 2b \n" \
  309. " .previous \n" \
  310. " .section __ex_table,\"a\" \n" \
  311. " " __UA_ADDR " 1b, 3b \n" \
  312. " .previous \n" \
  313. : "=r" (__pu_err) \
  314. : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
  315. "i" (-EFAULT)); \
  316. }
  317. #define __put_user_asm_ll32(ptr) \
  318. { \
  319. __asm__ __volatile__( \
  320. "1: sw %2, (%3) # __put_user_asm_ll32 \n" \
  321. "2: sw %D2, 4(%3) \n" \
  322. "3: \n" \
  323. " .section .fixup,\"ax\" \n" \
  324. "4: li %0, %4 \n" \
  325. " j 3b \n" \
  326. " .previous \n" \
  327. " .section __ex_table,\"a\" \n" \
  328. " " __UA_ADDR " 1b, 4b \n" \
  329. " " __UA_ADDR " 2b, 4b \n" \
  330. " .previous" \
  331. : "=r" (__pu_err) \
  332. : "0" (0), "r" (__pu_val), "r" (ptr), \
  333. "i" (-EFAULT)); \
  334. }
  335. extern void __put_user_unknown(void);
  336. /*
  337. * put_user_unaligned: - Write a simple value into user space.
  338. * @x: Value to copy to user space.
  339. * @ptr: Destination address, in user space.
  340. *
  341. * Context: User context only. This function may sleep.
  342. *
  343. * This macro copies a single simple value from kernel space to user
  344. * space. It supports simple types like char and int, but not larger
  345. * data types like structures or arrays.
  346. *
  347. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  348. * to the result of dereferencing @ptr.
  349. *
  350. * Returns zero on success, or -EFAULT on error.
  351. */
  352. #define put_user_unaligned(x,ptr) \
  353. __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
  354. /*
  355. * get_user_unaligned: - Get a simple variable from user space.
  356. * @x: Variable to store result.
  357. * @ptr: Source address, in user space.
  358. *
  359. * Context: User context only. This function may sleep.
  360. *
  361. * This macro copies a single simple variable from user space to kernel
  362. * space. It supports simple types like char and int, but not larger
  363. * data types like structures or arrays.
  364. *
  365. * @ptr must have pointer-to-simple-variable type, and the result of
  366. * dereferencing @ptr must be assignable to @x without a cast.
  367. *
  368. * Returns zero on success, or -EFAULT on error.
  369. * On error, the variable @x is set to zero.
  370. */
  371. #define get_user_unaligned(x,ptr) \
  372. __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
  373. /*
  374. * __put_user_unaligned: - Write a simple value into user space, with less checking.
  375. * @x: Value to copy to user space.
  376. * @ptr: Destination address, in user space.
  377. *
  378. * Context: User context only. This function may sleep.
  379. *
  380. * This macro copies a single simple value from kernel space to user
  381. * space. It supports simple types like char and int, but not larger
  382. * data types like structures or arrays.
  383. *
  384. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  385. * to the result of dereferencing @ptr.
  386. *
  387. * Caller must check the pointer with access_ok() before calling this
  388. * function.
  389. *
  390. * Returns zero on success, or -EFAULT on error.
  391. */
  392. #define __put_user_unaligned(x,ptr) \
  393. __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
  394. /*
  395. * __get_user_unaligned: - Get a simple variable from user space, with less checking.
  396. * @x: Variable to store result.
  397. * @ptr: Source address, in user space.
  398. *
  399. * Context: User context only. This function may sleep.
  400. *
  401. * This macro copies a single simple variable from user space to kernel
  402. * space. It supports simple types like char and int, but not larger
  403. * data types like structures or arrays.
  404. *
  405. * @ptr must have pointer-to-simple-variable type, and the result of
  406. * dereferencing @ptr must be assignable to @x without a cast.
  407. *
  408. * Caller must check the pointer with access_ok() before calling this
  409. * function.
  410. *
  411. * Returns zero on success, or -EFAULT on error.
  412. * On error, the variable @x is set to zero.
  413. */
  414. #define __get_user_unaligned(x,ptr) \
  415. __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
  416. /*
  417. * Yuck. We need two variants, one for 64bit operation and one
  418. * for 32 bit mode and old iron.
  419. */
  420. #ifdef CONFIG_32BIT
  421. #define __GET_USER_UNALIGNED_DW(val, ptr) \
  422. __get_user_unaligned_asm_ll32(val, ptr)
  423. #endif
  424. #ifdef CONFIG_64BIT
  425. #define __GET_USER_UNALIGNED_DW(val, ptr) \
  426. __get_user_unaligned_asm(val, "uld", ptr)
  427. #endif
  428. extern void __get_user_unaligned_unknown(void);
  429. #define __get_user_unaligned_common(val, size, ptr) \
  430. do { \
  431. switch (size) { \
  432. case 1: __get_user_asm(val, "lb", ptr); break; \
  433. case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
  434. case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
  435. case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
  436. default: __get_user_unaligned_unknown(); break; \
  437. } \
  438. } while (0)
  439. #define __get_user_unaligned_nocheck(x,ptr,size) \
  440. ({ \
  441. int __gu_err; \
  442. \
  443. __get_user_unaligned_common((x), size, ptr); \
  444. __gu_err; \
  445. })
  446. #define __get_user_unaligned_check(x,ptr,size) \
  447. ({ \
  448. int __gu_err = -EFAULT; \
  449. const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
  450. \
  451. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
  452. __get_user_unaligned_common((x), size, __gu_ptr); \
  453. \
  454. __gu_err; \
  455. })
  456. #define __get_user_unaligned_asm(val, insn, addr) \
  457. { \
  458. long __gu_tmp; \
  459. \
  460. __asm__ __volatile__( \
  461. "1: " insn " %1, %3 \n" \
  462. "2: \n" \
  463. " .section .fixup,\"ax\" \n" \
  464. "3: li %0, %4 \n" \
  465. " j 2b \n" \
  466. " .previous \n" \
  467. " .section __ex_table,\"a\" \n" \
  468. " "__UA_ADDR "\t1b, 3b \n" \
  469. " "__UA_ADDR "\t1b + 4, 3b \n" \
  470. " .previous \n" \
  471. : "=r" (__gu_err), "=r" (__gu_tmp) \
  472. : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
  473. \
  474. (val) = (__typeof__(*(addr))) __gu_tmp; \
  475. }
  476. /*
  477. * Get a long long 64 using 32 bit registers.
  478. */
  479. #define __get_user_unaligned_asm_ll32(val, addr) \
  480. { \
  481. unsigned long long __gu_tmp; \
  482. \
  483. __asm__ __volatile__( \
  484. "1: ulw %1, (%3) \n" \
  485. "2: ulw %D1, 4(%3) \n" \
  486. " move %0, $0 \n" \
  487. "3: .section .fixup,\"ax\" \n" \
  488. "4: li %0, %4 \n" \
  489. " move %1, $0 \n" \
  490. " move %D1, $0 \n" \
  491. " j 3b \n" \
  492. " .previous \n" \
  493. " .section __ex_table,\"a\" \n" \
  494. " " __UA_ADDR " 1b, 4b \n" \
  495. " " __UA_ADDR " 1b + 4, 4b \n" \
  496. " " __UA_ADDR " 2b, 4b \n" \
  497. " " __UA_ADDR " 2b + 4, 4b \n" \
  498. " .previous \n" \
  499. : "=r" (__gu_err), "=&r" (__gu_tmp) \
  500. : "0" (0), "r" (addr), "i" (-EFAULT)); \
  501. (val) = (__typeof__(*(addr))) __gu_tmp; \
  502. }
  503. /*
  504. * Yuck. We need two variants, one for 64bit operation and one
  505. * for 32 bit mode and old iron.
  506. */
  507. #ifdef CONFIG_32BIT
  508. #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
  509. #endif
  510. #ifdef CONFIG_64BIT
  511. #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
  512. #endif
  513. #define __put_user_unaligned_nocheck(x,ptr,size) \
  514. ({ \
  515. __typeof__(*(ptr)) __pu_val; \
  516. int __pu_err = 0; \
  517. \
  518. __pu_val = (x); \
  519. switch (size) { \
  520. case 1: __put_user_asm("sb", ptr); break; \
  521. case 2: __put_user_unaligned_asm("ush", ptr); break; \
  522. case 4: __put_user_unaligned_asm("usw", ptr); break; \
  523. case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
  524. default: __put_user_unaligned_unknown(); break; \
  525. } \
  526. __pu_err; \
  527. })
  528. #define __put_user_unaligned_check(x,ptr,size) \
  529. ({ \
  530. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  531. __typeof__(*(ptr)) __pu_val = (x); \
  532. int __pu_err = -EFAULT; \
  533. \
  534. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
  535. switch (size) { \
  536. case 1: __put_user_asm("sb", __pu_addr); break; \
  537. case 2: __put_user_unaligned_asm("ush", __pu_addr); break; \
  538. case 4: __put_user_unaligned_asm("usw", __pu_addr); break; \
  539. case 8: __PUT_USER_UNALGINED_DW(__pu_addr); break; \
  540. default: __put_user_unaligned_unknown(); break; \
  541. } \
  542. } \
  543. __pu_err; \
  544. })
  545. #define __put_user_unaligned_asm(insn, ptr) \
  546. { \
  547. __asm__ __volatile__( \
  548. "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
  549. "2: \n" \
  550. " .section .fixup,\"ax\" \n" \
  551. "3: li %0, %4 \n" \
  552. " j 2b \n" \
  553. " .previous \n" \
  554. " .section __ex_table,\"a\" \n" \
  555. " " __UA_ADDR " 1b, 3b \n" \
  556. " .previous \n" \
  557. : "=r" (__pu_err) \
  558. : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
  559. "i" (-EFAULT)); \
  560. }
  561. #define __put_user_unaligned_asm_ll32(ptr) \
  562. { \
  563. __asm__ __volatile__( \
  564. "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
  565. "2: sw %D2, 4(%3) \n" \
  566. "3: \n" \
  567. " .section .fixup,\"ax\" \n" \
  568. "4: li %0, %4 \n" \
  569. " j 3b \n" \
  570. " .previous \n" \
  571. " .section __ex_table,\"a\" \n" \
  572. " " __UA_ADDR " 1b, 4b \n" \
  573. " " __UA_ADDR " 1b + 4, 4b \n" \
  574. " " __UA_ADDR " 2b, 4b \n" \
  575. " " __UA_ADDR " 2b + 4, 4b \n" \
  576. " .previous" \
  577. : "=r" (__pu_err) \
  578. : "0" (0), "r" (__pu_val), "r" (ptr), \
  579. "i" (-EFAULT)); \
  580. }
  581. extern void __put_user_unaligned_unknown(void);
  582. /*
  583. * We're generating jump to subroutines which will be outside the range of
  584. * jump instructions
  585. */
  586. #ifdef MODULE
  587. #define __MODULE_JAL(destination) \
  588. ".set\tnoat\n\t" \
  589. __UA_LA "\t$1, " #destination "\n\t" \
  590. "jalr\t$1\n\t" \
  591. ".set\tat\n\t"
  592. #else
  593. #define __MODULE_JAL(destination) \
  594. "jal\t" #destination "\n\t"
  595. #endif
  596. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  597. #define DADDI_SCRATCH "$0"
  598. #else
  599. #define DADDI_SCRATCH "$3"
  600. #endif
  601. extern size_t __copy_user(void *__to, const void *__from, size_t __n);
  602. #define __invoke_copy_to_user(to, from, n) \
  603. ({ \
  604. register void __user *__cu_to_r __asm__("$4"); \
  605. register const void *__cu_from_r __asm__("$5"); \
  606. register long __cu_len_r __asm__("$6"); \
  607. \
  608. __cu_to_r = (to); \
  609. __cu_from_r = (from); \
  610. __cu_len_r = (n); \
  611. __asm__ __volatile__( \
  612. __MODULE_JAL(__copy_user) \
  613. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  614. : \
  615. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  616. DADDI_SCRATCH, "memory"); \
  617. __cu_len_r; \
  618. })
  619. /*
  620. * __copy_to_user: - Copy a block of data into user space, with less checking.
  621. * @to: Destination address, in user space.
  622. * @from: Source address, in kernel space.
  623. * @n: Number of bytes to copy.
  624. *
  625. * Context: User context only. This function may sleep.
  626. *
  627. * Copy data from kernel space to user space. Caller must check
  628. * the specified block with access_ok() before calling this function.
  629. *
  630. * Returns number of bytes that could not be copied.
  631. * On success, this will be zero.
  632. */
  633. #define __copy_to_user(to, from, n) \
  634. ({ \
  635. void __user *__cu_to; \
  636. const void *__cu_from; \
  637. long __cu_len; \
  638. \
  639. might_sleep(); \
  640. __cu_to = (to); \
  641. __cu_from = (from); \
  642. __cu_len = (n); \
  643. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
  644. __cu_len; \
  645. })
  646. extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
  647. #define __copy_to_user_inatomic(to, from, n) \
  648. ({ \
  649. void __user *__cu_to; \
  650. const void *__cu_from; \
  651. long __cu_len; \
  652. \
  653. __cu_to = (to); \
  654. __cu_from = (from); \
  655. __cu_len = (n); \
  656. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
  657. __cu_len; \
  658. })
  659. #define __copy_from_user_inatomic(to, from, n) \
  660. ({ \
  661. void *__cu_to; \
  662. const void __user *__cu_from; \
  663. long __cu_len; \
  664. \
  665. __cu_to = (to); \
  666. __cu_from = (from); \
  667. __cu_len = (n); \
  668. __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
  669. __cu_len); \
  670. __cu_len; \
  671. })
  672. /*
  673. * copy_to_user: - Copy a block of data into user space.
  674. * @to: Destination address, in user space.
  675. * @from: Source address, in kernel space.
  676. * @n: Number of bytes to copy.
  677. *
  678. * Context: User context only. This function may sleep.
  679. *
  680. * Copy data from kernel space to user space.
  681. *
  682. * Returns number of bytes that could not be copied.
  683. * On success, this will be zero.
  684. */
  685. #define copy_to_user(to, from, n) \
  686. ({ \
  687. void __user *__cu_to; \
  688. const void *__cu_from; \
  689. long __cu_len; \
  690. \
  691. might_sleep(); \
  692. __cu_to = (to); \
  693. __cu_from = (from); \
  694. __cu_len = (n); \
  695. if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
  696. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  697. __cu_len); \
  698. __cu_len; \
  699. })
  700. #define __invoke_copy_from_user(to, from, n) \
  701. ({ \
  702. register void *__cu_to_r __asm__("$4"); \
  703. register const void __user *__cu_from_r __asm__("$5"); \
  704. register long __cu_len_r __asm__("$6"); \
  705. \
  706. __cu_to_r = (to); \
  707. __cu_from_r = (from); \
  708. __cu_len_r = (n); \
  709. __asm__ __volatile__( \
  710. ".set\tnoreorder\n\t" \
  711. __MODULE_JAL(__copy_user) \
  712. ".set\tnoat\n\t" \
  713. __UA_ADDU "\t$1, %1, %2\n\t" \
  714. ".set\tat\n\t" \
  715. ".set\treorder" \
  716. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  717. : \
  718. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  719. DADDI_SCRATCH, "memory"); \
  720. __cu_len_r; \
  721. })
  722. #define __invoke_copy_from_user_inatomic(to, from, n) \
  723. ({ \
  724. register void *__cu_to_r __asm__("$4"); \
  725. register const void __user *__cu_from_r __asm__("$5"); \
  726. register long __cu_len_r __asm__("$6"); \
  727. \
  728. __cu_to_r = (to); \
  729. __cu_from_r = (from); \
  730. __cu_len_r = (n); \
  731. __asm__ __volatile__( \
  732. ".set\tnoreorder\n\t" \
  733. __MODULE_JAL(__copy_user_inatomic) \
  734. ".set\tnoat\n\t" \
  735. __UA_ADDU "\t$1, %1, %2\n\t" \
  736. ".set\tat\n\t" \
  737. ".set\treorder" \
  738. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  739. : \
  740. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  741. DADDI_SCRATCH, "memory"); \
  742. __cu_len_r; \
  743. })
  744. /*
  745. * __copy_from_user: - Copy a block of data from user space, with less checking.
  746. * @to: Destination address, in kernel space.
  747. * @from: Source address, in user space.
  748. * @n: Number of bytes to copy.
  749. *
  750. * Context: User context only. This function may sleep.
  751. *
  752. * Copy data from user space to kernel space. Caller must check
  753. * the specified block with access_ok() before calling this function.
  754. *
  755. * Returns number of bytes that could not be copied.
  756. * On success, this will be zero.
  757. *
  758. * If some data could not be copied, this function will pad the copied
  759. * data to the requested size using zero bytes.
  760. */
  761. #define __copy_from_user(to, from, n) \
  762. ({ \
  763. void *__cu_to; \
  764. const void __user *__cu_from; \
  765. long __cu_len; \
  766. \
  767. might_sleep(); \
  768. __cu_to = (to); \
  769. __cu_from = (from); \
  770. __cu_len = (n); \
  771. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  772. __cu_len); \
  773. __cu_len; \
  774. })
  775. /*
  776. * copy_from_user: - Copy a block of data from user space.
  777. * @to: Destination address, in kernel space.
  778. * @from: Source address, in user space.
  779. * @n: Number of bytes to copy.
  780. *
  781. * Context: User context only. This function may sleep.
  782. *
  783. * Copy data from user space to kernel space.
  784. *
  785. * Returns number of bytes that could not be copied.
  786. * On success, this will be zero.
  787. *
  788. * If some data could not be copied, this function will pad the copied
  789. * data to the requested size using zero bytes.
  790. */
  791. #define copy_from_user(to, from, n) \
  792. ({ \
  793. void *__cu_to; \
  794. const void __user *__cu_from; \
  795. long __cu_len; \
  796. \
  797. might_sleep(); \
  798. __cu_to = (to); \
  799. __cu_from = (from); \
  800. __cu_len = (n); \
  801. if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
  802. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  803. __cu_len); \
  804. __cu_len; \
  805. })
  806. #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
  807. #define copy_in_user(to, from, n) \
  808. ({ \
  809. void __user *__cu_to; \
  810. const void __user *__cu_from; \
  811. long __cu_len; \
  812. \
  813. might_sleep(); \
  814. __cu_to = (to); \
  815. __cu_from = (from); \
  816. __cu_len = (n); \
  817. if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
  818. access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
  819. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  820. __cu_len); \
  821. __cu_len; \
  822. })
  823. /*
  824. * __clear_user: - Zero a block of memory in user space, with less checking.
  825. * @to: Destination address, in user space.
  826. * @n: Number of bytes to zero.
  827. *
  828. * Zero a block of memory in user space. Caller must check
  829. * the specified block with access_ok() before calling this function.
  830. *
  831. * Returns number of bytes that could not be cleared.
  832. * On success, this will be zero.
  833. */
  834. static inline __kernel_size_t
  835. __clear_user(void __user *addr, __kernel_size_t size)
  836. {
  837. __kernel_size_t res;
  838. might_sleep();
  839. __asm__ __volatile__(
  840. "move\t$4, %1\n\t"
  841. "move\t$5, $0\n\t"
  842. "move\t$6, %2\n\t"
  843. __MODULE_JAL(__bzero)
  844. "move\t%0, $6"
  845. : "=r" (res)
  846. : "r" (addr), "r" (size)
  847. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  848. return res;
  849. }
  850. #define clear_user(addr,n) \
  851. ({ \
  852. void __user * __cl_addr = (addr); \
  853. unsigned long __cl_size = (n); \
  854. if (__cl_size && access_ok(VERIFY_WRITE, \
  855. ((unsigned long)(__cl_addr)), __cl_size)) \
  856. __cl_size = __clear_user(__cl_addr, __cl_size); \
  857. __cl_size; \
  858. })
  859. /*
  860. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  861. * @dst: Destination address, in kernel space. This buffer must be at
  862. * least @count bytes long.
  863. * @src: Source address, in user space.
  864. * @count: Maximum number of bytes to copy, including the trailing NUL.
  865. *
  866. * Copies a NUL-terminated string from userspace to kernel space.
  867. * Caller must check the specified block with access_ok() before calling
  868. * this function.
  869. *
  870. * On success, returns the length of the string (not including the trailing
  871. * NUL).
  872. *
  873. * If access to userspace fails, returns -EFAULT (some data may have been
  874. * copied).
  875. *
  876. * If @count is smaller than the length of the string, copies @count bytes
  877. * and returns @count.
  878. */
  879. static inline long
  880. __strncpy_from_user(char *__to, const char __user *__from, long __len)
  881. {
  882. long res;
  883. might_sleep();
  884. __asm__ __volatile__(
  885. "move\t$4, %1\n\t"
  886. "move\t$5, %2\n\t"
  887. "move\t$6, %3\n\t"
  888. __MODULE_JAL(__strncpy_from_user_nocheck_asm)
  889. "move\t%0, $2"
  890. : "=r" (res)
  891. : "r" (__to), "r" (__from), "r" (__len)
  892. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  893. return res;
  894. }
  895. /*
  896. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  897. * @dst: Destination address, in kernel space. This buffer must be at
  898. * least @count bytes long.
  899. * @src: Source address, in user space.
  900. * @count: Maximum number of bytes to copy, including the trailing NUL.
  901. *
  902. * Copies a NUL-terminated string from userspace to kernel space.
  903. *
  904. * On success, returns the length of the string (not including the trailing
  905. * NUL).
  906. *
  907. * If access to userspace fails, returns -EFAULT (some data may have been
  908. * copied).
  909. *
  910. * If @count is smaller than the length of the string, copies @count bytes
  911. * and returns @count.
  912. */
  913. static inline long
  914. strncpy_from_user(char *__to, const char __user *__from, long __len)
  915. {
  916. long res;
  917. might_sleep();
  918. __asm__ __volatile__(
  919. "move\t$4, %1\n\t"
  920. "move\t$5, %2\n\t"
  921. "move\t$6, %3\n\t"
  922. __MODULE_JAL(__strncpy_from_user_asm)
  923. "move\t%0, $2"
  924. : "=r" (res)
  925. : "r" (__to), "r" (__from), "r" (__len)
  926. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  927. return res;
  928. }
  929. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  930. static inline long __strlen_user(const char __user *s)
  931. {
  932. long res;
  933. might_sleep();
  934. __asm__ __volatile__(
  935. "move\t$4, %1\n\t"
  936. __MODULE_JAL(__strlen_user_nocheck_asm)
  937. "move\t%0, $2"
  938. : "=r" (res)
  939. : "r" (s)
  940. : "$2", "$4", __UA_t0, "$31");
  941. return res;
  942. }
  943. /*
  944. * strlen_user: - Get the size of a string in user space.
  945. * @str: The string to measure.
  946. *
  947. * Context: User context only. This function may sleep.
  948. *
  949. * Get the size of a NUL-terminated string in user space.
  950. *
  951. * Returns the size of the string INCLUDING the terminating NUL.
  952. * On exception, returns 0.
  953. *
  954. * If there is a limit on the length of a valid string, you may wish to
  955. * consider using strnlen_user() instead.
  956. */
  957. static inline long strlen_user(const char __user *s)
  958. {
  959. long res;
  960. might_sleep();
  961. __asm__ __volatile__(
  962. "move\t$4, %1\n\t"
  963. __MODULE_JAL(__strlen_user_asm)
  964. "move\t%0, $2"
  965. : "=r" (res)
  966. : "r" (s)
  967. : "$2", "$4", __UA_t0, "$31");
  968. return res;
  969. }
  970. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  971. static inline long __strnlen_user(const char __user *s, long n)
  972. {
  973. long res;
  974. might_sleep();
  975. __asm__ __volatile__(
  976. "move\t$4, %1\n\t"
  977. "move\t$5, %2\n\t"
  978. __MODULE_JAL(__strnlen_user_nocheck_asm)
  979. "move\t%0, $2"
  980. : "=r" (res)
  981. : "r" (s), "r" (n)
  982. : "$2", "$4", "$5", __UA_t0, "$31");
  983. return res;
  984. }
  985. /*
  986. * strlen_user: - Get the size of a string in user space.
  987. * @str: The string to measure.
  988. *
  989. * Context: User context only. This function may sleep.
  990. *
  991. * Get the size of a NUL-terminated string in user space.
  992. *
  993. * Returns the size of the string INCLUDING the terminating NUL.
  994. * On exception, returns 0.
  995. *
  996. * If there is a limit on the length of a valid string, you may wish to
  997. * consider using strnlen_user() instead.
  998. */
  999. static inline long strnlen_user(const char __user *s, long n)
  1000. {
  1001. long res;
  1002. might_sleep();
  1003. __asm__ __volatile__(
  1004. "move\t$4, %1\n\t"
  1005. "move\t$5, %2\n\t"
  1006. __MODULE_JAL(__strnlen_user_asm)
  1007. "move\t%0, $2"
  1008. : "=r" (res)
  1009. : "r" (s), "r" (n)
  1010. : "$2", "$4", "$5", __UA_t0, "$31");
  1011. return res;
  1012. }
  1013. struct exception_table_entry
  1014. {
  1015. unsigned long insn;
  1016. unsigned long nextinsn;
  1017. };
  1018. extern int fixup_exception(struct pt_regs *regs);
  1019. #endif /* _ASM_UACCESS_H */