uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * Copyright (C) 2008-2009 Michal Simek <monstr@monstr.eu>
  3. * Copyright (C) 2008-2009 PetaLogix
  4. * Copyright (C) 2006 Atmark Techno, Inc.
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License. See the file "COPYING" in the main directory of this archive
  8. * for more details.
  9. */
  10. #ifndef _ASM_MICROBLAZE_UACCESS_H
  11. #define _ASM_MICROBLAZE_UACCESS_H
  12. #ifdef __KERNEL__
  13. #ifndef __ASSEMBLY__
  14. #include <linux/kernel.h>
  15. #include <linux/errno.h>
  16. #include <linux/sched.h> /* RLIMIT_FSIZE */
  17. #include <linux/mm.h>
  18. #include <asm/mmu.h>
  19. #include <asm/page.h>
  20. #include <asm/pgtable.h>
  21. #include <linux/string.h>
  22. #define VERIFY_READ 0
  23. #define VERIFY_WRITE 1
  24. /*
  25. * On Microblaze the fs value is actually the top of the corresponding
  26. * address space.
  27. *
  28. * The fs value determines whether argument validity checking should be
  29. * performed or not. If get_fs() == USER_DS, checking is performed, with
  30. * get_fs() == KERNEL_DS, checking is bypassed.
  31. *
  32. * For historical reasons, these macros are grossly misnamed.
  33. *
  34. * For non-MMU arch like Microblaze, KERNEL_DS and USER_DS is equal.
  35. */
  36. # define MAKE_MM_SEG(s) ((mm_segment_t) { (s) })
  37. # ifndef CONFIG_MMU
  38. # define KERNEL_DS MAKE_MM_SEG(0)
  39. # define USER_DS KERNEL_DS
  40. # else
  41. # define KERNEL_DS MAKE_MM_SEG(0xFFFFFFFF)
  42. # define USER_DS MAKE_MM_SEG(TASK_SIZE - 1)
  43. # endif
  44. # define get_ds() (KERNEL_DS)
  45. # define get_fs() (current_thread_info()->addr_limit)
  46. # define set_fs(val) (current_thread_info()->addr_limit = (val))
  47. # define segment_eq(a, b) ((a).seg == (b).seg)
  48. /*
  49. * The exception table consists of pairs of addresses: the first is the
  50. * address of an instruction that is allowed to fault, and the second is
  51. * the address at which the program should continue. No registers are
  52. * modified, so it is entirely up to the continuation code to figure out
  53. * what to do.
  54. *
  55. * All the routines below use bits of fixup code that are out of line
  56. * with the main instruction path. This means when everything is well,
  57. * we don't even have to jump over them. Further, they do not intrude
  58. * on our cache or tlb entries.
  59. */
  60. struct exception_table_entry {
  61. unsigned long insn, fixup;
  62. };
  63. #ifndef CONFIG_MMU
  64. /* Check against bounds of physical memory */
  65. static inline int ___range_ok(unsigned long addr, unsigned long size)
  66. {
  67. return ((addr < memory_start) ||
  68. ((addr + size) > memory_end));
  69. }
  70. #define __range_ok(addr, size) \
  71. ___range_ok((unsigned long)(addr), (unsigned long)(size))
  72. #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
  73. #else
  74. /*
  75. * Address is valid if:
  76. * - "addr", "addr + size" and "size" are all below the limit
  77. */
  78. #define access_ok(type, addr, size) \
  79. (get_fs().seg > (((unsigned long)(addr)) | \
  80. (size) | ((unsigned long)(addr) + (size))))
  81. /* || printk("access_ok failed for %s at 0x%08lx (size %d), seg 0x%08x\n",
  82. type?"WRITE":"READ",addr,size,get_fs().seg)) */
  83. #endif
  84. #ifdef CONFIG_MMU
  85. # define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
  86. # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
  87. #else
  88. # define __FIXUP_SECTION ".section .discard,\"ax\"\n"
  89. # define __EX_TABLE_SECTION ".section .discard,\"a\"\n"
  90. #endif
  91. #ifndef CONFIG_MMU
  92. /* Undefined function to trigger linker error */
  93. extern int bad_user_access_length(void);
  94. /* FIXME this is function for optimalization -> memcpy */
  95. #define __get_user(var, ptr) \
  96. ({ \
  97. int __gu_err = 0; \
  98. switch (sizeof(*(ptr))) { \
  99. case 1: \
  100. case 2: \
  101. case 4: \
  102. (var) = *(ptr); \
  103. break; \
  104. case 8: \
  105. memcpy((void *) &(var), (ptr), 8); \
  106. break; \
  107. default: \
  108. (var) = 0; \
  109. __gu_err = __get_user_bad(); \
  110. break; \
  111. } \
  112. __gu_err; \
  113. })
  114. #define __get_user_bad() (bad_user_access_length(), (-EFAULT))
  115. /* FIXME is not there defined __pu_val */
  116. #define __put_user(var, ptr) \
  117. ({ \
  118. int __pu_err = 0; \
  119. switch (sizeof(*(ptr))) { \
  120. case 1: \
  121. case 2: \
  122. case 4: \
  123. *(ptr) = (var); \
  124. break; \
  125. case 8: { \
  126. typeof(*(ptr)) __pu_val = (var); \
  127. memcpy(ptr, &__pu_val, sizeof(__pu_val)); \
  128. } \
  129. break; \
  130. default: \
  131. __pu_err = __put_user_bad(); \
  132. break; \
  133. } \
  134. __pu_err; \
  135. })
  136. #define __put_user_bad() (bad_user_access_length(), (-EFAULT))
  137. #define put_user(x, ptr) __put_user((x), (ptr))
  138. #define get_user(x, ptr) __get_user((x), (ptr))
  139. #define copy_to_user(to, from, n) (memcpy((to), (from), (n)), 0)
  140. #define copy_from_user(to, from, n) (memcpy((to), (from), (n)), 0)
  141. #define __copy_to_user(to, from, n) (copy_to_user((to), (from), (n)))
  142. #define __copy_from_user(to, from, n) (copy_from_user((to), (from), (n)))
  143. #define __copy_to_user_inatomic(to, from, n) \
  144. (__copy_to_user((to), (from), (n)))
  145. #define __copy_from_user_inatomic(to, from, n) \
  146. (__copy_from_user((to), (from), (n)))
  147. #define __clear_user(addr, n) (memset((void *)(addr), 0, (n)), 0)
  148. /* stejne s MMU */
  149. static inline unsigned long clear_user(void *addr, unsigned long size)
  150. {
  151. if (access_ok(VERIFY_WRITE, addr, size))
  152. size = __clear_user(addr, size);
  153. return size;
  154. }
  155. /* Returns 0 if exception not found and fixup otherwise. */
  156. extern unsigned long search_exception_table(unsigned long);
  157. extern long strncpy_from_user(char *dst, const char *src, long count);
  158. extern long strnlen_user(const char *src, long count);
  159. #else /* CONFIG_MMU */
  160. /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
  161. static inline unsigned long __must_check __clear_user(void __user *to,
  162. unsigned long n)
  163. {
  164. /* normal memset with two words to __ex_table */
  165. __asm__ __volatile__ ( \
  166. "1: sb r0, %2, r0;" \
  167. " addik %0, %0, -1;" \
  168. " bneid %0, 1b;" \
  169. " addik %2, %2, 1;" \
  170. "2: " \
  171. __EX_TABLE_SECTION \
  172. ".word 1b,2b;" \
  173. ".previous;" \
  174. : "=r"(n) \
  175. : "0"(n), "r"(to)
  176. );
  177. return n;
  178. }
  179. static inline unsigned long __must_check clear_user(void __user *to,
  180. unsigned long n)
  181. {
  182. might_sleep();
  183. if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
  184. return n;
  185. return __clear_user(to, n);
  186. }
  187. /* put_user and get_user macros */
  188. extern long __user_bad(void);
  189. #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
  190. ({ \
  191. __asm__ __volatile__ ( \
  192. "1:" insn " %1, %2, r0;" \
  193. " addk %0, r0, r0;" \
  194. "2: " \
  195. __FIXUP_SECTION \
  196. "3: brid 2b; " \
  197. " addik %0, r0, %3;" \
  198. ".previous;" \
  199. __EX_TABLE_SECTION \
  200. ".word 1b,3b;" \
  201. ".previous;" \
  202. : "=&r"(__gu_err), "=r"(__gu_val) \
  203. : "r"(__gu_ptr), "i"(-EFAULT) \
  204. ); \
  205. })
  206. #define __get_user(x, ptr) \
  207. ({ \
  208. unsigned long __gu_val; \
  209. /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \
  210. long __gu_err; \
  211. switch (sizeof(*(ptr))) { \
  212. case 1: \
  213. __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
  214. break; \
  215. case 2: \
  216. __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
  217. break; \
  218. case 4: \
  219. __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
  220. break; \
  221. default: \
  222. /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
  223. } \
  224. x = (__typeof__(*(ptr))) __gu_val; \
  225. __gu_err; \
  226. })
  227. /**
  228. * get_user: - Get a simple variable from user space.
  229. * @x: Variable to store result.
  230. * @ptr: Source address, in user space.
  231. *
  232. * Context: User context only. This function may sleep.
  233. *
  234. * This macro copies a single simple variable from user space to kernel
  235. * space. It supports simple types like char and int, but not larger
  236. * data types like structures or arrays.
  237. *
  238. * @ptr must have pointer-to-simple-variable type, and the result of
  239. * dereferencing @ptr must be assignable to @x without a cast.
  240. *
  241. * Returns zero on success, or -EFAULT on error.
  242. * On error, the variable @x is set to zero.
  243. */
  244. #define get_user(x, ptr) \
  245. ({ \
  246. access_ok(VERIFY_READ, (ptr), sizeof(*(ptr))) \
  247. ? __get_user((x), (ptr)) : -EFAULT; \
  248. })
  249. /**
  250. * put_user: - Write a simple value into user space.
  251. * @x: Value to copy to user space.
  252. * @ptr: Destination address, in user space.
  253. *
  254. * Context: User context only. This function may sleep.
  255. *
  256. * This macro copies a single simple value from kernel space to user
  257. * space. It supports simple types like char and int, but not larger
  258. * data types like structures or arrays.
  259. *
  260. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  261. * to the result of dereferencing @ptr.
  262. *
  263. * Returns zero on success, or -EFAULT on error.
  264. */
  265. #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
  266. ({ \
  267. __asm__ __volatile__ ( \
  268. "1:" insn " %1, %2, r0;" \
  269. " addk %0, r0, r0;" \
  270. "2: " \
  271. __FIXUP_SECTION \
  272. "3: brid 2b;" \
  273. " addik %0, r0, %3;" \
  274. ".previous;" \
  275. __EX_TABLE_SECTION \
  276. ".word 1b,3b;" \
  277. ".previous;" \
  278. : "=&r"(__gu_err) \
  279. : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
  280. ); \
  281. })
  282. #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
  283. ({ \
  284. __asm__ __volatile__ (" lwi %0, %1, 0;" \
  285. "1: swi %0, %2, 0;" \
  286. " lwi %0, %1, 4;" \
  287. "2: swi %0, %2, 4;" \
  288. " addk %0, r0, r0;" \
  289. "3: " \
  290. __FIXUP_SECTION \
  291. "4: brid 3b;" \
  292. " addik %0, r0, %3;" \
  293. ".previous;" \
  294. __EX_TABLE_SECTION \
  295. ".word 1b,4b,2b,4b;" \
  296. ".previous;" \
  297. : "=&r"(__gu_err) \
  298. : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
  299. ); \
  300. })
  301. #define __put_user(x, ptr) \
  302. ({ \
  303. __typeof__(*(ptr)) volatile __gu_val = (x); \
  304. long __gu_err = 0; \
  305. switch (sizeof(__gu_val)) { \
  306. case 1: \
  307. __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
  308. break; \
  309. case 2: \
  310. __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
  311. break; \
  312. case 4: \
  313. __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
  314. break; \
  315. case 8: \
  316. __put_user_asm_8((ptr), __gu_val, __gu_err); \
  317. break; \
  318. default: \
  319. /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \
  320. } \
  321. __gu_err; \
  322. })
  323. #define put_user(x, ptr) \
  324. ({ \
  325. access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) \
  326. ? __put_user((x), (ptr)) : -EFAULT; \
  327. })
  328. extern unsigned long __copy_tofrom_user(void __user *to,
  329. const void __user *from, unsigned long size);
  330. #define __copy_from_user(to, from, n) \
  331. __copy_tofrom_user((__force void __user *)(to), \
  332. (void __user *)(from), (n))
  333. #define __copy_from_user_inatomic(to, from, n) \
  334. copy_from_user((to), (from), (n))
  335. static inline long copy_from_user(void *to,
  336. const void __user *from, unsigned long n)
  337. {
  338. might_sleep();
  339. if (access_ok(VERIFY_READ, from, n))
  340. return __copy_from_user(to, from, n);
  341. else
  342. return n;
  343. }
  344. #define __copy_to_user(to, from, n) \
  345. __copy_tofrom_user((void __user *)(to), \
  346. (__force const void __user *)(from), (n))
  347. #define __copy_to_user_inatomic(to, from, n) copy_to_user((to), (from), (n))
  348. static inline long copy_to_user(void __user *to,
  349. const void *from, unsigned long n)
  350. {
  351. might_sleep();
  352. if (access_ok(VERIFY_WRITE, to, n))
  353. return __copy_to_user(to, from, n);
  354. else
  355. return n;
  356. }
  357. extern int __strncpy_user(char *to, const char __user *from, int len);
  358. extern int __strnlen_user(const char __user *sstr, int len);
  359. #define strncpy_from_user(to, from, len) \
  360. (access_ok(VERIFY_READ, from, 1) ? \
  361. __strncpy_user(to, from, len) : -EFAULT)
  362. #define strnlen_user(str, len) \
  363. (access_ok(VERIFY_READ, str, 1) ? __strnlen_user(str, len) : 0)
  364. #endif /* CONFIG_MMU */
  365. #endif /* __ASSEMBLY__ */
  366. #endif /* __KERNEL__ */
  367. #endif /* _ASM_MICROBLAZE_UACCESS_H */