syscall.c 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/ptrace.h>
  3. #include <linux/sched.h>
  4. #include <linux/sched/task_stack.h>
  5. #include <linux/export.h>
  6. #include <asm/syscall.h>
  7. static int collect_syscall(struct task_struct *target, long *callno,
  8. unsigned long args[6], unsigned int maxargs,
  9. unsigned long *sp, unsigned long *pc)
  10. {
  11. struct pt_regs *regs;
  12. if (!try_get_task_stack(target)) {
  13. /* Task has no stack, so the task isn't in a syscall. */
  14. *sp = *pc = 0;
  15. *callno = -1;
  16. return 0;
  17. }
  18. regs = task_pt_regs(target);
  19. if (unlikely(!regs)) {
  20. put_task_stack(target);
  21. return -EAGAIN;
  22. }
  23. *sp = user_stack_pointer(regs);
  24. *pc = instruction_pointer(regs);
  25. *callno = syscall_get_nr(target, regs);
  26. if (*callno != -1L && maxargs > 0)
  27. syscall_get_arguments(target, regs, 0, maxargs, args);
  28. put_task_stack(target);
  29. return 0;
  30. }
  31. /**
  32. * task_current_syscall - Discover what a blocked task is doing.
  33. * @target: thread to examine
  34. * @callno: filled with system call number or -1
  35. * @args: filled with @maxargs system call arguments
  36. * @maxargs: number of elements in @args to fill
  37. * @sp: filled with user stack pointer
  38. * @pc: filled with user PC
  39. *
  40. * If @target is blocked in a system call, returns zero with *@callno
  41. * set to the the call's number and @args filled in with its arguments.
  42. * Registers not used for system call arguments may not be available and
  43. * it is not kosher to use &struct user_regset calls while the system
  44. * call is still in progress. Note we may get this result if @target
  45. * has finished its system call but not yet returned to user mode, such
  46. * as when it's stopped for signal handling or syscall exit tracing.
  47. *
  48. * If @target is blocked in the kernel during a fault or exception,
  49. * returns zero with *@callno set to -1 and does not fill in @args.
  50. * If so, it's now safe to examine @target using &struct user_regset
  51. * get() calls as long as we're sure @target won't return to user mode.
  52. *
  53. * Returns -%EAGAIN if @target does not remain blocked.
  54. *
  55. * Returns -%EINVAL if @maxargs is too large (maximum is six).
  56. */
  57. int task_current_syscall(struct task_struct *target, long *callno,
  58. unsigned long args[6], unsigned int maxargs,
  59. unsigned long *sp, unsigned long *pc)
  60. {
  61. long state;
  62. unsigned long ncsw;
  63. if (unlikely(maxargs > 6))
  64. return -EINVAL;
  65. if (target == current)
  66. return collect_syscall(target, callno, args, maxargs, sp, pc);
  67. state = target->state;
  68. if (unlikely(!state))
  69. return -EAGAIN;
  70. ncsw = wait_task_inactive(target, state);
  71. if (unlikely(!ncsw) ||
  72. unlikely(collect_syscall(target, callno, args, maxargs, sp, pc)) ||
  73. unlikely(wait_task_inactive(target, state) != ncsw))
  74. return -EAGAIN;
  75. return 0;
  76. }