summaryrefslogtreecommitdiffstats
path: root/vendor/rustix/src/imp/linux_raw/arch/outline/x86.rs
blob: 938a4a09d29fa8bd4f56e0db81a5b1aa75517e6f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
//! Syscall wrappers for 32-bit x86.
//!
//! This module is similar to the `nr_last` module, except specialized for
//! 32-bit x86.
//!
//! The syscall convention passes all arguments in registers. The closest we
//! can easily get to that from Rust is to use the fastcall convention which
//! passes the first two arguments in `ecx` and `edx`, which are the second
//! and third Linux syscall arguments. To line them up, this function passes
//! the second and third syscall argument as the first and second argument to
//! the outline assembly, followed by the first syscall argument, and then the
//! rest of the syscall arguments. The assembly code still has to do some work,
//! but at least we can get up to two arguments into the right place for it.

#![allow(dead_code, unused_imports)]

use crate::imp::reg::{ArgReg, RetReg, SyscallNumber, A0, A1, A2, A3, A4, A5, R0};
use crate::imp::vdso_wrappers::SyscallType;

// First we declare the actual assembly routines with `*_nr_last_fastcall`
// names and reordered arguments. If the signatures or calling conventions are
// ever changed, the symbol names should also be updated accordingly, to avoid
// collisions with other versions of this crate.
//
// We don't define `_readonly` versions of these because we have no way to tell
// Rust that calls to our outline assembly are readonly.
extern "fastcall" {
    fn rustix_syscall0_nr_last_fastcall(nr: SyscallNumber<'_>) -> RetReg<R0>;
    fn rustix_syscall1_nr_last_fastcall(a0: ArgReg<'_, A0>, nr: SyscallNumber<'_>) -> RetReg<R0>;
    fn rustix_syscall1_noreturn_nr_last_fastcall(a0: ArgReg<'_, A0>, nr: SyscallNumber<'_>) -> !;
    fn rustix_syscall2_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a0: ArgReg<'_, A0>,
        nr: SyscallNumber<'_>,
    ) -> RetReg<R0>;
    fn rustix_syscall3_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        nr: SyscallNumber<'_>,
    ) -> RetReg<R0>;
    fn rustix_syscall4_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        a3: ArgReg<'_, A3>,
        nr: SyscallNumber<'_>,
    ) -> RetReg<R0>;
    fn rustix_syscall5_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        a3: ArgReg<'_, A3>,
        a4: ArgReg<'_, A4>,
        nr: SyscallNumber<'_>,
    ) -> RetReg<R0>;
    fn rustix_syscall6_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        a3: ArgReg<'_, A3>,
        a4: ArgReg<'_, A4>,
        a5: ArgReg<'_, A5>,
        nr: SyscallNumber<'_>,
    ) -> RetReg<R0>;
}

// Then we define inline wrapper functions that do the reordering.

#[inline]
pub(in crate::imp) unsafe fn syscall0(nr: SyscallNumber<'_>) -> RetReg<R0> {
    rustix_syscall0_nr_last_fastcall(nr)
}
#[inline]
pub(in crate::imp) unsafe fn syscall1(nr: SyscallNumber<'_>, a0: ArgReg<'_, A0>) -> RetReg<R0> {
    rustix_syscall1_nr_last_fastcall(a0, nr)
}
#[inline]
pub(in crate::imp) unsafe fn syscall1_noreturn(nr: SyscallNumber<'_>, a0: ArgReg<'_, A0>) -> ! {
    rustix_syscall1_noreturn_nr_last_fastcall(a0, nr)
}
#[inline]
pub(in crate::imp) unsafe fn syscall2(
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
) -> RetReg<R0> {
    rustix_syscall2_nr_last_fastcall(a1, a0, nr)
}
#[inline]
pub(in crate::imp) unsafe fn syscall3(
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
) -> RetReg<R0> {
    rustix_syscall3_nr_last_fastcall(a1, a2, a0, nr)
}
#[inline]
pub(in crate::imp) unsafe fn syscall4(
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
    a3: ArgReg<'_, A3>,
) -> RetReg<R0> {
    rustix_syscall4_nr_last_fastcall(a1, a2, a0, a3, nr)
}
#[inline]
pub(in crate::imp) unsafe fn syscall5(
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
    a3: ArgReg<'_, A3>,
    a4: ArgReg<'_, A4>,
) -> RetReg<R0> {
    rustix_syscall5_nr_last_fastcall(a1, a2, a0, a3, a4, nr)
}
#[inline]
pub(in crate::imp) unsafe fn syscall6(
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
    a3: ArgReg<'_, A3>,
    a4: ArgReg<'_, A4>,
    a5: ArgReg<'_, A5>,
) -> RetReg<R0> {
    rustix_syscall6_nr_last_fastcall(a1, a2, a0, a3, a4, a5, nr)
}

