Blame view

lib/syscall.c 2.66 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
  {
4f134b89a   Willy Tarreau   lib/syscall: fix ...
9
  	unsigned long args[6] = { };
aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
10
11
12
13
  	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...
14
15
  		memset(info, 0, sizeof(*info));
  		info->data.nr = -1;
aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
16
17
18
19
20
21
  		return 0;
  	}
  
  	regs = task_pt_regs(target);
  	if (unlikely(!regs)) {
  		put_task_stack(target);
bbc698636   Roland McGrath   task_current_syscall
22
  		return -EAGAIN;
aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
23
  	}
bbc698636   Roland McGrath   task_current_syscall
24

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

631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
28
29
  	info->data.nr = syscall_get_nr(target, regs);
  	if (info->data.nr != -1L)
4f134b89a   Willy Tarreau   lib/syscall: fix ...
30
31
32
33
34
35
36
37
  		syscall_get_arguments(target, regs, args);
  
  	info->data.args[0] = args[0];
  	info->data.args[1] = args[1];
  	info->data.args[2] = args[2];
  	info->data.args[3] = args[3];
  	info->data.args[4] = args[4];
  	info->data.args[5] = args[5];
bbc698636   Roland McGrath   task_current_syscall
38

aa1f1a639   Andy Lutomirski   lib/syscall: Pin ...
39
  	put_task_stack(target);
bbc698636   Roland McGrath   task_current_syscall
40
41
42
43
44
45
  	return 0;
  }
  
  /**
   * task_current_syscall - Discover what a blocked task is doing.
   * @target:		thread to examine
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
46
47
48
49
50
   * @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
51
   *
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
52
   * If @target is blocked in a system call, returns zero with @info.data.nr
408a93a2b   Randy Dunlap   lib: syscall: del...
53
   * set to the call's number and @info.data.args filled in with its
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
54
55
   * 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
56
57
58
59
60
   * 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...
61
62
63
64
   * 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
65
66
   *
   * Returns -%EAGAIN if @target does not remain blocked.
bbc698636   Roland McGrath   task_current_syscall
67
   */
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
68
  int task_current_syscall(struct task_struct *target, struct syscall_info *info)
bbc698636   Roland McGrath   task_current_syscall
69
70
71
  {
  	long state;
  	unsigned long ncsw;
bbc698636   Roland McGrath   task_current_syscall
72
  	if (target == current)
631b7abac   Steven Rostedt (Red Hat)   ptrace: Remove ma...
73
  		return collect_syscall(target, info);
bbc698636   Roland McGrath   task_current_syscall
74
75
76
77
78
79
80
  
  	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...
81
  	    unlikely(collect_syscall(target, info)) ||
bbc698636   Roland McGrath   task_current_syscall
82
83
84
85
86
  	    unlikely(wait_task_inactive(target, state) != ncsw))
  		return -EAGAIN;
  
  	return 0;
  }