Blame view

lib/syscall.c 2.49 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
bbc698636   Roland McGrath   task_current_syscall
2
3
  #include <linux/ptrace.h>
  #include <linux/sched.h>
68db0cf10   Ingo Molnar   sched/headers: Pr...
4
  #include <linux/sched/task_stack.h>
8bc3bcc93   Paul Gortmaker   lib: reduce the u...
5
  #include <linux/export.h>
bbc698636   Roland McGrath   task_current_syscall
6
  #include <asm/syscall.h>
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
7
  static int collect_syscall(struct task_struct *target, struct syscall_info *info)
bbc698636   Roland McGrath   task_current_syscall
8
  {
aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
9
10
11
12
  	struct pt_regs *regs;
  
  	if (!try_get_task_stack(target)) {
  		/* Task has no stack, so the task isn't in a syscall. */
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
13
14
  		memset(info, 0, sizeof(*info));
  		info->data.nr = -1;
aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
15
16
17
18
19
20
  		return 0;
  	}
  
  	regs = task_pt_regs(target);
  	if (unlikely(!regs)) {
  		put_task_stack(target);
bbc698636   Roland McGrath   task_current_syscall
21
  		return -EAGAIN;
aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
22
  	}
bbc698636   Roland McGrath   task_current_syscall
23

631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
24
25
  	info->sp = user_stack_pointer(regs);
  	info->data.instruction_pointer = instruction_pointer(regs);
bbc698636   Roland McGrath   task_current_syscall
26

631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
27
28
  	info->data.nr = syscall_get_nr(target, regs);
  	if (info->data.nr != -1L)
b35f549df   Steven Rostedt (Red Hat)   syscalls: Remove ...
29
  		syscall_get_arguments(target, regs,
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
30
  				      (unsigned long *)&info->data.args[0]);
bbc698636   Roland McGrath   task_current_syscall
31

aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
32
  	put_task_stack(target);
bbc698636   Roland McGrath   task_current_syscall
33
34
35
36
37
38
  	return 0;
  }
  
  /**
   * task_current_syscall - Discover what a blocked task is doing.
   * @target:		thread to examine
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
39
40
41
42
43
   * @info:		structure with the following fields:
   *			 .sp        - filled with user stack pointer
   *			 .data.nr   - filled with system call number or -1
   *			 .data.args - filled with @maxargs system call arguments
   *			 .data.instruction_pointer - filled with user PC
bbc698636   Roland McGrath   task_current_syscall
44
   *
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
45
46
47
48
   * If @target is blocked in a system call, returns zero with @info.data.nr
   * set to the the call's number and @info.data.args filled in with its
   * arguments. Registers not used for system call arguments may not be available
   * and it is not kosher to use &struct user_regset calls while the system
bbc698636   Roland McGrath   task_current_syscall
49
50
51
52
53
   * call is still in progress.  Note we may get this result if @target
   * has finished its system call but not yet returned to user mode, such
   * as when it's stopped for signal handling or syscall exit tracing.
   *
   * If @target is blocked in the kernel during a fault or exception,
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
54
55
56
57
   * returns zero with *@info.data.nr set to -1 and does not fill in
   * @info.data.args. If so, it's now safe to examine @target using
   * &struct user_regset get() calls as long as we're sure @target won't return
   * to user mode.
bbc698636   Roland McGrath   task_current_syscall
58
59
   *
   * Returns -%EAGAIN if @target does not remain blocked.
bbc698636   Roland McGrath   task_current_syscall
60
   */
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
61
  int task_current_syscall(struct task_struct *target, struct syscall_info *info)
bbc698636   Roland McGrath   task_current_syscall
62
63
64
  {
  	long state;
  	unsigned long ncsw;
bbc698636   Roland McGrath   task_current_syscall
65
  	if (target == current)
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
66
  		return collect_syscall(target, info);
bbc698636   Roland McGrath   task_current_syscall
67
68
69
70
71
72
73
  
  	state = target->state;
  	if (unlikely(!state))
  		return -EAGAIN;
  
  	ncsw = wait_task_inactive(target, state);
  	if (unlikely(!ncsw) ||
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
74
  	    unlikely(collect_syscall(target, info)) ||
bbc698636   Roland McGrath   task_current_syscall
75
76
77
78
79
  	    unlikely(wait_task_inactive(target, state) != ncsw))
  		return -EAGAIN;
  
  	return 0;
  }