xref: /openbmc/linux/lib/syscall.c (revision 9dbbc3b9)
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 
8 static int collect_syscall(struct task_struct *target, struct syscall_info *info)
9 {
10 	unsigned long args[6] = { };
11 	struct pt_regs *regs;
12 
13 	if (!try_get_task_stack(target)) {
14 		/* Task has no stack, so the task isn't in a syscall. */
15 		memset(info, 0, sizeof(*info));
16 		info->data.nr = -1;
17 		return 0;
18 	}
19 
20 	regs = task_pt_regs(target);
21 	if (unlikely(!regs)) {
22 		put_task_stack(target);
23 		return -EAGAIN;
24 	}
25 
26 	info->sp = user_stack_pointer(regs);
27 	info->data.instruction_pointer = instruction_pointer(regs);
28 
29 	info->data.nr = syscall_get_nr(target, regs);
30 	if (info->data.nr != -1L)
31 		syscall_get_arguments(target, regs, args);
32 
33 	info->data.args[0] = args[0];
34 	info->data.args[1] = args[1];
35 	info->data.args[2] = args[2];
36 	info->data.args[3] = args[3];
37 	info->data.args[4] = args[4];
38 	info->data.args[5] = args[5];
39 
40 	put_task_stack(target);
41 	return 0;
42 }
43 
44 /**
45  * task_current_syscall - Discover what a blocked task is doing.
46  * @target:		thread to examine
47  * @info:		structure with the following fields:
48  *			 .sp        - filled with user stack pointer
49  *			 .data.nr   - filled with system call number or -1
50  *			 .data.args - filled with @maxargs system call arguments
51  *			 .data.instruction_pointer - filled with user PC
52  *
53  * If @target is blocked in a system call, returns zero with @info.data.nr
54  * set to the call's number and @info.data.args filled in with its
55  * arguments. Registers not used for system call arguments may not be available
56  * and it is not kosher to use &struct user_regset calls while the system
57  * call is still in progress.  Note we may get this result if @target
58  * has finished its system call but not yet returned to user mode, such
59  * as when it's stopped for signal handling or syscall exit tracing.
60  *
61  * If @target is blocked in the kernel during a fault or exception,
62  * returns zero with *@info.data.nr set to -1 and does not fill in
63  * @info.data.args. If so, it's now safe to examine @target using
64  * &struct user_regset get() calls as long as we're sure @target won't return
65  * to user mode.
66  *
67  * Returns -%EAGAIN if @target does not remain blocked.
68  */
69 int task_current_syscall(struct task_struct *target, struct syscall_info *info)
70 {
71 	unsigned long ncsw;
72 	unsigned int state;
73 
74 	if (target == current)
75 		return collect_syscall(target, info);
76 
77 	state = READ_ONCE(target->__state);
78 	if (unlikely(!state))
79 		return -EAGAIN;
80 
81 	ncsw = wait_task_inactive(target, state);
82 	if (unlikely(!ncsw) ||
83 	    unlikely(collect_syscall(target, info)) ||
84 	    unlikely(wait_task_inactive(target, state) != ncsw))
85 		return -EAGAIN;
86 
87 	return 0;
88 }
89