uaccess.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507
  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. if (eva_kernel_access()) {
  1159. __asm__ __volatile__(
  1160. "move\t$4, %1\n\t"
  1161. "move\t$5, $0\n\t"
  1162. "move\t$6, %2\n\t"
  1163. __MODULE_JAL(__bzero_kernel)
  1164. "move\t%0, $6"
  1165. : "=r" (res)
  1166. : "r" (addr), "r" (size)
  1167. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  1168. } else {
  1169. might_fault();
  1170. __asm__ __volatile__(
  1171. "move\t$4, %1\n\t"
  1172. "move\t$5, $0\n\t"
  1173. "move\t$6, %2\n\t"
  1174. __MODULE_JAL(__bzero)
  1175. "move\t%0, $6"
  1176. : "=r" (res)
  1177. : "r" (addr), "r" (size)
  1178. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  1179. }
  1180. return res;
  1181. }
  1182. #define clear_user(addr,n) \
  1183. ({ \
  1184. void __user * __cl_addr = (addr); \
  1185. unsigned long __cl_size = (n); \
  1186. if (__cl_size && access_ok(VERIFY_WRITE, \
  1187. __cl_addr, __cl_size)) \
  1188. __cl_size = __clear_user(__cl_addr, __cl_size); \
  1189. __cl_size; \
  1190. })
  1191. /*
  1192. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  1193. * @dst: Destination address, in kernel space. This buffer must be at
  1194. * least @count bytes long.
  1195. * @src: Source address, in user space.
  1196. * @count: Maximum number of bytes to copy, including the trailing NUL.
  1197. *
  1198. * Copies a NUL-terminated string from userspace to kernel space.
  1199. * Caller must check the specified block with access_ok() before calling
  1200. * this function.
  1201. *
  1202. * On success, returns the length of the string (not including the trailing
  1203. * NUL).
  1204. *
  1205. * If access to userspace fails, returns -EFAULT (some data may have been
  1206. * copied).
  1207. *
  1208. * If @count is smaller than the length of the string, copies @count bytes
  1209. * and returns @count.
  1210. */
  1211. static inline long
  1212. __strncpy_from_user(char *__to, const char __user *__from, long __len)
  1213. {
  1214. long res;
  1215. if (eva_kernel_access()) {
  1216. __asm__ __volatile__(
  1217. "move\t$4, %1\n\t"
  1218. "move\t$5, %2\n\t"
  1219. "move\t$6, %3\n\t"
  1220. __MODULE_JAL(__strncpy_from_kernel_nocheck_asm)
  1221. "move\t%0, $2"
  1222. : "=r" (res)
  1223. : "r" (__to), "r" (__from), "r" (__len)
  1224. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1225. } else {
  1226. might_fault();
  1227. __asm__ __volatile__(
  1228. "move\t$4, %1\n\t"
  1229. "move\t$5, %2\n\t"
  1230. "move\t$6, %3\n\t"
  1231. __MODULE_JAL(__strncpy_from_user_nocheck_asm)
  1232. "move\t%0, $2"
  1233. : "=r" (res)
  1234. : "r" (__to), "r" (__from), "r" (__len)
  1235. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1236. }
  1237. return res;
  1238. }
  1239. /*
  1240. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  1241. * @dst: Destination address, in kernel space. This buffer must be at
  1242. * least @count bytes long.
  1243. * @src: Source address, in user space.
  1244. * @count: Maximum number of bytes to copy, including the trailing NUL.
  1245. *
  1246. * Copies a NUL-terminated string from userspace to kernel space.
  1247. *
  1248. * On success, returns the length of the string (not including the trailing
  1249. * NUL).
  1250. *
  1251. * If access to userspace fails, returns -EFAULT (some data may have been
  1252. * copied).
  1253. *
  1254. * If @count is smaller than the length of the string, copies @count bytes
  1255. * and returns @count.
  1256. */
  1257. static inline long
  1258. strncpy_from_user(char *__to, const char __user *__from, long __len)
  1259. {
  1260. long res;
  1261. if (eva_kernel_access()) {
  1262. __asm__ __volatile__(
  1263. "move\t$4, %1\n\t"
  1264. "move\t$5, %2\n\t"
  1265. "move\t$6, %3\n\t"
  1266. __MODULE_JAL(__strncpy_from_kernel_asm)
  1267. "move\t%0, $2"
  1268. : "=r" (res)
  1269. : "r" (__to), "r" (__from), "r" (__len)
  1270. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1271. } else {
  1272. might_fault();
  1273. __asm__ __volatile__(
  1274. "move\t$4, %1\n\t"
  1275. "move\t$5, %2\n\t"
  1276. "move\t$6, %3\n\t"
  1277. __MODULE_JAL(__strncpy_from_user_asm)
  1278. "move\t%0, $2"
  1279. : "=r" (res)
  1280. : "r" (__to), "r" (__from), "r" (__len)
  1281. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1282. }
  1283. return res;
  1284. }
  1285. /*
  1286. * strlen_user: - Get the size of a string in user space.
  1287. * @str: The string to measure.
  1288. *
  1289. * Context: User context only. This function may sleep if pagefaults are
  1290. * enabled.
  1291. *
  1292. * Get the size of a NUL-terminated string in user space.
  1293. *
  1294. * Returns the size of the string INCLUDING the terminating NUL.
  1295. * On exception, returns 0.
  1296. *
  1297. * If there is a limit on the length of a valid string, you may wish to
  1298. * consider using strnlen_user() instead.
  1299. */
  1300. static inline long strlen_user(const char __user *s)
  1301. {
  1302. long res;
  1303. if (eva_kernel_access()) {
  1304. __asm__ __volatile__(
  1305. "move\t$4, %1\n\t"
  1306. __MODULE_JAL(__strlen_kernel_asm)
  1307. "move\t%0, $2"
  1308. : "=r" (res)
  1309. : "r" (s)
  1310. : "$2", "$4", __UA_t0, "$31");
  1311. } else {
  1312. might_fault();
  1313. __asm__ __volatile__(
  1314. "move\t$4, %1\n\t"
  1315. __MODULE_JAL(__strlen_user_asm)
  1316. "move\t%0, $2"
  1317. : "=r" (res)
  1318. : "r" (s)
  1319. : "$2", "$4", __UA_t0, "$31");
  1320. }
  1321. return res;
  1322. }
  1323. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  1324. static inline long __strnlen_user(const char __user *s, long n)
  1325. {
  1326. long res;
  1327. if (eva_kernel_access()) {
  1328. __asm__ __volatile__(
  1329. "move\t$4, %1\n\t"
  1330. "move\t$5, %2\n\t"
  1331. __MODULE_JAL(__strnlen_kernel_nocheck_asm)
  1332. "move\t%0, $2"
  1333. : "=r" (res)
  1334. : "r" (s), "r" (n)
  1335. : "$2", "$4", "$5", __UA_t0, "$31");
  1336. } else {
  1337. might_fault();
  1338. __asm__ __volatile__(
  1339. "move\t$4, %1\n\t"
  1340. "move\t$5, %2\n\t"
  1341. __MODULE_JAL(__strnlen_user_nocheck_asm)
  1342. "move\t%0, $2"
  1343. : "=r" (res)
  1344. : "r" (s), "r" (n)
  1345. : "$2", "$4", "$5", __UA_t0, "$31");
  1346. }
  1347. return res;
  1348. }
  1349. /*
  1350. * strnlen_user: - Get the size of a string in user space.
  1351. * @str: The string to measure.
  1352. *
  1353. * Context: User context only. This function may sleep if pagefaults are
  1354. * enabled.
  1355. *
  1356. * Get the size of a NUL-terminated string in user space.
  1357. *
  1358. * Returns the size of the string INCLUDING the terminating NUL.
  1359. * On exception, returns 0.
  1360. * If the string is too long, returns a value greater than @n.
  1361. */
  1362. static inline long strnlen_user(const char __user *s, long n)
  1363. {
  1364. long res;
  1365. might_fault();
  1366. if (eva_kernel_access()) {
  1367. __asm__ __volatile__(
  1368. "move\t$4, %1\n\t"
  1369. "move\t$5, %2\n\t"
  1370. __MODULE_JAL(__strnlen_kernel_asm)
  1371. "move\t%0, $2"
  1372. : "=r" (res)
  1373. : "r" (s), "r" (n)
  1374. : "$2", "$4", "$5", __UA_t0, "$31");
  1375. } else {
  1376. __asm__ __volatile__(
  1377. "move\t$4, %1\n\t"
  1378. "move\t$5, %2\n\t"
  1379. __MODULE_JAL(__strnlen_user_asm)
  1380. "move\t%0, $2"
  1381. : "=r" (res)
  1382. : "r" (s), "r" (n)
  1383. : "$2", "$4", "$5", __UA_t0, "$31");
  1384. }
  1385. return res;
  1386. }
  1387. #endif /* _ASM_UACCESS_H */