xref: /linux/arch/x86/include/asm/syscall.h (revision 1e3ad78334a69b36e107232e337f9d693dcc9df2)
12522fe45SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2bb898558SAl Viro /*
3bb898558SAl Viro  * Access to user system call parameters and results
4bb898558SAl Viro  *
518c1e2c8SRoland McGrath  * Copyright (C) 2008-2009 Red Hat, Inc.  All rights reserved.
6bb898558SAl Viro  *
7bb898558SAl Viro  * See asm-generic/syscall.h for descriptions of what we must do here.
8bb898558SAl Viro  */
9bb898558SAl Viro 
105e1b0075SH. Peter Anvin #ifndef _ASM_X86_SYSCALL_H
115e1b0075SH. Peter Anvin #define _ASM_X86_SYSCALL_H
12bb898558SAl Viro 
13579ec9e1SEric Paris #include <uapi/linux/audit.h>
14bb898558SAl Viro #include <linux/sched.h>
15bb898558SAl Viro #include <linux/err.h>
16b7456536SWill Drewry #include <asm/thread_info.h>	/* for TS_COMPAT */
17fca460f9SH. Peter Anvin #include <asm/unistd.h>
18bb898558SAl Viro 
19*1e3ad783SLinus Torvalds /* This is used purely for kernel/trace/trace_syscalls.c */
200f78ff17SBrian Gerst typedef long (*sys_call_ptr_t)(const struct pt_regs *);
211599e8fcSAndi Kleen extern const sys_call_ptr_t sys_call_table[];
22e7b8e675SMike Frysinger 
23dce0aa3bSH. Peter Anvin (Intel) /*
24dce0aa3bSH. Peter Anvin (Intel)  * These may not exist, but still put the prototypes in so we
25dce0aa3bSH. Peter Anvin (Intel)  * can use IS_ENABLED().
26dce0aa3bSH. Peter Anvin (Intel)  */
27*1e3ad783SLinus Torvalds extern long ia32_sys_call(const struct pt_regs *, unsigned int nr);
28*1e3ad783SLinus Torvalds extern long x32_sys_call(const struct pt_regs *, unsigned int nr);
29*1e3ad783SLinus Torvalds extern long x64_sys_call(const struct pt_regs *, unsigned int nr);
306365b842SAndy Lutomirski 
31bb898558SAl Viro /*
3218c1e2c8SRoland McGrath  * Only the low 32 bits of orig_ax are meaningful, so we return int.
3318c1e2c8SRoland McGrath  * This importantly ignores the high bits on 64-bit, so comparisons
3418c1e2c8SRoland McGrath  * sign-extend the low 32 bits.
35bb898558SAl Viro  */
3618c1e2c8SRoland McGrath static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
3718c1e2c8SRoland McGrath {
388b4b9f27SPaul Moore 	return regs->orig_ax;
39bb898558SAl Viro }
40bb898558SAl Viro 
41bb898558SAl Viro static inline void syscall_rollback(struct task_struct *task,
42bb898558SAl Viro 				    struct pt_regs *regs)
43bb898558SAl Viro {
448b4b9f27SPaul Moore 	regs->ax = regs->orig_ax;
45bb898558SAl Viro }
46bb898558SAl Viro 
47bb898558SAl Viro static inline long syscall_get_error(struct task_struct *task,
48bb898558SAl Viro 				     struct pt_regs *regs)
49bb898558SAl Viro {
50bb898558SAl Viro 	unsigned long error = regs->ax;
51bb898558SAl Viro #ifdef CONFIG_IA32_EMULATION
52bb898558SAl Viro 	/*
53bb898558SAl Viro 	 * TS_COMPAT is set for 32-bit syscall entries and then
54bb898558SAl Viro 	 * remains set until we return to user mode.
55bb898558SAl Viro 	 */
5637a8f7c3SAndy Lutomirski 	if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED))
57bb898558SAl Viro 		/*
58bb898558SAl Viro 		 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
59bb898558SAl Viro 		 * and will match correctly in comparisons.
60bb898558SAl Viro 		 */
61bb898558SAl Viro 		error = (long) (int) error;
62bb898558SAl Viro #endif
63bb898558SAl Viro 	return IS_ERR_VALUE(error) ? error : 0;
64bb898558SAl Viro }
65bb898558SAl Viro 
66bb898558SAl Viro static inline long syscall_get_return_value(struct task_struct *task,
67bb898558SAl Viro 					    struct pt_regs *regs)
68bb898558SAl Viro {
69bb898558SAl Viro 	return regs->ax;
70bb898558SAl Viro }
71bb898558SAl Viro 
72bb898558SAl Viro static inline void syscall_set_return_value(struct task_struct *task,
73bb898558SAl Viro 					    struct pt_regs *regs,
74bb898558SAl Viro 					    int error, long val)
75bb898558SAl Viro {
76bb898558SAl Viro 	regs->ax = (long) error ?: val;
77bb898558SAl Viro }
78bb898558SAl Viro 
79bb898558SAl Viro #ifdef CONFIG_X86_32
80bb898558SAl Viro 
81bb898558SAl Viro static inline void syscall_get_arguments(struct task_struct *task,
82bb898558SAl Viro 					 struct pt_regs *regs,
83bb898558SAl Viro 					 unsigned long *args)
84bb898558SAl Viro {
85b35f549dSSteven Rostedt (Red Hat) 	memcpy(args, &regs->bx, 6 * sizeof(args[0]));
86bb898558SAl Viro }
87bb898558SAl Viro 
8816add411SDmitry V. Levin static inline int syscall_get_arch(struct task_struct *task)
89b7456536SWill Drewry {
90b7456536SWill Drewry 	return AUDIT_ARCH_I386;
91b7456536SWill Drewry }
92b7456536SWill Drewry 
93bb898558SAl Viro #else	 /* CONFIG_X86_64 */
94bb898558SAl Viro 
95bb898558SAl Viro static inline void syscall_get_arguments(struct task_struct *task,
96bb898558SAl Viro 					 struct pt_regs *regs,
97bb898558SAl Viro 					 unsigned long *args)
98bb898558SAl Viro {
99bb898558SAl Viro # ifdef CONFIG_IA32_EMULATION
100b35f549dSSteven Rostedt (Red Hat) 	if (task->thread_info.status & TS_COMPAT) {
101bb898558SAl Viro 		*args++ = regs->bx;
102c3c9897cSLinus Torvalds 		*args++ = regs->cx;
103c3c9897cSLinus Torvalds 		*args++ = regs->dx;
104c3c9897cSLinus Torvalds 		*args++ = regs->si;
105c3c9897cSLinus Torvalds 		*args++ = regs->di;
106b35f549dSSteven Rostedt (Red Hat) 		*args   = regs->bp;
107b35f549dSSteven Rostedt (Red Hat) 	} else
108bb898558SAl Viro # endif
109b35f549dSSteven Rostedt (Red Hat) 	{
110bb898558SAl Viro 		*args++ = regs->di;
111c3c9897cSLinus Torvalds 		*args++ = regs->si;
112c3c9897cSLinus Torvalds 		*args++ = regs->dx;
113c3c9897cSLinus Torvalds 		*args++ = regs->r10;
114c3c9897cSLinus Torvalds 		*args++ = regs->r8;
115b35f549dSSteven Rostedt (Red Hat) 		*args   = regs->r9;
116bb898558SAl Viro 	}
117bb898558SAl Viro }
118bb898558SAl Viro 
11916add411SDmitry V. Levin static inline int syscall_get_arch(struct task_struct *task)
120b7456536SWill Drewry {
121b9d989c7SAndy Lutomirski 	/* x32 tasks should be considered AUDIT_ARCH_X86_64. */
12216add411SDmitry V. Levin 	return (IS_ENABLED(CONFIG_IA32_EMULATION) &&
12316add411SDmitry V. Levin 		task->thread_info.status & TS_COMPAT)
12416add411SDmitry V. Levin 		? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
125b7456536SWill Drewry }
12699ce3255SBenjamin Thiel 
127ca282b48SBrian Gerst bool do_syscall_64(struct pt_regs *regs, int nr);
12899ce3255SBenjamin Thiel 
129bb898558SAl Viro #endif	/* CONFIG_X86_32 */
130bb898558SAl Viro 
131f34f0d3cSArnd Bergmann void do_int80_syscall_32(struct pt_regs *regs);
1320d3109adSBrian Gerst bool do_fast_syscall_32(struct pt_regs *regs);
1330d3109adSBrian Gerst bool do_SYSENTER_32(struct pt_regs *regs);
134f34f0d3cSArnd Bergmann 
1355e1b0075SH. Peter Anvin #endif	/* _ASM_X86_SYSCALL_H */
136