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