syscall.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. typedef long (*syscall_fn_t)(const struct pt_regs *regs);
  22. extern const syscall_fn_t sys_call_table[];
  23. #ifdef CONFIG_COMPAT
  24. extern const syscall_fn_t compat_sys_call_table[];
  25. #endif
  26. static inline int syscall_get_nr(struct task_struct *task,
  27. struct pt_regs *regs)
  28. {
  29. return regs->syscallno;
  30. }
  31. static inline void syscall_rollback(struct task_struct *task,
  32. struct pt_regs *regs)
  33. {
  34. regs->regs[0] = regs->orig_x0;
  35. }
  36. static inline long syscall_get_error(struct task_struct *task,
  37. struct pt_regs *regs)
  38. {
  39. unsigned long error = regs->regs[0];
  40. if (is_compat_thread(task_thread_info(task)))
  41. error = sign_extend64(error, 31);
  42. return IS_ERR_VALUE(error) ? error : 0;
  43. }
  44. static inline long syscall_get_return_value(struct task_struct *task,
  45. struct pt_regs *regs)
  46. {
  47. return regs->regs[0];
  48. }
  49. static inline void syscall_set_return_value(struct task_struct *task,
  50. struct pt_regs *regs,
  51. int error, long val)
  52. {
  53. if (error)
  54. val = error;
  55. if (is_compat_thread(task_thread_info(task)))
  56. val = lower_32_bits(val);
  57. regs->regs[0] = val;
  58. }
  59. #define SYSCALL_MAX_ARGS 6
  60. static inline void syscall_get_arguments(struct task_struct *task,
  61. struct pt_regs *regs,
  62. unsigned int i, unsigned int n,
  63. unsigned long *args)
  64. {
  65. if (n == 0)
  66. return;
  67. if (i + n > SYSCALL_MAX_ARGS) {
  68. unsigned long *args_bad = args + SYSCALL_MAX_ARGS - i;
  69. unsigned int n_bad = n + i - SYSCALL_MAX_ARGS;
  70. pr_warning("%s called with max args %d, handling only %d\n",
  71. __func__, i + n, SYSCALL_MAX_ARGS);
  72. memset(args_bad, 0, n_bad * sizeof(args[0]));
  73. }
  74. if (i == 0) {
  75. args[0] = regs->orig_x0;
  76. args++;
  77. i++;
  78. n--;
  79. }
  80. memcpy(args, &regs->regs[i], n * sizeof(args[0]));
  81. }
  82. static inline void syscall_set_arguments(struct task_struct *task,
  83. struct pt_regs *regs,
  84. unsigned int i, unsigned int n,
  85. const unsigned long *args)
  86. {
  87. if (n == 0)
  88. return;
  89. if (i + n > SYSCALL_MAX_ARGS) {
  90. pr_warning("%s called with max args %d, handling only %d\n",
  91. __func__, i + n, SYSCALL_MAX_ARGS);
  92. n = SYSCALL_MAX_ARGS - i;
  93. }
  94. if (i == 0) {
  95. regs->orig_x0 = args[0];
  96. args++;
  97. i++;
  98. n--;
  99. }
  100. memcpy(&regs->regs[i], args, n * sizeof(args[0]));
  101. }
  102. /*
  103. * We don't care about endianness (__AUDIT_ARCH_LE bit) here because
  104. * AArch64 has the same system calls both on little- and big- endian.
  105. */
  106. static inline int syscall_get_arch(void)
  107. {
  108. if (is_compat_task())
  109. return AUDIT_ARCH_ARM;
  110. return AUDIT_ARCH_AARCH64;
  111. }
  112. #endif /* __ASM_SYSCALL_H */