blob: 2e7bffe9ae7d09284f076f1f8822677708842966 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/* SPDX-License-Identifier: GPL-2.0-only */
2/*
3 * Access to user system call parameters and results
4 *
5 * Copyright (C) 2008-2009 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_X86_SYSCALL_H
11#define _ASM_X86_SYSCALL_H
12
13#include <uapi/linux/audit.h>
14#include <linux/sched.h>
15#include <linux/err.h>
16#include <asm/asm-offsets.h> /* For NR_syscalls */
17#include <asm/thread_info.h> /* for TS_COMPAT */
18#include <asm/unistd.h>
19
20#ifdef CONFIG_X86_64
21typedef asmlinkage long (*sys_call_ptr_t)(const struct pt_regs *);
22#else
23typedef asmlinkage long (*sys_call_ptr_t)(unsigned long, unsigned long,
24 unsigned long, unsigned long,
25 unsigned long, unsigned long);
26#endif /* CONFIG_X86_64 */
27extern const sys_call_ptr_t sys_call_table[];
28
29#if defined(CONFIG_X86_32)
30#define ia32_sys_call_table sys_call_table
31#define __NR_syscall_compat_max __NR_syscall_max
32#define IA32_NR_syscalls NR_syscalls
33#endif
34
35#if defined(CONFIG_IA32_EMULATION)
36extern const sys_call_ptr_t ia32_sys_call_table[];
37#endif
38
39#ifdef CONFIG_X86_X32_ABI
40extern const sys_call_ptr_t x32_sys_call_table[];
41#endif
42
43/*
44 * Only the low 32 bits of orig_ax are meaningful, so we return int.
45 * This importantly ignores the high bits on 64-bit, so comparisons
46 * sign-extend the low 32 bits.
47 */
48static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
49{
50 return regs->orig_ax;
51}
52
53static inline void syscall_rollback(struct task_struct *task,
54 struct pt_regs *regs)
55{
56 regs->ax = regs->orig_ax;
57}
58
59static inline long syscall_get_error(struct task_struct *task,
60 struct pt_regs *regs)
61{
62 unsigned long error = regs->ax;
63#ifdef CONFIG_IA32_EMULATION
64 /*
65 * TS_COMPAT is set for 32-bit syscall entries and then
66 * remains set until we return to user mode.
67 */
68 if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED))
69 /*
70 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
71 * and will match correctly in comparisons.
72 */
73 error = (long) (int) error;
74#endif
75 return IS_ERR_VALUE(error) ? error : 0;
76}
77
78static inline long syscall_get_return_value(struct task_struct *task,
79 struct pt_regs *regs)
80{
81 return regs->ax;
82}
83
84static inline void syscall_set_return_value(struct task_struct *task,
85 struct pt_regs *regs,
86 int error, long val)
87{
88 regs->ax = (long) error ?: val;
89}
90
91#ifdef CONFIG_X86_32
92
93static inline void syscall_get_arguments(struct task_struct *task,
94 struct pt_regs *regs,
95 unsigned long *args)
96{
97 args[0] = regs->bx;
98 args[1] = regs->cx;
99 args[2] = regs->dx;
100 args[3] = regs->si;
101 args[4] = regs->di;
102 args[5] = regs->bp;
103}
104
105static inline void syscall_set_arguments(struct task_struct *task,
106 struct pt_regs *regs,
107 unsigned int i, unsigned int n,
108 const unsigned long *args)
109{
110 BUG_ON(i + n > 6);
111 memcpy(&regs->bx + i, args, n * sizeof(args[0]));
112}
113
114static inline int syscall_get_arch(struct task_struct *task)
115{
116 return AUDIT_ARCH_I386;
117}
118
119#else /* CONFIG_X86_64 */
120
121static inline void syscall_get_arguments(struct task_struct *task,
122 struct pt_regs *regs,
123 unsigned long *args)
124{
125# ifdef CONFIG_IA32_EMULATION
126 if (task->thread_info.status & TS_COMPAT) {
127 *args++ = regs->bx;
128 *args++ = regs->cx;
129 *args++ = regs->dx;
130 *args++ = regs->si;
131 *args++ = regs->di;
132 *args = regs->bp;
133 } else
134# endif
135 {
136 *args++ = regs->di;
137 *args++ = regs->si;
138 *args++ = regs->dx;
139 *args++ = regs->r10;
140 *args++ = regs->r8;
141 *args = regs->r9;
142 }
143}
144
145static inline void syscall_set_arguments(struct task_struct *task,
146 struct pt_regs *regs,
147 const unsigned long *args)
148{
149# ifdef CONFIG_IA32_EMULATION
150 if (task->thread_info.status & TS_COMPAT) {
151 regs->bx = *args++;
152 regs->cx = *args++;
153 regs->dx = *args++;
154 regs->si = *args++;
155 regs->di = *args++;
156 regs->bp = *args;
157 } else
158# endif
159 {
160 regs->di = *args++;
161 regs->si = *args++;
162 regs->dx = *args++;
163 regs->r10 = *args++;
164 regs->r8 = *args++;
165 regs->r9 = *args;
166 }
167}
168
169static inline int syscall_get_arch(struct task_struct *task)
170{
171 /* x32 tasks should be considered AUDIT_ARCH_X86_64. */
172 return (IS_ENABLED(CONFIG_IA32_EMULATION) &&
173 task->thread_info.status & TS_COMPAT)
174 ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
175}
176#endif /* CONFIG_X86_32 */
177
178#endif /* _ASM_X86_SYSCALL_H */