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