uaccess.h 43 KB

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