跳转到内容

std.os.linux.x86

标准库Zig 0.17.0官方原文
源码std/os/linux/x86.zig

第 1 行

std.os.linux.x86.builtin

常量

第 1 行

std.os.linux.x86.clone

函数

第 138 行

pub fn clone() callconv(.naked) u32

std.os.linux.x86.restore

函数

第 188 行

pub fn restore() callconv(.naked) noreturn

std.os.linux.x86.restore_rt

函数

第 198 行

pub fn restore_rt() callconv(.naked) noreturn

std.os.linux.x86.SC

命名空间

第 230 行

socketcall() 调用编号

std.os.linux.x86.SC.accept

常量

第 235 行

std.os.linux.x86.SC.accept4

常量

第 248 行

std.os.linux.x86.SC.bind

常量

第 232 行

std.os.linux.x86.SC.connect

常量

第 233 行

std.os.linux.x86.SC.getpeername

常量

第 237 行

std.os.linux.x86.SC.getsockname

常量

第 236 行

std.os.linux.x86.SC.getsockopt

常量

第 245 行

std.os.linux.x86.SC.listen

常量

第 234 行

std.os.linux.x86.SC.recv

常量

第 240 行

std.os.linux.x86.SC.recvfrom

常量

第 242 行

std.os.linux.x86.SC.recvmmsg

常量

第 249 行

std.os.linux.x86.SC.recvmsg

常量

第 247 行

std.os.linux.x86.SC.send

常量

第 239 行

std.os.linux.x86.SC.sendmmsg

常量

第 250 行

std.os.linux.x86.SC.sendmsg

常量

第 246 行

std.os.linux.x86.SC.sendto

常量

第 241 行

std.os.linux.x86.SC.setsockopt

常量

第 244 行

std.os.linux.x86.SC.shutdown

常量

第 243 行

std.os.linux.x86.SC.socket

常量

第 231 行

std.os.linux.x86.SC.socketpair

常量

第 238 行

std.os.linux.x86.socketcall

函数

第 129 行

pub fn socketcall(call: u32, args: [*]const u32) u32

参数

call: u32
args: [*]const u32

std.os.linux.x86.std

别名

第 2 行

别名目标 std

std.os.linux.x86.SYS

别名

第 3 行

别名目标 std.os.linux.SYS

std.os.linux.x86.syscall_arg_t

类型

第 5 行

std.os.linux.x86.syscall0

函数

第 7 行

pub fn syscall0( number: SYS, ) u32

参数

number: SYS

std.os.linux.x86.syscall1

函数

第 16 行

pub fn syscall1( number: SYS, arg1: syscall_arg_t, ) u32

参数

number: SYS
arg1: syscall_arg_t

std.os.linux.x86.syscall2

函数

第 27 行

pub fn syscall2( number: SYS, arg1: syscall_arg_t, arg2: syscall_arg_t, ) u32

参数

number: SYS
arg1: syscall_arg_t
arg2: syscall_arg_t

std.os.linux.x86.syscall3

函数

第 40 行

pub fn syscall3( number: SYS, arg1: syscall_arg_t, arg2: syscall_arg_t, arg3: syscall_arg_t, ) u32

参数

number: SYS
arg1: syscall_arg_t
arg2: syscall_arg_t
arg3: syscall_arg_t

std.os.linux.x86.syscall4

函数

第 55 行

pub fn syscall4( number: SYS, arg1: syscall_arg_t, arg2: syscall_arg_t, arg3: syscall_arg_t, arg4: syscall_arg_t, ) u32

参数

number: SYS
arg1: syscall_arg_t
arg2: syscall_arg_t
arg3: syscall_arg_t
arg4: syscall_arg_t

std.os.linux.x86.syscall5

函数

第 72 行

pub fn syscall5( number: SYS, arg1: syscall_arg_t, arg2: syscall_arg_t, arg3: syscall_arg_t, arg4: syscall_arg_t, arg5: syscall_arg_t, ) u32

参数

std.os.linux.x86.syscall6

函数

第 91 行

pub fn syscall6( number: SYS, arg1: syscall_arg_t, arg2: syscall_arg_t, arg3: syscall_arg_t, arg4: syscall_arg_t, arg5: syscall_arg_t, arg6: syscall_arg_t, ) u32

参数

