syscall.h 5.1 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. extern const unsigned long sys_call_table[];
  21. /*
  22. * Only the low 32 bits of orig_ax are meaningful, so we return int.
  23. * This importantly ignores the high bits on 64-bit, so comparisons
  24. * sign-extend the low 32 bits.
  25. */
  26. static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
  27. {
  28. return regs->orig_ax;
  29. }
  30. static inline void syscall_rollback(struct task_struct *task,
  31. struct pt_regs *regs)
  32. {
  33. regs->ax = regs->orig_ax;
  34. }
  35. static inline long syscall_get_error(struct task_struct *task,
  36. struct pt_regs *regs)
  37. {
  38. unsigned long error = regs->ax;
  39. #ifdef CONFIG_IA32_EMULATION
  40. /*
  41. * TS_COMPAT is set for 32-bit syscall entries and then
  42. * remains set until we return to user mode.
  43. */
  44. if (task_thread_info(task)->status & TS_COMPAT)
  45. /*
  46. * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
  47. * and will match correctly in comparisons.
  48. */
  49. error = (long) (int) error;
  50. #endif
  51. return IS_ERR_VALUE(error) ? error : 0;
  52. }
  53. static inline long syscall_get_return_value(struct task_struct *task,
  54. struct pt_regs *regs)
  55. {
  56. return regs->ax;
  57. }
  58. static inline void syscall_set_return_value(struct task_struct *task,
  59. struct pt_regs *regs,
  60. int error, long val)
  61. {
  62. regs->ax = (long) error ?: val;
  63. }
  64. #ifdef CONFIG_X86_32
  65. static inline void syscall_get_arguments(struct task_struct *task,
  66. struct pt_regs *regs,
  67. unsigned int i, unsigned int n,
  68. unsigned long *args)
  69. {
  70. BUG_ON(i + n > 6);
  71. memcpy(args, &regs->bx + i, n * sizeof(args[0]));
  72. }
  73. static inline void syscall_set_arguments(struct task_struct *task,
  74. struct pt_regs *regs,
  75. unsigned int i, unsigned int n,
  76. const unsigned long *args)
  77. {
  78. BUG_ON(i + n > 6);
  79. memcpy(&regs->bx + i, args, n * sizeof(args[0]));
  80. }
  81. static inline int syscall_get_arch(struct task_struct *task,
  82. struct pt_regs *regs)
  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(struct task_struct *task,
  208. struct pt_regs *regs)
  209. {
  210. #ifdef CONFIG_IA32_EMULATION
  211. /*
  212. * TS_COMPAT is set for 32-bit syscall entry and then
  213. * remains set until we return to user mode.
  214. *
  215. * TIF_IA32 tasks should always have TS_COMPAT set at
  216. * system call time.
  217. *
  218. * x32 tasks should be considered AUDIT_ARCH_X86_64.
  219. */
  220. if (task_thread_info(task)->status & TS_COMPAT)
  221. return AUDIT_ARCH_I386;
  222. #endif
  223. /* Both x32 and x86_64 are considered "64-bit". */
  224. return AUDIT_ARCH_X86_64;
  225. }
  226. #endif /* CONFIG_X86_32 */
  227. #endif /* _ASM_X86_SYSCALL_H */