uaccess.h 42 KB

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