Debian Codegraph @

debian/missing-sources/rustix/src/backend/linux_raw/arch/x86.rs

raw
1 //! 32-bit x86 Linux system calls.
2 //!
3 //! There are two forms; `indirect_*` which take a callee, which allow calling
4 //! through the vDSO when possible, and plain forms, which use the `int 0x80`
5 //! instruction.
6 //!
7 //! Most `rustix` syscalls use the vsyscall mechanism rather than going using
8 //! `int 0x80` sequences, as vsyscall is much faster.
9 //!
10 //! Syscalls made with `int 0x80` preserve the flags register, while syscalls
11 //! made using vsyscall do not.
12
13 #![allow(dead_code)]
14
15 use crate::backend::reg::{
16 ArgReg, FromAsm, RetReg, SyscallNumber, ToAsm as _, A0, A1, A2, A3, A4, A5, R0,
17 };
18 use crate::backend::vdso_wrappers::SyscallType;
19 use core::arch::asm;
20
21 #[inline]
22 pub(in crate::backend) unsafe fn indirect_syscall0(
23 callee: SyscallType,
24 nr: SyscallNumber<'_>,
25 ) -> RetReg<R0> {
26 let r0;
27 asm!(
28 "call {callee}",
29 callee = in(reg) callee,
30 inlateout("eax") nr.to_asm() => r0,
31 );
32 FromAsm::from_asm(r0)
33 }
34
35 #[inline]
36 pub(in crate::backend) unsafe fn indirect_syscall1(
37 callee: SyscallType,
38 nr: SyscallNumber<'_>,
39 a0: ArgReg<'_, A0>,
40 ) -> RetReg<R0> {
41 let r0;
42 asm!(
43 "call {callee}",
44 callee = in(reg) callee,
45 inlateout("eax") nr.to_asm() => r0,
46 in("ebx") a0.to_asm(),
47 );
48 FromAsm::from_asm(r0)
49 }
50
51 #[inline]
52 pub(in crate::backend) unsafe fn indirect_syscall1_noreturn(
53 callee: SyscallType,
54 nr: SyscallNumber<'_>,
55 a0: ArgReg<'_, A0>,
56 ) -> ! {
57 asm!(
58 "call {callee}",
59 "ud2",
60 callee = in(reg) callee,
61 in("eax") nr.to_asm(),
62 in("ebx") a0.to_asm(),
63 options(noreturn)
64 )
65 }
66
67 #[inline]
68 pub(in crate::backend) unsafe fn indirect_syscall2(
69 callee: SyscallType,
70 nr: SyscallNumber<'_>,
71 a0: ArgReg<'_, A0>,
72 a1: ArgReg<'_, A1>,
73 ) -> RetReg<R0> {
74 let r0;
75 asm!(
76 "call {callee}",
77 callee = in(reg) callee,
78 inlateout("eax") nr.to_asm() => r0,
79 in("ebx") a0.to_asm(),
80 in("ecx") a1.to_asm(),
81 );
82 FromAsm::from_asm(r0)
83 }
84
85 #[inline]
86 pub(in crate::backend) unsafe fn indirect_syscall3(
87 callee: SyscallType,
88 nr: SyscallNumber<'_>,
89 a0: ArgReg<'_, A0>,
90 a1: ArgReg<'_, A1>,
91 a2: ArgReg<'_, A2>,
92 ) -> RetReg<R0> {
93 let r0;
94 asm!(
95 "call {callee}",
96 callee = in(reg) callee,
97 inlateout("eax") nr.to_asm() => r0,
98 in("ebx") a0.to_asm(),
99 in("ecx") a1.to_asm(),
100 in("edx") a2.to_asm(),
101 );
102 FromAsm::from_asm(r0)
103 }
104
105 #[inline]
106 pub(in crate::backend) unsafe fn indirect_syscall4(
107 callee: SyscallType,
108 nr: SyscallNumber<'_>,
109 a0: ArgReg<'_, A0>,
110 a1: ArgReg<'_, A1>,
111 a2: ArgReg<'_, A2>,
112 a3: ArgReg<'_, A3>,
113 ) -> RetReg<R0> {
114 let r0;
115 // a3 should go in esi, but `asm!` won't let us use it as an operand.
116 // Temporarily swap it into place, and then swap it back afterward.
117 //
118 // We hard-code the callee operand to use edi instead of `in(reg)` because
119 // even though we can't name esi as an operand, the compiler can use esi to
120 // satisfy `in(reg)`.
121 asm!(
122 "xchg esi, {a3}",
123 "call edi",
124 "xchg esi, {a3}",
125 a3 = in(reg) a3.to_asm(),
126 in("edi") callee,
127 inlateout("eax") nr.to_asm() => r0,
128 in("ebx") a0.to_asm(),
129 in("ecx") a1.to_asm(),
130 in("edx") a2.to_asm(),
131 );
132 FromAsm::from_asm(r0)
133 }
134
135 #[inline]
136 pub(in crate::backend) unsafe fn indirect_syscall5(
137 callee: SyscallType,
138 nr: SyscallNumber<'_>,
139 a0: ArgReg<'_, A0>,
140 a1: ArgReg<'_, A1>,
141 a2: ArgReg<'_, A2>,
142 a3: ArgReg<'_, A3>,
143 a4: ArgReg<'_, A4>,
144 ) -> RetReg<R0> {
145 let r0;
146 // Oof. a3 should go in esi, and `asm!` won't let us use that register as
147 // an operand. And we can't request stack slots. And there are no other
148 // registers free. Use eax as a temporary pointer to a slice, since it gets
149 // clobbered as the return value anyway.
150 asm!(
151 "push esi",
152 "push [eax + 0]",
153 "mov esi, [eax + 4]",
154 "mov eax, [eax + 8]",
155 "call [esp]",
156 "pop esi",
157 "pop esi",
158 inout("eax") &[callee as _, a3.to_asm(), nr.to_asm()] => r0,
159 in("ebx") a0.to_asm(),
160 in("ecx") a1.to_asm(),
161 in("edx") a2.to_asm(),
162 in("edi") a4.to_asm(),
163 );
164 FromAsm::from_asm(r0)
165 }
166
167 #[inline]
168 pub(in crate::backend) unsafe fn indirect_syscall6(
169 callee: SyscallType,
170 nr: SyscallNumber<'_>,
171 a0: ArgReg<'_, A0>,
172 a1: ArgReg<'_, A1>,
173 a2: ArgReg<'_, A2>,
174 a3: ArgReg<'_, A3>,
175 a4: ArgReg<'_, A4>,
176 a5: ArgReg<'_, A5>,
177 ) -> RetReg<R0> {
178 let r0;
179 // Oof again. a3 should go in esi, and a5 should go in ebp, and `asm!`
180 // won't let us use either of those registers as operands. And we can't
181 // request stack slots. And there are no other registers free. Use eax as a
182 // temporary pointer to a slice, since it gets clobbered as the return
183 // value anyway.
184 //
185 // This is another reason that syscalls should be compiler intrinsics
186 // rather than inline asm.
187 asm!(
188 "push ebp",
189 "push esi",
190 "push [eax + 0]",
191 "mov esi, [eax + 4]",
192 "mov ebp, [eax + 8]",
193 "mov eax, [eax + 12]",
194 "call [esp]",
195 "pop esi",
196 "pop esi",
197 "pop ebp",
198 inout("eax") &[callee as _, a3.to_asm(), a5.to_asm(), nr.to_asm()] => r0,
199 in("ebx") a0.to_asm(),
200 in("ecx") a1.to_asm(),
201 in("edx") a2.to_asm(),
202 in("edi") a4.to_asm(),
203 );
204 FromAsm::from_asm(r0)
205 }
206
207 #[inline]
208 pub(in crate::backend) unsafe fn syscall0_readonly(nr: SyscallNumber<'_>) -> RetReg<R0> {
209 let r0;
210 asm!(
211 "int $$0x80",
212 inlateout("eax") nr.to_asm() => r0,
213 options(nostack, preserves_flags, readonly)
214 );
215 FromAsm::from_asm(r0)
216 }
217
218 #[inline]
219 pub(in crate::backend) unsafe fn syscall1(nr: SyscallNumber<'_>, a0: ArgReg<'_, A0>) -> RetReg<R0> {
220 let r0;
221 asm!(
222 "int $$0x80",
223 inlateout("eax") nr.to_asm() => r0,
224 in("ebx") a0.to_asm(),
225 options(nostack, preserves_flags)
226 );
227 FromAsm::from_asm(r0)
228 }
229
230 #[inline]
231 pub(in crate::backend) unsafe fn syscall1_readonly(
232 nr: SyscallNumber<'_>,
233 a0: ArgReg<'_, A0>,
234 ) -> RetReg<R0> {
235 let r0;
236 asm!(
237 "int $$0x80",
238 inlateout("eax") nr.to_asm() => r0,
239 in("ebx") a0.to_asm(),
240 options(nostack, preserves_flags, readonly)
241 );
242 FromAsm::from_asm(r0)
243 }
244
245 #[inline]
246 pub(in crate::backend) unsafe fn syscall1_noreturn(nr: SyscallNumber<'_>, a0: ArgReg<'_, A0>) -> ! {
247 asm!(
248 "int $$0x80",
249 "ud2",
250 in("eax") nr.to_asm(),
251 in("ebx") a0.to_asm(),
252 options(nostack, noreturn)
253 )
254 }
255
256 #[inline]
257 pub(in crate::backend) unsafe fn syscall2(
258 nr: SyscallNumber<'_>,
259 a0: ArgReg<'_, A0>,
260 a1: ArgReg<'_, A1>,
261 ) -> RetReg<R0> {
262 let r0;
263 asm!(
264 "int $$0x80",
265 inlateout("eax") nr.to_asm() => r0,
266 in("ebx") a0.to_asm(),
267 in("ecx") a1.to_asm(),
268 options(nostack, preserves_flags)
269 );
270 FromAsm::from_asm(r0)
271 }
272
273 #[inline]
274 pub(in crate::backend) unsafe fn syscall2_readonly(
275 nr: SyscallNumber<'_>,
276 a0: ArgReg<'_, A0>,
277 a1: ArgReg<'_, A1>,
278 ) -> RetReg<R0> {
279 let r0;
280 asm!(
281 "int $$0x80",
282 inlateout("eax") nr.to_asm() => r0,
283 in("ebx") a0.to_asm(),
284 in("ecx") a1.to_asm(),
285 options(nostack, preserves_flags, readonly)
286 );
287 FromAsm::from_asm(r0)
288 }
289
290 #[inline]
291 pub(in crate::backend) unsafe fn syscall3(
292 nr: SyscallNumber<'_>,
293 a0: ArgReg<'_, A0>,
294 a1: ArgReg<'_, A1>,
295 a2: ArgReg<'_, A2>,
296 ) -> RetReg<R0> {
297 let r0;
298 asm!(
299 "int $$0x80",
300 inlateout("eax") nr.to_asm() => r0,
301 in("ebx") a0.to_asm(),
302 in("ecx") a1.to_asm(),
303 in("edx") a2.to_asm(),
304 options(nostack, preserves_flags)
305 );
306 FromAsm::from_asm(r0)
307 }
308
309 #[inline]
310 pub(in crate::backend) unsafe fn syscall3_readonly(
311 nr: SyscallNumber<'_>,
312 a0: ArgReg<'_, A0>,
313 a1: ArgReg<'_, A1>,
314 a2: ArgReg<'_, A2>,
315 ) -> RetReg<R0> {
316 let r0;
317 asm!(
318 "int $$0x80",
319 inlateout("eax") nr.to_asm() => r0,
320 in("ebx") a0.to_asm(),
321 in("ecx") a1.to_asm(),
322 in("edx") a2.to_asm(),
323 options(nostack, preserves_flags, readonly)
324 );
325 FromAsm::from_asm(r0)
326 }
327
328 #[inline]
329 pub(in crate::backend) unsafe fn syscall4(
330 nr: SyscallNumber<'_>,
331 a0: ArgReg<'_, A0>,
332 a1: ArgReg<'_, A1>,
333 a2: ArgReg<'_, A2>,
334 a3: ArgReg<'_, A3>,
335 ) -> RetReg<R0> {
336 let r0;
337 // a3 should go in esi, but `asm!` won't let us use it as an operand.
338 // Temporarily swap it into place, and then swap it back afterward.
339 asm!(
340 "xchg esi, {a3}",
341 "int $$0x80",
342 "xchg esi, {a3}",
343 a3 = in(reg) a3.to_asm(),
344 inlateout("eax") nr.to_asm() => r0,
345 in("ebx") a0.to_asm(),
346 in("ecx") a1.to_asm(),
347 in("edx") a2.to_asm(),
348 options(nostack, preserves_flags)
349 );
350 FromAsm::from_asm(r0)
351 }
352
353 #[inline]
354 pub(in crate::backend) unsafe fn syscall4_readonly(
355 nr: SyscallNumber<'_>,
356 a0: ArgReg<'_, A0>,
357 a1: ArgReg<'_, A1>,
358 a2: ArgReg<'_, A2>,
359 a3: ArgReg<'_, A3>,
360 ) -> RetReg<R0> {
361 let r0;
362 // See the comments in `syscall4`.
363 asm!(
364 "xchg esi, {a3}",
365 "int $$0x80",
366 "xchg esi, {a3}",
367 a3 = in(reg) a3.to_asm(),
368 inlateout("eax") nr.to_asm() => r0,
369 in("ebx") a0.to_asm(),
370 in("ecx") a1.to_asm(),
371 in("edx") a2.to_asm(),
372 options(nostack, preserves_flags, readonly)
373 );
374 FromAsm::from_asm(r0)
375 }
376
377 #[inline]
378 pub(in crate::backend) unsafe fn syscall5(
379 nr: SyscallNumber<'_>,
380 a0: ArgReg<'_, A0>,
381 a1: ArgReg<'_, A1>,
382 a2: ArgReg<'_, A2>,
383 a3: ArgReg<'_, A3>,
384 a4: ArgReg<'_, A4>,
385 ) -> RetReg<R0> {
386 let r0;
387 // As in `syscall4`, use xchg to handle a3. a4 should go in edi, and we can
388 // use that register as an operand. Unlike in `indirect_syscall5`, we don't
389 // have a `callee` operand taking up a register, so we have enough
390 // registers and don't need to use a slice.
391 asm!(
392 "xchg esi, {a3}",
393 "int $$0x80",
394 "xchg esi, {a3}",
395 a3 = in(reg) a3.to_asm(),
396 inlateout("eax") nr.to_asm() => r0,
397 in("ebx") a0.to_asm(),
398 in("ecx") a1.to_asm(),
399 in("edx") a2.to_asm(),
400 in("edi") a4.to_asm(),
401 options(nostack, preserves_flags)
402 );
403 FromAsm::from_asm(r0)
404 }
405
406 #[inline]
407 pub(in crate::backend) unsafe fn syscall5_readonly(
408 nr: SyscallNumber<'_>,
409 a0: ArgReg<'_, A0>,
410 a1: ArgReg<'_, A1>,
411 a2: ArgReg<'_, A2>,
412 a3: ArgReg<'_, A3>,
413 a4: ArgReg<'_, A4>,
414 ) -> RetReg<R0> {
415 let r0;
416 // See the comments in `syscall5`.
417 asm!(
418 "xchg esi, {a3}",
419 "int $$0x80",
420 "xchg esi, {a3}",
421 a3 = in(reg) a3.to_asm(),
422 inlateout("eax") nr.to_asm() => r0,
423 in("ebx") a0.to_asm(),
424 in("ecx") a1.to_asm(),
425 in("edx") a2.to_asm(),
426 in("edi") a4.to_asm(),
427 options(nostack, preserves_flags, readonly)
428 );
429 FromAsm::from_asm(r0)
430 }
431
432 #[inline]
433 pub(in crate::backend) unsafe fn syscall6(
434 nr: SyscallNumber<'_>,
435 a0: ArgReg<'_, A0>,
436 a1: ArgReg<'_, A1>,
437 a2: ArgReg<'_, A2>,
438 a3: ArgReg<'_, A3>,
439 a4: ArgReg<'_, A4>,
440 a5: ArgReg<'_, A5>,
441 ) -> RetReg<R0> {
442 let r0;
443 // See the comments in `indirect_syscall6`.
444 asm!(
445 "push ebp",
446 "push esi",
447 "mov esi, [eax + 0]",
448 "mov ebp, [eax + 4]",
449 "mov eax, [eax + 8]",
450 "int $$0x80",
451 "pop esi",
452 "pop ebp",
453 inout("eax") &[a3.to_asm(), a5.to_asm(), nr.to_asm()] => r0,
454 in("ebx") a0.to_asm(),
455 in("ecx") a1.to_asm(),
456 in("edx") a2.to_asm(),
457 in("edi") a4.to_asm(),
458 options(preserves_flags)
459 );
460 FromAsm::from_asm(r0)
461 }
462
463 #[inline]
464 pub(in crate::backend) unsafe fn syscall6_readonly(
465 nr: SyscallNumber<'_>,
466 a0: ArgReg<'_, A0>,
467 a1: ArgReg<'_, A1>,
468 a2: ArgReg<'_, A2>,
469 a3: ArgReg<'_, A3>,
470 a4: ArgReg<'_, A4>,
471 a5: ArgReg<'_, A5>,
472 ) -> RetReg<R0> {
473 let r0;
474 // See the comments in `indirect_syscall6`.
475 asm!(
476 "push ebp",
477 "push esi",
478 "mov esi, [eax + 0]",
479 "mov ebp, [eax + 4]",
480 "mov eax, [eax + 8]",
481 "int $$0x80",
482 "pop esi",
483 "pop ebp",
484 inout("eax") &[a3.to_asm(), a5.to_asm(), nr.to_asm()] => r0,
485 in("ebx") a0.to_asm(),
486 in("ecx") a1.to_asm(),
487 in("edx") a2.to_asm(),
488 in("edi") a4.to_asm(),
489 options(preserves_flags, readonly)
490 );
491 FromAsm::from_asm(r0)
492 }