syscall.c 2.4 KB

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