uaccess.h 42 KB

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