syscall.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 <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(struct task_struct *task,
  83. struct pt_regs *regs)
  84. {
  85. return AUDIT_ARCH_I386;
  86. }
  87. #else /* CONFIG_X86_64 */
  88. static inline void syscall_get_arguments(struct task_struct *task,
  89. struct pt_regs *regs,
  90. unsigned int i, unsigned int n,
  91. unsigned long *args)
  92. {
  93. # ifdef CONFIG_IA32_EMULATION
  94. if (task_thread_info(task)->status & TS_COMPAT)
  95. switch (i) {
  96. case 0:
  97. if (!n--) break;
  98. *args++ = regs->bx;
  99. case 1:
  100. if (!n--) break;
  101. *args++ = regs->cx;
  102. case 2:
  103. if (!n--) break;
  104. *args++ = regs->dx;
  105. case 3:
  106. if (!n--) break;
  107. *args++ = regs->si;
  108. case 4:
  109. if (!n--) break;
  110. *args++ = regs->di;
  111. case 5:
  112. if (!n--) break;
  113. *args++ = regs->bp;
  114. case 6:
  115. if (!n--) break;
  116. default:
  117. BUG();
  118. break;
  119. }
  120. else
  121. # endif
  122. switch (i) {
  123. case 0:
  124. if (!n--) break;
  125. *args++ = regs->di;
  126. case 1:
  127. if (!n--) break;
  128. *args++ = regs->si;
  129. case 2:
  130. if (!n--) break;
  131. *args++ = regs->dx;
  132. case 3:
  133. if (!n--) break;
  134. *args++ = regs->r10;
  135. case 4:
  136. if (!n--) break;
  137. *args++ = regs->r8;
  138. case 5:
  139. if (!n--) break;
  140. *args++ = regs->r9;
  141. case 6:
  142. if (!n--) break;
  143. default:
  144. BUG();
  145. break;
  146. }
  147. }
  148. static inline void syscall_set_arguments(struct task_struct *task,
  149. struct pt_regs *regs,
  150. unsigned int i, unsigned int n,
  151. const unsigned long *args)
  152. {
  153. # ifdef CONFIG_IA32_EMULATION
  154. if (task_thread_info(task)->status & TS_COMPAT)
  155. switch (i) {
  156. case 0:
  157. if (!n--) break;
  158. regs->bx = *args++;
  159. case 1:
  160. if (!n--) break;
  161. regs->cx = *args++;
  162. case 2:
  163. if (!n--) break;
  164. regs->dx = *args++;
  165. case 3:
  166. if (!n--) break;
  167. regs->si = *args++;
  168. case 4:
  169. if (!n--) break;
  170. regs->di = *args++;
  171. case 5:
  172. if (!n--) break;
  173. regs->bp = *args++;
  174. case 6:
  175. if (!n--) break;
  176. default:
  177. BUG();
  178. break;
  179. }
  180. else
  181. # endif
  182. switch (i) {
  183. case 0:
  184. if (!n--) break;
  185. regs->di = *args++;
  186. case 1:
  187. if (!n--) break;
  188. regs->si = *args++;
  189. case 2:
  190. if (!n--) break;
  191. regs->dx = *args++;
  192. case 3:
  193. if (!n--) break;
  194. regs->r10 = *args++;
  195. case 4:
  196. if (!n--) break;
  197. regs->r8 = *args++;
  198. case 5:
  199. if (!n--) break;
  200. regs->r9 = *args++;
  201. case 6:
  202. if (!n--) break;
  203. default:
  204. BUG();
  205. break;
  206. }
  207. }
  208. static inline int syscall_get_arch(struct task_struct *task,
  209. struct pt_regs *regs)
  210. {
  211. #ifdef CONFIG_IA32_EMULATION
  212. /*
  213. * TS_COMPAT is set for 32-bit syscall entry and then
  214. * remains set until we return to user mode.
  215. *
  216. * TIF_IA32 tasks should always have TS_COMPAT set at
  217. * system call time.
  218. *
  219. * x32 tasks should be considered AUDIT_ARCH_X86_64.
  220. */
  221. if (task_thread_info(task)->status & TS_COMPAT)
  222. return AUDIT_ARCH_I386;
  223. #endif
  224. /* Both x32 and x86_64 are considered "64-bit". */
  225. return AUDIT_ARCH_X86_64;
  226. }
  227. #endif /* CONFIG_X86_32 */
  228. #endif /* _ASM_X86_SYSCALL_H */