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 | |
| 16 | #include <linux/sched.h> |
Petr Tesarik | 4ab4ba3 | 2008-09-03 13:31:42 +0200 | [diff] [blame] | 17 | #include <linux/err.h> |
H. Peter Anvin | 72142fd | 2012-01-07 14:10:18 -0800 | [diff] [blame] | 18 | #include <asm/asm-offsets.h> /* For NR_syscalls */ |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 19 | |
Mike Frysinger | e7b8e67 | 2010-01-26 04:40:03 -0500 | [diff] [blame] | 20 | extern const unsigned long sys_call_table[]; |
| 21 | |
Roland McGrath | 18c1e2c | 2009-09-22 19:57:51 -0700 | [diff] [blame] | 22 | /* |
| 23 | * Only the low 32 bits of orig_ax are meaningful, so we return int. |
| 24 | * This importantly ignores the high bits on 64-bit, so comparisons |
| 25 | * sign-extend the low 32 bits. |
| 26 | */ |
| 27 | 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] | 28 | { |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 29 | return regs->orig_ax; |
| 30 | } |
| 31 | |
| 32 | static inline void syscall_rollback(struct task_struct *task, |
| 33 | struct pt_regs *regs) |
| 34 | { |
| 35 | regs->ax = regs->orig_ax; |
| 36 | } |
| 37 | |
| 38 | static inline long syscall_get_error(struct task_struct *task, |
| 39 | struct pt_regs *regs) |
| 40 | { |
| 41 | unsigned long error = regs->ax; |
| 42 | #ifdef CONFIG_IA32_EMULATION |
| 43 | /* |
| 44 | * TS_COMPAT is set for 32-bit syscall entries and then |
| 45 | * remains set until we return to user mode. |
| 46 | */ |
| 47 | if (task_thread_info(task)->status & TS_COMPAT) |
| 48 | /* |
| 49 | * Sign-extend the value so (int)-EFOO becomes (long)-EFOO |
| 50 | * and will match correctly in comparisons. |
| 51 | */ |
| 52 | error = (long) (int) error; |
| 53 | #endif |
Petr Tesarik | 4ab4ba3 | 2008-09-03 13:31:42 +0200 | [diff] [blame] | 54 | return IS_ERR_VALUE(error) ? error : 0; |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | static inline long syscall_get_return_value(struct task_struct *task, |
| 58 | struct pt_regs *regs) |
| 59 | { |
| 60 | return regs->ax; |
| 61 | } |
| 62 | |
| 63 | static inline void syscall_set_return_value(struct task_struct *task, |
| 64 | struct pt_regs *regs, |
| 65 | int error, long val) |
| 66 | { |
| 67 | regs->ax = (long) error ?: val; |
| 68 | } |
| 69 | |
| 70 | #ifdef CONFIG_X86_32 |
| 71 | |
| 72 | static inline void syscall_get_arguments(struct task_struct *task, |
| 73 | struct pt_regs *regs, |
| 74 | unsigned int i, unsigned int n, |
| 75 | unsigned long *args) |
| 76 | { |
| 77 | BUG_ON(i + n > 6); |
| 78 | memcpy(args, ®s->bx + i, n * sizeof(args[0])); |
| 79 | } |
| 80 | |
| 81 | static inline void syscall_set_arguments(struct task_struct *task, |
| 82 | struct pt_regs *regs, |
| 83 | unsigned int i, unsigned int n, |
| 84 | const unsigned long *args) |
| 85 | { |
| 86 | BUG_ON(i + n > 6); |
| 87 | memcpy(®s->bx + i, args, n * sizeof(args[0])); |
| 88 | } |
| 89 | |
| 90 | #else /* CONFIG_X86_64 */ |
| 91 | |
| 92 | static inline void syscall_get_arguments(struct task_struct *task, |
| 93 | struct pt_regs *regs, |
| 94 | unsigned int i, unsigned int n, |
| 95 | unsigned long *args) |
| 96 | { |
| 97 | # ifdef CONFIG_IA32_EMULATION |
| 98 | if (task_thread_info(task)->status & TS_COMPAT) |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 99 | switch (i) { |
| 100 | case 0: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 101 | if (!n--) break; |
| 102 | *args++ = regs->bx; |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 103 | case 1: |
| 104 | if (!n--) break; |
| 105 | *args++ = regs->cx; |
| 106 | case 2: |
| 107 | if (!n--) break; |
| 108 | *args++ = regs->dx; |
| 109 | case 3: |
| 110 | if (!n--) break; |
| 111 | *args++ = regs->si; |
| 112 | case 4: |
| 113 | if (!n--) break; |
| 114 | *args++ = regs->di; |
| 115 | case 5: |
| 116 | if (!n--) break; |
| 117 | *args++ = regs->bp; |
| 118 | case 6: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 119 | if (!n--) break; |
| 120 | default: |
| 121 | BUG(); |
| 122 | break; |
| 123 | } |
| 124 | else |
| 125 | # endif |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 126 | switch (i) { |
| 127 | case 0: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 128 | if (!n--) break; |
| 129 | *args++ = regs->di; |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 130 | case 1: |
| 131 | if (!n--) break; |
| 132 | *args++ = regs->si; |
| 133 | case 2: |
| 134 | if (!n--) break; |
| 135 | *args++ = regs->dx; |
| 136 | case 3: |
| 137 | if (!n--) break; |
| 138 | *args++ = regs->r10; |
| 139 | case 4: |
| 140 | if (!n--) break; |
| 141 | *args++ = regs->r8; |
| 142 | case 5: |
| 143 | if (!n--) break; |
| 144 | *args++ = regs->r9; |
| 145 | case 6: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 146 | if (!n--) break; |
| 147 | default: |
| 148 | BUG(); |
| 149 | break; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | static inline void syscall_set_arguments(struct task_struct *task, |
| 154 | struct pt_regs *regs, |
| 155 | unsigned int i, unsigned int n, |
| 156 | const unsigned long *args) |
| 157 | { |
| 158 | # ifdef CONFIG_IA32_EMULATION |
| 159 | if (task_thread_info(task)->status & TS_COMPAT) |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 160 | switch (i) { |
| 161 | case 0: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 162 | if (!n--) break; |
| 163 | regs->bx = *args++; |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 164 | case 1: |
| 165 | if (!n--) break; |
| 166 | regs->cx = *args++; |
| 167 | case 2: |
| 168 | if (!n--) break; |
| 169 | regs->dx = *args++; |
| 170 | case 3: |
| 171 | if (!n--) break; |
| 172 | regs->si = *args++; |
| 173 | case 4: |
| 174 | if (!n--) break; |
| 175 | regs->di = *args++; |
| 176 | case 5: |
| 177 | if (!n--) break; |
| 178 | regs->bp = *args++; |
| 179 | case 6: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 180 | if (!n--) break; |
| 181 | default: |
| 182 | BUG(); |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 183 | break; |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 184 | } |
| 185 | else |
| 186 | # endif |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 187 | switch (i) { |
| 188 | case 0: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 189 | if (!n--) break; |
| 190 | regs->di = *args++; |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 191 | case 1: |
| 192 | if (!n--) break; |
| 193 | regs->si = *args++; |
| 194 | case 2: |
| 195 | if (!n--) break; |
| 196 | regs->dx = *args++; |
| 197 | case 3: |
| 198 | if (!n--) break; |
| 199 | regs->r10 = *args++; |
| 200 | case 4: |
| 201 | if (!n--) break; |
| 202 | regs->r8 = *args++; |
| 203 | case 5: |
| 204 | if (!n--) break; |
| 205 | regs->r9 = *args++; |
| 206 | case 6: |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 207 | if (!n--) break; |
| 208 | default: |
| 209 | BUG(); |
Linus Torvalds | c3c9897 | 2008-10-23 12:38:39 -0700 | [diff] [blame] | 210 | break; |
Roland McGrath | 68bd0f4 | 2008-04-18 17:08:44 -0700 | [diff] [blame] | 211 | } |
| 212 | } |
| 213 | |
| 214 | #endif /* CONFIG_X86_32 */ |
| 215 | |
H. Peter Anvin | 5e1b007 | 2008-10-23 00:20:33 -0700 | [diff] [blame] | 216 | #endif /* _ASM_X86_SYSCALL_H */ |