uaccess.h 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470
  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. * Copyright (C) 2014, Imagination Technologies Ltd.
  10. */
  11. #ifndef _ASM_UACCESS_H
  12. #define _ASM_UACCESS_H
  13. #include <linux/kernel.h>
  14. #include <linux/errno.h>
  15. #include <linux/thread_info.h>
  16. #include <asm/asm-eva.h>
  17. /*
  18. * The fs value determines whether argument validity checking should be
  19. * performed or not. If get_fs() == USER_DS, checking is performed, with
  20. * get_fs() == KERNEL_DS, checking is bypassed.
  21. *
  22. * For historical reasons, these macros are grossly misnamed.
  23. */
  24. #ifdef CONFIG_32BIT
  25. #ifdef CONFIG_KVM_GUEST
  26. #define __UA_LIMIT 0x40000000UL
  27. #else
  28. #define __UA_LIMIT 0x80000000UL
  29. #endif
  30. #define __UA_ADDR ".word"
  31. #define __UA_LA "la"
  32. #define __UA_ADDU "addu"
  33. #define __UA_t0 "$8"
  34. #define __UA_t1 "$9"
  35. #endif /* CONFIG_32BIT */
  36. #ifdef CONFIG_64BIT
  37. extern u64 __ua_limit;
  38. #define __UA_LIMIT __ua_limit
  39. #define __UA_ADDR ".dword"
  40. #define __UA_LA "dla"
  41. #define __UA_ADDU "daddu"
  42. #define __UA_t0 "$12"
  43. #define __UA_t1 "$13"
  44. #endif /* CONFIG_64BIT */
  45. /*
  46. * USER_DS is a bitmask that has the bits set that may not be set in a valid
  47. * userspace address. Note that we limit 32-bit userspace to 0x7fff8000 but
  48. * the arithmetic we're doing only works if the limit is a power of two, so
  49. * we use 0x80000000 here on 32-bit kernels. If a process passes an invalid
  50. * address in this range it's the process's problem, not ours :-)
  51. */
  52. #ifdef CONFIG_KVM_GUEST
  53. #define KERNEL_DS ((mm_segment_t) { 0x80000000UL })
  54. #define USER_DS ((mm_segment_t) { 0xC0000000UL })
  55. #else
  56. #define KERNEL_DS ((mm_segment_t) { 0UL })
  57. #define USER_DS ((mm_segment_t) { __UA_LIMIT })
  58. #endif
  59. #define VERIFY_READ 0
  60. #define VERIFY_WRITE 1
  61. #define get_ds() (KERNEL_DS)
  62. #define get_fs() (current_thread_info()->addr_limit)
  63. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  64. #define segment_eq(a, b) ((a).seg == (b).seg)
  65. /*
  66. * Is a address valid? This does a straighforward calculation rather
  67. * than tests.
  68. *
  69. * Address valid if:
  70. * - "addr" doesn't have any high-bits set
  71. * - AND "size" doesn't have any high-bits set
  72. * - AND "addr+size" doesn't have any high-bits set
  73. * - OR we are in kernel mode.
  74. *
  75. * __ua_size() is a trick to avoid runtime checking of positive constant
  76. * sizes; for those we already know at compile time that the size is ok.
  77. */
  78. #define __ua_size(size) \
  79. ((__builtin_constant_p(size) && (signed long) (size) > 0) ? 0 : (size))
  80. /*
  81. * access_ok: - Checks if a user space pointer is valid
  82. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  83. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  84. * to write to a block, it is always safe to read from it.
  85. * @addr: User space pointer to start of block to check
  86. * @size: Size of block to check
  87. *
  88. * Context: User context only. This function may sleep.
  89. *
  90. * Checks if a pointer to a block of memory in user space is valid.
  91. *
  92. * Returns true (nonzero) if the memory block may be valid, false (zero)
  93. * if it is definitely invalid.
  94. *
  95. * Note that, depending on architecture, this function probably just
  96. * checks that the pointer is in the user space range - after calling
  97. * this function, memory access functions may still return -EFAULT.
  98. */
  99. #define __access_mask get_fs().seg
  100. #define __access_ok(addr, size, mask) \
  101. ({ \
  102. unsigned long __addr = (unsigned long) (addr); \
  103. unsigned long __size = size; \
  104. unsigned long __mask = mask; \
  105. unsigned long __ok; \
  106. \
  107. __chk_user_ptr(addr); \
  108. __ok = (signed long)(__mask & (__addr | (__addr + __size) | \
  109. __ua_size(__size))); \
  110. __ok == 0; \
  111. })
  112. #define access_ok(type, addr, size) \
  113. likely(__access_ok((addr), (size), __access_mask))
  114. /*
  115. * put_user: - Write a simple value into user space.
  116. * @x: Value to copy to user space.
  117. * @ptr: Destination address, in user space.
  118. *
  119. * Context: User context only. This function may sleep.
  120. *
  121. * This macro copies a single simple value from kernel space to user
  122. * space. It supports simple types like char and int, but not larger
  123. * data types like structures or arrays.
  124. *
  125. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  126. * to the result of dereferencing @ptr.
  127. *
  128. * Returns zero on success, or -EFAULT on error.
  129. */
  130. #define put_user(x,ptr) \
  131. __put_user_check((x), (ptr), sizeof(*(ptr)))
  132. /*
  133. * get_user: - Get a simple variable from user space.
  134. * @x: Variable to store result.
  135. * @ptr: Source address, in user space.
  136. *
  137. * Context: User context only. This function may sleep.
  138. *
  139. * This macro copies a single simple variable from user space to kernel
  140. * space. It supports simple types like char and int, but not larger
  141. * data types like structures or arrays.
  142. *
  143. * @ptr must have pointer-to-simple-variable type, and the result of
  144. * dereferencing @ptr must be assignable to @x without a cast.
  145. *
  146. * Returns zero on success, or -EFAULT on error.
  147. * On error, the variable @x is set to zero.
  148. */
  149. #define get_user(x,ptr) \
  150. __get_user_check((x), (ptr), sizeof(*(ptr)))
  151. /*
  152. * __put_user: - Write a simple value into user space, with less checking.
  153. * @x: Value to copy to user space.
  154. * @ptr: Destination address, in user space.
  155. *
  156. * Context: User context only. This function may sleep.
  157. *
  158. * This macro copies a single simple value from kernel space to user
  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 @x must be assignable
  163. * to the result of dereferencing @ptr.
  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. */
  170. #define __put_user(x,ptr) \
  171. __put_user_nocheck((x), (ptr), sizeof(*(ptr)))
  172. /*
  173. * __get_user: - Get a simple variable from user space, with less checking.
  174. * @x: Variable to store result.
  175. * @ptr: Source address, in user space.
  176. *
  177. * Context: User context only. This function may sleep.
  178. *
  179. * This macro copies a single simple variable from user space to kernel
  180. * space. It supports simple types like char and int, but not larger
  181. * data types like structures or arrays.
  182. *
  183. * @ptr must have pointer-to-simple-variable type, and the result of
  184. * dereferencing @ptr must be assignable to @x without a cast.
  185. *
  186. * Caller must check the pointer with access_ok() before calling this
  187. * function.
  188. *
  189. * Returns zero on success, or -EFAULT on error.
  190. * On error, the variable @x is set to zero.
  191. */
  192. #define __get_user(x,ptr) \
  193. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  194. struct __large_struct { unsigned long buf[100]; };
  195. #define __m(x) (*(struct __large_struct __user *)(x))
  196. /*
  197. * Yuck. We need two variants, one for 64bit operation and one
  198. * for 32 bit mode and old iron.
  199. */
  200. #ifndef CONFIG_EVA
  201. #define __get_kernel_common(val, size, ptr) __get_user_common(val, size, ptr)
  202. #else
  203. /*
  204. * Kernel specific functions for EVA. We need to use normal load instructions
  205. * to read data from kernel when operating in EVA mode. We use these macros to
  206. * avoid redefining __get_user_asm for EVA.
  207. */
  208. #undef _loadd
  209. #undef _loadw
  210. #undef _loadh
  211. #undef _loadb
  212. #ifdef CONFIG_32BIT
  213. #define _loadd _loadw
  214. #else
  215. #define _loadd(reg, addr) "ld " reg ", " addr
  216. #endif
  217. #define _loadw(reg, addr) "lw " reg ", " addr
  218. #define _loadh(reg, addr) "lh " reg ", " addr
  219. #define _loadb(reg, addr) "lb " reg ", " addr
  220. #define __get_kernel_common(val, size, ptr) \
  221. do { \
  222. switch (size) { \
  223. case 1: __get_data_asm(val, _loadb, ptr); break; \
  224. case 2: __get_data_asm(val, _loadh, ptr); break; \
  225. case 4: __get_data_asm(val, _loadw, ptr); break; \
  226. case 8: __GET_DW(val, _loadd, ptr); break; \
  227. default: __get_user_unknown(); break; \
  228. } \
  229. } while (0)
  230. #endif
  231. #ifdef CONFIG_32BIT
  232. #define __GET_DW(val, insn, ptr) __get_data_asm_ll32(val, insn, ptr)
  233. #endif
  234. #ifdef CONFIG_64BIT
  235. #define __GET_DW(val, insn, ptr) __get_data_asm(val, insn, ptr)
  236. #endif
  237. extern void __get_user_unknown(void);
  238. #define __get_user_common(val, size, ptr) \
  239. do { \
  240. switch (size) { \
  241. case 1: __get_data_asm(val, user_lb, ptr); break; \
  242. case 2: __get_data_asm(val, user_lh, ptr); break; \
  243. case 4: __get_data_asm(val, user_lw, ptr); break; \
  244. case 8: __GET_DW(val, user_ld, ptr); break; \
  245. default: __get_user_unknown(); break; \
  246. } \
  247. } while (0)
  248. #define __get_user_nocheck(x, ptr, size) \
  249. ({ \
  250. int __gu_err; \
  251. \
  252. if (segment_eq(get_fs(), get_ds())) { \
  253. __get_kernel_common((x), size, ptr); \
  254. } else { \
  255. __chk_user_ptr(ptr); \
  256. __get_user_common((x), size, ptr); \
  257. } \
  258. __gu_err; \
  259. })
  260. #define __get_user_check(x, ptr, size) \
  261. ({ \
  262. int __gu_err = -EFAULT; \
  263. const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
  264. \
  265. might_fault(); \
  266. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) { \
  267. if (segment_eq(get_fs(), get_ds())) \
  268. __get_kernel_common((x), size, __gu_ptr); \
  269. else \
  270. __get_user_common((x), size, __gu_ptr); \
  271. } \
  272. \
  273. __gu_err; \
  274. })
  275. #define __get_data_asm(val, insn, addr) \
  276. { \
  277. long __gu_tmp; \
  278. \
  279. __asm__ __volatile__( \
  280. "1: "insn("%1", "%3")" \n" \
  281. "2: \n" \
  282. " .insn \n" \
  283. " .section .fixup,\"ax\" \n" \
  284. "3: li %0, %4 \n" \
  285. " j 2b \n" \
  286. " .previous \n" \
  287. " .section __ex_table,\"a\" \n" \
  288. " "__UA_ADDR "\t1b, 3b \n" \
  289. " .previous \n" \
  290. : "=r" (__gu_err), "=r" (__gu_tmp) \
  291. : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
  292. \
  293. (val) = (__typeof__(*(addr))) __gu_tmp; \
  294. }
  295. /*
  296. * Get a long long 64 using 32 bit registers.
  297. */
  298. #define __get_data_asm_ll32(val, insn, addr) \
  299. { \
  300. union { \
  301. unsigned long long l; \
  302. __typeof__(*(addr)) t; \
  303. } __gu_tmp; \
  304. \
  305. __asm__ __volatile__( \
  306. "1: " insn("%1", "(%3)")" \n" \
  307. "2: " insn("%D1", "4(%3)")" \n" \
  308. "3: \n" \
  309. " .insn \n" \
  310. " .section .fixup,\"ax\" \n" \
  311. "4: li %0, %4 \n" \
  312. " move %1, $0 \n" \
  313. " move %D1, $0 \n" \
  314. " j 3b \n" \
  315. " .previous \n" \
  316. " .section __ex_table,\"a\" \n" \
  317. " " __UA_ADDR " 1b, 4b \n" \
  318. " " __UA_ADDR " 2b, 4b \n" \
  319. " .previous \n" \
  320. : "=r" (__gu_err), "=&r" (__gu_tmp.l) \
  321. : "0" (0), "r" (addr), "i" (-EFAULT)); \
  322. \
  323. (val) = __gu_tmp.t; \
  324. }
  325. #ifndef CONFIG_EVA
  326. #define __put_kernel_common(ptr, size) __put_user_common(ptr, size)
  327. #else
  328. /*
  329. * Kernel specific functions for EVA. We need to use normal load instructions
  330. * to read data from kernel when operating in EVA mode. We use these macros to
  331. * avoid redefining __get_data_asm for EVA.
  332. */
  333. #undef _stored
  334. #undef _storew
  335. #undef _storeh
  336. #undef _storeb
  337. #ifdef CONFIG_32BIT
  338. #define _stored _storew
  339. #else
  340. #define _stored(reg, addr) "ld " reg ", " addr
  341. #endif
  342. #define _storew(reg, addr) "sw " reg ", " addr
  343. #define _storeh(reg, addr) "sh " reg ", " addr
  344. #define _storeb(reg, addr) "sb " reg ", " addr
  345. #define __put_kernel_common(ptr, size) \
  346. do { \
  347. switch (size) { \
  348. case 1: __put_data_asm(_storeb, ptr); break; \
  349. case 2: __put_data_asm(_storeh, ptr); break; \
  350. case 4: __put_data_asm(_storew, ptr); break; \
  351. case 8: __PUT_DW(_stored, ptr); break; \
  352. default: __put_user_unknown(); break; \
  353. } \
  354. } while(0)
  355. #endif
  356. /*
  357. * Yuck. We need two variants, one for 64bit operation and one
  358. * for 32 bit mode and old iron.
  359. */
  360. #ifdef CONFIG_32BIT
  361. #define __PUT_DW(insn, ptr) __put_data_asm_ll32(insn, ptr)
  362. #endif
  363. #ifdef CONFIG_64BIT
  364. #define __PUT_DW(insn, ptr) __put_data_asm(insn, ptr)
  365. #endif
  366. #define __put_user_common(ptr, size) \
  367. do { \
  368. switch (size) { \
  369. case 1: __put_data_asm(user_sb, ptr); break; \
  370. case 2: __put_data_asm(user_sh, ptr); break; \
  371. case 4: __put_data_asm(user_sw, ptr); break; \
  372. case 8: __PUT_DW(user_sd, ptr); break; \
  373. default: __put_user_unknown(); break; \
  374. } \
  375. } while (0)
  376. #define __put_user_nocheck(x, ptr, size) \
  377. ({ \
  378. __typeof__(*(ptr)) __pu_val; \
  379. int __pu_err = 0; \
  380. \
  381. __pu_val = (x); \
  382. if (segment_eq(get_fs(), get_ds())) { \
  383. __put_kernel_common(ptr, size); \
  384. } else { \
  385. __chk_user_ptr(ptr); \
  386. __put_user_common(ptr, size); \
  387. } \
  388. __pu_err; \
  389. })
  390. #define __put_user_check(x, ptr, size) \
  391. ({ \
  392. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  393. __typeof__(*(ptr)) __pu_val = (x); \
  394. int __pu_err = -EFAULT; \
  395. \
  396. might_fault(); \
  397. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) { \
  398. if (segment_eq(get_fs(), get_ds())) \
  399. __put_kernel_common(__pu_addr, size); \
  400. else \
  401. __put_user_common(__pu_addr, size); \
  402. } \
  403. \
  404. __pu_err; \
  405. })
  406. #define __put_data_asm(insn, ptr) \
  407. { \
  408. __asm__ __volatile__( \
  409. "1: "insn("%z2", "%3")" # __put_data_asm \n" \
  410. "2: \n" \
  411. " .insn \n" \
  412. " .section .fixup,\"ax\" \n" \
  413. "3: li %0, %4 \n" \
  414. " j 2b \n" \
  415. " .previous \n" \
  416. " .section __ex_table,\"a\" \n" \
  417. " " __UA_ADDR " 1b, 3b \n" \
  418. " .previous \n" \
  419. : "=r" (__pu_err) \
  420. : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
  421. "i" (-EFAULT)); \
  422. }
  423. #define __put_data_asm_ll32(insn, ptr) \
  424. { \
  425. __asm__ __volatile__( \
  426. "1: "insn("%2", "(%3)")" # __put_data_asm_ll32 \n" \
  427. "2: "insn("%D2", "4(%3)")" \n" \
  428. "3: \n" \
  429. " .insn \n" \
  430. " .section .fixup,\"ax\" \n" \
  431. "4: li %0, %4 \n" \
  432. " j 3b \n" \
  433. " .previous \n" \
  434. " .section __ex_table,\"a\" \n" \
  435. " " __UA_ADDR " 1b, 4b \n" \
  436. " " __UA_ADDR " 2b, 4b \n" \
  437. " .previous" \
  438. : "=r" (__pu_err) \
  439. : "0" (0), "r" (__pu_val), "r" (ptr), \
  440. "i" (-EFAULT)); \
  441. }
  442. extern void __put_user_unknown(void);
  443. /*
  444. * ul{b,h,w} are macros and there are no equivalent macros for EVA.
  445. * EVA unaligned access is handled in the ADE exception handler.
  446. */
  447. #ifndef CONFIG_EVA
  448. /*
  449. * put_user_unaligned: - Write a simple value into user space.
  450. * @x: Value to copy to user space.
  451. * @ptr: Destination address, in user space.
  452. *
  453. * Context: User context only. This function may sleep.
  454. *
  455. * This macro copies a single simple value from kernel space to user
  456. * space. It supports simple types like char and int, but not larger
  457. * data types like structures or arrays.
  458. *
  459. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  460. * to the result of dereferencing @ptr.
  461. *
  462. * Returns zero on success, or -EFAULT on error.
  463. */
  464. #define put_user_unaligned(x,ptr) \
  465. __put_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
  466. /*
  467. * get_user_unaligned: - Get a simple variable from user space.
  468. * @x: Variable to store result.
  469. * @ptr: Source address, in user space.
  470. *
  471. * Context: User context only. This function may sleep.
  472. *
  473. * This macro copies a single simple variable from user space to kernel
  474. * space. It supports simple types like char and int, but not larger
  475. * data types like structures or arrays.
  476. *
  477. * @ptr must have pointer-to-simple-variable type, and the result of
  478. * dereferencing @ptr must be assignable to @x without a cast.
  479. *
  480. * Returns zero on success, or -EFAULT on error.
  481. * On error, the variable @x is set to zero.
  482. */
  483. #define get_user_unaligned(x,ptr) \
  484. __get_user_unaligned_check((x),(ptr),sizeof(*(ptr)))
  485. /*
  486. * __put_user_unaligned: - Write a simple value into user space, with less checking.
  487. * @x: Value to copy to user space.
  488. * @ptr: Destination address, in user space.
  489. *
  490. * Context: User context only. This function may sleep.
  491. *
  492. * This macro copies a single simple value from kernel space to user
  493. * space. It supports simple types like char and int, but not larger
  494. * data types like structures or arrays.
  495. *
  496. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  497. * to the result of dereferencing @ptr.
  498. *
  499. * Caller must check the pointer with access_ok() before calling this
  500. * function.
  501. *
  502. * Returns zero on success, or -EFAULT on error.
  503. */
  504. #define __put_user_unaligned(x,ptr) \
  505. __put_user_unaligned_nocheck((x),(ptr),sizeof(*(ptr)))
  506. /*
  507. * __get_user_unaligned: - Get a simple variable from user space, with less checking.
  508. * @x: Variable to store result.
  509. * @ptr: Source address, in user space.
  510. *
  511. * Context: User context only. This function may sleep.
  512. *
  513. * This macro copies a single simple variable from user space to kernel
  514. * space. It supports simple types like char and int, but not larger
  515. * data types like structures or arrays.
  516. *
  517. * @ptr must have pointer-to-simple-variable type, and the result of
  518. * dereferencing @ptr must be assignable to @x without a cast.
  519. *
  520. * Caller must check the pointer with access_ok() before calling this
  521. * function.
  522. *
  523. * Returns zero on success, or -EFAULT on error.
  524. * On error, the variable @x is set to zero.
  525. */
  526. #define __get_user_unaligned(x,ptr) \
  527. __get_user__unalignednocheck((x),(ptr),sizeof(*(ptr)))
  528. /*
  529. * Yuck. We need two variants, one for 64bit operation and one
  530. * for 32 bit mode and old iron.
  531. */
  532. #ifdef CONFIG_32BIT
  533. #define __GET_USER_UNALIGNED_DW(val, ptr) \
  534. __get_user_unaligned_asm_ll32(val, ptr)
  535. #endif
  536. #ifdef CONFIG_64BIT
  537. #define __GET_USER_UNALIGNED_DW(val, ptr) \
  538. __get_user_unaligned_asm(val, "uld", ptr)
  539. #endif
  540. extern void __get_user_unaligned_unknown(void);
  541. #define __get_user_unaligned_common(val, size, ptr) \
  542. do { \
  543. switch (size) { \
  544. case 1: __get_data_asm(val, "lb", ptr); break; \
  545. case 2: __get_user_unaligned_asm(val, "ulh", ptr); break; \
  546. case 4: __get_user_unaligned_asm(val, "ulw", ptr); break; \
  547. case 8: __GET_USER_UNALIGNED_DW(val, ptr); break; \
  548. default: __get_user_unaligned_unknown(); break; \
  549. } \
  550. } while (0)
  551. #define __get_user_unaligned_nocheck(x,ptr,size) \
  552. ({ \
  553. int __gu_err; \
  554. \
  555. __get_user_unaligned_common((x), size, ptr); \
  556. __gu_err; \
  557. })
  558. #define __get_user_unaligned_check(x,ptr,size) \
  559. ({ \
  560. int __gu_err = -EFAULT; \
  561. const __typeof__(*(ptr)) __user * __gu_ptr = (ptr); \
  562. \
  563. if (likely(access_ok(VERIFY_READ, __gu_ptr, size))) \
  564. __get_user_unaligned_common((x), size, __gu_ptr); \
  565. \
  566. __gu_err; \
  567. })
  568. #define __get_data_unaligned_asm(val, insn, addr) \
  569. { \
  570. long __gu_tmp; \
  571. \
  572. __asm__ __volatile__( \
  573. "1: " insn " %1, %3 \n" \
  574. "2: \n" \
  575. " .insn \n" \
  576. " .section .fixup,\"ax\" \n" \
  577. "3: li %0, %4 \n" \
  578. " j 2b \n" \
  579. " .previous \n" \
  580. " .section __ex_table,\"a\" \n" \
  581. " "__UA_ADDR "\t1b, 3b \n" \
  582. " "__UA_ADDR "\t1b + 4, 3b \n" \
  583. " .previous \n" \
  584. : "=r" (__gu_err), "=r" (__gu_tmp) \
  585. : "0" (0), "o" (__m(addr)), "i" (-EFAULT)); \
  586. \
  587. (val) = (__typeof__(*(addr))) __gu_tmp; \
  588. }
  589. /*
  590. * Get a long long 64 using 32 bit registers.
  591. */
  592. #define __get_user_unaligned_asm_ll32(val, addr) \
  593. { \
  594. unsigned long long __gu_tmp; \
  595. \
  596. __asm__ __volatile__( \
  597. "1: ulw %1, (%3) \n" \
  598. "2: ulw %D1, 4(%3) \n" \
  599. " move %0, $0 \n" \
  600. "3: \n" \
  601. " .insn \n" \
  602. " .section .fixup,\"ax\" \n" \
  603. "4: li %0, %4 \n" \
  604. " move %1, $0 \n" \
  605. " move %D1, $0 \n" \
  606. " j 3b \n" \
  607. " .previous \n" \
  608. " .section __ex_table,\"a\" \n" \
  609. " " __UA_ADDR " 1b, 4b \n" \
  610. " " __UA_ADDR " 1b + 4, 4b \n" \
  611. " " __UA_ADDR " 2b, 4b \n" \
  612. " " __UA_ADDR " 2b + 4, 4b \n" \
  613. " .previous \n" \
  614. : "=r" (__gu_err), "=&r" (__gu_tmp) \
  615. : "0" (0), "r" (addr), "i" (-EFAULT)); \
  616. (val) = (__typeof__(*(addr))) __gu_tmp; \
  617. }
  618. /*
  619. * Yuck. We need two variants, one for 64bit operation and one
  620. * for 32 bit mode and old iron.
  621. */
  622. #ifdef CONFIG_32BIT
  623. #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm_ll32(ptr)
  624. #endif
  625. #ifdef CONFIG_64BIT
  626. #define __PUT_USER_UNALIGNED_DW(ptr) __put_user_unaligned_asm("usd", ptr)
  627. #endif
  628. #define __put_user_unaligned_common(ptr, size) \
  629. do { \
  630. switch (size) { \
  631. case 1: __put_data_asm("sb", ptr); break; \
  632. case 2: __put_user_unaligned_asm("ush", ptr); break; \
  633. case 4: __put_user_unaligned_asm("usw", ptr); break; \
  634. case 8: __PUT_USER_UNALIGNED_DW(ptr); break; \
  635. default: __put_user_unaligned_unknown(); break; \
  636. } while (0)
  637. #define __put_user_unaligned_nocheck(x,ptr,size) \
  638. ({ \
  639. __typeof__(*(ptr)) __pu_val; \
  640. int __pu_err = 0; \
  641. \
  642. __pu_val = (x); \
  643. __put_user_unaligned_common(ptr, size); \
  644. __pu_err; \
  645. })
  646. #define __put_user_unaligned_check(x,ptr,size) \
  647. ({ \
  648. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  649. __typeof__(*(ptr)) __pu_val = (x); \
  650. int __pu_err = -EFAULT; \
  651. \
  652. if (likely(access_ok(VERIFY_WRITE, __pu_addr, size))) \
  653. __put_user_unaligned_common(__pu_addr, size); \
  654. \
  655. __pu_err; \
  656. })
  657. #define __put_user_unaligned_asm(insn, ptr) \
  658. { \
  659. __asm__ __volatile__( \
  660. "1: " insn " %z2, %3 # __put_user_unaligned_asm\n" \
  661. "2: \n" \
  662. " .insn \n" \
  663. " .section .fixup,\"ax\" \n" \
  664. "3: li %0, %4 \n" \
  665. " j 2b \n" \
  666. " .previous \n" \
  667. " .section __ex_table,\"a\" \n" \
  668. " " __UA_ADDR " 1b, 3b \n" \
  669. " .previous \n" \
  670. : "=r" (__pu_err) \
  671. : "0" (0), "Jr" (__pu_val), "o" (__m(ptr)), \
  672. "i" (-EFAULT)); \
  673. }
  674. #define __put_user_unaligned_asm_ll32(ptr) \
  675. { \
  676. __asm__ __volatile__( \
  677. "1: sw %2, (%3) # __put_user_unaligned_asm_ll32 \n" \
  678. "2: sw %D2, 4(%3) \n" \
  679. "3: \n" \
  680. " .insn \n" \
  681. " .section .fixup,\"ax\" \n" \
  682. "4: li %0, %4 \n" \
  683. " j 3b \n" \
  684. " .previous \n" \
  685. " .section __ex_table,\"a\" \n" \
  686. " " __UA_ADDR " 1b, 4b \n" \
  687. " " __UA_ADDR " 1b + 4, 4b \n" \
  688. " " __UA_ADDR " 2b, 4b \n" \
  689. " " __UA_ADDR " 2b + 4, 4b \n" \
  690. " .previous" \
  691. : "=r" (__pu_err) \
  692. : "0" (0), "r" (__pu_val), "r" (ptr), \
  693. "i" (-EFAULT)); \
  694. }
  695. extern void __put_user_unaligned_unknown(void);
  696. #endif
  697. /*
  698. * We're generating jump to subroutines which will be outside the range of
  699. * jump instructions
  700. */
  701. #ifdef MODULE
  702. #define __MODULE_JAL(destination) \
  703. ".set\tnoat\n\t" \
  704. __UA_LA "\t$1, " #destination "\n\t" \
  705. "jalr\t$1\n\t" \
  706. ".set\tat\n\t"
  707. #else
  708. #define __MODULE_JAL(destination) \
  709. "jal\t" #destination "\n\t"
  710. #endif
  711. #ifndef CONFIG_CPU_DADDI_WORKAROUNDS
  712. #define DADDI_SCRATCH "$0"
  713. #else
  714. #define DADDI_SCRATCH "$3"
  715. #endif
  716. extern size_t __copy_user(void *__to, const void *__from, size_t __n);
  717. #ifndef CONFIG_EVA
  718. #define __invoke_copy_to_user(to, from, n) \
  719. ({ \
  720. register void __user *__cu_to_r __asm__("$4"); \
  721. register const void *__cu_from_r __asm__("$5"); \
  722. register long __cu_len_r __asm__("$6"); \
  723. \
  724. __cu_to_r = (to); \
  725. __cu_from_r = (from); \
  726. __cu_len_r = (n); \
  727. __asm__ __volatile__( \
  728. __MODULE_JAL(__copy_user) \
  729. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  730. : \
  731. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  732. DADDI_SCRATCH, "memory"); \
  733. __cu_len_r; \
  734. })
  735. #define __invoke_copy_to_kernel(to, from, n) \
  736. __invoke_copy_to_user(to, from, n)
  737. #endif
  738. /*
  739. * __copy_to_user: - Copy a block of data into user space, with less checking.
  740. * @to: Destination address, in user space.
  741. * @from: Source address, in kernel space.
  742. * @n: Number of bytes to copy.
  743. *
  744. * Context: User context only. This function may sleep.
  745. *
  746. * Copy data from kernel space to user space. Caller must check
  747. * the specified block with access_ok() before calling this function.
  748. *
  749. * Returns number of bytes that could not be copied.
  750. * On success, this will be zero.
  751. */
  752. #define __copy_to_user(to, from, n) \
  753. ({ \
  754. void __user *__cu_to; \
  755. const void *__cu_from; \
  756. long __cu_len; \
  757. \
  758. __cu_to = (to); \
  759. __cu_from = (from); \
  760. __cu_len = (n); \
  761. might_fault(); \
  762. if (segment_eq(get_fs(), get_ds())) \
  763. __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
  764. __cu_len); \
  765. else \
  766. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  767. __cu_len); \
  768. __cu_len; \
  769. })
  770. extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
  771. #define __copy_to_user_inatomic(to, from, n) \
  772. ({ \
  773. void __user *__cu_to; \
  774. const void *__cu_from; \
  775. long __cu_len; \
  776. \
  777. __cu_to = (to); \
  778. __cu_from = (from); \
  779. __cu_len = (n); \
  780. if (segment_eq(get_fs(), get_ds())) \
  781. __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
  782. __cu_len); \
  783. else \
  784. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  785. __cu_len); \
  786. __cu_len; \
  787. })
  788. #define __copy_from_user_inatomic(to, from, n) \
  789. ({ \
  790. void *__cu_to; \
  791. const void __user *__cu_from; \
  792. long __cu_len; \
  793. \
  794. __cu_to = (to); \
  795. __cu_from = (from); \
  796. __cu_len = (n); \
  797. if (segment_eq(get_fs(), get_ds())) \
  798. __cu_len = __invoke_copy_from_kernel_inatomic(__cu_to, \
  799. __cu_from,\
  800. __cu_len);\
  801. else \
  802. __cu_len = __invoke_copy_from_user_inatomic(__cu_to, \
  803. __cu_from, \
  804. __cu_len); \
  805. __cu_len; \
  806. })
  807. /*
  808. * copy_to_user: - Copy a block of data into user space.
  809. * @to: Destination address, in user space.
  810. * @from: Source address, in kernel space.
  811. * @n: Number of bytes to copy.
  812. *
  813. * Context: User context only. This function may sleep.
  814. *
  815. * Copy data from kernel space to user space.
  816. *
  817. * Returns number of bytes that could not be copied.
  818. * On success, this will be zero.
  819. */
  820. #define copy_to_user(to, from, n) \
  821. ({ \
  822. void __user *__cu_to; \
  823. const void *__cu_from; \
  824. long __cu_len; \
  825. \
  826. __cu_to = (to); \
  827. __cu_from = (from); \
  828. __cu_len = (n); \
  829. if (segment_eq(get_fs(), get_ds())) { \
  830. __cu_len = __invoke_copy_to_kernel(__cu_to, \
  831. __cu_from, \
  832. __cu_len); \
  833. } else { \
  834. if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
  835. might_fault(); \
  836. __cu_len = __invoke_copy_to_user(__cu_to, \
  837. __cu_from, \
  838. __cu_len); \
  839. } \
  840. } \
  841. __cu_len; \
  842. })
  843. #ifndef CONFIG_EVA
  844. #define __invoke_copy_from_user(to, from, n) \
  845. ({ \
  846. register void *__cu_to_r __asm__("$4"); \
  847. register const void __user *__cu_from_r __asm__("$5"); \
  848. register long __cu_len_r __asm__("$6"); \
  849. \
  850. __cu_to_r = (to); \
  851. __cu_from_r = (from); \
  852. __cu_len_r = (n); \
  853. __asm__ __volatile__( \
  854. ".set\tnoreorder\n\t" \
  855. __MODULE_JAL(__copy_user) \
  856. ".set\tnoat\n\t" \
  857. __UA_ADDU "\t$1, %1, %2\n\t" \
  858. ".set\tat\n\t" \
  859. ".set\treorder" \
  860. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  861. : \
  862. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  863. DADDI_SCRATCH, "memory"); \
  864. __cu_len_r; \
  865. })
  866. #define __invoke_copy_from_kernel(to, from, n) \
  867. __invoke_copy_from_user(to, from, n)
  868. /* For userland <-> userland operations */
  869. #define ___invoke_copy_in_user(to, from, n) \
  870. __invoke_copy_from_user(to, from, n)
  871. /* For kernel <-> kernel operations */
  872. #define ___invoke_copy_in_kernel(to, from, n) \
  873. __invoke_copy_from_user(to, from, n)
  874. #define __invoke_copy_from_user_inatomic(to, from, n) \
  875. ({ \
  876. register void *__cu_to_r __asm__("$4"); \
  877. register const void __user *__cu_from_r __asm__("$5"); \
  878. register long __cu_len_r __asm__("$6"); \
  879. \
  880. __cu_to_r = (to); \
  881. __cu_from_r = (from); \
  882. __cu_len_r = (n); \
  883. __asm__ __volatile__( \
  884. ".set\tnoreorder\n\t" \
  885. __MODULE_JAL(__copy_user_inatomic) \
  886. ".set\tnoat\n\t" \
  887. __UA_ADDU "\t$1, %1, %2\n\t" \
  888. ".set\tat\n\t" \
  889. ".set\treorder" \
  890. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  891. : \
  892. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  893. DADDI_SCRATCH, "memory"); \
  894. __cu_len_r; \
  895. })
  896. #define __invoke_copy_from_kernel_inatomic(to, from, n) \
  897. __invoke_copy_from_user_inatomic(to, from, n) \
  898. #else
  899. /* EVA specific functions */
  900. extern size_t __copy_user_inatomic_eva(void *__to, const void *__from,
  901. size_t __n);
  902. extern size_t __copy_from_user_eva(void *__to, const void *__from,
  903. size_t __n);
  904. extern size_t __copy_to_user_eva(void *__to, const void *__from,
  905. size_t __n);
  906. extern size_t __copy_in_user_eva(void *__to, const void *__from, size_t __n);
  907. #define __invoke_copy_from_user_eva_generic(to, from, n, func_ptr) \
  908. ({ \
  909. register void *__cu_to_r __asm__("$4"); \
  910. register const void __user *__cu_from_r __asm__("$5"); \
  911. register long __cu_len_r __asm__("$6"); \
  912. \
  913. __cu_to_r = (to); \
  914. __cu_from_r = (from); \
  915. __cu_len_r = (n); \
  916. __asm__ __volatile__( \
  917. ".set\tnoreorder\n\t" \
  918. __MODULE_JAL(func_ptr) \
  919. ".set\tnoat\n\t" \
  920. __UA_ADDU "\t$1, %1, %2\n\t" \
  921. ".set\tat\n\t" \
  922. ".set\treorder" \
  923. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  924. : \
  925. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  926. DADDI_SCRATCH, "memory"); \
  927. __cu_len_r; \
  928. })
  929. #define __invoke_copy_to_user_eva_generic(to, from, n, func_ptr) \
  930. ({ \
  931. register void *__cu_to_r __asm__("$4"); \
  932. register const void __user *__cu_from_r __asm__("$5"); \
  933. register long __cu_len_r __asm__("$6"); \
  934. \
  935. __cu_to_r = (to); \
  936. __cu_from_r = (from); \
  937. __cu_len_r = (n); \
  938. __asm__ __volatile__( \
  939. __MODULE_JAL(func_ptr) \
  940. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  941. : \
  942. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  943. DADDI_SCRATCH, "memory"); \
  944. __cu_len_r; \
  945. })
  946. /*
  947. * Source or destination address is in userland. We need to go through
  948. * the TLB
  949. */
  950. #define __invoke_copy_from_user(to, from, n) \
  951. __invoke_copy_from_user_eva_generic(to, from, n, __copy_from_user_eva)
  952. #define __invoke_copy_from_user_inatomic(to, from, n) \
  953. __invoke_copy_from_user_eva_generic(to, from, n, \
  954. __copy_user_inatomic_eva)
  955. #define __invoke_copy_to_user(to, from, n) \
  956. __invoke_copy_to_user_eva_generic(to, from, n, __copy_to_user_eva)
  957. #define ___invoke_copy_in_user(to, from, n) \
  958. __invoke_copy_from_user_eva_generic(to, from, n, __copy_in_user_eva)
  959. /*
  960. * Source or destination address in the kernel. We are not going through
  961. * the TLB
  962. */
  963. #define __invoke_copy_from_kernel(to, from, n) \
  964. __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
  965. #define __invoke_copy_from_kernel_inatomic(to, from, n) \
  966. __invoke_copy_from_user_eva_generic(to, from, n, __copy_user_inatomic)
  967. #define __invoke_copy_to_kernel(to, from, n) \
  968. __invoke_copy_to_user_eva_generic(to, from, n, __copy_user)
  969. #define ___invoke_copy_in_kernel(to, from, n) \
  970. __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
  971. #endif /* CONFIG_EVA */
  972. /*
  973. * __copy_from_user: - Copy a block of data from user space, with less checking.
  974. * @to: Destination address, in kernel space.
  975. * @from: Source address, in user space.
  976. * @n: Number of bytes to copy.
  977. *
  978. * Context: User context only. This function may sleep.
  979. *
  980. * Copy data from user space to kernel space. Caller must check
  981. * the specified block with access_ok() before calling this function.
  982. *
  983. * Returns number of bytes that could not be copied.
  984. * On success, this will be zero.
  985. *
  986. * If some data could not be copied, this function will pad the copied
  987. * data to the requested size using zero bytes.
  988. */
  989. #define __copy_from_user(to, from, n) \
  990. ({ \
  991. void *__cu_to; \
  992. const void __user *__cu_from; \
  993. long __cu_len; \
  994. \
  995. __cu_to = (to); \
  996. __cu_from = (from); \
  997. __cu_len = (n); \
  998. might_fault(); \
  999. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  1000. __cu_len); \
  1001. __cu_len; \
  1002. })
  1003. /*
  1004. * copy_from_user: - Copy a block of data from user space.
  1005. * @to: Destination address, in kernel space.
  1006. * @from: Source address, in user space.
  1007. * @n: Number of bytes to copy.
  1008. *
  1009. * Context: User context only. This function may sleep.
  1010. *
  1011. * Copy data from user space to kernel space.
  1012. *
  1013. * Returns number of bytes that could not be copied.
  1014. * On success, this will be zero.
  1015. *
  1016. * If some data could not be copied, this function will pad the copied
  1017. * data to the requested size using zero bytes.
  1018. */
  1019. #define copy_from_user(to, from, n) \
  1020. ({ \
  1021. void *__cu_to; \
  1022. const void __user *__cu_from; \
  1023. long __cu_len; \
  1024. \
  1025. __cu_to = (to); \
  1026. __cu_from = (from); \
  1027. __cu_len = (n); \
  1028. if (segment_eq(get_fs(), get_ds())) { \
  1029. __cu_len = __invoke_copy_from_kernel(__cu_to, \
  1030. __cu_from, \
  1031. __cu_len); \
  1032. } else { \
  1033. if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
  1034. might_fault(); \
  1035. __cu_len = __invoke_copy_from_user(__cu_to, \
  1036. __cu_from, \
  1037. __cu_len); \
  1038. } \
  1039. } \
  1040. __cu_len; \
  1041. })
  1042. #define __copy_in_user(to, from, n) \
  1043. ({ \
  1044. void __user *__cu_to; \
  1045. const void __user *__cu_from; \
  1046. long __cu_len; \
  1047. \
  1048. __cu_to = (to); \
  1049. __cu_from = (from); \
  1050. __cu_len = (n); \
  1051. if (segment_eq(get_fs(), get_ds())) { \
  1052. __cu_len = ___invoke_copy_in_kernel(__cu_to, __cu_from, \
  1053. __cu_len); \
  1054. } else { \
  1055. might_fault(); \
  1056. __cu_len = ___invoke_copy_in_user(__cu_to, __cu_from, \
  1057. __cu_len); \
  1058. } \
  1059. __cu_len; \
  1060. })
  1061. #define copy_in_user(to, from, n) \
  1062. ({ \
  1063. void __user *__cu_to; \
  1064. const void __user *__cu_from; \
  1065. long __cu_len; \
  1066. \
  1067. __cu_to = (to); \
  1068. __cu_from = (from); \
  1069. __cu_len = (n); \
  1070. if (segment_eq(get_fs(), get_ds())) { \
  1071. __cu_len = ___invoke_copy_in_kernel(__cu_to,__cu_from, \
  1072. __cu_len); \
  1073. } else { \
  1074. if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&\
  1075. access_ok(VERIFY_WRITE, __cu_to, __cu_len))) {\
  1076. might_fault(); \
  1077. __cu_len = ___invoke_copy_in_user(__cu_to, \
  1078. __cu_from, \
  1079. __cu_len); \
  1080. } \
  1081. } \
  1082. __cu_len; \
  1083. })
  1084. /*
  1085. * __clear_user: - Zero a block of memory in user space, with less checking.
  1086. * @to: Destination address, in user space.
  1087. * @n: Number of bytes to zero.
  1088. *
  1089. * Zero a block of memory in user space. Caller must check
  1090. * the specified block with access_ok() before calling this function.
  1091. *
  1092. * Returns number of bytes that could not be cleared.
  1093. * On success, this will be zero.
  1094. */
  1095. static inline __kernel_size_t
  1096. __clear_user(void __user *addr, __kernel_size_t size)
  1097. {
  1098. __kernel_size_t res;
  1099. might_fault();
  1100. __asm__ __volatile__(
  1101. "move\t$4, %1\n\t"
  1102. "move\t$5, $0\n\t"
  1103. "move\t$6, %2\n\t"
  1104. __MODULE_JAL(__bzero)
  1105. "move\t%0, $6"
  1106. : "=r" (res)
  1107. : "r" (addr), "r" (size)
  1108. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  1109. return res;
  1110. }
  1111. #define clear_user(addr,n) \
  1112. ({ \
  1113. void __user * __cl_addr = (addr); \
  1114. unsigned long __cl_size = (n); \
  1115. if (__cl_size && access_ok(VERIFY_WRITE, \
  1116. __cl_addr, __cl_size)) \
  1117. __cl_size = __clear_user(__cl_addr, __cl_size); \
  1118. __cl_size; \
  1119. })
  1120. /*
  1121. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  1122. * @dst: Destination address, in kernel space. This buffer must be at
  1123. * least @count bytes long.
  1124. * @src: Source address, in user space.
  1125. * @count: Maximum number of bytes to copy, including the trailing NUL.
  1126. *
  1127. * Copies a NUL-terminated string from userspace to kernel space.
  1128. * Caller must check the specified block with access_ok() before calling
  1129. * this function.
  1130. *
  1131. * On success, returns the length of the string (not including the trailing
  1132. * NUL).
  1133. *
  1134. * If access to userspace fails, returns -EFAULT (some data may have been
  1135. * copied).
  1136. *
  1137. * If @count is smaller than the length of the string, copies @count bytes
  1138. * and returns @count.
  1139. */
  1140. static inline long
  1141. __strncpy_from_user(char *__to, const char __user *__from, long __len)
  1142. {
  1143. long res;
  1144. if (segment_eq(get_fs(), get_ds())) {
  1145. __asm__ __volatile__(
  1146. "move\t$4, %1\n\t"
  1147. "move\t$5, %2\n\t"
  1148. "move\t$6, %3\n\t"
  1149. __MODULE_JAL(__strncpy_from_kernel_nocheck_asm)
  1150. "move\t%0, $2"
  1151. : "=r" (res)
  1152. : "r" (__to), "r" (__from), "r" (__len)
  1153. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1154. } else {
  1155. might_fault();
  1156. __asm__ __volatile__(
  1157. "move\t$4, %1\n\t"
  1158. "move\t$5, %2\n\t"
  1159. "move\t$6, %3\n\t"
  1160. __MODULE_JAL(__strncpy_from_user_nocheck_asm)
  1161. "move\t%0, $2"
  1162. : "=r" (res)
  1163. : "r" (__to), "r" (__from), "r" (__len)
  1164. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1165. }
  1166. return res;
  1167. }
  1168. /*
  1169. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  1170. * @dst: Destination address, in kernel space. This buffer must be at
  1171. * least @count bytes long.
  1172. * @src: Source address, in user space.
  1173. * @count: Maximum number of bytes to copy, including the trailing NUL.
  1174. *
  1175. * Copies a NUL-terminated string from userspace to kernel space.
  1176. *
  1177. * On success, returns the length of the string (not including the trailing
  1178. * NUL).
  1179. *
  1180. * If access to userspace fails, returns -EFAULT (some data may have been
  1181. * copied).
  1182. *
  1183. * If @count is smaller than the length of the string, copies @count bytes
  1184. * and returns @count.
  1185. */
  1186. static inline long
  1187. strncpy_from_user(char *__to, const char __user *__from, long __len)
  1188. {
  1189. long res;
  1190. if (segment_eq(get_fs(), get_ds())) {
  1191. __asm__ __volatile__(
  1192. "move\t$4, %1\n\t"
  1193. "move\t$5, %2\n\t"
  1194. "move\t$6, %3\n\t"
  1195. __MODULE_JAL(__strncpy_from_kernel_asm)
  1196. "move\t%0, $2"
  1197. : "=r" (res)
  1198. : "r" (__to), "r" (__from), "r" (__len)
  1199. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1200. } else {
  1201. might_fault();
  1202. __asm__ __volatile__(
  1203. "move\t$4, %1\n\t"
  1204. "move\t$5, %2\n\t"
  1205. "move\t$6, %3\n\t"
  1206. __MODULE_JAL(__strncpy_from_user_asm)
  1207. "move\t%0, $2"
  1208. : "=r" (res)
  1209. : "r" (__to), "r" (__from), "r" (__len)
  1210. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1211. }
  1212. return res;
  1213. }
  1214. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  1215. static inline long __strlen_user(const char __user *s)
  1216. {
  1217. long res;
  1218. if (segment_eq(get_fs(), get_ds())) {
  1219. __asm__ __volatile__(
  1220. "move\t$4, %1\n\t"
  1221. __MODULE_JAL(__strlen_kernel_nocheck_asm)
  1222. "move\t%0, $2"
  1223. : "=r" (res)
  1224. : "r" (s)
  1225. : "$2", "$4", __UA_t0, "$31");
  1226. } else {
  1227. might_fault();
  1228. __asm__ __volatile__(
  1229. "move\t$4, %1\n\t"
  1230. __MODULE_JAL(__strlen_user_nocheck_asm)
  1231. "move\t%0, $2"
  1232. : "=r" (res)
  1233. : "r" (s)
  1234. : "$2", "$4", __UA_t0, "$31");
  1235. }
  1236. return res;
  1237. }
  1238. /*
  1239. * strlen_user: - Get the size of a string in user space.
  1240. * @str: The string to measure.
  1241. *
  1242. * Context: User context only. This function may sleep.
  1243. *
  1244. * Get the size of a NUL-terminated string in user space.
  1245. *
  1246. * Returns the size of the string INCLUDING the terminating NUL.
  1247. * On exception, returns 0.
  1248. *
  1249. * If there is a limit on the length of a valid string, you may wish to
  1250. * consider using strnlen_user() instead.
  1251. */
  1252. static inline long strlen_user(const char __user *s)
  1253. {
  1254. long res;
  1255. if (segment_eq(get_fs(), get_ds())) {
  1256. __asm__ __volatile__(
  1257. "move\t$4, %1\n\t"
  1258. __MODULE_JAL(__strlen_kernel_asm)
  1259. "move\t%0, $2"
  1260. : "=r" (res)
  1261. : "r" (s)
  1262. : "$2", "$4", __UA_t0, "$31");
  1263. } else {
  1264. might_fault();
  1265. __asm__ __volatile__(
  1266. "move\t$4, %1\n\t"
  1267. __MODULE_JAL(__strlen_kernel_asm)
  1268. "move\t%0, $2"
  1269. : "=r" (res)
  1270. : "r" (s)
  1271. : "$2", "$4", __UA_t0, "$31");
  1272. }
  1273. return res;
  1274. }
  1275. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  1276. static inline long __strnlen_user(const char __user *s, long n)
  1277. {
  1278. long res;
  1279. if (segment_eq(get_fs(), get_ds())) {
  1280. __asm__ __volatile__(
  1281. "move\t$4, %1\n\t"
  1282. "move\t$5, %2\n\t"
  1283. __MODULE_JAL(__strnlen_kernel_nocheck_asm)
  1284. "move\t%0, $2"
  1285. : "=r" (res)
  1286. : "r" (s), "r" (n)
  1287. : "$2", "$4", "$5", __UA_t0, "$31");
  1288. } else {
  1289. might_fault();
  1290. __asm__ __volatile__(
  1291. "move\t$4, %1\n\t"
  1292. "move\t$5, %2\n\t"
  1293. __MODULE_JAL(__strnlen_user_nocheck_asm)
  1294. "move\t%0, $2"
  1295. : "=r" (res)
  1296. : "r" (s), "r" (n)
  1297. : "$2", "$4", "$5", __UA_t0, "$31");
  1298. }
  1299. return res;
  1300. }
  1301. /*
  1302. * strlen_user: - Get the size of a string in user space.
  1303. * @str: The string to measure.
  1304. *
  1305. * Context: User context only. This function may sleep.
  1306. *
  1307. * Get the size of a NUL-terminated string in user space.
  1308. *
  1309. * Returns the size of the string INCLUDING the terminating NUL.
  1310. * On exception, returns 0.
  1311. *
  1312. * If there is a limit on the length of a valid string, you may wish to
  1313. * consider using strnlen_user() instead.
  1314. */
  1315. static inline long strnlen_user(const char __user *s, long n)
  1316. {
  1317. long res;
  1318. might_fault();
  1319. if (segment_eq(get_fs(), get_ds())) {
  1320. __asm__ __volatile__(
  1321. "move\t$4, %1\n\t"
  1322. "move\t$5, %2\n\t"
  1323. __MODULE_JAL(__strnlen_kernel_asm)
  1324. "move\t%0, $2"
  1325. : "=r" (res)
  1326. : "r" (s), "r" (n)
  1327. : "$2", "$4", "$5", __UA_t0, "$31");
  1328. } else {
  1329. __asm__ __volatile__(
  1330. "move\t$4, %1\n\t"
  1331. "move\t$5, %2\n\t"
  1332. __MODULE_JAL(__strnlen_user_asm)
  1333. "move\t%0, $2"
  1334. : "=r" (res)
  1335. : "r" (s), "r" (n)
  1336. : "$2", "$4", "$5", __UA_t0, "$31");
  1337. }
  1338. return res;
  1339. }
  1340. struct exception_table_entry
  1341. {
  1342. unsigned long insn;
  1343. unsigned long nextinsn;
  1344. };
  1345. extern int fixup_exception(struct pt_regs *regs);
  1346. #endif /* _ASM_UACCESS_H */