uaccess.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  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_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 *)(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 __mips64
  180. #define __GET_USER_DW(__gu_err) __get_user_asm("ld", __gu_err)
  181. #else
  182. #define __GET_USER_DW(__gu_err) __get_user_asm_ll32(__gu_err)
  183. #endif
  184. #define __get_user_nocheck(x,ptr,size) \
  185. ({ \
  186. __typeof(*(ptr)) __gu_val = 0; \
  187. long __gu_addr; \
  188. long __gu_err = 0; \
  189. \
  190. might_sleep(); \
  191. __gu_addr = (long) (ptr); \
  192. switch (size) { \
  193. case 1: __get_user_asm("lb", __gu_err); break; \
  194. case 2: __get_user_asm("lh", __gu_err); break; \
  195. case 4: __get_user_asm("lw", __gu_err); break; \
  196. case 8: __GET_USER_DW(__gu_err); break; \
  197. default: __get_user_unknown(); break; \
  198. } \
  199. x = (__typeof__(*(ptr))) __gu_val; \
  200. __gu_err; \
  201. })
  202. #define __get_user_check(x,ptr,size) \
  203. ({ \
  204. __typeof__(*(ptr)) __gu_val = 0; \
  205. long __gu_addr; \
  206. long __gu_err; \
  207. \
  208. might_sleep(); \
  209. __gu_addr = (long) (ptr); \
  210. __gu_err = access_ok(VERIFY_READ, (void *) __gu_addr, size) \
  211. ? 0 : -EFAULT; \
  212. \
  213. if (likely(!__gu_err)) { \
  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. } \
  222. x = (__typeof__(*(ptr))) __gu_val; \
  223. __gu_err; \
  224. })
  225. #define __get_user_asm(insn,__gu_err) \
  226. ({ \
  227. __asm__ __volatile__( \
  228. "1: " insn " %1, %3 \n" \
  229. "2: \n" \
  230. " .section .fixup,\"ax\" \n" \
  231. "3: li %0, %4 \n" \
  232. " j 2b \n" \
  233. " .previous \n" \
  234. " .section __ex_table,\"a\" \n" \
  235. " "__UA_ADDR "\t1b, 3b \n" \
  236. " .previous \n" \
  237. : "=r" (__gu_err), "=r" (__gu_val) \
  238. : "0" (__gu_err), "o" (__m(__gu_addr)), "i" (-EFAULT)); \
  239. })
  240. /*
  241. * Get a long long 64 using 32 bit registers.
  242. */
  243. #define __get_user_asm_ll32(__gu_err) \
  244. ({ \
  245. __asm__ __volatile__( \
  246. "1: lw %1, %3 \n" \
  247. "2: lw %D1, %4 \n" \
  248. " move %0, $0 \n" \
  249. "3: .section .fixup,\"ax\" \n" \
  250. "4: li %0, %5 \n" \
  251. " move %1, $0 \n" \
  252. " move %D1, $0 \n" \
  253. " j 3b \n" \
  254. " .previous \n" \
  255. " .section __ex_table,\"a\" \n" \
  256. " " __UA_ADDR " 1b, 4b \n" \
  257. " " __UA_ADDR " 2b, 4b \n" \
  258. " .previous \n" \
  259. : "=r" (__gu_err), "=&r" (__gu_val) \
  260. : "0" (__gu_err), "o" (__m(__gu_addr)), \
  261. "o" (__m(__gu_addr + 4)), "i" (-EFAULT)); \
  262. })
  263. extern void __get_user_unknown(void);
  264. /*
  265. * Yuck. We need two variants, one for 64bit operation and one
  266. * for 32 bit mode and old iron.
  267. */
  268. #ifdef __mips64
  269. #define __PUT_USER_DW(__pu_val) __put_user_asm("sd", __pu_val)
  270. #else
  271. #define __PUT_USER_DW(__pu_val) __put_user_asm_ll32(__pu_val)
  272. #endif
  273. #define __put_user_nocheck(x,ptr,size) \
  274. ({ \
  275. __typeof__(*(ptr)) __pu_val; \
  276. long __pu_addr; \
  277. long __pu_err = 0; \
  278. \
  279. might_sleep(); \
  280. __pu_val = (x); \
  281. __pu_addr = (long) (ptr); \
  282. switch (size) { \
  283. case 1: __put_user_asm("sb", __pu_val); break; \
  284. case 2: __put_user_asm("sh", __pu_val); break; \
  285. case 4: __put_user_asm("sw", __pu_val); break; \
  286. case 8: __PUT_USER_DW(__pu_val); break; \
  287. default: __put_user_unknown(); break; \
  288. } \
  289. __pu_err; \
  290. })
  291. #define __put_user_check(x,ptr,size) \
  292. ({ \
  293. __typeof__(*(ptr)) __pu_val; \
  294. long __pu_addr; \
  295. long __pu_err; \
  296. \
  297. might_sleep(); \
  298. __pu_val = (x); \
  299. __pu_addr = (long) (ptr); \
  300. __pu_err = access_ok(VERIFY_WRITE, (void *) __pu_addr, size) \
  301. ? 0 : -EFAULT; \
  302. \
  303. if (likely(!__pu_err)) { \
  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. } \
  312. __pu_err; \
  313. })
  314. #define __put_user_asm(insn, __pu_val) \
  315. ({ \
  316. __asm__ __volatile__( \
  317. "1: " insn " %z2, %3 # __put_user_asm\n" \
  318. "2: \n" \
  319. " .section .fixup,\"ax\" \n" \
  320. "3: li %0, %4 \n" \
  321. " j 2b \n" \
  322. " .previous \n" \
  323. " .section __ex_table,\"a\" \n" \
  324. " " __UA_ADDR " 1b, 3b \n" \
  325. " .previous \n" \
  326. : "=r" (__pu_err) \
  327. : "0" (__pu_err), "Jr" (__pu_val), "o" (__m(__pu_addr)), \
  328. "i" (-EFAULT)); \
  329. })
  330. #define __put_user_asm_ll32(__pu_val) \
  331. ({ \
  332. __asm__ __volatile__( \
  333. "1: sw %2, %3 # __put_user_asm_ll32 \n" \
  334. "2: sw %D2, %4 \n" \
  335. "3: \n" \
  336. " .section .fixup,\"ax\" \n" \
  337. "4: li %0, %5 \n" \
  338. " j 3b \n" \
  339. " .previous \n" \
  340. " .section __ex_table,\"a\" \n" \
  341. " " __UA_ADDR " 1b, 4b \n" \
  342. " " __UA_ADDR " 2b, 4b \n" \
  343. " .previous" \
  344. : "=r" (__pu_err) \
  345. : "0" (__pu_err), "r" (__pu_val), "o" (__m(__pu_addr)), \
  346. "o" (__m(__pu_addr + 4)), "i" (-EFAULT)); \
  347. })
  348. extern void __put_user_unknown(void);
  349. /*
  350. * We're generating jump to subroutines which will be outside the range of
  351. * jump instructions
  352. */
  353. #ifdef MODULE
  354. #define __MODULE_JAL(destination) \
  355. ".set\tnoat\n\t" \
  356. __UA_LA "\t$1, " #destination "\n\t" \
  357. "jalr\t$1\n\t" \
  358. ".set\tat\n\t"
  359. #else
  360. #define __MODULE_JAL(destination) \
  361. "jal\t" #destination "\n\t"
  362. #endif
  363. extern size_t __copy_user(void *__to, const void *__from, size_t __n);
  364. #define __invoke_copy_to_user(to,from,n) \
  365. ({ \
  366. register void *__cu_to_r __asm__ ("$4"); \
  367. register const void *__cu_from_r __asm__ ("$5"); \
  368. register long __cu_len_r __asm__ ("$6"); \
  369. \
  370. __cu_to_r = (to); \
  371. __cu_from_r = (from); \
  372. __cu_len_r = (n); \
  373. __asm__ __volatile__( \
  374. __MODULE_JAL(__copy_user) \
  375. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  376. : \
  377. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  378. "memory"); \
  379. __cu_len_r; \
  380. })
  381. /*
  382. * __copy_to_user: - Copy a block of data into user space, with less checking.
  383. * @to: Destination address, in user space.
  384. * @from: Source address, in kernel space.
  385. * @n: Number of bytes to copy.
  386. *
  387. * Context: User context only. This function may sleep.
  388. *
  389. * Copy data from kernel space to user space. Caller must check
  390. * the specified block with access_ok() before calling this function.
  391. *
  392. * Returns number of bytes that could not be copied.
  393. * On success, this will be zero.
  394. */
  395. #define __copy_to_user(to,from,n) \
  396. ({ \
  397. void *__cu_to; \
  398. const void *__cu_from; \
  399. long __cu_len; \
  400. \
  401. might_sleep(); \
  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_to_user_inatomic __copy_to_user
  409. #define __copy_from_user_inatomic __copy_from_user
  410. /*
  411. * copy_to_user: - Copy a block of data into user space.
  412. * @to: Destination address, in user space.
  413. * @from: Source address, in kernel space.
  414. * @n: Number of bytes to copy.
  415. *
  416. * Context: User context only. This function may sleep.
  417. *
  418. * Copy data from kernel space to user space.
  419. *
  420. * Returns number of bytes that could not be copied.
  421. * On success, this will be zero.
  422. */
  423. #define copy_to_user(to,from,n) \
  424. ({ \
  425. void *__cu_to; \
  426. const void *__cu_from; \
  427. long __cu_len; \
  428. \
  429. might_sleep(); \
  430. __cu_to = (to); \
  431. __cu_from = (from); \
  432. __cu_len = (n); \
  433. if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) \
  434. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  435. __cu_len); \
  436. __cu_len; \
  437. })
  438. #define __invoke_copy_from_user(to,from,n) \
  439. ({ \
  440. register void *__cu_to_r __asm__ ("$4"); \
  441. register const void *__cu_from_r __asm__ ("$5"); \
  442. register long __cu_len_r __asm__ ("$6"); \
  443. \
  444. __cu_to_r = (to); \
  445. __cu_from_r = (from); \
  446. __cu_len_r = (n); \
  447. __asm__ __volatile__( \
  448. ".set\tnoreorder\n\t" \
  449. __MODULE_JAL(__copy_user) \
  450. ".set\tnoat\n\t" \
  451. __UA_ADDU "\t$1, %1, %2\n\t" \
  452. ".set\tat\n\t" \
  453. ".set\treorder" \
  454. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  455. : \
  456. : "$8", "$9", "$10", "$11", "$12", "$15", "$24", "$31", \
  457. "memory"); \
  458. __cu_len_r; \
  459. })
  460. /*
  461. * __copy_from_user: - Copy a block of data from user space, with less checking. * @to: Destination address, in kernel space.
  462. * @from: Source address, in user space.
  463. * @n: Number of bytes to copy.
  464. *
  465. * Context: User context only. This function may sleep.
  466. *
  467. * Copy data from user space to kernel space. Caller must check
  468. * the specified block with access_ok() before calling this function.
  469. *
  470. * Returns number of bytes that could not be copied.
  471. * On success, this will be zero.
  472. *
  473. * If some data could not be copied, this function will pad the copied
  474. * data to the requested size using zero bytes.
  475. */
  476. #define __copy_from_user(to,from,n) \
  477. ({ \
  478. void *__cu_to; \
  479. const void *__cu_from; \
  480. long __cu_len; \
  481. \
  482. might_sleep(); \
  483. __cu_to = (to); \
  484. __cu_from = (from); \
  485. __cu_len = (n); \
  486. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  487. __cu_len); \
  488. __cu_len; \
  489. })
  490. /*
  491. * copy_from_user: - Copy a block of data from user space.
  492. * @to: Destination address, in kernel space.
  493. * @from: Source address, in user space.
  494. * @n: Number of bytes to copy.
  495. *
  496. * Context: User context only. This function may sleep.
  497. *
  498. * Copy data from user space to kernel space.
  499. *
  500. * Returns number of bytes that could not be copied.
  501. * On success, this will be zero.
  502. *
  503. * If some data could not be copied, this function will pad the copied
  504. * data to the requested size using zero bytes.
  505. */
  506. #define copy_from_user(to,from,n) \
  507. ({ \
  508. void *__cu_to; \
  509. const void *__cu_from; \
  510. long __cu_len; \
  511. \
  512. might_sleep(); \
  513. __cu_to = (to); \
  514. __cu_from = (from); \
  515. __cu_len = (n); \
  516. if (access_ok(VERIFY_READ, __cu_from, __cu_len)) \
  517. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  518. __cu_len); \
  519. __cu_len; \
  520. })
  521. #define __copy_in_user(to, from, n) __copy_from_user(to, from, n)
  522. #define copy_in_user(to,from,n) \
  523. ({ \
  524. void *__cu_to; \
  525. const void *__cu_from; \
  526. long __cu_len; \
  527. \
  528. might_sleep(); \
  529. __cu_to = (to); \
  530. __cu_from = (from); \
  531. __cu_len = (n); \
  532. if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) && \
  533. access_ok(VERIFY_WRITE, __cu_to, __cu_len))) \
  534. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  535. __cu_len); \
  536. __cu_len; \
  537. })
  538. /*
  539. * __clear_user: - Zero a block of memory in user space, with less checking.
  540. * @to: Destination address, in user space.
  541. * @n: Number of bytes to zero.
  542. *
  543. * Zero a block of memory in user space. Caller must check
  544. * the specified block with access_ok() before calling this function.
  545. *
  546. * Returns number of bytes that could not be cleared.
  547. * On success, this will be zero.
  548. */
  549. static inline __kernel_size_t
  550. __clear_user(void *addr, __kernel_size_t size)
  551. {
  552. __kernel_size_t res;
  553. might_sleep();
  554. __asm__ __volatile__(
  555. "move\t$4, %1\n\t"
  556. "move\t$5, $0\n\t"
  557. "move\t$6, %2\n\t"
  558. __MODULE_JAL(__bzero)
  559. "move\t%0, $6"
  560. : "=r" (res)
  561. : "r" (addr), "r" (size)
  562. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  563. return res;
  564. }
  565. #define clear_user(addr,n) \
  566. ({ \
  567. void * __cl_addr = (addr); \
  568. unsigned long __cl_size = (n); \
  569. if (__cl_size && access_ok(VERIFY_WRITE, \
  570. ((unsigned long)(__cl_addr)), __cl_size)) \
  571. __cl_size = __clear_user(__cl_addr, __cl_size); \
  572. __cl_size; \
  573. })
  574. /*
  575. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  576. * @dst: Destination address, in kernel space. This buffer must be at
  577. * least @count bytes long.
  578. * @src: Source address, in user space.
  579. * @count: Maximum number of bytes to copy, including the trailing NUL.
  580. *
  581. * Copies a NUL-terminated string from userspace to kernel space.
  582. * Caller must check the specified block with access_ok() before calling
  583. * this function.
  584. *
  585. * On success, returns the length of the string (not including the trailing
  586. * NUL).
  587. *
  588. * If access to userspace fails, returns -EFAULT (some data may have been
  589. * copied).
  590. *
  591. * If @count is smaller than the length of the string, copies @count bytes
  592. * and returns @count.
  593. */
  594. static inline long
  595. __strncpy_from_user(char *__to, const char *__from, long __len)
  596. {
  597. long res;
  598. might_sleep();
  599. __asm__ __volatile__(
  600. "move\t$4, %1\n\t"
  601. "move\t$5, %2\n\t"
  602. "move\t$6, %3\n\t"
  603. __MODULE_JAL(__strncpy_from_user_nocheck_asm)
  604. "move\t%0, $2"
  605. : "=r" (res)
  606. : "r" (__to), "r" (__from), "r" (__len)
  607. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  608. return res;
  609. }
  610. /*
  611. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  612. * @dst: Destination address, in kernel space. This buffer must be at
  613. * least @count bytes long.
  614. * @src: Source address, in user space.
  615. * @count: Maximum number of bytes to copy, including the trailing NUL.
  616. *
  617. * Copies a NUL-terminated string from userspace to kernel space.
  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 *__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_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. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  645. static inline long __strlen_user(const char *s)
  646. {
  647. long res;
  648. might_sleep();
  649. __asm__ __volatile__(
  650. "move\t$4, %1\n\t"
  651. __MODULE_JAL(__strlen_user_nocheck_asm)
  652. "move\t%0, $2"
  653. : "=r" (res)
  654. : "r" (s)
  655. : "$2", "$4", __UA_t0, "$31");
  656. return res;
  657. }
  658. /*
  659. * strlen_user: - Get the size of a string in user space.
  660. * @str: The string to measure.
  661. *
  662. * Context: User context only. This function may sleep.
  663. *
  664. * Get the size of a NUL-terminated string in user space.
  665. *
  666. * Returns the size of the string INCLUDING the terminating NUL.
  667. * On exception, returns 0.
  668. *
  669. * If there is a limit on the length of a valid string, you may wish to
  670. * consider using strnlen_user() instead.
  671. */
  672. static inline long strlen_user(const char *s)
  673. {
  674. long res;
  675. might_sleep();
  676. __asm__ __volatile__(
  677. "move\t$4, %1\n\t"
  678. __MODULE_JAL(__strlen_user_asm)
  679. "move\t%0, $2"
  680. : "=r" (res)
  681. : "r" (s)
  682. : "$2", "$4", __UA_t0, "$31");
  683. return res;
  684. }
  685. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  686. static inline long __strnlen_user(const char *s, long n)
  687. {
  688. long res;
  689. might_sleep();
  690. __asm__ __volatile__(
  691. "move\t$4, %1\n\t"
  692. "move\t$5, %2\n\t"
  693. __MODULE_JAL(__strnlen_user_nocheck_asm)
  694. "move\t%0, $2"
  695. : "=r" (res)
  696. : "r" (s), "r" (n)
  697. : "$2", "$4", "$5", __UA_t0, "$31");
  698. return res;
  699. }
  700. /*
  701. * strlen_user: - Get the size of a string in user space.
  702. * @str: The string to measure.
  703. *
  704. * Context: User context only. This function may sleep.
  705. *
  706. * Get the size of a NUL-terminated string in user space.
  707. *
  708. * Returns the size of the string INCLUDING the terminating NUL.
  709. * On exception, returns 0.
  710. *
  711. * If there is a limit on the length of a valid string, you may wish to
  712. * consider using strnlen_user() instead.
  713. */
  714. static inline long strnlen_user(const char *s, long n)
  715. {
  716. long res;
  717. might_sleep();
  718. __asm__ __volatile__(
  719. "move\t$4, %1\n\t"
  720. "move\t$5, %2\n\t"
  721. __MODULE_JAL(__strnlen_user_asm)
  722. "move\t%0, $2"
  723. : "=r" (res)
  724. : "r" (s), "r" (n)
  725. : "$2", "$4", "$5", __UA_t0, "$31");
  726. return res;
  727. }
  728. struct exception_table_entry
  729. {
  730. unsigned long insn;
  731. unsigned long nextinsn;
  732. };
  733. extern int fixup_exception(struct pt_regs *regs);
  734. #endif /* _ASM_UACCESS_H */