uaccess.h 24 KB

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