uaccess.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845
  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/kernel.h>
  12. #include <linux/errno.h>
  13. #include <linux/thread_info.h>
  14. #include <asm-generic/uaccess.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. long __gu_err; \
  198. \
  199. __get_user_common((x), size, ptr); \
  200. __gu_err; \
  201. })
  202. #define __get_user_check(x, ptr, size) \
  203. ({ \
  204. long __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. long __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. long __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. * We're generating jump to subroutines which will be outside the range of
  338. * jump instructions
  339. */
  340. #ifdef MODULE
  341. #define __MODULE_JAL(destination) \
  342. ".set\tnoat\n\t" \
  343. __UA_LA "\t$1, " #destination "\n\t" \
  344. "jalr\t$1\n\t" \
  345. ".set\tat\n\t"
  346. #else
  347. #define __MODULE_JAL(destination) \
  348. "jal\t" #destination "\n\t"
  349. #endif
  350. extern size_t __copy_user(void *__to, const void *__from, size_t __n);
  351. #define __invoke_copy_to_user(to, from, n) \
  352. ({ \
  353. register void __user *__cu_to_r __asm__("$4"); \
  354. register const void *__cu_from_r __asm__("$5"); \
  355. register long __cu_len_r __asm__("$6"); \
  356. \
  357. __cu_to_r = (to); \
  358. __cu_from_r = (from); \
  359. __cu_len_r = (n); \
  360. __asm__ __volatile__( \
  361. __MODULE_JAL(__copy_user) \
  362. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  363. : \
  364. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  365. "memory"); \
  366. __cu_len_r; \
  367. })
  368. /*
  369. * __copy_to_user: - Copy a block of data into user space, with less checking.
  370. * @to: Destination address, in user space.
  371. * @from: Source address, in kernel space.
  372. * @n: Number of bytes to copy.
  373. *
  374. * Context: User context only. This function may sleep.
  375. *
  376. * Copy data from kernel space to user space. Caller must check
  377. * the specified block with access_ok() before calling this function.
  378. *
  379. * Returns number of bytes that could not be copied.
  380. * On success, this will be zero.
  381. */
  382. #define __copy_to_user(to, from, n) \
  383. ({ \
  384. void __user *__cu_to; \
  385. const void *__cu_from; \
  386. long __cu_len; \
  387. \
  388. might_sleep(); \
  389. __cu_to = (to); \
  390. __cu_from = (from); \
  391. __cu_len = (n); \
  392. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
  393. __cu_len; \
  394. })
  395. extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
  396. #define __copy_to_user_inatomic(to, from, n) \
  397. ({ \
  398. void __user *__cu_to; \
  399. const void *__cu_from; \
  400. long __cu_len; \
  401. \
  402. __cu_to = (to); \
  403. __cu_from = (from); \
  404. __cu_len = (n); \
  405. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, __cu_len); \
  406. __cu_len; \
  407. })
  408. #define __copy_from_user_inatomic(to, from, n) \
  409. ({ \
  410. void *__cu_to; \
  411. const void __user *__cu_from; \
  412. long __cu_len; \
  413. \
  414. __cu_to = (to); \
  415. __cu_from = (from); \
  416. __cu_len = (n); \
  417. __cu_len = __invoke_copy_from_user_inatomic(__cu_to, __cu_from, \
  418. __cu_len); \
  419. __cu_len; \
  420. })
  421. /*
  422. * copy_to_user: - Copy a block of data into user space.
  423. * @to: Destination address, in user space.
  424. * @from: Source address, in kernel space.
  425. * @n: Number of bytes to copy.
  426. *
  427. * Context: User context only. This function may sleep.
  428. *
  429. * Copy data from kernel space to user space.
  430. *
  431. * Returns number of bytes that could not be copied.
  432. * On success, this will be zero.
  433. */
  434. #define copy_to_user(to, from, n) \
  435. ({ \
  436. void __user *__cu_to; \
  437. const void *__cu_from; \
  438. long __cu_len; \
  439. \
  440. might_sleep(); \
  441. __cu_to = (to); \
  442. __cu_from = (from); \
  443. __cu_len = (n); \
  444. if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
  445. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  446. __cu_len); \
  447. __cu_len; \
  448. })
  449. #define __invoke_copy_from_user(to, from, n) \
  450. ({ \
  451. register void *__cu_to_r __asm__("$4"); \
  452. register const void __user *__cu_from_r __asm__("$5"); \
  453. register long __cu_len_r __asm__("$6"); \
  454. \
  455. __cu_to_r = (to); \
  456. __cu_from_r = (from); \
  457. __cu_len_r = (n); \
  458. __asm__ __volatile__( \
  459. ".set\tnoreorder\n\t" \
  460. __MODULE_JAL(__copy_user) \
  461. ".set\tnoat\n\t" \
  462. __UA_ADDU "\t$1, %1, %2\n\t" \
  463. ".set\tat\n\t" \
  464. ".set\treorder" \
  465. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  466. : \
  467. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  468. "memory"); \
  469. __cu_len_r; \
  470. })
  471. #define __invoke_copy_from_user_inatomic(to, from, n) \
  472. ({ \
  473. register void *__cu_to_r __asm__("$4"); \
  474. register const void __user *__cu_from_r __asm__("$5"); \
  475. register long __cu_len_r __asm__("$6"); \
  476. \
  477. __cu_to_r = (to); \
  478. __cu_from_r = (from); \
  479. __cu_len_r = (n); \
  480. __asm__ __volatile__( \
  481. ".set\tnoreorder\n\t" \
  482. __MODULE_JAL(__copy_user_inatomic) \
  483. ".set\tnoat\n\t" \
  484. __UA_ADDU "\t$1, %1, %2\n\t" \
  485. ".set\tat\n\t" \
  486. ".set\treorder" \
  487. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  488. : \
  489. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  490. "memory"); \
  491. __cu_len_r; \
  492. })
  493. /*
  494. * __copy_from_user: - Copy a block of data from user space, with less checking.
  495. * @to: Destination address, in kernel space.
  496. * @from: Source address, in user space.
  497. * @n: Number of bytes to copy.
  498. *
  499. * Context: User context only. This function may sleep.
  500. *
  501. * Copy data from user space to kernel space. Caller must check
  502. * the specified block with access_ok() before calling this function.
  503. *
  504. * Returns number of bytes that could not be copied.
  505. * On success, this will be zero.
  506. *
  507. * If some data could not be copied, this function will pad the copied
  508. * data to the requested size using zero bytes.
  509. */
  510. #define __copy_from_user(to, from, n) \
  511. ({ \
  512. void *__cu_to; \
  513. const void __user *__cu_from; \
  514. long __cu_len; \
  515. \
  516. might_sleep(); \
  517. __cu_to = (to); \
  518. __cu_from = (from); \
  519. __cu_len = (n); \
  520. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  521. __cu_len); \
  522. __cu_len; \
  523. })
  524. /*
  525. * copy_from_user: - Copy a block of data from user space.
  526. * @to: Destination address, in kernel space.
  527. * @from: Source address, in user space.
  528. * @n: Number of bytes to copy.
  529. *
  530. * Context: User context only. This function may sleep.
  531. *
  532. * Copy data from user space to kernel space.
  533. *
  534. * Returns number of bytes that could not be copied.
  535. * On success, this will be zero.
  536. *
  537. * If some data could not be copied, this function will pad the copied
  538. * data to the requested size using zero bytes.
  539. */
  540. #define copy_from_user(to, from, n) \
  541. ({ \
  542. void *__cu_to; \
  543. const void __user *__cu_from; \
  544. long __cu_len; \
  545. \
  546. might_sleep(); \
  547. __cu_to = (to); \
  548. __cu_from = (from); \
  549. __cu_len = (n); \
  550. if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
  551. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  552. __cu_len); \
  553. __cu_len; \
  554. })
  555. #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
  556. #define copy_in_user(to, from, n) \
  557. ({ \
  558. void __user *__cu_to; \
  559. const void __user *__cu_from; \
  560. long __cu_len; \
  561. \
  562. might_sleep(); \
  563. __cu_to = (to); \
  564. __cu_from = (from); \
  565. __cu_len = (n); \
  566. if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
  567. access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
  568. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  569. __cu_len); \
  570. __cu_len; \
  571. })
  572. /*
  573. * __clear_user: - Zero a block of memory in user space, with less checking.
  574. * @to: Destination address, in user space.
  575. * @n: Number of bytes to zero.
  576. *
  577. * Zero a block of memory in user space. Caller must check
  578. * the specified block with access_ok() before calling this function.
  579. *
  580. * Returns number of bytes that could not be cleared.
  581. * On success, this will be zero.
  582. */
  583. static inline __kernel_size_t
  584. __clear_user(void __user *addr, __kernel_size_t size)
  585. {
  586. __kernel_size_t res;
  587. might_sleep();
  588. __asm__ __volatile__(
  589. "move\t$4, %1\n\t"
  590. "move\t$5, $0\n\t"
  591. "move\t$6, %2\n\t"
  592. __MODULE_JAL(__bzero)
  593. "move\t%0, $6"
  594. : "=r" (res)
  595. : "r" (addr), "r" (size)
  596. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  597. return res;
  598. }
  599. #define clear_user(addr,n) \
  600. ({ \
  601. void __user * __cl_addr = (addr); \
  602. unsigned long __cl_size = (n); \
  603. if (__cl_size && access_ok(VERIFY_WRITE, \
  604. ((unsigned long)(__cl_addr)), __cl_size)) \
  605. __cl_size = __clear_user(__cl_addr, __cl_size); \
  606. __cl_size; \
  607. })
  608. /*
  609. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  610. * @dst: Destination address, in kernel space. This buffer must be at
  611. * least @count bytes long.
  612. * @src: Source address, in user space.
  613. * @count: Maximum number of bytes to copy, including the trailing NUL.
  614. *
  615. * Copies a NUL-terminated string from userspace to kernel space.
  616. * Caller must check the specified block with access_ok() before calling
  617. * this function.
  618. *
  619. * On success, returns the length of the string (not including the trailing
  620. * NUL).
  621. *
  622. * If access to userspace fails, returns -EFAULT (some data may have been
  623. * copied).
  624. *
  625. * If @count is smaller than the length of the string, copies @count bytes
  626. * and returns @count.
  627. */
  628. static inline long
  629. __strncpy_from_user(char *__to, const char __user *__from, long __len)
  630. {
  631. long res;
  632. might_sleep();
  633. __asm__ __volatile__(
  634. "move\t$4, %1\n\t"
  635. "move\t$5, %2\n\t"
  636. "move\t$6, %3\n\t"
  637. __MODULE_JAL(__strncpy_from_user_nocheck_asm)
  638. "move\t%0, $2"
  639. : "=r" (res)
  640. : "r" (__to), "r" (__from), "r" (__len)
  641. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  642. return res;
  643. }
  644. /*
  645. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  646. * @dst: Destination address, in kernel space. This buffer must be at
  647. * least @count bytes long.
  648. * @src: Source address, in user space.
  649. * @count: Maximum number of bytes to copy, including the trailing NUL.
  650. *
  651. * Copies a NUL-terminated string from userspace to kernel space.
  652. *
  653. * On success, returns the length of the string (not including the trailing
  654. * NUL).
  655. *
  656. * If access to userspace fails, returns -EFAULT (some data may have been
  657. * copied).
  658. *
  659. * If @count is smaller than the length of the string, copies @count bytes
  660. * and returns @count.
  661. */
  662. static inline long
  663. strncpy_from_user(char *__to, const char __user *__from, long __len)
  664. {
  665. long res;
  666. might_sleep();
  667. __asm__ __volatile__(
  668. "move\t$4, %1\n\t"
  669. "move\t$5, %2\n\t"
  670. "move\t$6, %3\n\t"
  671. __MODULE_JAL(__strncpy_from_user_asm)
  672. "move\t%0, $2"
  673. : "=r" (res)
  674. : "r" (__to), "r" (__from), "r" (__len)
  675. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  676. return res;
  677. }
  678. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  679. static inline long __strlen_user(const char __user *s)
  680. {
  681. long res;
  682. might_sleep();
  683. __asm__ __volatile__(
  684. "move\t$4, %1\n\t"
  685. __MODULE_JAL(__strlen_user_nocheck_asm)
  686. "move\t%0, $2"
  687. : "=r" (res)
  688. : "r" (s)
  689. : "$2", "$4", __UA_t0, "$31");
  690. return res;
  691. }
  692. /*
  693. * strlen_user: - Get the size of a string in user space.
  694. * @str: The string to measure.
  695. *
  696. * Context: User context only. This function may sleep.
  697. *
  698. * Get the size of a NUL-terminated string in user space.
  699. *
  700. * Returns the size of the string INCLUDING the terminating NUL.
  701. * On exception, returns 0.
  702. *
  703. * If there is a limit on the length of a valid string, you may wish to
  704. * consider using strnlen_user() instead.
  705. */
  706. static inline long strlen_user(const char __user *s)
  707. {
  708. long res;
  709. might_sleep();
  710. __asm__ __volatile__(
  711. "move\t$4, %1\n\t"
  712. __MODULE_JAL(__strlen_user_asm)
  713. "move\t%0, $2"
  714. : "=r" (res)
  715. : "r" (s)
  716. : "$2", "$4", __UA_t0, "$31");
  717. return res;
  718. }
  719. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  720. static inline long __strnlen_user(const char __user *s, long n)
  721. {
  722. long res;
  723. might_sleep();
  724. __asm__ __volatile__(
  725. "move\t$4, %1\n\t"
  726. "move\t$5, %2\n\t"
  727. __MODULE_JAL(__strnlen_user_nocheck_asm)
  728. "move\t%0, $2"
  729. : "=r" (res)
  730. : "r" (s), "r" (n)
  731. : "$2", "$4", "$5", __UA_t0, "$31");
  732. return res;
  733. }
  734. /*
  735. * strlen_user: - Get the size of a string in user space.
  736. * @str: The string to measure.
  737. *
  738. * Context: User context only. This function may sleep.
  739. *
  740. * Get the size of a NUL-terminated string in user space.
  741. *
  742. * Returns the size of the string INCLUDING the terminating NUL.
  743. * On exception, returns 0.
  744. *
  745. * If there is a limit on the length of a valid string, you may wish to
  746. * consider using strnlen_user() instead.
  747. */
  748. static inline long strnlen_user(const char __user *s, long n)
  749. {
  750. long res;
  751. might_sleep();
  752. __asm__ __volatile__(
  753. "move\t$4, %1\n\t"
  754. "move\t$5, %2\n\t"
  755. __MODULE_JAL(__strnlen_user_asm)
  756. "move\t%0, $2"
  757. : "=r" (res)
  758. : "r" (s), "r" (n)
  759. : "$2", "$4", "$5", __UA_t0, "$31");
  760. return res;
  761. }
  762. struct exception_table_entry
  763. {
  764. unsigned long insn;
  765. unsigned long nextinsn;
  766. };
  767. extern int fixup_exception(struct pt_regs *regs);
  768. #endif /* _ASM_UACCESS_H */