uaccess.h 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489
  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. might_fault(); \
  792. if (eva_kernel_access()) \
  793. __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
  794. __cu_len); \
  795. else \
  796. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  797. __cu_len); \
  798. __cu_len; \
  799. })
  800. extern size_t __copy_user_inatomic(void *__to, const void *__from, size_t __n);
  801. #define __copy_to_user_inatomic(to, from, n) \
  802. ({ \
  803. void __user *__cu_to; \
  804. const void *__cu_from; \
  805. long __cu_len; \
  806. \
  807. __cu_to = (to); \
  808. __cu_from = (from); \
  809. __cu_len = (n); \
  810. if (eva_kernel_access()) \
  811. __cu_len = __invoke_copy_to_kernel(__cu_to, __cu_from, \
  812. __cu_len); \
  813. else \
  814. __cu_len = __invoke_copy_to_user(__cu_to, __cu_from, \
  815. __cu_len); \
  816. __cu_len; \
  817. })
  818. #define __copy_from_user_inatomic(to, from, n) \
  819. ({ \
  820. void *__cu_to; \
  821. const void __user *__cu_from; \
  822. long __cu_len; \
  823. \
  824. __cu_to = (to); \
  825. __cu_from = (from); \
  826. __cu_len = (n); \
  827. if (eva_kernel_access()) \
  828. __cu_len = __invoke_copy_from_kernel_inatomic(__cu_to, \
  829. __cu_from,\
  830. __cu_len);\
  831. else \
  832. __cu_len = __invoke_copy_from_user_inatomic(__cu_to, \
  833. __cu_from, \
  834. __cu_len); \
  835. __cu_len; \
  836. })
  837. /*
  838. * copy_to_user: - Copy a block of data into user space.
  839. * @to: Destination address, in user space.
  840. * @from: Source address, in kernel space.
  841. * @n: Number of bytes to copy.
  842. *
  843. * Context: User context only. This function may sleep if pagefaults are
  844. * enabled.
  845. *
  846. * Copy data from kernel space to user space.
  847. *
  848. * Returns number of bytes that could not be copied.
  849. * On success, this will be zero.
  850. */
  851. #define copy_to_user(to, from, n) \
  852. ({ \
  853. void __user *__cu_to; \
  854. const void *__cu_from; \
  855. long __cu_len; \
  856. \
  857. __cu_to = (to); \
  858. __cu_from = (from); \
  859. __cu_len = (n); \
  860. if (eva_kernel_access()) { \
  861. __cu_len = __invoke_copy_to_kernel(__cu_to, \
  862. __cu_from, \
  863. __cu_len); \
  864. } else { \
  865. if (access_ok(VERIFY_WRITE, __cu_to, __cu_len)) { \
  866. might_fault(); \
  867. __cu_len = __invoke_copy_to_user(__cu_to, \
  868. __cu_from, \
  869. __cu_len); \
  870. } \
  871. } \
  872. __cu_len; \
  873. })
  874. #ifndef CONFIG_EVA
  875. #define __invoke_copy_from_user(to, from, n) \
  876. ({ \
  877. register void *__cu_to_r __asm__("$4"); \
  878. register const void __user *__cu_from_r __asm__("$5"); \
  879. register long __cu_len_r __asm__("$6"); \
  880. \
  881. __cu_to_r = (to); \
  882. __cu_from_r = (from); \
  883. __cu_len_r = (n); \
  884. __asm__ __volatile__( \
  885. ".set\tnoreorder\n\t" \
  886. __MODULE_JAL(__copy_user) \
  887. ".set\tnoat\n\t" \
  888. __UA_ADDU "\t$1, %1, %2\n\t" \
  889. ".set\tat\n\t" \
  890. ".set\treorder" \
  891. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  892. : \
  893. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  894. DADDI_SCRATCH, "memory"); \
  895. __cu_len_r; \
  896. })
  897. #define __invoke_copy_from_kernel(to, from, n) \
  898. __invoke_copy_from_user(to, from, n)
  899. /* For userland <-> userland operations */
  900. #define ___invoke_copy_in_user(to, from, n) \
  901. __invoke_copy_from_user(to, from, n)
  902. /* For kernel <-> kernel operations */
  903. #define ___invoke_copy_in_kernel(to, from, n) \
  904. __invoke_copy_from_user(to, from, n)
  905. #define __invoke_copy_from_user_inatomic(to, from, n) \
  906. ({ \
  907. register void *__cu_to_r __asm__("$4"); \
  908. register const void __user *__cu_from_r __asm__("$5"); \
  909. register long __cu_len_r __asm__("$6"); \
  910. \
  911. __cu_to_r = (to); \
  912. __cu_from_r = (from); \
  913. __cu_len_r = (n); \
  914. __asm__ __volatile__( \
  915. ".set\tnoreorder\n\t" \
  916. __MODULE_JAL(__copy_user_inatomic) \
  917. ".set\tnoat\n\t" \
  918. __UA_ADDU "\t$1, %1, %2\n\t" \
  919. ".set\tat\n\t" \
  920. ".set\treorder" \
  921. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  922. : \
  923. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  924. DADDI_SCRATCH, "memory"); \
  925. __cu_len_r; \
  926. })
  927. #define __invoke_copy_from_kernel_inatomic(to, from, n) \
  928. __invoke_copy_from_user_inatomic(to, from, n) \
  929. #else
  930. /* EVA specific functions */
  931. extern size_t __copy_user_inatomic_eva(void *__to, const void *__from,
  932. size_t __n);
  933. extern size_t __copy_from_user_eva(void *__to, const void *__from,
  934. size_t __n);
  935. extern size_t __copy_to_user_eva(void *__to, const void *__from,
  936. size_t __n);
  937. extern size_t __copy_in_user_eva(void *__to, const void *__from, size_t __n);
  938. #define __invoke_copy_from_user_eva_generic(to, from, n, func_ptr) \
  939. ({ \
  940. register void *__cu_to_r __asm__("$4"); \
  941. register const void __user *__cu_from_r __asm__("$5"); \
  942. register long __cu_len_r __asm__("$6"); \
  943. \
  944. __cu_to_r = (to); \
  945. __cu_from_r = (from); \
  946. __cu_len_r = (n); \
  947. __asm__ __volatile__( \
  948. ".set\tnoreorder\n\t" \
  949. __MODULE_JAL(func_ptr) \
  950. ".set\tnoat\n\t" \
  951. __UA_ADDU "\t$1, %1, %2\n\t" \
  952. ".set\tat\n\t" \
  953. ".set\treorder" \
  954. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  955. : \
  956. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  957. DADDI_SCRATCH, "memory"); \
  958. __cu_len_r; \
  959. })
  960. #define __invoke_copy_to_user_eva_generic(to, from, n, func_ptr) \
  961. ({ \
  962. register void *__cu_to_r __asm__("$4"); \
  963. register const void __user *__cu_from_r __asm__("$5"); \
  964. register long __cu_len_r __asm__("$6"); \
  965. \
  966. __cu_to_r = (to); \
  967. __cu_from_r = (from); \
  968. __cu_len_r = (n); \
  969. __asm__ __volatile__( \
  970. __MODULE_JAL(func_ptr) \
  971. : "+r" (__cu_to_r), "+r" (__cu_from_r), "+r" (__cu_len_r) \
  972. : \
  973. : "$8", "$9", "$10", "$11", "$12", "$14", "$15", "$24", "$31", \
  974. DADDI_SCRATCH, "memory"); \
  975. __cu_len_r; \
  976. })
  977. /*
  978. * Source or destination address is in userland. We need to go through
  979. * the TLB
  980. */
  981. #define __invoke_copy_from_user(to, from, n) \
  982. __invoke_copy_from_user_eva_generic(to, from, n, __copy_from_user_eva)
  983. #define __invoke_copy_from_user_inatomic(to, from, n) \
  984. __invoke_copy_from_user_eva_generic(to, from, n, \
  985. __copy_user_inatomic_eva)
  986. #define __invoke_copy_to_user(to, from, n) \
  987. __invoke_copy_to_user_eva_generic(to, from, n, __copy_to_user_eva)
  988. #define ___invoke_copy_in_user(to, from, n) \
  989. __invoke_copy_from_user_eva_generic(to, from, n, __copy_in_user_eva)
  990. /*
  991. * Source or destination address in the kernel. We are not going through
  992. * the TLB
  993. */
  994. #define __invoke_copy_from_kernel(to, from, n) \
  995. __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
  996. #define __invoke_copy_from_kernel_inatomic(to, from, n) \
  997. __invoke_copy_from_user_eva_generic(to, from, n, __copy_user_inatomic)
  998. #define __invoke_copy_to_kernel(to, from, n) \
  999. __invoke_copy_to_user_eva_generic(to, from, n, __copy_user)
  1000. #define ___invoke_copy_in_kernel(to, from, n) \
  1001. __invoke_copy_from_user_eva_generic(to, from, n, __copy_user)
  1002. #endif /* CONFIG_EVA */
  1003. /*
  1004. * __copy_from_user: - Copy a block of data from user space, with less checking.
  1005. * @to: Destination address, in kernel space.
  1006. * @from: Source address, in user space.
  1007. * @n: Number of bytes to copy.
  1008. *
  1009. * Context: User context only. This function may sleep if pagefaults are
  1010. * enabled.
  1011. *
  1012. * Copy data from user space to kernel space. Caller must check
  1013. * the specified block with access_ok() before calling this function.
  1014. *
  1015. * Returns number of bytes that could not be copied.
  1016. * On success, this will be zero.
  1017. *
  1018. * If some data could not be copied, this function will pad the copied
  1019. * data to the requested size using zero bytes.
  1020. */
  1021. #define __copy_from_user(to, from, n) \
  1022. ({ \
  1023. void *__cu_to; \
  1024. const void __user *__cu_from; \
  1025. long __cu_len; \
  1026. \
  1027. __cu_to = (to); \
  1028. __cu_from = (from); \
  1029. __cu_len = (n); \
  1030. if (eva_kernel_access()) { \
  1031. __cu_len = __invoke_copy_from_kernel(__cu_to, \
  1032. __cu_from, \
  1033. __cu_len); \
  1034. } else { \
  1035. might_fault(); \
  1036. __cu_len = __invoke_copy_from_user(__cu_to, __cu_from, \
  1037. __cu_len); \
  1038. } \
  1039. __cu_len; \
  1040. })
  1041. /*
  1042. * copy_from_user: - Copy a block of data from user space.
  1043. * @to: Destination address, in kernel space.
  1044. * @from: Source address, in user space.
  1045. * @n: Number of bytes to copy.
  1046. *
  1047. * Context: User context only. This function may sleep if pagefaults are
  1048. * enabled.
  1049. *
  1050. * Copy data from user space to kernel space.
  1051. *
  1052. * Returns number of bytes that could not be copied.
  1053. * On success, this will be zero.
  1054. *
  1055. * If some data could not be copied, this function will pad the copied
  1056. * data to the requested size using zero bytes.
  1057. */
  1058. #define copy_from_user(to, from, n) \
  1059. ({ \
  1060. void *__cu_to; \
  1061. const void __user *__cu_from; \
  1062. long __cu_len; \
  1063. \
  1064. __cu_to = (to); \
  1065. __cu_from = (from); \
  1066. __cu_len = (n); \
  1067. if (eva_kernel_access()) { \
  1068. __cu_len = __invoke_copy_from_kernel(__cu_to, \
  1069. __cu_from, \
  1070. __cu_len); \
  1071. } else { \
  1072. if (access_ok(VERIFY_READ, __cu_from, __cu_len)) { \
  1073. might_fault(); \
  1074. __cu_len = __invoke_copy_from_user(__cu_to, \
  1075. __cu_from, \
  1076. __cu_len); \
  1077. } else { \
  1078. memset(__cu_to, 0, __cu_len); \
  1079. } \
  1080. } \
  1081. __cu_len; \
  1082. })
  1083. #define __copy_in_user(to, from, n) \
  1084. ({ \
  1085. void __user *__cu_to; \
  1086. const void __user *__cu_from; \
  1087. long __cu_len; \
  1088. \
  1089. __cu_to = (to); \
  1090. __cu_from = (from); \
  1091. __cu_len = (n); \
  1092. if (eva_kernel_access()) { \
  1093. __cu_len = ___invoke_copy_in_kernel(__cu_to, __cu_from, \
  1094. __cu_len); \
  1095. } else { \
  1096. might_fault(); \
  1097. __cu_len = ___invoke_copy_in_user(__cu_to, __cu_from, \
  1098. __cu_len); \
  1099. } \
  1100. __cu_len; \
  1101. })
  1102. #define copy_in_user(to, from, n) \
  1103. ({ \
  1104. void __user *__cu_to; \
  1105. const void __user *__cu_from; \
  1106. long __cu_len; \
  1107. \
  1108. __cu_to = (to); \
  1109. __cu_from = (from); \
  1110. __cu_len = (n); \
  1111. if (eva_kernel_access()) { \
  1112. __cu_len = ___invoke_copy_in_kernel(__cu_to,__cu_from, \
  1113. __cu_len); \
  1114. } else { \
  1115. if (likely(access_ok(VERIFY_READ, __cu_from, __cu_len) &&\
  1116. access_ok(VERIFY_WRITE, __cu_to, __cu_len))) {\
  1117. might_fault(); \
  1118. __cu_len = ___invoke_copy_in_user(__cu_to, \
  1119. __cu_from, \
  1120. __cu_len); \
  1121. } \
  1122. } \
  1123. __cu_len; \
  1124. })
  1125. /*
  1126. * __clear_user: - Zero a block of memory in user space, with less checking.
  1127. * @to: Destination address, in user space.
  1128. * @n: Number of bytes to zero.
  1129. *
  1130. * Zero a block of memory in user space. Caller must check
  1131. * the specified block with access_ok() before calling this function.
  1132. *
  1133. * Returns number of bytes that could not be cleared.
  1134. * On success, this will be zero.
  1135. */
  1136. static inline __kernel_size_t
  1137. __clear_user(void __user *addr, __kernel_size_t size)
  1138. {
  1139. __kernel_size_t res;
  1140. if (eva_kernel_access()) {
  1141. __asm__ __volatile__(
  1142. "move\t$4, %1\n\t"
  1143. "move\t$5, $0\n\t"
  1144. "move\t$6, %2\n\t"
  1145. __MODULE_JAL(__bzero_kernel)
  1146. "move\t%0, $6"
  1147. : "=r" (res)
  1148. : "r" (addr), "r" (size)
  1149. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  1150. } else {
  1151. might_fault();
  1152. __asm__ __volatile__(
  1153. "move\t$4, %1\n\t"
  1154. "move\t$5, $0\n\t"
  1155. "move\t$6, %2\n\t"
  1156. __MODULE_JAL(__bzero)
  1157. "move\t%0, $6"
  1158. : "=r" (res)
  1159. : "r" (addr), "r" (size)
  1160. : "$4", "$5", "$6", __UA_t0, __UA_t1, "$31");
  1161. }
  1162. return res;
  1163. }
  1164. #define clear_user(addr,n) \
  1165. ({ \
  1166. void __user * __cl_addr = (addr); \
  1167. unsigned long __cl_size = (n); \
  1168. if (__cl_size && access_ok(VERIFY_WRITE, \
  1169. __cl_addr, __cl_size)) \
  1170. __cl_size = __clear_user(__cl_addr, __cl_size); \
  1171. __cl_size; \
  1172. })
  1173. /*
  1174. * __strncpy_from_user: - Copy a NUL terminated string from userspace, with less checking.
  1175. * @dst: Destination address, in kernel space. This buffer must be at
  1176. * least @count bytes long.
  1177. * @src: Source address, in user space.
  1178. * @count: Maximum number of bytes to copy, including the trailing NUL.
  1179. *
  1180. * Copies a NUL-terminated string from userspace to kernel space.
  1181. * Caller must check the specified block with access_ok() before calling
  1182. * this function.
  1183. *
  1184. * On success, returns the length of the string (not including the trailing
  1185. * NUL).
  1186. *
  1187. * If access to userspace fails, returns -EFAULT (some data may have been
  1188. * copied).
  1189. *
  1190. * If @count is smaller than the length of the string, copies @count bytes
  1191. * and returns @count.
  1192. */
  1193. static inline long
  1194. __strncpy_from_user(char *__to, const char __user *__from, long __len)
  1195. {
  1196. long res;
  1197. if (eva_kernel_access()) {
  1198. __asm__ __volatile__(
  1199. "move\t$4, %1\n\t"
  1200. "move\t$5, %2\n\t"
  1201. "move\t$6, %3\n\t"
  1202. __MODULE_JAL(__strncpy_from_kernel_nocheck_asm)
  1203. "move\t%0, $2"
  1204. : "=r" (res)
  1205. : "r" (__to), "r" (__from), "r" (__len)
  1206. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1207. } else {
  1208. might_fault();
  1209. __asm__ __volatile__(
  1210. "move\t$4, %1\n\t"
  1211. "move\t$5, %2\n\t"
  1212. "move\t$6, %3\n\t"
  1213. __MODULE_JAL(__strncpy_from_user_nocheck_asm)
  1214. "move\t%0, $2"
  1215. : "=r" (res)
  1216. : "r" (__to), "r" (__from), "r" (__len)
  1217. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1218. }
  1219. return res;
  1220. }
  1221. /*
  1222. * strncpy_from_user: - Copy a NUL terminated string from userspace.
  1223. * @dst: Destination address, in kernel space. This buffer must be at
  1224. * least @count bytes long.
  1225. * @src: Source address, in user space.
  1226. * @count: Maximum number of bytes to copy, including the trailing NUL.
  1227. *
  1228. * Copies a NUL-terminated string from userspace to kernel space.
  1229. *
  1230. * On success, returns the length of the string (not including the trailing
  1231. * NUL).
  1232. *
  1233. * If access to userspace fails, returns -EFAULT (some data may have been
  1234. * copied).
  1235. *
  1236. * If @count is smaller than the length of the string, copies @count bytes
  1237. * and returns @count.
  1238. */
  1239. static inline long
  1240. strncpy_from_user(char *__to, const char __user *__from, long __len)
  1241. {
  1242. long res;
  1243. if (eva_kernel_access()) {
  1244. __asm__ __volatile__(
  1245. "move\t$4, %1\n\t"
  1246. "move\t$5, %2\n\t"
  1247. "move\t$6, %3\n\t"
  1248. __MODULE_JAL(__strncpy_from_kernel_asm)
  1249. "move\t%0, $2"
  1250. : "=r" (res)
  1251. : "r" (__to), "r" (__from), "r" (__len)
  1252. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1253. } else {
  1254. might_fault();
  1255. __asm__ __volatile__(
  1256. "move\t$4, %1\n\t"
  1257. "move\t$5, %2\n\t"
  1258. "move\t$6, %3\n\t"
  1259. __MODULE_JAL(__strncpy_from_user_asm)
  1260. "move\t%0, $2"
  1261. : "=r" (res)
  1262. : "r" (__to), "r" (__from), "r" (__len)
  1263. : "$2", "$3", "$4", "$5", "$6", __UA_t0, "$31", "memory");
  1264. }
  1265. return res;
  1266. }
  1267. /*
  1268. * strlen_user: - Get the size of a string in user space.
  1269. * @str: The string to measure.
  1270. *
  1271. * Context: User context only. This function may sleep if pagefaults are
  1272. * enabled.
  1273. *
  1274. * Get the size of a NUL-terminated string in user space.
  1275. *
  1276. * Returns the size of the string INCLUDING the terminating NUL.
  1277. * On exception, returns 0.
  1278. *
  1279. * If there is a limit on the length of a valid string, you may wish to
  1280. * consider using strnlen_user() instead.
  1281. */
  1282. static inline long strlen_user(const char __user *s)
  1283. {
  1284. long res;
  1285. if (eva_kernel_access()) {
  1286. __asm__ __volatile__(
  1287. "move\t$4, %1\n\t"
  1288. __MODULE_JAL(__strlen_kernel_asm)
  1289. "move\t%0, $2"
  1290. : "=r" (res)
  1291. : "r" (s)
  1292. : "$2", "$4", __UA_t0, "$31");
  1293. } else {
  1294. might_fault();
  1295. __asm__ __volatile__(
  1296. "move\t$4, %1\n\t"
  1297. __MODULE_JAL(__strlen_user_asm)
  1298. "move\t%0, $2"
  1299. : "=r" (res)
  1300. : "r" (s)
  1301. : "$2", "$4", __UA_t0, "$31");
  1302. }
  1303. return res;
  1304. }
  1305. /* Returns: 0 if bad, string length+1 (memory size) of string if ok */
  1306. static inline long __strnlen_user(const char __user *s, long n)
  1307. {
  1308. long res;
  1309. if (eva_kernel_access()) {
  1310. __asm__ __volatile__(
  1311. "move\t$4, %1\n\t"
  1312. "move\t$5, %2\n\t"
  1313. __MODULE_JAL(__strnlen_kernel_nocheck_asm)
  1314. "move\t%0, $2"
  1315. : "=r" (res)
  1316. : "r" (s), "r" (n)
  1317. : "$2", "$4", "$5", __UA_t0, "$31");
  1318. } else {
  1319. might_fault();
  1320. __asm__ __volatile__(
  1321. "move\t$4, %1\n\t"
  1322. "move\t$5, %2\n\t"
  1323. __MODULE_JAL(__strnlen_user_nocheck_asm)
  1324. "move\t%0, $2"
  1325. : "=r" (res)
  1326. : "r" (s), "r" (n)
  1327. : "$2", "$4", "$5", __UA_t0, "$31");
  1328. }
  1329. return res;
  1330. }
  1331. /*
  1332. * strnlen_user: - Get the size of a string in user space.
  1333. * @str: The string to measure.
  1334. *
  1335. * Context: User context only. This function may sleep if pagefaults are
  1336. * enabled.
  1337. *
  1338. * Get the size of a NUL-terminated string in user space.
  1339. *
  1340. * Returns the size of the string INCLUDING the terminating NUL.
  1341. * On exception, returns 0.
  1342. * If the string is too long, returns a value greater than @n.
  1343. */
  1344. static inline long strnlen_user(const char __user *s, long n)
  1345. {
  1346. long res;
  1347. might_fault();
  1348. if (eva_kernel_access()) {
  1349. __asm__ __volatile__(
  1350. "move\t$4, %1\n\t"
  1351. "move\t$5, %2\n\t"
  1352. __MODULE_JAL(__strnlen_kernel_asm)
  1353. "move\t%0, $2"
  1354. : "=r" (res)
  1355. : "r" (s), "r" (n)
  1356. : "$2", "$4", "$5", __UA_t0, "$31");
  1357. } else {
  1358. __asm__ __volatile__(
  1359. "move\t$4, %1\n\t"
  1360. "move\t$5, %2\n\t"
  1361. __MODULE_JAL(__strnlen_user_asm)
  1362. "move\t%0, $2"
  1363. : "=r" (res)
  1364. : "r" (s), "r" (n)
  1365. : "$2", "$4", "$5", __UA_t0, "$31");
  1366. }
  1367. return res;
  1368. }
  1369. #endif /* _ASM_UACCESS_H */