// Then we define the `_readonly` versions of the wrappers. We don't have
// separate `_readonly` implementations, so these can just be aliases to
// their non-`_readonly` counterparts.
pub(in crate::imp) use {
    syscall0 as syscall0_readonly, syscall1 as syscall1_readonly, syscall2 as syscall2_readonly,
    syscall3 as syscall3_readonly, syscall4 as syscall4_readonly, syscall5 as syscall5_readonly,
    syscall6 as syscall6_readonly,
};

// x86 prefers to route all syscalls through the vDSO, though this isn't
// always possible, so it also has a special form for doing the dispatch.
//
// First we declare the actual assembly routines with `*_nr_last_fastcall`
// names and reordered arguments. If the signatures or calling conventions are
// ever changed, the symbol names should also be updated accordingly, to avoid
// collisions with other versions of this crate.
extern "fastcall" {
    fn rustix_indirect_syscall0_nr_last_fastcall(
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> RetReg<R0>;
    fn rustix_indirect_syscall1_nr_last_fastcall(
        a0: ArgReg<'_, A0>,
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> RetReg<R0>;
    fn rustix_indirect_syscall1_noreturn_nr_last_fastcall(
        a0: ArgReg<'_, A0>,
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> !;
    fn rustix_indirect_syscall2_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a0: ArgReg<'_, A0>,
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> RetReg<R0>;
    fn rustix_indirect_syscall3_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> RetReg<R0>;
    fn rustix_indirect_syscall4_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        a3: ArgReg<'_, A3>,
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> RetReg<R0>;
    fn rustix_indirect_syscall5_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        a3: ArgReg<'_, A3>,
        a4: ArgReg<'_, A4>,
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> RetReg<R0>;
    fn rustix_indirect_syscall6_nr_last_fastcall(
        a1: ArgReg<'_, A1>,
        a2: ArgReg<'_, A2>,
        a0: ArgReg<'_, A0>,
        a3: ArgReg<'_, A3>,
        a4: ArgReg<'_, A4>,
        a5: ArgReg<'_, A5>,
        nr: SyscallNumber<'_>,
        callee: SyscallType,
    ) -> RetReg<R0>;
}

// Then we define inline wrapper functions that do the reordering.

#[inline]
pub(in crate::imp) unsafe fn indirect_syscall0(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
) -> RetReg<R0> {
    rustix_indirect_syscall0_nr_last_fastcall(nr, callee)
}
#[inline]
pub(in crate::imp) unsafe fn indirect_syscall1(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
) -> RetReg<R0> {
    rustix_indirect_syscall1_nr_last_fastcall(a0, nr, callee)
}
#[inline]
pub(in crate::imp) unsafe fn indirect_syscall1_noreturn(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
) -> ! {
    rustix_indirect_syscall1_noreturn_nr_last_fastcall(a0, nr, callee)
}
#[inline]
pub(in crate::imp) unsafe fn indirect_syscall2(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
) -> RetReg<R0> {
    rustix_indirect_syscall2_nr_last_fastcall(a1, a0, nr, callee)
}
#[inline]
pub(in crate::imp) unsafe fn indirect_syscall3(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
) -> RetReg<R0> {
    rustix_indirect_syscall3_nr_last_fastcall(a1, a2, a0, nr, callee)
}
#[inline]
pub(in crate::imp) unsafe fn indirect_syscall4(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
    a3: ArgReg<'_, A3>,
) -> RetReg<R0> {
    rustix_indirect_syscall4_nr_last_fastcall(a1, a2, a0, a3, nr, callee)
}
#[inline]
pub(in crate::imp) unsafe fn indirect_syscall5(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
    a3: ArgReg<'_, A3>,
    a4: ArgReg<'_, A4>,
) -> RetReg<R0> {
    rustix_indirect_syscall5_nr_last_fastcall(a1, a2, a0, a3, a4, nr, callee)
}
#[inline]
pub(in crate::imp) unsafe fn indirect_syscall6(
    callee: SyscallType,
    nr: SyscallNumber<'_>,
    a0: ArgReg<'_, A0>,
    a1: ArgReg<'_, A1>,
    a2: ArgReg<'_, A2>,
    a3: ArgReg<'_, A3>,
    a4: ArgReg<'_, A4>,
    a5: ArgReg<'_, A5>,
) -> RetReg<R0> {
    rustix_indirect_syscall6_nr_last_fastcall(a1, a2, a0, a3, a4, a5, nr, callee)
}