blob: 7cbf733d11afd9fe150cbd8676065821a928d9ad [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001/* SPDX-License-Identifier: GPL-2.0-only */
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Access to user system call parameters and results
4 *
5 * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved.
6 *
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00007 * 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>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000016#include <asm/thread_info.h> /* for TS_COMPAT */
17#include <asm/unistd.h>
18
Olivier Deprez157378f2022-04-04 15:47:50 +020019typedef long (*sys_call_ptr_t)(const struct pt_regs *);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000020extern const sys_call_ptr_t sys_call_table[];
21
22#if defined(CONFIG_X86_32)
23#define ia32_sys_call_table sys_call_table
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000024#endif
25
26#if defined(CONFIG_IA32_EMULATION)
27extern const sys_call_ptr_t ia32_sys_call_table[];
28#endif
29
David Brazdil0f672f62019-12-10 10:32:29 +000030#ifdef CONFIG_X86_X32_ABI
31extern const sys_call_ptr_t x32_sys_call_table[];
32#endif
33
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000034/*
35 * Only the low 32 bits of orig_ax are meaningful, so we return int.
36 * This importantly ignores the high bits on 64-bit, so comparisons
37 * sign-extend the low 32 bits.
38 */
39static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs)
40{
41 return regs->orig_ax;
42}
43
44static inline void syscall_rollback(struct task_struct *task,
45 struct pt_regs *regs)
46{
47 regs->ax = regs->orig_ax;
48}
49
50static inline long syscall_get_error(struct task_struct *task,
51 struct pt_regs *regs)
52{
53 unsigned long error = regs->ax;
54#ifdef CONFIG_IA32_EMULATION
55 /*
56 * TS_COMPAT is set for 32-bit syscall entries and then
57 * remains set until we return to user mode.
58 */
59 if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED))
60 /*
61 * Sign-extend the value so (int)-EFOO becomes (long)-EFOO
62 * and will match correctly in comparisons.
63 */
64 error = (long) (int) error;
65#endif
66 return IS_ERR_VALUE(error) ? error : 0;
67}
68
69static inline long syscall_get_return_value(struct task_struct *task,
70 struct pt_regs *regs)
71{
72 return regs->ax;
73}
74
75static inline void syscall_set_return_value(struct task_struct *task,
76 struct pt_regs *regs,
77 int error, long val)
78{
79 regs->ax = (long) error ?: val;
80}
81
82#ifdef CONFIG_X86_32
83
84static inline void syscall_get_arguments(struct task_struct *task,
85 struct pt_regs *regs,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000086 unsigned long *args)
87{
David Brazdil0f672f62019-12-10 10:32:29 +000088 memcpy(args, &regs->bx, 6 * sizeof(args[0]));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089}
90
91static inline void syscall_set_arguments(struct task_struct *task,
92 struct pt_regs *regs,
93 unsigned int i, unsigned int n,
94 const unsigned long *args)
95{
96 BUG_ON(i + n > 6);
97 memcpy(&regs->bx + i, args, n * sizeof(args[0]));
98}
99
David Brazdil0f672f62019-12-10 10:32:29 +0000100static inline int syscall_get_arch(struct task_struct *task)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000101{
102 return AUDIT_ARCH_I386;
103}
104
105#else /* CONFIG_X86_64 */
106
107static inline void syscall_get_arguments(struct task_struct *task,
108 struct pt_regs *regs,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000109 unsigned long *args)
110{
111# ifdef CONFIG_IA32_EMULATION
David Brazdil0f672f62019-12-10 10:32:29 +0000112 if (task->thread_info.status & TS_COMPAT) {
113 *args++ = regs->bx;
114 *args++ = regs->cx;
115 *args++ = regs->dx;
116 *args++ = regs->si;
117 *args++ = regs->di;
118 *args = regs->bp;
119 } else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000120# endif
David Brazdil0f672f62019-12-10 10:32:29 +0000121 {
122 *args++ = regs->di;
123 *args++ = regs->si;
124 *args++ = regs->dx;
125 *args++ = regs->r10;
126 *args++ = regs->r8;
127 *args = regs->r9;
128 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000129}
130
131static inline void syscall_set_arguments(struct task_struct *task,
132 struct pt_regs *regs,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000133 const unsigned long *args)
134{
135# ifdef CONFIG_IA32_EMULATION
David Brazdil0f672f62019-12-10 10:32:29 +0000136 if (task->thread_info.status & TS_COMPAT) {
137 regs->bx = *args++;
138 regs->cx = *args++;
139 regs->dx = *args++;
140 regs->si = *args++;
141 regs->di = *args++;
142 regs->bp = *args;
143 } else
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000144# endif
David Brazdil0f672f62019-12-10 10:32:29 +0000145 {
146 regs->di = *args++;
147 regs->si = *args++;
148 regs->dx = *args++;
149 regs->r10 = *args++;
150 regs->r8 = *args++;
151 regs->r9 = *args;
152 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000153}
154
David Brazdil0f672f62019-12-10 10:32:29 +0000155static inline int syscall_get_arch(struct task_struct *task)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000156{
157 /* x32 tasks should be considered AUDIT_ARCH_X86_64. */
David Brazdil0f672f62019-12-10 10:32:29 +0000158 return (IS_ENABLED(CONFIG_IA32_EMULATION) &&
159 task->thread_info.status & TS_COMPAT)
160 ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000161}
Olivier Deprez157378f2022-04-04 15:47:50 +0200162
163void do_syscall_64(unsigned long nr, struct pt_regs *regs);
164void do_int80_syscall_32(struct pt_regs *regs);
165long do_fast_syscall_32(struct pt_regs *regs);
166
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000167#endif /* CONFIG_X86_32 */
168
169#endif /* _ASM_X86_SYSCALL_H */