getuser.S 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * __get_user functions.
  4. *
  5. * (C) Copyright 1998 Linus Torvalds
  6. * (C) Copyright 2005 Andi Kleen
  7. * (C) Copyright 2008 Glauber Costa
  8. *
  9. * These functions have a non-standard call interface
  10. * to make them more efficient, especially as they
  11. * return an error value in addition to the "real"
  12. * return value.
  13. */
  14. /*
  15. * __get_user_X
  16. *
  17. * Inputs: %[r|e]ax contains the address.
  18. *
  19. * Outputs: %[r|e]ax is error code (0 or -EFAULT)
  20. * %[r|e]dx contains zero-extended value
  21. * %ecx contains the high half for 32-bit __get_user_8
  22. *
  23. *
  24. * These functions should not modify any other registers,
  25. * as they get called from within inline assembly.
  26. */
  27. #include <linux/linkage.h>
  28. #include <asm/page_types.h>
  29. #include <asm/errno.h>
  30. #include <asm/asm-offsets.h>
  31. #include <asm/thread_info.h>
  32. #include <asm/asm.h>
  33. #include <asm/smap.h>
  34. #include <asm/export.h>
  35. #define ASM_BARRIER_NOSPEC ALTERNATIVE "", "lfence", X86_FEATURE_LFENCE_RDTSC
  36. #ifdef CONFIG_X86_5LEVEL
  37. #define LOAD_TASK_SIZE_MINUS_N(n) \
  38. ALTERNATIVE __stringify(mov $((1 << 47) - 4096 - (n)),%rdx), \
  39. __stringify(mov $((1 << 56) - 4096 - (n)),%rdx), X86_FEATURE_LA57
  40. #else
  41. #define LOAD_TASK_SIZE_MINUS_N(n) \
  42. mov $(TASK_SIZE_MAX - (n)),%_ASM_DX
  43. #endif
  44. .text
  45. SYM_FUNC_START(__get_user_1)
  46. LOAD_TASK_SIZE_MINUS_N(0)
  47. cmp %_ASM_DX,%_ASM_AX
  48. jae bad_get_user
  49. sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
  50. and %_ASM_DX, %_ASM_AX
  51. ASM_STAC
  52. 1: movzbl (%_ASM_AX),%edx
  53. xor %eax,%eax
  54. ASM_CLAC
  55. RET
  56. SYM_FUNC_END(__get_user_1)
  57. EXPORT_SYMBOL(__get_user_1)
  58. SYM_FUNC_START(__get_user_2)
  59. LOAD_TASK_SIZE_MINUS_N(1)
  60. cmp %_ASM_DX,%_ASM_AX
  61. jae bad_get_user
  62. sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
  63. and %_ASM_DX, %_ASM_AX
  64. ASM_STAC
  65. 2: movzwl (%_ASM_AX),%edx
  66. xor %eax,%eax
  67. ASM_CLAC
  68. RET
  69. SYM_FUNC_END(__get_user_2)
  70. EXPORT_SYMBOL(__get_user_2)
  71. SYM_FUNC_START(__get_user_4)
  72. LOAD_TASK_SIZE_MINUS_N(3)
  73. cmp %_ASM_DX,%_ASM_AX
  74. jae bad_get_user
  75. sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
  76. and %_ASM_DX, %_ASM_AX
  77. ASM_STAC
  78. 3: movl (%_ASM_AX),%edx
  79. xor %eax,%eax
  80. ASM_CLAC
  81. RET
  82. SYM_FUNC_END(__get_user_4)
  83. EXPORT_SYMBOL(__get_user_4)
  84. SYM_FUNC_START(__get_user_8)
  85. #ifdef CONFIG_X86_64
  86. LOAD_TASK_SIZE_MINUS_N(7)
  87. cmp %_ASM_DX,%_ASM_AX
  88. jae bad_get_user
  89. sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
  90. and %_ASM_DX, %_ASM_AX
  91. ASM_STAC
  92. 4: movq (%_ASM_AX),%rdx
  93. xor %eax,%eax
  94. ASM_CLAC
  95. RET
  96. #else
  97. LOAD_TASK_SIZE_MINUS_N(7)
  98. cmp %_ASM_DX,%_ASM_AX
  99. jae bad_get_user_8
  100. sbb %_ASM_DX, %_ASM_DX /* array_index_mask_nospec() */
  101. and %_ASM_DX, %_ASM_AX
  102. ASM_STAC
  103. 4: movl (%_ASM_AX),%edx
  104. 5: movl 4(%_ASM_AX),%ecx
  105. xor %eax,%eax
  106. ASM_CLAC
  107. RET
  108. #endif
  109. SYM_FUNC_END(__get_user_8)
  110. EXPORT_SYMBOL(__get_user_8)
  111. /* .. and the same for __get_user, just without the range checks */
  112. SYM_FUNC_START(__get_user_nocheck_1)
  113. ASM_STAC
  114. ASM_BARRIER_NOSPEC
  115. 6: movzbl (%_ASM_AX),%edx
  116. xor %eax,%eax
  117. ASM_CLAC
  118. RET
  119. SYM_FUNC_END(__get_user_nocheck_1)
  120. EXPORT_SYMBOL(__get_user_nocheck_1)
  121. SYM_FUNC_START(__get_user_nocheck_2)
  122. ASM_STAC
  123. ASM_BARRIER_NOSPEC
  124. 7: movzwl (%_ASM_AX),%edx
  125. xor %eax,%eax
  126. ASM_CLAC
  127. RET
  128. SYM_FUNC_END(__get_user_nocheck_2)
  129. EXPORT_SYMBOL(__get_user_nocheck_2)
  130. SYM_FUNC_START(__get_user_nocheck_4)
  131. ASM_STAC
  132. ASM_BARRIER_NOSPEC
  133. 8: movl (%_ASM_AX),%edx
  134. xor %eax,%eax
  135. ASM_CLAC
  136. RET
  137. SYM_FUNC_END(__get_user_nocheck_4)
  138. EXPORT_SYMBOL(__get_user_nocheck_4)
  139. SYM_FUNC_START(__get_user_nocheck_8)
  140. ASM_STAC
  141. ASM_BARRIER_NOSPEC
  142. #ifdef CONFIG_X86_64
  143. 9: movq (%_ASM_AX),%rdx
  144. #else
  145. 9: movl (%_ASM_AX),%edx
  146. 10: movl 4(%_ASM_AX),%ecx
  147. #endif
  148. xor %eax,%eax
  149. ASM_CLAC
  150. RET
  151. SYM_FUNC_END(__get_user_nocheck_8)
  152. EXPORT_SYMBOL(__get_user_nocheck_8)
  153. SYM_CODE_START_LOCAL(.Lbad_get_user_clac)
  154. ASM_CLAC
  155. bad_get_user:
  156. xor %edx,%edx
  157. mov $(-EFAULT),%_ASM_AX
  158. RET
  159. SYM_CODE_END(.Lbad_get_user_clac)
  160. #ifdef CONFIG_X86_32
  161. SYM_CODE_START_LOCAL(.Lbad_get_user_8_clac)
  162. ASM_CLAC
  163. bad_get_user_8:
  164. xor %edx,%edx
  165. xor %ecx,%ecx
  166. mov $(-EFAULT),%_ASM_AX
  167. RET
  168. SYM_CODE_END(.Lbad_get_user_8_clac)
  169. #endif
  170. /* get_user */
  171. _ASM_EXTABLE_UA(1b, .Lbad_get_user_clac)
  172. _ASM_EXTABLE_UA(2b, .Lbad_get_user_clac)
  173. _ASM_EXTABLE_UA(3b, .Lbad_get_user_clac)
  174. #ifdef CONFIG_X86_64
  175. _ASM_EXTABLE_UA(4b, .Lbad_get_user_clac)
  176. #else
  177. _ASM_EXTABLE_UA(4b, .Lbad_get_user_8_clac)
  178. _ASM_EXTABLE_UA(5b, .Lbad_get_user_8_clac)
  179. #endif
  180. /* __get_user */
  181. _ASM_EXTABLE_UA(6b, .Lbad_get_user_clac)
  182. _ASM_EXTABLE_UA(7b, .Lbad_get_user_clac)
  183. _ASM_EXTABLE_UA(8b, .Lbad_get_user_clac)
  184. #ifdef CONFIG_X86_64
  185. _ASM_EXTABLE_UA(9b, .Lbad_get_user_clac)
  186. #else
  187. _ASM_EXTABLE_UA(9b, .Lbad_get_user_8_clac)
  188. _ASM_EXTABLE_UA(10b, .Lbad_get_user_8_clac)
  189. #endif