std.os.linux.x86.time_t

类型

第 212 行

std.os.linux.x86.user_desc

容器

第 214 行

字段

entry_number: u32
base_addr: u32
limit: u32
flags: packed struct(u32) {
    seg_32bit: u1,
    contents: u2,
    read_exec_only: u1,
    limit_in_pages: u1,
    seg_not_present: u1,
    useable: u1,
    _: u25 = undefined,
}

std.os.linux.x86.VDSO

命名空间

第 207 行

std.os.linux.x86.VDSO.CGT_SYM

常量

第 208 行

std.os.linux.x86.VDSO.CGT_VER

常量

第 209 行

源码

std/os/linux/x86.zig

const builtin = @import("builtin");
const std = @import("../../std.zig");
const SYS = std.os.linux.SYS;

pub const syscall_arg_t = u32;

pub fn syscall0(
    number: SYS,
) u32 {
    return asm volatile ("int $0x80"
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(number)),
        : .{ .memory = true });
}

pub fn syscall1(
    number: SYS,
    arg1: syscall_arg_t,
) u32 {
    return asm volatile ("int $0x80"
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(number)),
          [arg1] "{ebx}" (arg1),
        : .{ .memory = true });
}

pub fn syscall2(
    number: SYS,
    arg1: syscall_arg_t,
    arg2: syscall_arg_t,
) u32 {
    return asm volatile ("int $0x80"
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(number)),
          [arg1] "{ebx}" (arg1),
          [arg2] "{ecx}" (arg2),
        : .{ .memory = true });
}

pub fn syscall3(
    number: SYS,
    arg1: syscall_arg_t,
    arg2: syscall_arg_t,
    arg3: syscall_arg_t,
) u32 {
    return asm volatile ("int $0x80"
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(number)),
          [arg1] "{ebx}" (arg1),
          [arg2] "{ecx}" (arg2),
          [arg3] "{edx}" (arg3),
        : .{ .memory = true });
}

pub fn syscall4(
    number: SYS,
    arg1: syscall_arg_t,
    arg2: syscall_arg_t,
    arg3: syscall_arg_t,
    arg4: syscall_arg_t,
) u32 {
    return asm volatile ("int $0x80"
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(number)),
          [arg1] "{ebx}" (arg1),
          [arg2] "{ecx}" (arg2),
          [arg3] "{edx}" (arg3),
          [arg4] "{esi}" (arg4),
        : .{ .memory = true });
}

pub fn syscall5(
    number: SYS,
    arg1: syscall_arg_t,
    arg2: syscall_arg_t,
    arg3: syscall_arg_t,
    arg4: syscall_arg_t,
    arg5: syscall_arg_t,
) u32 {
    return asm volatile ("int $0x80"
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(number)),
          [arg1] "{ebx}" (arg1),
          [arg2] "{ecx}" (arg2),
          [arg3] "{edx}" (arg3),
          [arg4] "{esi}" (arg4),
          [arg5] "{edi}" (arg5),
        : .{ .memory = true });
}

pub fn syscall6(
    number: SYS,
    arg1: syscall_arg_t,
    arg2: syscall_arg_t,
    arg3: syscall_arg_t,
    arg4: syscall_arg_t,
    arg5: syscall_arg_t,
    arg6: syscall_arg_t,
) u32 {
    // arg6 无法通过寄存器传给汇编,因为 ebp 可能已保留用作帧指针,且没有更多通用寄存器可用,因此必须为其使用内存操作数。增加该内存操作数意味着在位置无关代码中可能需要引用 GOT,而 GOT 又需要自己的通用寄存器,所以还必须通过内存传入另一个参数。
    // 要正确处理这一点非常困难,因为在处理完内存输入之前,不能改动 esp 或 ebp;该输入可能相对于 esp 或 ebp 寻址。
    //
    //
    //
    //
    const args56: [2]syscall_arg_t = .{ arg5, arg6 };
    return asm volatile (
        \\ push %[args56]
        \\ push %%ebp
        \\ mov 4(%%esp), %%ebp
        \\ mov %%edi, 4(%%esp)
        \\ // The saved %%edi and %%ebp are on the stack, and %%ebp points to `args56`.
        \\ // Prepare the last two args, syscall, then pop the saved %%ebp and %%edi.
        \\ mov (%%ebp), %%edi
        \\ mov 4(%%ebp), %%ebp
        \\ int  $0x80
        \\ pop  %%ebp
        \\ pop  %%edi
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(number)),
          [arg1] "{ebx}" (arg1),
          [arg2] "{ecx}" (arg2),
          [arg3] "{edx}" (arg3),
          [arg4] "{esi}" (arg4),
          [args56] "rm" (&args56),
        : .{ .memory = true });
}

