uaccess.h 41 KB

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