syscall.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (C) 2012 ARM Ltd.
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License version 2 as
  6. * published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #ifndef __ASM_SYSCALL_H
  17. #define __ASM_SYSCALL_H
  18. #include <uapi/linux/audit.h>
  19. #include <linux/compat.h>
  20. #include <linux/err.h>
  21. extern const void *sys_call_table[];
  22. static inline int syscall_get_nr(struct task_struct *task,
  23. struct pt_regs *regs)
  24. {
  25. return regs->syscallno;
  26. }
  27. static inline void syscall_rollback(struct task_struct *task,
  28. struct pt_regs *regs)
  29. {
  30. regs->regs[0] = regs->orig_x0;
  31. }
  32. static inline long syscall_get_error(struct task_struct *task,
  33. struct pt_regs *regs)
  34. {
  35. unsigned long error = regs->regs[0];
  36. return IS_ERR_VALUE(error) ? error : 0;
  37. }
  38. static inline long syscall_get_return_value(struct task_struct *task,
  39. struct pt_regs *regs)
  40. {
  41. return regs->regs[0];
  42. }
  43. static inline void syscall_set_return_value(struct task_struct *task,
  44. struct pt_regs *regs,
  45. int error, long val)
  46. {
  47. regs->regs[0] = (long) error ? error : val;
  48. }
  49. #define SYSCALL_MAX_ARGS 6
  50. static inline void syscall_get_arguments(struct task_struct *task,
  51. struct pt_regs *regs,
  52. unsigned int i, unsigned int n,
  53. unsigned long *args)
  54. {
  55. if (n == 0)
  56. return;
  57. if (i + n > SYSCALL_MAX_ARGS) {
  58. unsigned long *args_bad = args + SYSCALL_MAX_ARGS - i;
  59. unsigned int n_bad = n + i - SYSCALL_MAX_ARGS;
  60. pr_warning("%s called with max args %d, handling only %d\n",
  61. __func__, i + n, SYSCALL_MAX_ARGS);
  62. memset(args_bad, 0, n_bad * sizeof(args[0]));
  63. }
  64. if (i == 0) {
  65. args[0] = regs->orig_x0;
  66. args++;
  67. i++;
  68. n--;
  69. }
  70. memcpy(args, &regs->regs[i], n * sizeof(args[0]));
  71. }
  72. static inline void syscall_set_arguments(struct task_struct *task,
  73. struct pt_regs *regs,
  74. unsigned int i, unsigned int n,
  75. const unsigned long *args)
  76. {
  77. if (n == 0)
  78. return;
  79. if (i + n > SYSCALL_MAX_ARGS) {
  80. pr_warning("%s called with max args %d, handling only %d\n",
  81. __func__, i + n, SYSCALL_MAX_ARGS);
  82. n = SYSCALL_MAX_ARGS - i;
  83. }
  84. if (i == 0) {
  85. regs->orig_x0 = args[0];
  86. args++;
  87. i++;
  88. n--;
  89. }
  90. memcpy(&regs->regs[i], args, n * sizeof(args[0]));
  91. }
  92. /*
  93. * We don't care about endianness (__AUDIT_ARCH_LE bit) here because
  94. * AArch64 has the same system calls both on little- and big- endian.
  95. */
  96. static inline int syscall_get_arch(void)
  97. {
  98. if (is_compat_task())
  99. return AUDIT_ARCH_ARM;
  100. return AUDIT_ARCH_AARCH64;
  101. }
  102. #endif /* __ASM_SYSCALL_H */