pub fn socketcall(call: u32, args: [*]const u32) u32 {
    return asm volatile ("int $0x80"
        : [ret] "={eax}" (-> u32),
        : [number] "{eax}" (@backingInt(SYS.socketcall)),
          [arg1] "{ebx}" (call),
          [arg2] "{ecx}" (@intFromPtr(args)),
        : .{ .memory = true });
}

pub fn clone() callconv(.naked) u32 {
    // __clone(func, stack, flags, arg, ptid, tls, ctid)
    //         +8,   +12,   +16,   +20, +24,  +28, +32
    //
    // syscall(SYS_clone, flags, stack, ptid, tls, ctid)
    //         eax,       ebx,   ecx,   edx,  esi, edi
    asm volatile (
        \\ pushl %%ebp
        \\ movl %%esp,%%ebp
        \\ pushl %%ebx
        \\ pushl %%esi
        \\ pushl %%edi
        \\ // Setup the arguments
        \\ movl 16(%%ebp),%%ebx
        \\ movl 12(%%ebp),%%ecx
        \\ andl $-16,%%ecx
        \\ subl $20,%%ecx
        \\ movl 20(%%ebp),%%eax
        \\ movl %%eax,4(%%ecx)
        \\ movl 8(%%ebp),%%eax
        \\ movl %%eax,0(%%ecx)
        \\ movl 24(%%ebp),%%edx
        \\ movl 28(%%ebp),%%esi
        \\ movl 32(%%ebp),%%edi
        \\ movl $120,%%eax // SYS_clone
        \\ int $128
        \\ testl %%eax,%%eax
        \\ jz 1f
        \\ popl %%edi
        \\ popl %%esi
        \\ popl %%ebx
        \\ popl %%ebp
        \\ retl
        \\
        \\1:
    );
    if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
        \\ .cfi_undefined %%eip
    );
    asm volatile (
        \\ xorl %%ebp,%%ebp
        \\
        \\ popl %%eax
        \\ calll *%%eax
        \\ movl %%eax,%%ebx
        \\ movl $1,%%eax // SYS_exit
        \\ int $128
    );
}

pub fn restore() callconv(.naked) noreturn {
    asm volatile (
        \\ addl $4, %%esp
        \\ movl %[number], %%eax
        \\ int $0x80
        :
        : [number] "i" (@backingInt(SYS.sigreturn)),
    );
}

pub fn restore_rt() callconv(.naked) noreturn {
    asm volatile (
        \\ movl %[number], %%eax
        \\ int $0x80
        :
        : [number] "i" (@backingInt(SYS.rt_sigreturn)),
    );
}

pub const VDSO = struct {
    pub const CGT_SYM = "__vdso_clock_gettime";
    pub const CGT_VER = "LINUX_2.6";
};

pub const time_t = i32;

pub const user_desc = extern struct {
    entry_number: u32,
    base_addr: u32,
    limit: u32,
    flags: packed struct(u32) {
        seg_32bit: u1,
        contents: u2,
        read_exec_only: u1,
        limit_in_pages: u1,
        seg_not_present: u1,
        useable: u1,
        _: u25 = undefined,
    },
};

/// socketcall() 调用编号
pub const SC = struct {
    pub const socket = 1;
    pub const bind = 2;
    pub const connect = 3;
    pub const listen = 4;
    pub const accept = 5;
    pub const getsockname = 6;
    pub const getpeername = 7;
    pub const socketpair = 8;
    pub const send = 9;
    pub const recv = 10;
    pub const sendto = 11;
    pub const recvfrom = 12;
    pub const shutdown = 13;
    pub const setsockopt = 14;
    pub const getsockopt = 15;
    pub const sendmsg = 16;
    pub const recvmsg = 17;
    pub const accept4 = 18;
    pub const recvmmsg = 19;
    pub const sendmmsg = 20;
};