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
|
const builtin = @import("builtin");
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const linux = std.os.linux;
const SYS = linux.SYS;
const socklen_t = linux.socklen_t;
const sockaddr = linux.sockaddr;
const iovec = std.posix.iovec;
const iovec_const = std.posix.iovec_const;
const uid_t = linux.uid_t;
const gid_t = linux.gid_t;
const pid_t = linux.pid_t;
const stack_t = linux.stack_t;
const sigset_t = linux.sigset_t;
const timespec = std.os.linux.timespec;
pub fn syscall0(number: SYS) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize),
: [number] "{x8}" (@intFromEnum(number)),
: .{ .memory = true });
}
pub fn syscall1(number: SYS, arg1: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize),
: [number] "{x8}" (@intFromEnum(number)),
[arg1] "{x0}" (arg1),
: .{ .memory = true });
}
pub fn syscall2(number: SYS, arg1: usize, arg2: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize),
: [number] "{x8}" (@intFromEnum(number)),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
: .{ .memory = true });
}
pub fn syscall3(number: SYS, arg1: usize, arg2: usize, arg3: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize),
: [number] "{x8}" (@intFromEnum(number)),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3),
: .{ .memory = true });
}
pub fn syscall4(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize),
: [number] "{x8}" (@intFromEnum(number)),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3),
[arg4] "{x3}" (arg4),
: .{ .memory = true });
}
pub fn syscall5(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize),
: [number] "{x8}" (@intFromEnum(number)),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3),
[arg4] "{x3}" (arg4),
[arg5] "{x4}" (arg5),
: .{ .memory = true });
}
pub fn syscall6(
number: SYS,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
arg6: usize,
) usize {
return asm volatile ("svc #0"
: [ret] "={x0}" (-> usize),
: [number] "{x8}" (@intFromEnum(number)),
[arg1] "{x0}" (arg1),
[arg2] "{x1}" (arg2),
[arg3] "{x2}" (arg3),
[arg4] "{x3}" (arg4),
[arg5] "{x4}" (arg5),
[arg6] "{x5}" (arg6),
: .{ .memory = true });
}
pub fn clone() callconv(.naked) usize {
// __clone(func, stack, flags, arg, ptid, tls, ctid)
// x0, x1, w2, x3, x4, x5, x6
//
// syscall(SYS_clone, flags, stack, ptid, tls, ctid)
// x8, x0, x1, x2, x3, x4
asm volatile (
\\ // align stack and save func,arg
\\ and x1,x1,#-16
\\ stp x0,x3,[x1,#-16]!
\\
\\ // syscall
\\ uxtw x0,w2
\\ mov x2,x4
\\ mov x3,x5
\\ mov x4,x6
\\ mov x8,#220 // SYS_clone
\\ svc #0
\\
\\ cbz x0,1f
\\ // parent
\\ ret
\\
\\ // child
\\1:
);
if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
\\ .cfi_undefined lr
);
asm volatile (
\\ mov fp, 0
\\ mov lr, 0
\\
\\ ldp x1,x0,[sp],#16
\\ blr x1
\\ mov x8,#93 // SYS_exit
\\ svc #0
);
}
pub const restore = restore_rt;
pub fn restore_rt() callconv(.naked) noreturn {
switch (@import("builtin").zig_backend) {
.stage2_c => asm volatile (
\\ mov x8, %[number]
\\ svc #0
:
: [number] "i" (@intFromEnum(SYS.rt_sigreturn)),
),
else => asm volatile (
\\ svc #0
:
: [number] "{x8}" (@intFromEnum(SYS.rt_sigreturn)),
),
}
}
pub const F = struct {
pub const DUPFD = 0;
pub const GETFD = 1;
pub const SETFD = 2;
pub const GETFL = 3;
pub const SETFL = 4;
pub const SETOWN = 8;
pub const GETOWN = 9;
pub const SETSIG = 10;
pub const GETSIG = 11;
pub const GETLK = 5;
pub const SETLK = 6;
pub const SETLKW = 7;
pub const RDLCK = 0;
pub const WRLCK = 1;
pub const UNLCK = 2;
pub const SETOWN_EX = 15;
pub const GETOWN_EX = 16;
pub const GETOWNER_UIDS = 17;
};
pub const VDSO = struct {
pub const CGT_SYM = "__kernel_clock_gettime";
pub const CGT_VER = "LINUX_2.6.39";
};
pub const Flock = extern struct {
type: i16,
whence: i16,
start: off_t,
len: off_t,
pid: pid_t,
__unused: [4]u8,
};
pub const blksize_t = i32;
pub const nlink_t = u32;
pub const time_t = isize;
pub const mode_t = u32;
pub const off_t = isize;
pub const ino_t = usize;
pub const dev_t = usize;
pub const blkcnt_t = isize;
// The `stat` definition used by the Linux kernel.
pub const Stat = extern struct {
dev: dev_t,
ino: ino_t,
mode: mode_t,
nlink: nlink_t,
uid: uid_t,
gid: gid_t,
rdev: dev_t,
__pad: usize,
size: off_t,
blksize: blksize_t,
__pad2: i32,
blocks: blkcnt_t,
atim: timespec,
mtim: timespec,
ctim: timespec,
__unused: [2]u32,
pub fn atime(self: @This()) timespec {
return self.atim;
}
pub fn mtime(self: @This()) timespec {
return self.mtim;
}
pub fn ctime(self: @This()) timespec {
return self.ctim;
}
};
|