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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
const std = @import("../../std.zig");
const maxInt = std.math.maxInt;
const pid_t = linux.pid_t;
const uid_t = linux.uid_t;
const clock_t = linux.clock_t;
const stack_t = linux.stack_t;
const sigset_t = linux.sigset_t;
const linux = std.os.linux;
const SYS = linux.SYS;
const sockaddr = linux.sockaddr;
const socklen_t = linux.socklen_t;
const iovec = std.os.iovec;
const iovec_const = std.os.iovec_const;
const timespec = linux.timespec;
pub fn syscall_pipe(fd: *[2]i32) usize {
return asm volatile (
\\ mov %[arg], %%g3
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ # Return the error code
\\ ba 2f
\\ neg %%o0
\\1:
\\ st %%o0, [%%g3+0]
\\ st %%o1, [%%g3+4]
\\ clr %%o0
\\2:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(SYS.pipe)),
[arg] "r" (fd),
: "memory", "g3"
);
}
pub fn syscall_fork() usize {
// Linux/sparc64 fork() returns two values in %o0 and %o1:
// - On the parent's side, %o0 is the child's PID and %o1 is 0.
// - On the child's side, %o0 is the parent's PID and %o1 is 1.
// We need to clear the child's %o0 so that the return values
// conform to the libc convention.
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ ba 2f
\\ neg %%o0
\\ 1:
\\ # Clear the child's %%o0
\\ dec %%o1
\\ and %%o1, %%o0, %%o0
\\ 2:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(SYS.fork)),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub fn syscall0(number: SYS) usize {
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ neg %%o0
\\ 1:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(number)),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub fn syscall1(number: SYS, arg1: usize) usize {
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ neg %%o0
\\ 1:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(number)),
[arg1] "{o0}" (arg1),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub fn syscall2(number: SYS, arg1: usize, arg2: usize) usize {
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ neg %%o0
\\ 1:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(number)),
[arg1] "{o0}" (arg1),
[arg2] "{o1}" (arg2),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub fn syscall3(number: SYS, arg1: usize, arg2: usize, arg3: usize) usize {
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ neg %%o0
\\ 1:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(number)),
[arg1] "{o0}" (arg1),
[arg2] "{o1}" (arg2),
[arg3] "{o2}" (arg3),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub fn syscall4(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize) usize {
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ neg %%o0
\\ 1:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(number)),
[arg1] "{o0}" (arg1),
[arg2] "{o1}" (arg2),
[arg3] "{o2}" (arg3),
[arg4] "{o3}" (arg4),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub fn syscall5(number: SYS, arg1: usize, arg2: usize, arg3: usize, arg4: usize, arg5: usize) usize {
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ neg %%o0
\\ 1:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(number)),
[arg1] "{o0}" (arg1),
[arg2] "{o1}" (arg2),
[arg3] "{o2}" (arg3),
[arg4] "{o3}" (arg4),
[arg5] "{o4}" (arg5),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub fn syscall6(
number: SYS,
arg1: usize,
arg2: usize,
arg3: usize,
arg4: usize,
arg5: usize,
arg6: usize,
) usize {
return asm volatile (
\\ t 0x6d
\\ bcc,pt %%xcc, 1f
\\ nop
\\ neg %%o0
\\ 1:
: [ret] "={o0}" (-> usize),
: [number] "{g1}" (@enumToInt(number)),
[arg1] "{o0}" (arg1),
[arg2] "{o1}" (arg2),
[arg3] "{o2}" (arg3),
[arg4] "{o3}" (arg4),
[arg5] "{o4}" (arg5),
[arg6] "{o5}" (arg6),
: "memory", "xcc", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
const CloneFn = std.meta.FnPtr(fn (arg: usize) callconv(.C) u8);
/// This matches the libc clone function.
pub extern fn clone(func: CloneFn, stack: usize, flags: usize, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
pub const restore = restore_rt;
// Need to use C ABI here instead of naked
// to prevent an infinite loop when calling rt_sigreturn.
pub fn restore_rt() callconv(.C) void {
return asm volatile ("t 0x6d"
:
: [number] "{g1}" (@enumToInt(SYS.rt_sigreturn)),
: "memory", "xcc", "o0", "o1", "o2", "o3", "o4", "o5", "o7"
);
}
pub const O = struct {
pub const CREAT = 0x200;
pub const EXCL = 0x800;
pub const NOCTTY = 0x8000;
pub const TRUNC = 0x400;
pub const APPEND = 0x8;
pub const NONBLOCK = 0x4000;
pub const SYNC = 0x802000;
pub const DSYNC = 0x2000;
pub const RSYNC = SYNC;
pub const DIRECTORY = 0x10000;
pub const NOFOLLOW = 0x20000;
pub const CLOEXEC = 0x400000;
pub const ASYNC = 0x40;
pub const DIRECT = 0x100000;
pub const LARGEFILE = 0;
pub const NOATIME = 0x200000;
pub const PATH = 0x1000000;
pub const TMPFILE = 0x2010000;
pub const NDELAY = NONBLOCK | 0x4;
};
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 = 5;
pub const GETOWN = 6;
pub const GETLK = 7;
pub const SETLK = 8;
pub const SETLKW = 9;
pub const RDLCK = 1;
pub const WRLCK = 2;
pub const UNLCK = 3;
pub const SETOWN_EX = 15;
pub const GETOWN_EX = 16;
pub const GETOWNER_UIDS = 17;
};
pub const LOCK = struct {
pub const SH = 1;
pub const EX = 2;
pub const NB = 4;
pub const UN = 8;
};
pub const MAP = struct {
/// stack-like segment
pub const GROWSDOWN = 0x0200;
/// ETXTBSY
pub const DENYWRITE = 0x0800;
/// mark it as an executable
pub const EXECUTABLE = 0x1000;
/// pages are locked
pub const LOCKED = 0x0100;
/// don't check for reservations
pub const NORESERVE = 0x0040;
};
pub const VDSO = struct {
pub const CGT_SYM = "__vdso_clock_gettime";
pub const CGT_VER = "LINUX_2.6";
};
pub const Flock = extern struct {
type: i16,
whence: i16,
start: off_t,
len: off_t,
pid: pid_t,
};
pub const msghdr = extern struct {
name: ?*sockaddr,
namelen: socklen_t,
iov: [*]iovec,
iovlen: u64,
control: ?*anyopaque,
controllen: u64,
flags: i32,
};
pub const msghdr_const = extern struct {
name: ?*const sockaddr,
namelen: socklen_t,
iov: [*]const iovec_const,
iovlen: u64,
control: ?*const anyopaque,
controllen: u64,
flags: i32,
};
pub const off_t = i64;
pub const ino_t = u64;
pub const mode_t = u32;
pub const dev_t = usize;
pub const nlink_t = u32;
pub const blksize_t = isize;
pub const blkcnt_t = isize;
// The `stat64` definition used by the kernel.
pub const Stat = extern struct {
dev: u64,
ino: u64,
nlink: u64,
mode: u32,
uid: u32,
gid: u32,
__pad0: u32,
rdev: u64,
size: i64,
blksize: i64,
blocks: i64,
atim: timespec,
mtim: timespec,
ctim: timespec,
__unused: [3]u64,
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;
}
};
pub const timeval = extern struct {
tv_sec: isize,
tv_usec: i32,
};
pub const timezone = extern struct {
tz_minuteswest: i32,
tz_dsttime: i32,
};
// TODO I'm not sure if the code below is correct, need someone with more
// knowledge about sparc64 linux internals to look into.
pub const Elf_Symndx = u32;
pub const fpstate = extern struct {
regs: [32]u64,
fsr: u64,
gsr: u64,
fprs: u64,
};
pub const __fpq = extern struct {
fpq_addr: *u32,
fpq_instr: u32,
};
pub const __fq = extern struct {
FQu: extern union {
whole: f64,
fpq: __fpq,
},
};
pub const fpregset_t = extern struct {
fpu_fr: extern union {
fpu_regs: [32]u32,
fpu_dregs: [32]f64,
fpu_qregs: [16]c_longdouble,
},
fpu_q: *__fq,
fpu_fsr: u64,
fpu_qcnt: u8,
fpu_q_entrysize: u8,
fpu_en: u8,
};
pub const siginfo_fpu_t = extern struct {
float_regs: [64]u32,
fsr: u64,
gsr: u64,
fprs: u64,
};
pub const sigcontext = extern struct {
info: [128]i8,
regs: extern struct {
u_regs: [16]u64,
tstate: u64,
tpc: u64,
tnpc: u64,
y: u64,
fprs: u64,
},
fpu_save: *siginfo_fpu_t,
stack: extern struct {
sp: usize,
flags: i32,
size: u64,
},
mask: u64,
};
pub const greg_t = u64;
pub const gregset_t = [19]greg_t;
pub const fq = extern struct {
addr: *u64,
insn: u32,
};
pub const fpu_t = extern struct {
fregs: extern union {
sregs: [32]u32,
dregs: [32]u64,
qregs: [16]c_longdouble,
},
fsr: u64,
fprs: u64,
gsr: u64,
fq: *fq,
qcnt: u8,
qentsz: u8,
enab: u8,
};
pub const mcontext_t = extern struct {
gregs: gregset_t,
fp: greg_t,
@"i7": greg_t,
fpregs: fpu_t,
};
pub const ucontext_t = extern struct {
link: ?*ucontext_t,
flags: u64,
sigmask: u64,
mcontext: mcontext_t,
stack: stack_t,
sigmask: sigset_t,
};
pub const rlimit_resource = enum(c_int) {
/// Per-process CPU limit, in seconds.
CPU,
/// Largest file that can be created, in bytes.
FSIZE,
/// Maximum size of data segment, in bytes.
DATA,
/// Maximum size of stack segment, in bytes.
STACK,
/// Largest core file that can be created, in bytes.
CORE,
/// Largest resident set size, in bytes.
/// This affects swapping; processes that are exceeding their
/// resident set size will be more likely to have physical memory
/// taken from them.
RSS,
/// Number of open files.
NOFILE,
/// Number of processes.
NPROC,
/// Locked-in-memory address space.
MEMLOCK,
/// Address space limit.
AS,
/// Maximum number of file locks.
LOCKS,
/// Maximum number of pending signals.
SIGPENDING,
/// Maximum bytes in POSIX message queues.
MSGQUEUE,
/// Maximum nice priority allowed to raise to.
/// Nice levels 19 .. -20 correspond to 0 .. 39
/// values of this resource limit.
NICE,
/// Maximum realtime priority allowed for non-priviledged
/// processes.
RTPRIO,
/// Maximum CPU time in µs that a process scheduled under a real-time
/// scheduling policy may consume without making a blocking system
/// call before being forcibly descheduled.
RTTIME,
_,
};
|