projects/linux·blob master · refs

148 lines · 3.8 KiB

1/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Access to user system call parameters and results
4 *
5 * Copyright (C) 2008 Red Hat, Inc. All rights reserved.
6 *
7 * See asm-generic/syscall.h for descriptions of what we must do here.
8 */
9
10#ifndef _ASM_SYSCALL_H
11#define _ASM_SYSCALL_H 1
12
13#include <uapi/linux/audit.h>
14#include <linux/sched.h>
15#include <linux/thread_info.h>
16
17#ifdef CONFIG_ARCH_HAS_SYSCALL_WRAPPER
18typedef long (*syscall_fn)(const struct pt_regs *);
19#else
20typedef long (*syscall_fn)(unsigned long, unsigned long, unsigned long,
21 unsigned long, unsigned long, unsigned long);
22#endif
23
24/* ftrace syscalls requires exporting the sys_call_table */
25extern const syscall_fn sys_call_table[];
26extern const syscall_fn compat_sys_call_table[];
27
28static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
29{
30 /*
31 * Note that we are returning an int here. That means 0xffffffff, ie.
32 * 32-bit negative 1, will be interpreted as -1 on a 64-bit kernel.
33 * This is important for seccomp so that compat tasks can set r0 = -1
34 * to reject the syscall.
35 */
36 if (trap_is_syscall(regs))
37 return regs->gpr[0];
38 else
39 return -1;
40}
41
42static inline void syscall_set_nr(struct task_struct *task, struct pt_regs *regs, int nr)
43{
44 /*
45 * Unlike syscall_get_nr(), syscall_set_nr() can be called only when
46 * the target task is stopped for tracing on entering syscall, so
47 * there is no need to have the same check syscall_get_nr() has.
48 */
49 regs->gpr[0] = nr;
50}
51
52static inline void syscall_rollback(struct task_struct *task,
53 struct pt_regs *regs)
54{
55 regs->gpr[3] = regs->orig_gpr3;
56}
57
58static inline long syscall_get_error(struct task_struct *task,
59 struct pt_regs *regs)
60{
61 if (trap_is_scv(regs)) {
62 unsigned long error = regs->gpr[3];
63
64 return IS_ERR_VALUE(error) ? error : 0;
65 } else {
66 /*
67 * If the system call failed,
68 * regs->gpr[3] contains a positive ERRORCODE.
69 */
70 return (regs->ccr & 0x10000000UL) ? -regs->gpr[3] : 0;
71 }
72}
73
74static inline long syscall_get_return_value(struct task_struct *task,
75 struct pt_regs *regs)
76{
77 return regs->gpr[3];
78}
79
80static inline void syscall_set_return_value(struct task_struct *task,
81 struct pt_regs *regs,
82 int error, long val)
83{
84 if (trap_is_scv(regs)) {
85 regs->gpr[3] = (long) error ?: val;
86 } else {
87 /*
88 * In the general case it's not obvious that we must deal with
89 * CCR here, as the syscall exit path will also do that for us.
90 * However there are some places, eg. the signal code, which
91 * check ccr to decide if the value in r3 is actually an error.
92 */
93 if (error) {
94 regs->ccr |= 0x10000000L;
95 regs->gpr[3] = error;
96 } else {
97 regs->ccr &= ~0x10000000L;
98 regs->gpr[3] = val;
99 }
100 }
101 /*
102 * Mark that a return value has been explicitly set by seccomp or
103 * ptrace so that system_call_exception() can skip the syscall
104 * unconditionally, even when the user requested syscall(-1).
105 */
106 set_thread_flag(TIF_SYSCALL_RET);
107}
108
109static inline void syscall_get_arguments(struct task_struct *task,
110 struct pt_regs *regs,
111 unsigned long *args)
112{
113 unsigned long val, mask = -1UL;
114 unsigned int n = 6;
115
116 if (is_tsk_32bit_task(task))
117 mask = 0xffffffff;
118
119 while (n--) {
120 if (n == 0)
121 val = regs->orig_gpr3;
122 else
123 val = regs->gpr[3 + n];
124
125 args[n] = val & mask;
126 }
127}
128
129static inline void syscall_set_arguments(struct task_struct *task,
130 struct pt_regs *regs,
131 const unsigned long *args)
132{
133 memcpy(&regs->gpr[3], args, 6 * sizeof(args[0]));
134
135 /* Also copy the first argument into orig_gpr3 */
136 regs->orig_gpr3 = args[0];
137}
138
139static inline int syscall_get_arch(struct task_struct *task)
140{
141 if (is_tsk_32bit_task(task))
142 return AUDIT_ARCH_PPC;
143 else if (IS_ENABLED(CONFIG_CPU_LITTLE_ENDIAN))
144 return AUDIT_ARCH_PPC64LE;
145 else
146 return AUDIT_ARCH_PPC64;
147}
148#endif /* _ASM_SYSCALL_H */