1const builtin = @import("builtin");
2const std = @import("../../std.zig");
3const SYS = std.os.linux.SYS;
4
5pub const syscall_arg_t = u64;
6
7pub fn syscall0(
8 number: SYS,
9) u64 {
10 return asm volatile ("svc #0"
11 : [ret] "={x0}" (-> u64),
12 : [number] "{x8}" (@backingInt(number)),
13 : .{ .memory = true });
14}
15
16pub fn syscall1(
17 number: SYS,
18 arg1: syscall_arg_t,
19) u64 {
20 return asm volatile ("svc #0"
21 : [ret] "={x0}" (-> u64),
22 : [number] "{x8}" (@backingInt(number)),
23 [arg1] "{x0}" (arg1),
24 : .{ .memory = true });
25}
26
27pub fn syscall2(
28 number: SYS,
29 arg1: syscall_arg_t,
30 arg2: syscall_arg_t,
31) u64 {
32 return asm volatile ("svc #0"
33 : [ret] "={x0}" (-> u64),
34 : [number] "{x8}" (@backingInt(number)),
35 [arg1] "{x0}" (arg1),
36 [arg2] "{x1}" (arg2),
37 : .{ .memory = true });
38}
39
40pub fn syscall3(
41 number: SYS,
42 arg1: syscall_arg_t,
43 arg2: syscall_arg_t,
44 arg3: syscall_arg_t,
45) u64 {
46 return asm volatile ("svc #0"
47 : [ret] "={x0}" (-> u64),
48 : [number] "{x8}" (@backingInt(number)),
49 [arg1] "{x0}" (arg1),
50 [arg2] "{x1}" (arg2),
51 [arg3] "{x2}" (arg3),
52 : .{ .memory = true });
53}
54
55pub fn syscall4(
56 number: SYS,
57 arg1: syscall_arg_t,
58 arg2: syscall_arg_t,
59 arg3: syscall_arg_t,
60 arg4: syscall_arg_t,
61) u64 {
62 return asm volatile ("svc #0"
63 : [ret] "={x0}" (-> u64),
64 : [number] "{x8}" (@backingInt(number)),
65 [arg1] "{x0}" (arg1),
66 [arg2] "{x1}" (arg2),
67 [arg3] "{x2}" (arg3),
68 [arg4] "{x3}" (arg4),
69 : .{ .memory = true });
70}
71
72pub fn syscall5(
73 number: SYS,
74 arg1: syscall_arg_t,
75 arg2: syscall_arg_t,
76 arg3: syscall_arg_t,
77 arg4: syscall_arg_t,
78 arg5: syscall_arg_t,
79) u64 {
80 return asm volatile ("svc #0"
81 : [ret] "={x0}" (-> u64),
82 : [number] "{x8}" (@backingInt(number)),
83 [arg1] "{x0}" (arg1),
84 [arg2] "{x1}" (arg2),
85 [arg3] "{x2}" (arg3),
86 [arg4] "{x3}" (arg4),
87 [arg5] "{x4}" (arg5),
88 : .{ .memory = true });
89}
90
91pub fn syscall6(
92 number: SYS,
93 arg1: syscall_arg_t,
94 arg2: syscall_arg_t,
95 arg3: syscall_arg_t,
96 arg4: syscall_arg_t,
97 arg5: syscall_arg_t,
98 arg6: syscall_arg_t,
99) u64 {
100 return asm volatile ("svc #0"
101 : [ret] "={x0}" (-> u64),
102 : [number] "{x8}" (@backingInt(number)),
103 [arg1] "{x0}" (arg1),
104 [arg2] "{x1}" (arg2),
105 [arg3] "{x2}" (arg3),
106 [arg4] "{x3}" (arg4),
107 [arg5] "{x4}" (arg5),
108 [arg6] "{x5}" (arg6),
109 : .{ .memory = true });
110}
111
112pub fn clone() callconv(.naked) u64 {
113 // __clone(func, stack, flags, arg, ptid, tls, ctid)
114 // x0, x1, w2, x3, x4, x5, x6
115 //
116 // syscall(SYS_clone, flags, stack, ptid, tls, ctid)
117 // x8, x0, x1, x2, x3, x4
118 asm volatile (
119 \\ // align stack and save func,arg
120 \\ and x1,x1,#-16
121 \\ stp x0,x3,[x1,#-16]!
122 \\
123 \\ // syscall
124 \\ uxtw x0,w2
125 \\ mov x2,x4
126 \\ mov x3,x5
127 \\ mov x4,x6
128 \\ mov x8,#220 // SYS_clone
129 \\ svc #0
130 \\
131 \\ cbz x0,1f
132 \\ // parent
133 \\ ret
134 \\
135 \\ // child
136 \\1:
137 );
138 if (builtin.unwind_tables != .none or !builtin.strip_debug_info) asm volatile (
139 \\ .cfi_undefined lr
140 );
141 asm volatile (
142 \\ mov fp, 0
143 \\ mov lr, 0
144 \\
145 \\ ldp x1,x0,[sp],#16
146 \\ blr x1
147 \\ mov x8,#93 // SYS_exit
148 \\ svc #0
149 );
150}
151
152pub const restore = restore_rt;
153
154pub fn restore_rt() callconv(.naked) noreturn {
155 asm volatile (
156 \\ mov w8, %[number]
157 \\ svc #0
158 :
159 : [number] "i" (@backingInt(SYS.rt_sigreturn)),
160 );
161}
162
163pub const VDSO = struct {
164 pub const CGT_SYM = "__kernel_clock_gettime";
165 pub const CGT_VER = "LINUX_2.6.39";
166};
167
168pub const time_t = i64;