Skip to content

Commit c966441

Browse files
committed
Expand the comments in the outline asm wrapper code.
Add module-level comments which walk through the purpose for this code and how the code implements it.
1 parent cb8f43f commit c966441

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

src/imp/linux_raw/arch/outline/nr_last.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
1+
//! Syscall wrappers for platforms which pass the syscall number specially.
2+
//!
3+
//! Rustix aims to minimize the amount of assembly code it needs. To that end,
4+
//! this code reorders syscall arguments as close as feasible to the actual
5+
//! syscall convention before calling the assembly functions.
6+
//!
7+
//! Many architectures use a convention where the syscall number is passed in a
8+
//! special register, with the regular syscall arguments passed in either the
9+
//! same or similar registers as the platform C convention. This code
10+
//! approximates that order by passing the regular syscall arguments first, and
11+
//! the syscall number last. That way, the outline assembly code typically just
12+
//! needs to move the syscall number to its special register, and leave the
13+
//! other arguments mostly as they are.
14+
115
use crate::imp::reg::{ArgReg, RetReg, SyscallNumber, A0, A1, A2, A3, A4, A5, R0};
216

3-
// Some architectures' outline assembly code prefers to see the `nr` argument
4-
// last, as that lines up the syscall calling convention with the userspace
5-
// calling convention better.
6-
//
717
// First we declare the actual assembly routines with `*_nr_last` names and
818
// reordered arguments. If the signatures or calling conventions are ever
919
// changed, the symbol names should also be updated accordingly, to avoid
@@ -125,8 +135,9 @@ pub(in crate::imp) unsafe fn syscall6(
125135
rustix_syscall6_nr_last(a0, a1, a2, a3, a4, a5, nr)
126136
}
127137

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

src/imp/linux_raw/arch/outline/x86.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
1+
//! Syscall wrappers for 32-bit x86.
2+
//!
3+
//! This module is similar to the `nr_last` module, except specialized for
4+
//! 32-bit x86.
5+
//!
6+
//! The syscall convention passes all arguments in registers. The closest we
7+
//! can easily get to that from Rust is to use the fastcall convention which
8+
//! passes the first two arguments in `ecx` and `edx`, which are the second
9+
//! and third Linux syscall arguments. To line them up, this function passes
10+
//! the second and third syscall argument as the first and second argument to
11+
//! the outline assembly, followed by the first syscall argument, and then the
12+
//! rest of the syscall arguments. The assembly code still has to do some work,
13+
//! but at least we can get up to two arguments into the right place for it.
14+
115
#![allow(dead_code, unused_imports)]
216

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

6-
// x86 (using fastcall) prefers to pass a1 and a2 first, before a0, because
7-
// fastcall passes the first two arguments in ecx and edx, which are the second
8-
// and third Linux syscall arguments.
9-
//
1020
// First we declare the actual assembly routines with `*_nr_last_fastcall`
1121
// names and reordered arguments. If the signatures or calling conventions are
1222
// ever changed, the symbol names should also be updated accordingly, to avoid
@@ -128,8 +138,9 @@ pub(in crate::imp) unsafe fn syscall6(
128138
rustix_syscall6_nr_last_fastcall(a1, a2, a0, a3, a4, a5, nr)
129139
}
130140

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

0 commit comments

Comments
 (0)