blob: b05ad16174e5c7736565e6cab7f8046a308bec82 [file] [log] [blame]
Roland McGrath68bd0f42008-04-18 17:08:44 -07001/*
2 * Access to user system call parameters and results
3 *
Roland McGrath18c1e2c2009-09-22 19:57:51 -07004 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
Roland McGrath68bd0f42008-04-18 17:08:44 -07005 *
6 * This copyrighted material is made available to anyone wishing to use,
7 * modify, copy, or redistribute it subject to the terms and conditions
8 * of the GNU General Public License v.2.
9 *
10 * See asm-generic/syscall.h for descriptions of what we must do here.
11 */
12
H. Peter Anvin5e1b0072008-10-23 00:20:33 -070013#ifndef _ASM_X86_SYSCALL_H
14#define _ASM_X86_SYSCALL_H
Roland McGrath68bd0f42008-04-18 17:08:44 -070015
Eric Paris579ec9e2014-03-11 12:55:42 -040016#include <uapi/linux/audit.h>
Roland McGrath68bd0f42008-04-18 17:08:44 -070017#include <linux/sched.h>
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020018#include <linux/err.h>
H. Peter Anvin72142fd2012-01-07 14:10:18 -080019#include <asm/asm-offsets.h> /* For NR_syscalls */
Will Drewryb7456532012-04-12 16:47:56 -050020#include <asm/thread_info.h> /* for TS_COMPAT */
H. Peter Anvinfca460f2012-02-19 07:56:26 -080021#include <asm/unistd.h>
Roland McGrath68bd0f42008-04-18 17:08:44 -070022
Dominik Brodowskif8781c42018-04-05 11:53:05 +020023#ifdef CONFIG_X86_64
Dominik Brodowskifa697142018-04-05 11:53:02 +020024typedef asmlinkage long (*sys_call_ptr_t)(const struct pt_regs *);
25#else
Andy Lutomirskieb974c62015-10-05 17:48:07 -070026typedef asmlinkage long (*sys_call_ptr_t)(unsigned long, unsigned long,
27 unsigned long, unsigned long,
28 unsigned long, unsigned long);
Dominik Brodowskif8781c42018-04-05 11:53:05 +020029#endif /* CONFIG_X86_64 */
Andi Kleen1599e8f2013-08-05 15:02:35 -070030extern const sys_call_ptr_t sys_call_table[];
Mike Frysingere7b8e672010-01-26 04:40:03 -050031
Andy Lutomirski034042c2015-10-05 17:48:06 -070032#if defined(CONFIG_X86_32)
33#define ia32_sys_call_table sys_call_table
34#define __NR_syscall_compat_max __NR_syscall_max
35#define IA32_NR_syscalls NR_syscalls
36#endif
37
38#if defined(CONFIG_IA32_EMULATION)
39extern const sys_call_ptr_t ia32_sys_call_table[];
40#endif
41
Roland McGrath18c1e2c2009-09-22 19:57:51 -070042/*
43 * Only the low 32 bits of orig_ax are meaningful, so we return int.
44 * This importantly ignores the high bits on 64-bit, so comparisons
45 * sign-extend the low 32 bits.
46 */
47static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
Roland McGrath68bd0f42008-04-18 17:08:44 -070048{
Paul Moore8b4b9f22013-02-15 12:21:43 -050049 return regs->orig_ax;
Roland McGrath68bd0f42008-04-18 17:08:44 -070050}
51
52static inline void syscall_rollback(struct task_struct *task,
53 struct pt_regs *regs)
54{
Paul Moore8b4b9f22013-02-15 12:21:43 -050055 regs->ax = regs->orig_ax;
Roland McGrath68bd0f42008-04-18 17:08:44 -070056}
57
58static inline long syscall_get_error(struct task_struct *task,
59 struct pt_regs *regs)
60{
61 unsigned long error = regs->ax;
62#ifdef CONFIG_IA32_EMULATION
63 /*
64 * TS_COMPAT is set for 32-bit syscall entries and then
65 * remains set until we return to user mode.
66 */
Andy Lutomirski37a8f7c2018-01-28 10:38:50 -080067 if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED))
Roland McGrath68bd0f42008-04-18 17:08:44 -070068 /*
69 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
70 * and will match correctly in comparisons.
71 */
72 error = (long) (int) error;
73#endif
Petr Tesarik4ab4ba32008-09-03 13:31:42 +020074 return IS_ERR_VALUE(error) ? error : 0;
Roland McGrath68bd0f42008-04-18 17:08:44 -070075}
76
77static inline long syscall_get_return_value(struct task_struct *task,
78 struct pt_regs *regs)
79{
80 return regs->ax;
81}
82
83static inline void syscall_set_return_value(struct task_struct *task,
84 struct pt_regs *regs,
85 int error, long val)
86{
87 regs->ax = (long) error ?: val;
88}
89
90#ifdef CONFIG_X86_32
91
92static inline void syscall_get_arguments(struct task_struct *task,
93 struct pt_regs *regs,
Roland McGrath68bd0f42008-04-18 17:08:44 -070094 unsigned long *args)
95{
Steven Rostedt (Red Hat)b35f5492016-11-07 16:26:37 -050096 memcpy(args, &regs->bx, 6 * sizeof(args[0]));
Roland McGrath68bd0f42008-04-18 17:08:44 -070097}
98
99static inline void syscall_set_arguments(struct task_struct *task,
100 struct pt_regs *regs,
101 unsigned int i, unsigned int n,
102 const unsigned long *args)
103{
104 BUG_ON(i + n > 6);
105 memcpy(&regs->bx + i, args, n * sizeof(args[0]));
106}
107
Dmitry V. Levin16add412019-03-18 02:30:18 +0300108static inline int syscall_get_arch(struct task_struct *task)
Will Drewryb7456532012-04-12 16:47:56 -0500109{
110 return AUDIT_ARCH_I386;
111}
112
Roland McGrath68bd0f42008-04-18 17:08:44 -0700113#else /* CONFIG_X86_64 */
114
115static inline void syscall_get_arguments(struct task_struct *task,
116 struct pt_regs *regs,
Roland McGrath68bd0f42008-04-18 17:08:44 -0700117 unsigned long *args)
118{
119# ifdef CONFIG_IA32_EMULATION
Steven Rostedt (Red Hat)b35f5492016-11-07 16:26:37 -0500120 if (task->thread_info.status & TS_COMPAT) {
121 *args++ = regs->bx;
122 *args++ = regs->cx;
123 *args++ = regs->dx;
124 *args++ = regs->si;
125 *args++ = regs->di;
126 *args = regs->bp;
127 } else
Roland McGrath68bd0f42008-04-18 17:08:44 -0700128# endif
Steven Rostedt (Red Hat)b35f5492016-11-07 16:26:37 -0500129 {
130 *args++ = regs->di;
131 *args++ = regs->si;
132 *args++ = regs->dx;
133 *args++ = regs->r10;
134 *args++ = regs->r8;
135 *args = regs->r9;
136 }
Roland McGrath68bd0f42008-04-18 17:08:44 -0700137}
138
139static inline void syscall_set_arguments(struct task_struct *task,
140 struct pt_regs *regs,
Roland McGrath68bd0f42008-04-18 17:08:44 -0700141 const unsigned long *args)
142{
143# ifdef CONFIG_IA32_EMULATION
Steven Rostedt (VMware)32d92582019-03-27 20:07:31 -0400144 if (task->thread_info.status & TS_COMPAT) {
145 regs->bx = *args++;
146 regs->cx = *args++;
147 regs->dx = *args++;
148 regs->si = *args++;
149 regs->di = *args++;
150 regs->bp = *args;
151 } else
Roland McGrath68bd0f42008-04-18 17:08:44 -0700152# endif
Steven Rostedt (VMware)32d92582019-03-27 20:07:31 -0400153 {
154 regs->di = *args++;
155 regs->si = *args++;
156 regs->dx = *args++;
157 regs->r10 = *args++;
158 regs->r8 = *args++;
159 regs->r9 = *args;
160 }
Roland McGrath68bd0f42008-04-18 17:08:44 -0700161}
162
Dmitry V. Levin16add412019-03-18 02:30:18 +0300163static inline int syscall_get_arch(struct task_struct *task)
Will Drewryb7456532012-04-12 16:47:56 -0500164{
Andy Lutomirskib9d989c2016-09-13 14:29:21 -0700165 /* x32 tasks should be considered AUDIT_ARCH_X86_64. */
Dmitry V. Levin16add412019-03-18 02:30:18 +0300166 return (IS_ENABLED(CONFIG_IA32_EMULATION) &&
167 task->thread_info.status & TS_COMPAT)
168 ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
Will Drewryb7456532012-04-12 16:47:56 -0500169}
Roland McGrath68bd0f42008-04-18 17:08:44 -0700170#endif /* CONFIG_X86_32 */
171
H. Peter Anvin5e1b0072008-10-23 00:20:33 -0700172#endif /* _ASM_X86_SYSCALL_H */