uaccess.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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. /* Returns 0 if exception not found and fixup otherwise. */
  64. extern unsigned long search_exception_table(unsigned long);
  65. #ifndef CONFIG_MMU
  66. /* Check against bounds of physical memory */
  67. static inline int ___range_ok(unsigned long addr, unsigned long size)
  68. {
  69. return ((addr < memory_start) ||
  70. ((addr + size) > memory_end));
  71. }
  72. #define __range_ok(addr, size) \
  73. ___range_ok((unsigned long)(addr), (unsigned long)(size))
  74. #define access_ok(type, addr, size) (__range_ok((addr), (size)) == 0)
  75. #else
  76. /*
  77. * Address is valid if:
  78. * - "addr", "addr + size" and "size" are all below the limit
  79. */
  80. #define access_ok(type, addr, size) \
  81. (get_fs().seg > (((unsigned long)(addr)) | \
  82. (size) | ((unsigned long)(addr) + (size))))
  83. /* || printk("access_ok failed for %s at 0x%08lx (size %d), seg 0x%08x\n",
  84. type?"WRITE":"READ",addr,size,get_fs().seg)) */
  85. #endif
  86. #ifdef CONFIG_MMU
  87. # define __FIXUP_SECTION ".section .fixup,\"ax\"\n"
  88. # define __EX_TABLE_SECTION ".section __ex_table,\"a\"\n"
  89. #else
  90. # define __FIXUP_SECTION ".section .discard,\"ax\"\n"
  91. # define __EX_TABLE_SECTION ".section .discard,\"a\"\n"
  92. #endif
  93. /* Return: number of not copied bytes, i.e. 0 if OK or non-zero if fail. */
  94. static inline unsigned long __must_check __clear_user(void __user *to,
  95. unsigned long n)
  96. {
  97. /* normal memset with two words to __ex_table */
  98. __asm__ __volatile__ ( \
  99. "1: sb r0, %2, r0;" \
  100. " addik %0, %0, -1;" \
  101. " bneid %0, 1b;" \
  102. " addik %2, %2, 1;" \
  103. "2: " \
  104. __EX_TABLE_SECTION \
  105. ".word 1b,2b;" \
  106. ".previous;" \
  107. : "=r"(n) \
  108. : "0"(n), "r"(to)
  109. );
  110. return n;
  111. }
  112. static inline unsigned long __must_check clear_user(void __user *to,
  113. unsigned long n)
  114. {
  115. might_sleep();
  116. if (unlikely(!access_ok(VERIFY_WRITE, to, n)))
  117. return n;
  118. return __clear_user(to, n);
  119. }
  120. /* put_user and get_user macros */
  121. extern long __user_bad(void);
  122. #define __get_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
  123. ({ \
  124. __asm__ __volatile__ ( \
  125. "1:" insn " %1, %2, r0;" \
  126. " addk %0, r0, r0;" \
  127. "2: " \
  128. __FIXUP_SECTION \
  129. "3: brid 2b;" \
  130. " addik %0, r0, %3;" \
  131. ".previous;" \
  132. __EX_TABLE_SECTION \
  133. ".word 1b,3b;" \
  134. ".previous;" \
  135. : "=&r"(__gu_err), "=r"(__gu_val) \
  136. : "r"(__gu_ptr), "i"(-EFAULT) \
  137. ); \
  138. })
  139. /**
  140. * get_user: - Get a simple variable from user space.
  141. * @x: Variable to store result.
  142. * @ptr: Source address, in user space.
  143. *
  144. * Context: User context only. This function may sleep.
  145. *
  146. * This macro copies a single simple variable from user space to kernel
  147. * space. It supports simple types like char and int, but not larger
  148. * data types like structures or arrays.
  149. *
  150. * @ptr must have pointer-to-simple-variable type, and the result of
  151. * dereferencing @ptr must be assignable to @x without a cast.
  152. *
  153. * Returns zero on success, or -EFAULT on error.
  154. * On error, the variable @x is set to zero.
  155. */
  156. #define __get_user(x, ptr) \
  157. ({ \
  158. unsigned long __gu_val; \
  159. /*unsigned long __gu_ptr = (unsigned long)(ptr);*/ \
  160. long __gu_err; \
  161. switch (sizeof(*(ptr))) { \
  162. case 1: \
  163. __get_user_asm("lbu", (ptr), __gu_val, __gu_err); \
  164. break; \
  165. case 2: \
  166. __get_user_asm("lhu", (ptr), __gu_val, __gu_err); \
  167. break; \
  168. case 4: \
  169. __get_user_asm("lw", (ptr), __gu_val, __gu_err); \
  170. break; \
  171. default: \
  172. /* __gu_val = 0; __gu_err = -EINVAL;*/ __gu_err = __user_bad();\
  173. } \
  174. x = (__typeof__(*(ptr))) __gu_val; \
  175. __gu_err; \
  176. })
  177. #define get_user(x, ptr) \
  178. ({ \
  179. access_ok(VERIFY_READ, (ptr), sizeof(*(ptr))) \
  180. ? __get_user((x), (ptr)) : -EFAULT; \
  181. })
  182. #define __put_user_asm(insn, __gu_ptr, __gu_val, __gu_err) \
  183. ({ \
  184. __asm__ __volatile__ ( \
  185. "1:" insn " %1, %2, r0;" \
  186. " addk %0, r0, r0;" \
  187. "2: " \
  188. __FIXUP_SECTION \
  189. "3: brid 2b;" \
  190. " addik %0, r0, %3;" \
  191. ".previous;" \
  192. __EX_TABLE_SECTION \
  193. ".word 1b,3b;" \
  194. ".previous;" \
  195. : "=&r"(__gu_err) \
  196. : "r"(__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
  197. ); \
  198. })
  199. #define __put_user_asm_8(__gu_ptr, __gu_val, __gu_err) \
  200. ({ \
  201. __asm__ __volatile__ (" lwi %0, %1, 0;" \
  202. "1: swi %0, %2, 0;" \
  203. " lwi %0, %1, 4;" \
  204. "2: swi %0, %2, 4;" \
  205. " addk %0, r0, r0;" \
  206. "3: " \
  207. __FIXUP_SECTION \
  208. "4: brid 3b;" \
  209. " addik %0, r0, %3;" \
  210. ".previous;" \
  211. __EX_TABLE_SECTION \
  212. ".word 1b,4b,2b,4b;" \
  213. ".previous;" \
  214. : "=&r"(__gu_err) \
  215. : "r"(&__gu_val), "r"(__gu_ptr), "i"(-EFAULT) \
  216. ); \
  217. })
  218. /**
  219. * put_user: - Write a simple value into user space.
  220. * @x: Value to copy to user space.
  221. * @ptr: Destination address, in user space.
  222. *
  223. * Context: User context only. This function may sleep.
  224. *
  225. * This macro copies a single simple value from kernel space to user
  226. * space. It supports simple types like char and int, but not larger
  227. * data types like structures or arrays.
  228. *
  229. * @ptr must have pointer-to-simple-variable type, and @x must be assignable
  230. * to the result of dereferencing @ptr.
  231. *
  232. * Returns zero on success, or -EFAULT on error.
  233. */
  234. #define __put_user(x, ptr) \
  235. ({ \
  236. __typeof__(*(ptr)) volatile __gu_val = (x); \
  237. long __gu_err = 0; \
  238. switch (sizeof(__gu_val)) { \
  239. case 1: \
  240. __put_user_asm("sb", (ptr), __gu_val, __gu_err); \
  241. break; \
  242. case 2: \
  243. __put_user_asm("sh", (ptr), __gu_val, __gu_err); \
  244. break; \
  245. case 4: \
  246. __put_user_asm("sw", (ptr), __gu_val, __gu_err); \
  247. break; \
  248. case 8: \
  249. __put_user_asm_8((ptr), __gu_val, __gu_err); \
  250. break; \
  251. default: \
  252. /*__gu_err = -EINVAL;*/ __gu_err = __user_bad(); \
  253. } \
  254. __gu_err; \
  255. })
  256. #ifndef CONFIG_MMU
  257. #define put_user(x, ptr) __put_user((x), (ptr))
  258. static inline long strnlen_user(const char __user *src, long count)
  259. {
  260. return strlen(src) + 1;
  261. }
  262. #define __do_strncpy_from_user(dst, src, count, res) \
  263. do { \
  264. char *tmp; \
  265. strncpy(dst, src, count); \
  266. for (tmp = dst; *tmp && count > 0; tmp++, count--) \
  267. ; \
  268. res = (tmp - dst); \
  269. } while (0)
  270. static inline long __strncpy_from_user(char *dst,
  271. const char __user *src, long count)
  272. {
  273. long res;
  274. __do_strncpy_from_user(dst, src, count, res);
  275. return res;
  276. }
  277. static inline long strncpy_from_user(char *dst,
  278. const char __user *src, long count)
  279. {
  280. long res = -EFAULT;
  281. if (access_ok(VERIFY_READ, src, 1))
  282. __do_strncpy_from_user(dst, src, count, res);
  283. return res;
  284. }
  285. static inline unsigned long __copy_tofrom_user(void __user *to,
  286. const void __user *from, unsigned long size)
  287. {
  288. memcpy(to, from, size);
  289. return 0;
  290. }
  291. #define copy_to_user(to, from, n) (memcpy((to), (from), (n)), 0)
  292. #define copy_from_user(to, from, n) (memcpy((to), (from), (n)), 0)
  293. #define __copy_to_user(to, from, n) (copy_to_user((to), (from), (n)))
  294. #define __copy_from_user(to, from, n) (copy_from_user((to), (from), (n)))
  295. #define __copy_to_user_inatomic(to, from, n) \
  296. (__copy_to_user((to), (from), (n)))
  297. #define __copy_from_user_inatomic(to, from, n) \
  298. (__copy_from_user((to), (from), (n)))
  299. extern long strncpy_from_user(char *dst, const char *src, long count);
  300. extern long strnlen_user(const char *src, long count);
  301. #else /* CONFIG_MMU */
  302. #define put_user(x, ptr) \
  303. ({ \
  304. access_ok(VERIFY_WRITE, (ptr), sizeof(*(ptr))) \
  305. ? __put_user((x), (ptr)) : -EFAULT; \
  306. })
  307. extern unsigned long __copy_tofrom_user(void __user *to,
  308. const void __user *from, unsigned long size);
  309. #define __copy_from_user(to, from, n) \
  310. __copy_tofrom_user((__force void __user *)(to), \
  311. (void __user *)(from), (n))
  312. #define __copy_from_user_inatomic(to, from, n) \
  313. copy_from_user((to), (from), (n))
  314. static inline long copy_from_user(void *to,
  315. const void __user *from, unsigned long n)
  316. {
  317. might_sleep();
  318. if (access_ok(VERIFY_READ, from, n))
  319. return __copy_from_user(to, from, n);
  320. else
  321. return n;
  322. }
  323. #define __copy_to_user(to, from, n) \
  324. __copy_tofrom_user((void __user *)(to), \
  325. (__force const void __user *)(from), (n))
  326. #define __copy_to_user_inatomic(to, from, n) copy_to_user((to), (from), (n))
  327. static inline long copy_to_user(void __user *to,
  328. const void *from, unsigned long n)
  329. {
  330. might_sleep();
  331. if (access_ok(VERIFY_WRITE, to, n))
  332. return __copy_to_user(to, from, n);
  333. else
  334. return n;
  335. }
  336. extern int __strncpy_user(char *to, const char __user *from, int len);
  337. #define __strncpy_from_user __strncpy_user
  338. static inline long
  339. strncpy_from_user(char *dst, const char __user *src, long count)
  340. {
  341. if (!access_ok(VERIFY_READ, src, 1))
  342. return -EFAULT;
  343. return __strncpy_from_user(dst, src, count);
  344. }
  345. extern int __strnlen_user(const char __user *sstr, int len);
  346. #define strnlen_user(str, len) \
  347. (access_ok(VERIFY_READ, str, 1) ? __strnlen_user(str, len) : 0)
  348. #endif /* CONFIG_MMU */
  349. #endif /* __ASSEMBLY__ */
  350. #endif /* __KERNEL__ */
  351. #endif /* _ASM_MICROBLAZE_UACCESS_H */