uaccess.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #ifndef __ALPHA_UACCESS_H
  2. #define __ALPHA_UACCESS_H
  3. /*
  4. * The fs value determines whether argument validity checking should be
  5. * performed or not. If get_fs() == USER_DS, checking is performed, with
  6. * get_fs() == KERNEL_DS, checking is bypassed.
  7. *
  8. * Or at least it did once upon a time. Nowadays it is a mask that
  9. * defines which bits of the address space are off limits. This is a
  10. * wee bit faster than the above.
  11. *
  12. * For historical reasons, these macros are grossly misnamed.
  13. */
  14. #define KERNEL_DS ((mm_segment_t) { 0UL })
  15. #define USER_DS ((mm_segment_t) { -0x40000000000UL })
  16. #define get_fs() (current_thread_info()->addr_limit)
  17. #define get_ds() (KERNEL_DS)
  18. #define set_fs(x) (current_thread_info()->addr_limit = (x))
  19. #define segment_eq(a, b) ((a).seg == (b).seg)
  20. /*
  21. * Is a address valid? This does a straightforward calculation rather
  22. * than tests.
  23. *
  24. * Address valid if:
  25. * - "addr" doesn't have any high-bits set
  26. * - AND "size" doesn't have any high-bits set
  27. * - AND "addr+size" doesn't have any high-bits set
  28. * - OR we are in kernel mode.
  29. */
  30. #define __access_ok(addr, size, segment) \
  31. (((segment).seg & (addr | size | (addr+size))) == 0)
  32. #define access_ok(type, addr, size) \
  33. ({ \
  34. __chk_user_ptr(addr); \
  35. __access_ok(((unsigned long)(addr)), (size), get_fs()); \
  36. })
  37. /*
  38. * These are the main single-value transfer routines. They automatically
  39. * use the right size if we just have the right pointer type.
  40. *
  41. * As the alpha uses the same address space for kernel and user
  42. * data, we can just do these as direct assignments. (Of course, the
  43. * exception handling means that it's no longer "just"...)
  44. *
  45. * Careful to not
  46. * (a) re-use the arguments for side effects (sizeof/typeof is ok)
  47. * (b) require any knowledge of processes at this stage
  48. */
  49. #define put_user(x, ptr) \
  50. __put_user_check((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)), get_fs())
  51. #define get_user(x, ptr) \
  52. __get_user_check((x), (ptr), sizeof(*(ptr)), get_fs())
  53. /*
  54. * The "__xxx" versions do not do address space checking, useful when
  55. * doing multiple accesses to the same area (the programmer has to do the
  56. * checks by hand with "access_ok()")
  57. */
  58. #define __put_user(x, ptr) \
  59. __put_user_nocheck((__typeof__(*(ptr)))(x), (ptr), sizeof(*(ptr)))
  60. #define __get_user(x, ptr) \
  61. __get_user_nocheck((x), (ptr), sizeof(*(ptr)))
  62. /*
  63. * The "lda %1, 2b-1b(%0)" bits are magic to get the assembler to
  64. * encode the bits we need for resolving the exception. See the
  65. * more extensive comments with fixup_inline_exception below for
  66. * more information.
  67. */
  68. extern void __get_user_unknown(void);
  69. #define __get_user_nocheck(x, ptr, size) \
  70. ({ \
  71. long __gu_err = 0; \
  72. unsigned long __gu_val; \
  73. __chk_user_ptr(ptr); \
  74. switch (size) { \
  75. case 1: __get_user_8(ptr); break; \
  76. case 2: __get_user_16(ptr); break; \
  77. case 4: __get_user_32(ptr); break; \
  78. case 8: __get_user_64(ptr); break; \
  79. default: __get_user_unknown(); break; \
  80. } \
  81. (x) = (__force __typeof__(*(ptr))) __gu_val; \
  82. __gu_err; \
  83. })
  84. #define __get_user_check(x, ptr, size, segment) \
  85. ({ \
  86. long __gu_err = -EFAULT; \
  87. unsigned long __gu_val = 0; \
  88. const __typeof__(*(ptr)) __user *__gu_addr = (ptr); \
  89. if (__access_ok((unsigned long)__gu_addr, size, segment)) { \
  90. __gu_err = 0; \
  91. switch (size) { \
  92. case 1: __get_user_8(__gu_addr); break; \
  93. case 2: __get_user_16(__gu_addr); break; \
  94. case 4: __get_user_32(__gu_addr); break; \
  95. case 8: __get_user_64(__gu_addr); break; \
  96. default: __get_user_unknown(); break; \
  97. } \
  98. } \
  99. (x) = (__force __typeof__(*(ptr))) __gu_val; \
  100. __gu_err; \
  101. })
  102. struct __large_struct { unsigned long buf[100]; };
  103. #define __m(x) (*(struct __large_struct __user *)(x))
  104. #define __get_user_64(addr) \
  105. __asm__("1: ldq %0,%2\n" \
  106. "2:\n" \
  107. ".section __ex_table,\"a\"\n" \
  108. " .long 1b - .\n" \
  109. " lda %0, 2b-1b(%1)\n" \
  110. ".previous" \
  111. : "=r"(__gu_val), "=r"(__gu_err) \
  112. : "m"(__m(addr)), "1"(__gu_err))
  113. #define __get_user_32(addr) \
  114. __asm__("1: ldl %0,%2\n" \
  115. "2:\n" \
  116. ".section __ex_table,\"a\"\n" \
  117. " .long 1b - .\n" \
  118. " lda %0, 2b-1b(%1)\n" \
  119. ".previous" \
  120. : "=r"(__gu_val), "=r"(__gu_err) \
  121. : "m"(__m(addr)), "1"(__gu_err))
  122. #ifdef __alpha_bwx__
  123. /* Those lucky bastards with ev56 and later CPUs can do byte/word moves. */
  124. #define __get_user_16(addr) \
  125. __asm__("1: ldwu %0,%2\n" \
  126. "2:\n" \
  127. ".section __ex_table,\"a\"\n" \
  128. " .long 1b - .\n" \
  129. " lda %0, 2b-1b(%1)\n" \
  130. ".previous" \
  131. : "=r"(__gu_val), "=r"(__gu_err) \
  132. : "m"(__m(addr)), "1"(__gu_err))
  133. #define __get_user_8(addr) \
  134. __asm__("1: ldbu %0,%2\n" \
  135. "2:\n" \
  136. ".section __ex_table,\"a\"\n" \
  137. " .long 1b - .\n" \
  138. " lda %0, 2b-1b(%1)\n" \
  139. ".previous" \
  140. : "=r"(__gu_val), "=r"(__gu_err) \
  141. : "m"(__m(addr)), "1"(__gu_err))
  142. #else
  143. /* Unfortunately, we can't get an unaligned access trap for the sub-word
  144. load, so we have to do a general unaligned operation. */
  145. #define __get_user_16(addr) \
  146. { \
  147. long __gu_tmp; \
  148. __asm__("1: ldq_u %0,0(%3)\n" \
  149. "2: ldq_u %1,1(%3)\n" \
  150. " extwl %0,%3,%0\n" \
  151. " extwh %1,%3,%1\n" \
  152. " or %0,%1,%0\n" \
  153. "3:\n" \
  154. ".section __ex_table,\"a\"\n" \
  155. " .long 1b - .\n" \
  156. " lda %0, 3b-1b(%2)\n" \
  157. " .long 2b - .\n" \
  158. " lda %0, 3b-2b(%2)\n" \
  159. ".previous" \
  160. : "=&r"(__gu_val), "=&r"(__gu_tmp), "=r"(__gu_err) \
  161. : "r"(addr), "2"(__gu_err)); \
  162. }
  163. #define __get_user_8(addr) \
  164. __asm__("1: ldq_u %0,0(%2)\n" \
  165. " extbl %0,%2,%0\n" \
  166. "2:\n" \
  167. ".section __ex_table,\"a\"\n" \
  168. " .long 1b - .\n" \
  169. " lda %0, 2b-1b(%1)\n" \
  170. ".previous" \
  171. : "=&r"(__gu_val), "=r"(__gu_err) \
  172. : "r"(addr), "1"(__gu_err))
  173. #endif
  174. extern void __put_user_unknown(void);
  175. #define __put_user_nocheck(x, ptr, size) \
  176. ({ \
  177. long __pu_err = 0; \
  178. __chk_user_ptr(ptr); \
  179. switch (size) { \
  180. case 1: __put_user_8(x, ptr); break; \
  181. case 2: __put_user_16(x, ptr); break; \
  182. case 4: __put_user_32(x, ptr); break; \
  183. case 8: __put_user_64(x, ptr); break; \
  184. default: __put_user_unknown(); break; \
  185. } \
  186. __pu_err; \
  187. })
  188. #define __put_user_check(x, ptr, size, segment) \
  189. ({ \
  190. long __pu_err = -EFAULT; \
  191. __typeof__(*(ptr)) __user *__pu_addr = (ptr); \
  192. if (__access_ok((unsigned long)__pu_addr, size, segment)) { \
  193. __pu_err = 0; \
  194. switch (size) { \
  195. case 1: __put_user_8(x, __pu_addr); break; \
  196. case 2: __put_user_16(x, __pu_addr); break; \
  197. case 4: __put_user_32(x, __pu_addr); break; \
  198. case 8: __put_user_64(x, __pu_addr); break; \
  199. default: __put_user_unknown(); break; \
  200. } \
  201. } \
  202. __pu_err; \
  203. })
  204. /*
  205. * The "__put_user_xx()" macros tell gcc they read from memory
  206. * instead of writing: this is because they do not write to
  207. * any memory gcc knows about, so there are no aliasing issues
  208. */
  209. #define __put_user_64(x, addr) \
  210. __asm__ __volatile__("1: stq %r2,%1\n" \
  211. "2:\n" \
  212. ".section __ex_table,\"a\"\n" \
  213. " .long 1b - .\n" \
  214. " lda $31,2b-1b(%0)\n" \
  215. ".previous" \
  216. : "=r"(__pu_err) \
  217. : "m" (__m(addr)), "rJ" (x), "0"(__pu_err))
  218. #define __put_user_32(x, addr) \
  219. __asm__ __volatile__("1: stl %r2,%1\n" \
  220. "2:\n" \
  221. ".section __ex_table,\"a\"\n" \
  222. " .long 1b - .\n" \
  223. " lda $31,2b-1b(%0)\n" \
  224. ".previous" \
  225. : "=r"(__pu_err) \
  226. : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
  227. #ifdef __alpha_bwx__
  228. /* Those lucky bastards with ev56 and later CPUs can do byte/word moves. */
  229. #define __put_user_16(x, addr) \
  230. __asm__ __volatile__("1: stw %r2,%1\n" \
  231. "2:\n" \
  232. ".section __ex_table,\"a\"\n" \
  233. " .long 1b - .\n" \
  234. " lda $31,2b-1b(%0)\n" \
  235. ".previous" \
  236. : "=r"(__pu_err) \
  237. : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
  238. #define __put_user_8(x, addr) \
  239. __asm__ __volatile__("1: stb %r2,%1\n" \
  240. "2:\n" \
  241. ".section __ex_table,\"a\"\n" \
  242. " .long 1b - .\n" \
  243. " lda $31,2b-1b(%0)\n" \
  244. ".previous" \
  245. : "=r"(__pu_err) \
  246. : "m"(__m(addr)), "rJ"(x), "0"(__pu_err))
  247. #else
  248. /* Unfortunately, we can't get an unaligned access trap for the sub-word
  249. write, so we have to do a general unaligned operation. */
  250. #define __put_user_16(x, addr) \
  251. { \
  252. long __pu_tmp1, __pu_tmp2, __pu_tmp3, __pu_tmp4; \
  253. __asm__ __volatile__( \
  254. "1: ldq_u %2,1(%5)\n" \
  255. "2: ldq_u %1,0(%5)\n" \
  256. " inswh %6,%5,%4\n" \
  257. " inswl %6,%5,%3\n" \
  258. " mskwh %2,%5,%2\n" \
  259. " mskwl %1,%5,%1\n" \
  260. " or %2,%4,%2\n" \
  261. " or %1,%3,%1\n" \
  262. "3: stq_u %2,1(%5)\n" \
  263. "4: stq_u %1,0(%5)\n" \
  264. "5:\n" \
  265. ".section __ex_table,\"a\"\n" \
  266. " .long 1b - .\n" \
  267. " lda $31, 5b-1b(%0)\n" \
  268. " .long 2b - .\n" \
  269. " lda $31, 5b-2b(%0)\n" \
  270. " .long 3b - .\n" \
  271. " lda $31, 5b-3b(%0)\n" \
  272. " .long 4b - .\n" \
  273. " lda $31, 5b-4b(%0)\n" \
  274. ".previous" \
  275. : "=r"(__pu_err), "=&r"(__pu_tmp1), \
  276. "=&r"(__pu_tmp2), "=&r"(__pu_tmp3), \
  277. "=&r"(__pu_tmp4) \
  278. : "r"(addr), "r"((unsigned long)(x)), "0"(__pu_err)); \
  279. }
  280. #define __put_user_8(x, addr) \
  281. { \
  282. long __pu_tmp1, __pu_tmp2; \
  283. __asm__ __volatile__( \
  284. "1: ldq_u %1,0(%4)\n" \
  285. " insbl %3,%4,%2\n" \
  286. " mskbl %1,%4,%1\n" \
  287. " or %1,%2,%1\n" \
  288. "2: stq_u %1,0(%4)\n" \
  289. "3:\n" \
  290. ".section __ex_table,\"a\"\n" \
  291. " .long 1b - .\n" \
  292. " lda $31, 3b-1b(%0)\n" \
  293. " .long 2b - .\n" \
  294. " lda $31, 3b-2b(%0)\n" \
  295. ".previous" \
  296. : "=r"(__pu_err), \
  297. "=&r"(__pu_tmp1), "=&r"(__pu_tmp2) \
  298. : "r"((unsigned long)(x)), "r"(addr), "0"(__pu_err)); \
  299. }
  300. #endif
  301. /*
  302. * Complex access routines
  303. */
  304. /* This little bit of silliness is to get the GP loaded for a function
  305. that ordinarily wouldn't. Otherwise we could have it done by the macro
  306. directly, which can be optimized the linker. */
  307. #ifdef MODULE
  308. #define __module_address(sym) "r"(sym),
  309. #define __module_call(ra, arg, sym) "jsr $" #ra ",(%" #arg ")," #sym
  310. #else
  311. #define __module_address(sym)
  312. #define __module_call(ra, arg, sym) "bsr $" #ra "," #sym " !samegp"
  313. #endif
  314. extern void __copy_user(void);
  315. extern inline long
  316. __copy_tofrom_user_nocheck(void *to, const void *from, long len)
  317. {
  318. register void * __cu_to __asm__("$6") = to;
  319. register const void * __cu_from __asm__("$7") = from;
  320. register long __cu_len __asm__("$0") = len;
  321. __asm__ __volatile__(
  322. __module_call(28, 3, __copy_user)
  323. : "=r" (__cu_len), "=r" (__cu_from), "=r" (__cu_to)
  324. : __module_address(__copy_user)
  325. "0" (__cu_len), "1" (__cu_from), "2" (__cu_to)
  326. : "$1", "$2", "$3", "$4", "$5", "$28", "memory");
  327. return __cu_len;
  328. }
  329. #define __copy_to_user(to, from, n) \
  330. ({ \
  331. __chk_user_ptr(to); \
  332. __copy_tofrom_user_nocheck((__force void *)(to), (from), (n)); \
  333. })
  334. #define __copy_from_user(to, from, n) \
  335. ({ \
  336. __chk_user_ptr(from); \
  337. __copy_tofrom_user_nocheck((to), (__force void *)(from), (n)); \
  338. })
  339. #define __copy_to_user_inatomic __copy_to_user
  340. #define __copy_from_user_inatomic __copy_from_user
  341. extern inline long
  342. copy_to_user(void __user *to, const void *from, long n)
  343. {
  344. if (likely(__access_ok((unsigned long)to, n, get_fs())))
  345. n = __copy_tofrom_user_nocheck((__force void *)to, from, n);
  346. return n;
  347. }
  348. extern inline long
  349. copy_from_user(void *to, const void __user *from, long n)
  350. {
  351. long res = n;
  352. if (likely(__access_ok((unsigned long)from, n, get_fs())))
  353. res = __copy_from_user_inatomic(to, from, n);
  354. if (unlikely(res))
  355. memset(to + (n - res), 0, res);
  356. return res;
  357. }
  358. extern void __do_clear_user(void);
  359. extern inline long
  360. __clear_user(void __user *to, long len)
  361. {
  362. register void __user * __cl_to __asm__("$6") = to;
  363. register long __cl_len __asm__("$0") = len;
  364. __asm__ __volatile__(
  365. __module_call(28, 2, __do_clear_user)
  366. : "=r"(__cl_len), "=r"(__cl_to)
  367. : __module_address(__do_clear_user)
  368. "0"(__cl_len), "1"(__cl_to)
  369. : "$1", "$2", "$3", "$4", "$5", "$28", "memory");
  370. return __cl_len;
  371. }
  372. extern inline long
  373. clear_user(void __user *to, long len)
  374. {
  375. if (__access_ok((unsigned long)to, len, get_fs()))
  376. len = __clear_user(to, len);
  377. return len;
  378. }
  379. #undef __module_address
  380. #undef __module_call
  381. #define user_addr_max() \
  382. (uaccess_kernel() ? ~0UL : TASK_SIZE)
  383. extern long strncpy_from_user(char *dest, const char __user *src, long count);
  384. extern __must_check long strlen_user(const char __user *str);
  385. extern __must_check long strnlen_user(const char __user *str, long n);
  386. /*
  387. * About the exception table:
  388. *
  389. * - insn is a 32-bit pc-relative offset from the faulting insn.
  390. * - nextinsn is a 16-bit offset off of the faulting instruction
  391. * (not off of the *next* instruction as branches are).
  392. * - errreg is the register in which to place -EFAULT.
  393. * - valreg is the final target register for the load sequence
  394. * and will be zeroed.
  395. *
  396. * Either errreg or valreg may be $31, in which case nothing happens.
  397. *
  398. * The exception fixup information "just so happens" to be arranged
  399. * as in a MEM format instruction. This lets us emit our three
  400. * values like so:
  401. *
  402. * lda valreg, nextinsn(errreg)
  403. *
  404. */
  405. struct exception_table_entry
  406. {
  407. signed int insn;
  408. union exception_fixup {
  409. unsigned unit;
  410. struct {
  411. signed int nextinsn : 16;
  412. unsigned int errreg : 5;
  413. unsigned int valreg : 5;
  414. } bits;
  415. } fixup;
  416. };
  417. /* Returns the new pc */
  418. #define fixup_exception(map_reg, _fixup, pc) \
  419. ({ \
  420. if ((_fixup)->fixup.bits.valreg != 31) \
  421. map_reg((_fixup)->fixup.bits.valreg) = 0; \
  422. if ((_fixup)->fixup.bits.errreg != 31) \
  423. map_reg((_fixup)->fixup.bits.errreg) = -EFAULT; \
  424. (pc) + (_fixup)->fixup.bits.nextinsn; \
  425. })
  426. #define ARCH_HAS_RELATIVE_EXTABLE
  427. #define swap_ex_entry_fixup(a, b, tmp, delta) \
  428. do { \
  429. (a)->fixup.unit = (b)->fixup.unit; \
  430. (b)->fixup.unit = (tmp).fixup.unit; \
  431. } while (0)
  432. #endif /* __ALPHA_UACCESS_H */