Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Access to user system call parameters and results |
| 3 | * |
Roland McGrath | 18c1e2c | 2009-09-22 19:57:51 -0700 | [diff] [blame] | 4 | * Copyright (C) 2008-2009 Red Hat, Inc. All rights reserved. |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 5 | * |
| 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 Anvin | 5e1b007 | 2008-10-23 00:20:33 -0700 | [diff] [blame] | 13 | #ifndef _ASM_X86_SYSCALL_H |
| 14 | #define _ASM_X86_SYSCALL_H |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 15 | |
Eric Paris | 579ec9e | 2014-03-11 12:55:42 -0400 | [diff] [blame] | 16 | #include <uapi/linux/audit.h> |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 17 | #include <linux/sched.h> |
Petr Tesarik | 4ab4ba3 | 2008-09-03 13:31:42 +0200 | [diff] [blame] | 18 | #include <linux/err.h> |
H. Peter Anvin | 72142fd | 2012-01-07 14:10:18 -0800 | [diff] [blame] | 19 | #include <asm/asm-offsets.h> /* For NR_syscalls */ |
Will Drewry | b745653 | 2012-04-12 16:47:56 -0500 | [diff] [blame] | 20 | #include <asm/thread_info.h> /* for TS_COMPAT */ |
H. Peter Anvin | fca460f | 2012-02-19 07:56:26 -0800 | [diff] [blame] | 21 | #include <asm/unistd.h> |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 22 | |
Dominik Brodowski | f8781c4 | 2018-04-05 11:53:05 +0200 | [diff] [blame] | 23 | #ifdef CONFIG_X86_64 |
Dominik Brodowski | fa69714 | 2018-04-05 11:53:02 +0200 | [diff] [blame] | 24 | typedef asmlinkage long (*sys_call_ptr_t)(const struct pt_regs *); |
| 25 | #else |
Andy Lutomirski | eb974c6 | 2015-10-05 17:48:07 -0700 | [diff] [blame] | 26 | typedef asmlinkage long (*sys_call_ptr_t)(unsigned long, unsigned long, |
| 27 | unsigned long, unsigned long, |
| 28 | unsigned long, unsigned long); |
Dominik Brodowski | f8781c4 | 2018-04-05 11:53:05 +0200 | [diff] [blame] | 29 | #endif /* CONFIG_X86_64 */ |
Andi Kleen | 1599e8f | 2013-08-05 15:02:35 -0700 | [diff] [blame] | 30 | extern const sys_call_ptr_t sys_call_table[]; |
Mike Frysinger | e7b8e67 | 2010-01-26 04:40:03 -0500 | [diff] [blame] | 31 | |
Andy Lutomirski | 034042c | 2015-10-05 17:48:06 -0700 | [diff] [blame] | 32 | #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) |
| 39 | extern const sys_call_ptr_t ia32_sys_call_table[]; |
| 40 | #endif |
| 41 | |
Roland McGrath | 18c1e2c | 2009-09-22 19:57:51 -0700 | [diff] [blame] | 42 | /* |
| 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 | */ |
| 47 | static inline int syscall_get_nr(struct task_struct *task, struct pt_regs *regs) |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 48 | { |
Paul Moore | 8b4b9f2 | 2013-02-15 12:21:43 -0500 | [diff] [blame] | 49 | return regs->orig_ax; |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | static inline void syscall_rollback(struct task_struct *task, |
| 53 | struct pt_regs *regs) |
| 54 | { |
Paul Moore | 8b4b9f2 | 2013-02-15 12:21:43 -0500 | [diff] [blame] | 55 | regs->ax = regs->orig_ax; |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | static 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 Lutomirski | 37a8f7c | 2018-01-28 10:38:50 -0800 | [diff] [blame] | 67 | if (task->thread_info.status & (TS_COMPAT|TS_I386_REGS_POKED)) |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 68 | /* |
| 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 Tesarik | 4ab4ba3 | 2008-09-03 13:31:42 +0200 | [diff] [blame] | 74 | return IS_ERR_VALUE(error) ? error : 0; |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | static inline long syscall_get_return_value(struct task_struct *task, |
| 78 | struct pt_regs *regs) |
| 79 | { |
| 80 | return regs->ax; |
| 81 | } |
| 82 | |
| 83 | static 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 | |
| 92 | static inline void syscall_get_arguments(struct task_struct *task, |
| 93 | struct pt_regs *regs, |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 94 | unsigned long *args) |
| 95 | { |
Steven Rostedt (Red Hat) | b35f549 | 2016-11-07 16:26:37 -0500 | [diff] [blame] | 96 | memcpy(args, ®s->bx, 6 * sizeof(args[0])); |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static 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(®s->bx + i, args, n * sizeof(args[0])); |
| 106 | } |
| 107 | |
Dmitry V. Levin | 16add41 | 2019-03-18 02:30:18 +0300 | [diff] [blame] | 108 | static inline int syscall_get_arch(struct task_struct *task) |
Will Drewry | b745653 | 2012-04-12 16:47:56 -0500 | [diff] [blame] | 109 | { |
| 110 | return AUDIT_ARCH_I386; |
| 111 | } |
| 112 | |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 113 | #else /* CONFIG_X86_64 */ |
| 114 | |
| 115 | static inline void syscall_get_arguments(struct task_struct *task, |
| 116 | struct pt_regs *regs, |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 117 | unsigned long *args) |
| 118 | { |
| 119 | # ifdef CONFIG_IA32_EMULATION |
Steven Rostedt (Red Hat) | b35f549 | 2016-11-07 16:26:37 -0500 | [diff] [blame] | 120 | 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 McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 128 | # endif |
Steven Rostedt (Red Hat) | b35f549 | 2016-11-07 16:26:37 -0500 | [diff] [blame] | 129 | { |
| 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 McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | static inline void syscall_set_arguments(struct task_struct *task, |
| 140 | struct pt_regs *regs, |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 141 | const unsigned long *args) |
| 142 | { |
| 143 | # ifdef CONFIG_IA32_EMULATION |
Steven Rostedt (VMware) | 32d9258 | 2019-03-27 20:07:31 -0400 | [diff] [blame] | 144 | 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 McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 152 | # endif |
Steven Rostedt (VMware) | 32d9258 | 2019-03-27 20:07:31 -0400 | [diff] [blame] | 153 | { |
| 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 McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Dmitry V. Levin | 16add41 | 2019-03-18 02:30:18 +0300 | [diff] [blame] | 163 | static inline int syscall_get_arch(struct task_struct *task) |
Will Drewry | b745653 | 2012-04-12 16:47:56 -0500 | [diff] [blame] | 164 | { |
Andy Lutomirski | b9d989c | 2016-09-13 14:29:21 -0700 | [diff] [blame] | 165 | /* x32 tasks should be considered AUDIT_ARCH_X86_64. */ |
Dmitry V. Levin | 16add41 | 2019-03-18 02:30:18 +0300 | [diff] [blame] | 166 | return (IS_ENABLED(CONFIG_IA32_EMULATION) && |
| 167 | task->thread_info.status & TS_COMPAT) |
| 168 | ? AUDIT_ARCH_I386 : AUDIT_ARCH_X86_64; |
Will Drewry | b745653 | 2012-04-12 16:47:56 -0500 | [diff] [blame] | 169 | } |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 170 | #endif /* CONFIG_X86_32 */ |
| 171 | |
H. Peter Anvin | 5e1b007 | 2008-10-23 00:20:33 -0700 | [diff] [blame] | 172 | #endif /* _ASM_X86_SYSCALL_H */ |