uaccess.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  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. */
  9. #ifndef _ASM_UACCESS_H
  10. #define _ASM_UACCESS_H
  11. #include <linux/config.h>
  12. #include <linux/kernel.h>
  13. #include <linux/errno.h>
  14. #include <linux/thread_info.h>
  15. #include <asm-generic/uaccess.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. #ifdef CONFIG_MIPS32
  24. #define __UA_LIMIT 0x80000000UL
  25. #define __UA_ADDR ".word"
  26. #define __UA_LA "la"
  27. #define __UA_ADDU "addu"
  28. #define __UA_t0 "$8"
  29. #define __UA_t1 "$9"
  30. #endif /* CONFIG_MIPS32 */
  31. #ifdef CONFIG_MIPS64
  32. #define __UA_LIMIT (- TASK_SIZE)
  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_MIPS64 */
  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. (((signed long)((mask) & ((addr) | ((addr) + (size)) | __ua_size(size)))) == 0)
  91. #define access_ok(type, addr, size) \
  92. likely(__access_ok((unsigned long)(addr), (size),__access_mask))
  93. /*
  94. * verify_area: - Obsolete/deprecated and will go away soon,
  95. * use access_ok() instead.
  96. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE
  97. * @addr: User space pointer to start of block to check
  98. * @size: Size of block to check
  99. *
  100. * Context: User context only. This function may sleep.
  101. *
  102. * This function has been replaced by access_ok().
  103. *
  104. * Checks if a pointer to a block of memory in user space is valid.
  105. *
  106. * Returns zero if the memory block may be valid, -EFAULT
  107. * if it is definitely invalid.
  108. *
  109. * See access_ok() for more details.
  110. */
  111. static inline int __deprecated verify_area(int type, const void * addr, unsigned long size)
  112. {
  113. return access_ok(type, addr, size) ? 0 : -EFAULT;
  114. }
  115. /*
  116. * put_user: - Write a simple value into user space.
  117. * @x: Value to copy to user space.
  118. * @ptr: Destination address, in user space.
  119. *
  120. * Context: User context only. This function may sleep.
  121. *
  122. * This macro copies a single simple value from kernel space to user
  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 @x must be assignable
  127. * to the result of dereferencing @ptr.
  128. *
  129. * Returns zero on success, or -EFAULT on error.
  130. */
  131. #define put_user(x,ptr) \
  132. __put_user_check((x),(ptr),sizeof(*(ptr)))
  133. /*
  134. * get_user: - Get a simple variable from user space.
  135. * @x: Variable to store result.
  136. * @ptr: Source address, in user space.
  137. *
  138. * Context: User context only. This function may sleep.
  139. *
  140. * This macro copies a single simple variable from user space to kernel
  141. * space. It supports simple types like char and int, but not larger
  142. * data types like structures or arrays.
  143. *
  144. * @ptr must have pointer-to-simple-variable type, and the result of
  145. * dereferencing @ptr must be assignable to @x without a cast.
  146. *
  147. * Returns zero on success, or -EFAULT on error.
  148. * On error, the variable @x is set to zero.
  149. */
  150. #define get_user(x,ptr) \
  151. __get_user_check((x),(ptr),sizeof(*(ptr)))
  152. /*
  153. * __put_user: - Write a simple value into user space, with less checking.
  154. * @x: Value to copy to user space.
  155. * @ptr: Destination address, in user space.
  156. *
  157. * Context: User context only. This function may sleep.
  158. *
  159. * This macro copies a single simple value from kernel space to user
  160. * space. It supports simple types like char and int, but not larger
  161. * data types like structures or arrays.
  162. *
  163. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  164. * to the result of dereferencing @ptr.
  165. *
  166. * Caller must check the pointer with access_ok() before calling this
  167. * function.
  168. *
  169. * Returns zero on success, or -EFAULT on error.
  170. */
  171. #define __put_user(x,ptr) \
  172. __put_user_nocheck((x),(ptr),sizeof(*(ptr)))
  173. /*
  174. * __get_user: - Get a simple variable from user space, with less checking.
  175. * @x: Variable to store result.
  176. * @ptr: Source address, in user space.
  177. *
  178. * Context: User context only. This function may sleep.
  179. *
  180. * This macro copies a single simple variable from user space to kernel
  181. * space. It supports simple types like char and int, but not larger
  182. * data types like structures or arrays.
  183. *
  184. * @ptr must have pointer-to-simple-variable type, and the result of
  185. * dereferencing @ptr must be assignable to @x without a cast.
  186. *
  187. * Caller must check the pointer with access_ok() before calling this
  188. * function.
  189. *
  190. * Returns zero on success, or -EFAULT on error.
  191. * On error, the variable @x is set to zero.
  192. */
  193. #define __get_user(x,ptr) \
  194. __get_user_nocheck((x),(ptr),sizeof(*(ptr)))
  195. struct __large_struct { unsigned long buf[100]; };
  196. #define __m(x) (*(struct __large_struct *)(x))
  197. /*
  198. * Yuck. We need two variants, one for 64bit operation and one
  199. * for 32 bit mode and old iron.
  200. */
  201. #ifdef __mips64
  202. #define __GET_USER_DW(__gu_err) __get_user_asm("ld", __gu_err)
  203. #else
  204. #define __GET_USER_DW(__gu_err) __get_user_asm_ll32(__gu_err)
  205. #endif
  206. #define __get_user_nocheck(x,ptr,size) \
  207. ({ \
  208. __typeof(*(ptr)) __gu_val = 0; \
  209. long __gu_addr; \
  210. long __gu_err = 0; \
  211. \
  212. might_sleep(); \
  213. __gu_addr = (long) (ptr); \
  214. switch (size) { \
  215. case 1: __get_user_asm("lb", __gu_err); break; \
  216. case 2: __get_user_asm("lh", __gu_err); break; \
  217. case 4: __get_user_asm("lw", __gu_err); break; \
  218. case 8: __GET_USER_DW(__gu_err); break; \
  219. default: __get_user_unknown(); break; \
  220. } \
  221. x = (__typeof__(*(ptr))) __gu_val; \
  222. __gu_err; \
  223. })
  224. #define __get_user_check(x,ptr,size) \
  225. ({ \
  226. __typeof__(*(ptr)) __gu_val = 0; \
  227. long __gu_addr; \
  228. long __gu_err; \
  229. \
  230. might_sleep(); \
  231. __gu_addr = (long) (ptr); \
  232. __gu_err = access_ok(VERIFY_READ, (void *) __gu_addr, size) \
  233. ? 0 : -EFAULT; \
  234. \
  235. if (likely(!__gu_err)) { \
  236. switch (size) { \
  237. case 1: __get_user_asm("lb", __gu_err); break; \
  238. case 2: __get_user_asm("lh", __gu_err); break; \
  239. case 4: __get_user_asm("lw", __gu_err); break; \
  240. case 8: __GET_USER_DW(__gu_err); break; \
  241. default: __get_user_unknown(); break; \
  242. } \
  243. } \
  244. x = (__typeof__(*(ptr))) __gu_val; \
  245. __gu_err; \
  246. })
  247. #define __get_user_asm(insn,__gu_err) \
  248. ({ \
  249. __asm__ __volatile__( \
  250. "1: " insn " %1, %3 \n" \
  251. "2: \n" \
  252. " .section .fixup,\"ax\" \n" \
  253. "3: li %0, %4 \n" \
  254. " j 2b \n" \
  255. " .previous \n" \
  256. " .section __ex_table,\"a\" \n" \
  257. " "__UA_ADDR "\t1b, 3b \n" \
  258. " .previous \n" \
  259. : "=r" (__gu_err), "=r" (__gu_val) \
  260. : "0" (__gu_err), "o" (__m(__gu_addr)), "i" (-EFAULT)); \
  261. })
  262. /*
  263. * Get a long long 64 using 32 bit registers.
  264. */
  265. #define __get_user_asm_ll32(__gu_err) \
  266. ({ \
  267. __asm__ __volatile__( \
  268. "1: lw %1, %3 \n" \
  269. "2: lw %D1, %4 \n" \
  270. " move %0, $0 \n" \
  271. "3: .section .fixup,\"ax\" \n" \
  272. "4: li %0, %5 \n" \
  273. " move %1, $0 \n" \
  274. " move %D1, $0 \n" \
  275. " j 3b \n" \
  276. " .previous \n" \
  277. " .section __ex_table,\"a\" \n" \
  278. " " __UA_ADDR " 1b, 4b \n" \
  279. " " __UA_ADDR " 2b, 4b \n" \
  280. " .previous \n" \
  281. : "=r" (__gu_err), "=&r" (__gu_val) \
  282. : "0" (__gu_err), "o" (__m(__gu_addr)), \
  283. "o" (__m(__gu_addr + 4)), "i" (-EFAULT)); \
  284. })
  285. extern void __get_user_unknown(void);
  286. /*
  287. * Yuck. We need two variants, one for 64bit operation and one
  288. * for 32 bit mode and old iron.
  289. */
  290. #ifdef __mips64
  291. #define __PUT_USER_DW(__pu_val) __put_user_asm("sd", __pu_val)
  292. #else
  293. #define __PUT_USER_DW(__pu_val) __put_user_asm_ll32(__pu_val)
  294. #endif
  295. #define __put_user_nocheck(x,ptr,size) \
  296. ({ \
  297. __typeof__(*(ptr)) __pu_val; \
  298. long __pu_addr; \
  299. long __pu_err = 0; \
  300. \
  301. might_sleep(); \
  302. __pu_val = (x); \
  303. __pu_addr = (long) (ptr); \
  304. switch (size) { \
  305. case 1: __put_user_asm("sb", __pu_val); break; \
  306. case 2: __put_user_asm("sh", __pu_val); break; \
  307. case 4: __put_user_asm("sw", __pu_val); break; \
  308. case 8: __PUT_USER_DW(__pu_val); break; \
  309. default: __put_user_unknown(); break; \
  310. } \
  311. __pu_err; \
  312. })
  313. #define __put_user_check(x,ptr,size) \
  314. ({ \
  315. __typeof__(*(ptr)) __pu_val; \
  316. long __pu_addr; \
  317. long __pu_err; \
  318. \
  319. might_sleep(); \
  320. __pu_val = (x); \
  321. __pu_addr = (long) (ptr); \
  322. __pu_err = access_ok(VERIFY_WRITE, (void *) __pu_addr, size) \
  323. ? 0 : -EFAULT; \
  324. \
  325. if (likely(!__pu_err)) { \
  326. switch (size) { \
  327. case 1: __put_user_asm("sb", __pu_val); break; \
  328. case 2: __put_user_asm("sh", __pu_val); break; \
  329. case 4: __put_user_asm("sw", __pu_val); break; \
  330. case 8: __PUT_USER_DW(__pu_val); break; \
  331. default: __put_user_unknown(); break; \
  332. } \
  333. } \
  334. __pu_err; \
  335. })
  336. #define __put_user_asm(insn, __pu_val) \
  337. ({ \
  338. __asm__ __volatile__( \
  339. "1: " insn " %z2, %3 # __put_user_asm\n" \
  340. "2: \n" \
  341. " .section .fixup,\"ax\" \n" \
  342. "3: li %0, %4 \n" \
  343. " j 2b \n" \
  344. " .previous \n" \
  345. " .section __ex_table,\"a\" \n" \
  346. " " __UA_ADDR " 1b, 3b \n" \
  347. " .previous \n" \
  348. : "=r" (__pu_err) \
  349. : "0" (__pu_err), "Jr" (__pu_val), "o" (__m(__pu_addr)), \
  350. "i" (-EFAULT)); \
  351. })
  352. #define __put_user_asm_ll32(__pu_val) \
  353. ({ \
  354. __asm__ __volatile__( \
  355. "1: sw %2, %3 # __put_user_asm_ll32 \n" \
  356. "2: sw %D2, %4 \n" \
  357. "3: \n" \
  358. " .section .fixup,\"ax\" \n" \
  359. "4: li %0, %5 \n" \
  360. " j 3b \n" \
  361. " .previous \n" \
  362. " .section __ex_table,\"a\" \n" \
  363. " " __UA_ADDR " 1b, 4b \n" \
  364. " " __UA_ADDR " 2b, 4b \n" \
  365. " .previous" \
  366. : "=r" (__pu_err) \
  367. : "0" (__pu_err), "r" (__pu_val), "o" (__m(__pu_addr)), \
  368. "o" (__m(__pu_addr + 4)), "i" (-EFAULT)); \
  369. })
  370. extern void __put_user_unknown(void);
  371. /*
  372. * We're generating jump to subroutines which will be outside the range of
  373. * jump instructions
  374. */
  375. #ifdef MODULE
  376. #define __MODULE_JAL(destination) \
  377. ".set\tnoat\n\t" \
  378. __UA_LA "\t$1, " #destination "\n\t" \
  379. "jalr\t$1\n\t" \
  380. ".set\tat\n\t"
  381. #else
  382. #define __MODULE_JAL(destination) \
  383. "jal\t" #destination "\n\t"
  384. #endif
  385. extern size_t __copy_user(void *__to, const void *__from, size_t __n);
  386. #define __invoke_copy_to_user(to,from,n) \
  387. ({ \
  388. register void *__cu_to_r __asm__ ("$4"); \
  389. register const void *__cu_from_r __asm__ ("$5"); \
  390. register long __cu_len_r __asm__ ("$6"); \
  391. \
  392. __cu_to_r = (to); \
  393. __cu_from_r = (from); \
  394. __cu_len_r = (n); \
  395. __asm__ __volatile__( \
  396. __MODULE_JAL(__copy_user) \
  397. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  398. : \
  399. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  400. "memory"); \
  401. __cu_len_r; \
  402. })
  403. /*
  404. * __copy_to_user: - Copy a block of data into user space, with less checking.
  405. * @to: Destination address, in user space.
  406. * @from: Source address, in kernel space.
  407. * @n: Number of bytes to copy.
  408. *
  409. * Context: User context only. This function may sleep.
  410. *
  411. * Copy data from kernel space to user space. Caller must check
  412. * the specified block with access_ok() before calling this function.
  413. *
  414. * Returns number of bytes that could not be copied.
  415. * On success, this will be zero.
  416. */
  417. #define __copy_to_user(to,from,n) \
  418. ({ \
  419. void *__cu_to; \
  420. const void *__cu_from; \
  421. long __cu_len; \
  422. \
  423. might_sleep(); \
  424. __cu_to = (to); \
  425. __cu_from = (from); \
  426. __cu_len = (n); \
  427. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
  428. __cu_len; \
  429. })
  430. #define __copy_to_user_inatomic __copy_to_user
  431. #define __copy_from_user_inatomic __copy_from_user
  432. /*
  433. * copy_to_user: - Copy a block of data into user space.
  434. * @to: Destination address, in user space.
  435. * @from: Source address, in kernel space.
  436. * @n: Number of bytes to copy.
  437. *
  438. * Context: User context only. This function may sleep.
  439. *
  440. * Copy data from kernel space to user space.
  441. *
  442. * Returns number of bytes that could not be copied.
  443. * On success, this will be zero.
  444. */
  445. #define copy_to_user(to,from,n) \
  446. ({ \
  447. void *__cu_to; \
  448. const void *__cu_from; \
  449. long __cu_len; \
  450. \
  451. might_sleep(); \
  452. __cu_to = (to); \
  453. __cu_from = (from); \
  454. __cu_len = (n); \
  455. if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
  456. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  457. __cu_len); \
  458. __cu_len; \
  459. })
  460. #define __invoke_copy_from_user(to,from,n) \
  461. ({ \
  462. register void *__cu_to_r __asm__ ("$4"); \
  463. register const void *__cu_from_r __asm__ ("$5"); \
  464. register long __cu_len_r __asm__ ("$6"); \
  465. \
  466. __cu_to_r = (to); \
  467. __cu_from_r = (from); \
  468. __cu_len_r = (n); \
  469. __asm__ __volatile__( \
  470. ".set\tnoreorder\n\t" \
  471. __MODULE_JAL(__copy_user) \
  472. ".set\tnoat\n\t" \
  473. __UA_ADDU "\t$1, %1, %2\n\t" \
  474. ".set\tat\n\t" \
  475. ".set\treorder" \
  476. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  477. : \
  478. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  479. "memory"); \
  480. __cu_len_r; \
  481. })
  482. /*
  483. * __copy_from_user: - Copy a block of data from user space, with less checking. * @to: Destination address, in kernel space.
  484. * @from: Source address, in user space.
  485. * @n: Number of bytes to copy.
  486. *
  487. * Context: User context only. This function may sleep.
  488. *
  489. * Copy data from user space to kernel space. Caller must check
  490. * the specified block with access_ok() before calling this function.
  491. *
  492. * Returns number of bytes that could not be copied.
  493. * On success, this will be zero.
  494. *
  495. * If some data could not be copied, this function will pad the copied
  496. * data to the requested size using zero bytes.
  497. */
  498. #define __copy_from_user(to,from,n) \
  499. ({ \
  500. void *__cu_to; \
  501. const void *__cu_from; \
  502. long __cu_len; \
  503. \
  504. might_sleep(); \
  505. __cu_to = (to); \
  506. __cu_from = (from); \
  507. __cu_len = (n); \
  508. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  509. __cu_len); \
  510. __cu_len; \
  511. })
  512. /*
  513. * copy_from_user: - Copy a block of data from user space.
  514. * @to: Destination address, in kernel space.
  515. * @from: Source address, in user space.
  516. * @n: Number of bytes to copy.
  517. *
  518. * Context: User context only. This function may sleep.
  519. *
  520. * Copy data from user space to kernel space.
  521. *
  522. * Returns number of bytes that could not be copied.
  523. * On success, this will be zero.
  524. *
  525. * If some data could not be copied, this function will pad the copied
  526. * data to the requested size using zero bytes.
  527. */
  528. #define copy_from_user(to,from,n) \
  529. ({ \
  530. void *__cu_to; \
  531. const void *__cu_from; \
  532. long __cu_len; \
  533. \
  534. might_sleep(); \
  535. __cu_to = (to); \
  536. __cu_from = (from); \
  537. __cu_len = (n); \
  538. if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
  539. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  540. __cu_len); \
  541. __cu_len; \
  542. })
  543. #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
  544. #define copy_in_user(to,from,n) \
  545. ({ \
  546. void *__cu_to; \
  547. const void *__cu_from; \
  548. long __cu_len; \
  549. \
  550. might_sleep(); \
  551. __cu_to = (to); \
  552. __cu_from = (from); \
  553. __cu_len = (n); \
  554. if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
  555. access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
  556. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  557. __cu_len); \
  558. __cu_len; \
  559. })
  560. /*
  561. * __clear_user: - Zero a block of memory in user space, with less checking.
  562. * @to: Destination address, in user space.
  563. * @n: Number of bytes to zero.
  564. *
  565. * Zero a block of memory in user space. Caller must check
  566. * the specified block with access_ok() before calling this function.
  567. *
  568. * Returns number of bytes that could not be cleared.
  569. * On success, this will be zero.
  570. */
  571. static inline __kernel_size_t
  572. __clear_user(void *addr, __kernel_size_t size)
  573. {
  574. __kernel_size_t res;
  575. might_sleep();
  576. __asm__ __volatile__(
  577. "move\t$4, %1\n\t"
  578. "move\t$5, $0\n\t"
  579. "move\t$6, %2\n\t"
  580. __MODULE_JAL(__bzero)
  581. "move\t%0, $6"
  582. : "=r" (res)
  583. : "r" (addr), "r" (size)
  584. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  585. return res;
  586. }
  587. #define clear_user(addr,n) \
  588. ({ \
  589. void * __cl_addr = (addr); \
  590. unsigned long __cl_size = (n); \
  591. if (__cl_size && access_ok(VERIFY_WRITE, \
  592. ((unsigned long)(__cl_addr)), __cl_size)) \
  593. __cl_size = __clear_user(__cl_addr, __cl_size); \
  594. __cl_size; \
  595. })
  596. /*
  597. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  598. * @dst: Destination address, in kernel space. This buffer must be at
  599. * least @count bytes long.
  600. * @src: Source address, in user space.
  601. * @count: Maximum number of bytes to copy, including the trailing NUL.
  602. *
  603. * Copies a NUL-terminated string from userspace to kernel space.
  604. * Caller must check the specified block with access_ok() before calling
  605. * this function.
  606. *
  607. * On success, returns the length of the string (not including the trailing
  608. * NUL).
  609. *
  610. * If access to userspace fails, returns -EFAULT (some data may have been
  611. * copied).
  612. *
  613. * If @count is smaller than the length of the string, copies @count bytes
  614. * and returns @count.
  615. */
  616. static inline long
  617. __strncpy_from_user(char *__to, const char *__from, long __len)
  618. {
  619. long res;
  620. might_sleep();
  621. __asm__ __volatile__(
  622. "move\t$4, %1\n\t"
  623. "move\t$5, %2\n\t"
  624. "move\t$6, %3\n\t"
  625. __MODULE_JAL(__strncpy_from_user_nocheck_asm)
  626. "move\t%0, $2"
  627. : "=r" (res)
  628. : "r" (__to), "r" (__from), "r" (__len)
  629. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  630. return res;
  631. }
  632. /*
  633. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  634. * @dst: Destination address, in kernel space. This buffer must be at
  635. * least @count bytes long.
  636. * @src: Source address, in user space.
  637. * @count: Maximum number of bytes to copy, including the trailing NUL.
  638. *
  639. * Copies a NUL-terminated string from userspace to kernel space.
  640. *
  641. * On success, returns the length of the string (not including the trailing
  642. * NUL).
  643. *
  644. * If access to userspace fails, returns -EFAULT (some data may have been
  645. * copied).
  646. *
  647. * If @count is smaller than the length of the string, copies @count bytes
  648. * and returns @count.
  649. */
  650. static inline long
  651. strncpy_from_user(char *__to, const char *__from, long __len)
  652. {
  653. long res;
  654. might_sleep();
  655. __asm__ __volatile__(
  656. "move\t$4, %1\n\t"
  657. "move\t$5, %2\n\t"
  658. "move\t$6, %3\n\t"
  659. __MODULE_JAL(__strncpy_from_user_asm)
  660. "move\t%0, $2"
  661. : "=r" (res)
  662. : "r" (__to), "r" (__from), "r" (__len)
  663. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  664. return res;
  665. }
  666. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  667. static inline long __strlen_user(const char *s)
  668. {
  669. long res;
  670. might_sleep();
  671. __asm__ __volatile__(
  672. "move\t$4, %1\n\t"
  673. __MODULE_JAL(__strlen_user_nocheck_asm)
  674. "move\t%0, $2"
  675. : "=r" (res)
  676. : "r" (s)
  677. : "$2", "$4", __UA_t0, "$31");
  678. return res;
  679. }
  680. /*
  681. * strlen_user: - Get the size of a string in user space.
  682. * @str: The string to measure.
  683. *
  684. * Context: User context only. This function may sleep.
  685. *
  686. * Get the size of a NUL-terminated string in user space.
  687. *
  688. * Returns the size of the string INCLUDING the terminating NUL.
  689. * On exception, returns 0.
  690. *
  691. * If there is a limit on the length of a valid string, you may wish to
  692. * consider using strnlen_user() instead.
  693. */
  694. static inline long strlen_user(const char *s)
  695. {
  696. long res;
  697. might_sleep();
  698. __asm__ __volatile__(
  699. "move\t$4, %1\n\t"
  700. __MODULE_JAL(__strlen_user_asm)
  701. "move\t%0, $2"
  702. : "=r" (res)
  703. : "r" (s)
  704. : "$2", "$4", __UA_t0, "$31");
  705. return res;
  706. }
  707. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  708. static inline long __strnlen_user(const char *s, long n)
  709. {
  710. long res;
  711. might_sleep();
  712. __asm__ __volatile__(
  713. "move\t$4, %1\n\t"
  714. "move\t$5, %2\n\t"
  715. __MODULE_JAL(__strnlen_user_nocheck_asm)
  716. "move\t%0, $2"
  717. : "=r" (res)
  718. : "r" (s), "r" (n)
  719. : "$2", "$4", "$5", __UA_t0, "$31");
  720. return res;
  721. }
  722. /*
  723. * strlen_user: - Get the size of a string in user space.
  724. * @str: The string to measure.
  725. *
  726. * Context: User context only. This function may sleep.
  727. *
  728. * Get the size of a NUL-terminated string in user space.
  729. *
  730. * Returns the size of the string INCLUDING the terminating NUL.
  731. * On exception, returns 0.
  732. *
  733. * If there is a limit on the length of a valid string, you may wish to
  734. * consider using strnlen_user() instead.
  735. */
  736. static inline long strnlen_user(const char *s, long n)
  737. {
  738. long res;
  739. might_sleep();
  740. __asm__ __volatile__(
  741. "move\t$4, %1\n\t"
  742. "move\t$5, %2\n\t"
  743. __MODULE_JAL(__strnlen_user_asm)
  744. "move\t%0, $2"
  745. : "=r" (res)
  746. : "r" (s), "r" (n)
  747. : "$2", "$4", "$5", __UA_t0, "$31");
  748. return res;
  749. }
  750. struct exception_table_entry
  751. {
  752. unsigned long insn;
  753. unsigned long nextinsn;
  754. };
  755. extern int fixup_exception(struct pt_regs *regs);
  756. #endif /* _ASM_UACCESS_H */