xref: /linux/arch/x86/include/asm/syscall.h (revision 44f57d78)
1 /* 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
21 typedef asmlinkage long (*sys_call_ptr_t)(const struct pt_regs *);
22 #else
23 typedef 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 */
27 extern 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)
36 extern const sys_call_ptr_t ia32_sys_call_table[];
37 #endif
38 
39 /*
40  * Only the low 32 bits of orig_ax are meaningful, so we return int.
41  * This importantly ignores the high bits on 64-bit, so comparisons
42  * sign-extend the low 32 bits.
43  */
44 static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
45 {
46 	return regs->orig_ax;
47 }
48 
49 static inline void syscall_rollback(struct task_struct *task,
50 				    struct pt_regs *regs)
51 {
52 	regs->ax = regs->orig_ax;
53 }
54 
55 static inline long syscall_get_error(struct task_struct *task,
56 				     struct pt_regs *regs)
57 {
58 	unsigned long error = regs->ax;
59 #ifdef CONFIG_IA32_EMULATION
60 	/*
61 	 * TS_COMPAT is set for 32-bit syscall entries and then
62 	 * remains set until we return to user mode.
63 	 */
64 	if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED))
65 		/*
66 		 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
67 		 * and will match correctly in comparisons.
68 		 */
69 		error = (long) (int) error;
70 #endif
71 	return IS_ERR_VALUE(error) ? error : 0;
72 }
73 
74 static inline long syscall_get_return_value(struct task_struct *task,
75 					    struct pt_regs *regs)
76 {
77 	return regs->ax;
78 }
79 
80 static inline void syscall_set_return_value(struct task_struct *task,
81 					    struct pt_regs *regs,
82 					    int error, long val)
83 {
84 	regs->ax = (long) error ?: val;
85 }
86 
87 #ifdef CONFIG_X86_32
88 
89 static inline void syscall_get_arguments(struct task_struct *task,
90 					 struct pt_regs *regs,
91 					 unsigned long *args)
92 {
93 	memcpy(args, &regs->bx, 6 * sizeof(args[0]));
94 }
95 
96 static inline void syscall_set_arguments(struct task_struct *task,
97 					 struct pt_regs *regs,
98 					 unsigned int i, unsigned int n,
99 					 const unsigned long *args)
100 {
101 	BUG_ON(i + n > 6);
102 	memcpy(&regs->bx + i, args, n * sizeof(args[0]));
103 }
104 
105 static inline int syscall_get_arch(struct task_struct *task)
106 {
107 	return AUDIT_ARCH_I386;
108 }
109 
110 #else	 /* CONFIG_X86_64 */
111 
112 static inline void syscall_get_arguments(struct task_struct *task,
113 					 struct pt_regs *regs,
114 					 unsigned long *args)
115 {
116 # ifdef CONFIG_IA32_EMULATION
117 	if (task->thread_info.status & TS_COMPAT) {
118 		*args++ = regs->bx;
119 		*args++ = regs->cx;
120 		*args++ = regs->dx;
121 		*args++ = regs->si;
122 		*args++ = regs->di;
123 		*args   = regs->bp;
124 	} else
125 # endif
126 	{
127 		*args++ = regs->di;
128 		*args++ = regs->si;
129 		*args++ = regs->dx;
130 		*args++ = regs->r10;
131 		*args++ = regs->r8;
132 		*args   = regs->r9;
133 	}
134 }
135 
136 static inline void syscall_set_arguments(struct task_struct *task,
137 					 struct pt_regs *regs,
138 					 const unsigned long *args)
139 {
140 # ifdef CONFIG_IA32_EMULATION
141 	if (task->thread_info.status & TS_COMPAT) {
142 		regs->bx = *args++;
143 		regs->cx = *args++;
144 		regs->dx = *args++;
145 		regs->si = *args++;
146 		regs->di = *args++;
147 		regs->bp = *args;
148 	} else
149 # endif
150 	{
151 		regs->di = *args++;
152 		regs->si = *args++;
153 		regs->dx = *args++;
154 		regs->r10 = *args++;
155 		regs->r8 = *args++;
156 		regs->r9 = *args;
157 	}
158 }
159 
160 static inline int syscall_get_arch(struct task_struct *task)
161 {
162 	/* x32 tasks should be considered AUDIT_ARCH_X86_64. */
163 	return (IS_ENABLED(CONFIG_IA32_EMULATION) &&
164 		task->thread_info.status & TS_COMPAT)
165 		? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
166 }
167 #endif	/* CONFIG_X86_32 */
168 
169 #endif	/* _ASM_X86_SYSCALL_H */
170