syscall.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Access to user system call parameters and results
  3. *
  4. * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
  5. *
  6. * This copyrighted material is made available to anyone wishing to use,
  7. * modify, copy, or redistribute it subject to the terms and conditions
  8. * of the GNU General Public License v.2.
  9. *
  10. * See asm-generic/syscall.h for descriptions of what we must do here.
  11. */
  12. #ifndef _ASM_X86_SYSCALL_H
  13. #define _ASM_X86_SYSCALL_H
  14. #include <uapi/linux/audit.h>
  15. #include <linux/sched.h>
  16. #include <linux/err.h>
  17. #include <asm/asm-offsets.h> /* For NR_syscalls */
  18. #include <asm/thread_info.h> /* for TS_COMPAT */
  19. #include <asm/unistd.h>
  20. typedef void (*sys_call_ptr_t)(void);
  21. extern const sys_call_ptr_t sys_call_table[];
  22. /*
  23. * Only the low 32 bits of orig_ax are meaningful, so we return int.
  24. * This importantly ignores the high bits on 64-bit, so comparisons
  25. * sign-extend the low 32 bits.
  26. */
  27. static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
  28. {
  29. return regs->orig_ax;
  30. }
  31. static inline void syscall_rollback(struct task_struct *task,
  32. struct pt_regs *regs)
  33. {
  34. regs->ax = regs->orig_ax;
  35. }
  36. static inline long syscall_get_error(struct task_struct *task,
  37. struct pt_regs *regs)
  38. {
  39. unsigned long error = regs->ax;
  40. #ifdef CONFIG_IA32_EMULATION
  41. /*
  42. * TS_COMPAT is set for 32-bit syscall entries and then
  43. * remains set until we return to user mode.
  44. */
  45. if (task_thread_info(task)->status & TS_COMPAT)
  46. /*
  47. * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
  48. * and will match correctly in comparisons.
  49. */
  50. error = (long) (int) error;
  51. #endif
  52. return IS_ERR_VALUE(error) ? error : 0;
  53. }
  54. static inline long syscall_get_return_value(struct task_struct *task,
  55. struct pt_regs *regs)
  56. {
  57. return regs->ax;
  58. }
  59. static inline void syscall_set_return_value(struct task_struct *task,
  60. struct pt_regs *regs,
  61. int error, long val)
  62. {
  63. regs->ax = (long) error ?: val;
  64. }
  65. #ifdef CONFIG_X86_32
  66. static inline void syscall_get_arguments(struct task_struct *task,
  67. struct pt_regs *regs,
  68. unsigned int i, unsigned int n,
  69. unsigned long *args)
  70. {
  71. BUG_ON(i + n > 6);
  72. memcpy(args, &regs->bx + i, n * sizeof(args[0]));
  73. }
  74. static inline void syscall_set_arguments(struct task_struct *task,
  75. struct pt_regs *regs,
  76. unsigned int i, unsigned int n,
  77. const unsigned long *args)
  78. {
  79. BUG_ON(i + n > 6);
  80. memcpy(&regs->bx + i, args, n * sizeof(args[0]));
  81. }
  82. static inline int syscall_get_arch(void)
  83. {
  84. return AUDIT_ARCH_I386;
  85. }
  86. #else /* CONFIG_X86_64 */
  87. static inline void syscall_get_arguments(struct task_struct *task,
  88. struct pt_regs *regs,
  89. unsigned int i, unsigned int n,
  90. unsigned long *args)
  91. {
  92. # ifdef CONFIG_IA32_EMULATION
  93. if (task_thread_info(task)->status & TS_COMPAT)
  94. switch (i) {
  95. case 0:
  96. if (!n--) break;
  97. *args++ = regs->bx;
  98. case 1:
  99. if (!n--) break;
  100. *args++ = regs->cx;
  101. case 2:
  102. if (!n--) break;
  103. *args++ = regs->dx;
  104. case 3:
  105. if (!n--) break;
  106. *args++ = regs->si;
  107. case 4:
  108. if (!n--) break;
  109. *args++ = regs->di;
  110. case 5:
  111. if (!n--) break;
  112. *args++ = regs->bp;
  113. case 6:
  114. if (!n--) break;
  115. default:
  116. BUG();
  117. break;
  118. }
  119. else
  120. # endif
  121. switch (i) {
  122. case 0:
  123. if (!n--) break;
  124. *args++ = regs->di;
  125. case 1:
  126. if (!n--) break;
  127. *args++ = regs->si;
  128. case 2:
  129. if (!n--) break;
  130. *args++ = regs->dx;
  131. case 3:
  132. if (!n--) break;
  133. *args++ = regs->r10;
  134. case 4:
  135. if (!n--) break;
  136. *args++ = regs->r8;
  137. case 5:
  138. if (!n--) break;
  139. *args++ = regs->r9;
  140. case 6:
  141. if (!n--) break;
  142. default:
  143. BUG();
  144. break;
  145. }
  146. }
  147. static inline void syscall_set_arguments(struct task_struct *task,
  148. struct pt_regs *regs,
  149. unsigned int i, unsigned int n,
  150. const unsigned long *args)
  151. {
  152. # ifdef CONFIG_IA32_EMULATION
  153. if (task_thread_info(task)->status & TS_COMPAT)
  154. switch (i) {
  155. case 0:
  156. if (!n--) break;
  157. regs->bx = *args++;
  158. case 1:
  159. if (!n--) break;
  160. regs->cx = *args++;
  161. case 2:
  162. if (!n--) break;
  163. regs->dx = *args++;
  164. case 3:
  165. if (!n--) break;
  166. regs->si = *args++;
  167. case 4:
  168. if (!n--) break;
  169. regs->di = *args++;
  170. case 5:
  171. if (!n--) break;
  172. regs->bp = *args++;
  173. case 6:
  174. if (!n--) break;
  175. default:
  176. BUG();
  177. break;
  178. }
  179. else
  180. # endif
  181. switch (i) {
  182. case 0:
  183. if (!n--) break;
  184. regs->di = *args++;
  185. case 1:
  186. if (!n--) break;
  187. regs->si = *args++;
  188. case 2:
  189. if (!n--) break;
  190. regs->dx = *args++;
  191. case 3:
  192. if (!n--) break;
  193. regs->r10 = *args++;
  194. case 4:
  195. if (!n--) break;
  196. regs->r8 = *args++;
  197. case 5:
  198. if (!n--) break;
  199. regs->r9 = *args++;
  200. case 6:
  201. if (!n--) break;
  202. default:
  203. BUG();
  204. break;
  205. }
  206. }
  207. static inline int syscall_get_arch(void)
  208. {
  209. #ifdef CONFIG_IA32_EMULATION
  210. /*
  211. * TS_COMPAT is set for 32-bit syscall entry and then
  212. * remains set until we return to user mode.
  213. *
  214. * TIF_IA32 tasks should always have TS_COMPAT set at
  215. * system call time.
  216. *
  217. * x32 tasks should be considered AUDIT_ARCH_X86_64.
  218. */
  219. if (task_thread_info(current)->status & TS_COMPAT)
  220. return AUDIT_ARCH_I386;
  221. #endif
  222. /* Both x32 and x86_64 are considered "64-bit". */
  223. return AUDIT_ARCH_X86_64;
  224. }
  225. #endif /* CONFIG_X86_32 */
  226. #endif /* _ASM_X86_SYSCALL_H */