uaccess.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * User memory access support for Hexagon
  3. *
  4. * Copyright (c) 2010-2011, Code Aurora Forum. All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 and
  8. * only version 2 as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA.
  19. */
  20. #ifndef _ASM_UACCESS_H
  21. #define _ASM_UACCESS_H
  22. /*
  23. * User space memory access functions
  24. */
  25. #include <linux/sched.h>
  26. #include <linux/mm.h>
  27. #include <asm/segment.h>
  28. #include <asm/sections.h>
  29. /*
  30. * access_ok: - Checks if a user space pointer is valid
  31. * @type: Type of access: %VERIFY_READ or %VERIFY_WRITE. Note that
  32. * %VERIFY_WRITE is a superset of %VERIFY_READ - if it is safe
  33. * to write to a block, it is always safe to read from it.
  34. * @addr: User space pointer to start of block to check
  35. * @size: Size of block to check
  36. *
  37. * Context: User context only. This function may sleep.
  38. *
  39. * Checks if a pointer to a block of memory in user space is valid.
  40. *
  41. * Returns true (nonzero) if the memory block *may* be valid, false (zero)
  42. * if it is definitely invalid.
  43. *
  44. * User address space in Hexagon, like x86, goes to 0xbfffffff, so the
  45. * simple MSB-based tests used by MIPS won't work. Some further
  46. * optimization is probably possible here, but for now, keep it
  47. * reasonably simple and not *too* slow. After all, we've got the
  48. * MMU for backup.
  49. */
  50. #define VERIFY_READ 0
  51. #define VERIFY_WRITE 1
  52. #define __access_ok(addr, size) \
  53. ((get_fs().seg == KERNEL_DS.seg) || \
  54. (((unsigned long)addr < get_fs().seg) && \
  55. (unsigned long)size < (get_fs().seg - (unsigned long)addr)))
  56. /*
  57. * When a kernel-mode page fault is taken, the faulting instruction
  58. * address is checked against a table of exception_table_entries.
  59. * Each entry is a tuple of the address of an instruction that may
  60. * be authorized to fault, and the address at which execution should
  61. * be resumed instead of the faulting instruction, so as to effect
  62. * a workaround.
  63. */
  64. /* Assembly somewhat optimized copy routines */
  65. unsigned long __copy_from_user_hexagon(void *to, const void __user *from,
  66. unsigned long n);
  67. unsigned long __copy_to_user_hexagon(void __user *to, const void *from,
  68. unsigned long n);
  69. #define __copy_from_user(to, from, n) __copy_from_user_hexagon(to, from, n)
  70. #define __copy_to_user(to, from, n) __copy_to_user_hexagon(to, from, n)
  71. /*
  72. * XXX todo: some additonal performance gain is possible by
  73. * implementing __copy_to/from_user_inatomic, which is much
  74. * like __copy_to/from_user, but performs slightly less checking.
  75. */
  76. __kernel_size_t __clear_user_hexagon(void __user *dest, unsigned long count);
  77. #define __clear_user(a, s) __clear_user_hexagon((a), (s))
  78. #define __strncpy_from_user(dst, src, n) hexagon_strncpy_from_user(dst, src, n)
  79. /* get around the ifndef in asm-generic/uaccess.h */
  80. #define __strnlen_user __strnlen_user
  81. extern long __strnlen_user(const char __user *src, long n);
  82. static inline long hexagon_strncpy_from_user(char *dst, const char __user *src,
  83. long n);
  84. #include <asm-generic/uaccess.h>
  85. /* Todo: an actual accelerated version of this. */
  86. static inline long hexagon_strncpy_from_user(char *dst, const char __user *src,
  87. long n)
  88. {
  89. long res = __strnlen_user(src, n);
  90. /* return from strnlen can't be zero -- that would be rubbish. */
  91. if (res > n) {
  92. copy_from_user(dst, src, n);
  93. return n;
  94. } else {
  95. copy_from_user(dst, src, res);
  96. return res-1;
  97. }
  98. }
  99. #endif