uaccess.h 11 KB

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