authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-09-25 12:08:47-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2019-09-25 12:08:47-04:00
log48c9e17aa1566ca7140d2460f2ed56daa8db7eeb
tree2db21d12ec37aae5e3c6408d10604708a6f39086
parent993d5bc9c945a40815b4c9f119f67917961e6c9f
parenta56b767c35b96f76f315e5deeddae62e6891a440
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #3308 from LemonBoy/riscv-things

Miscellaneous RISC-V related commits

5 files changed, 143 insertions(+), 104 deletions(-)

std/os/bits/linux/riscv64.zig+20-18
......@@ -298,25 +298,25 @@ pub const SYS_fspick = 433;
298298pub const SYS_pidfd_open = 434;
299299pub const SYS_clone3 = 435;
300300
301pub const O_CREAT = 0100;
302pub const O_EXCL = 0200;
303pub const O_NOCTTY = 0400;
304pub const O_TRUNC = 01000;
305pub const O_APPEND = 02000;
306pub const O_NONBLOCK = 04000;
307pub const O_DSYNC = 010000;
308pub const O_SYNC = 04010000;
309pub const O_RSYNC = 04010000;
310pub const O_DIRECTORY = 0200000;
311pub const O_NOFOLLOW = 0400000;
312pub const O_CLOEXEC = 02000000;
301pub const O_CREAT = 0o100;
302pub const O_EXCL = 0o200;
303pub const O_NOCTTY = 0o400;
304pub const O_TRUNC = 0o1000;
305pub const O_APPEND = 0o2000;
306pub const O_NONBLOCK = 0o4000;
307pub const O_DSYNC = 0o10000;
308pub const O_SYNC = 0o4010000;
309pub const O_RSYNC = 0o4010000;
310pub const O_DIRECTORY = 0o200000;
311pub const O_NOFOLLOW = 0o400000;
312pub const O_CLOEXEC = 0o2000000;
313313
314pub const O_ASYNC = 020000;
315pub const O_DIRECT = 040000;
316pub const O_LARGEFILE = 0100000;
317pub const O_NOATIME = 01000000;
318pub const O_PATH = 010000000;
319pub const O_TMPFILE = 020200000;
314pub const O_ASYNC = 0o20000;
315pub const O_DIRECT = 0o40000;
316pub const O_LARGEFILE = 0o100000;
317pub const O_NOATIME = 0o1000000;
318pub const O_PATH = 0o10000000;
319pub const O_TMPFILE = 0o20200000;
320320pub const O_NDELAY = O_NONBLOCK;
321321
322322pub const F_DUPFD = 0;
......@@ -386,3 +386,5 @@ pub const Stat = extern struct {
386386 return self.ctim;
387387 }
388388};
389
390pub const Elf_Symndx = u32;
std/os/linux.zig+1-1
......@@ -469,7 +469,7 @@ var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime);
469469const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize;
470470
471471pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
472 if (VDSO_CGT_SYM.len != 0) {
472 if (@hasDecl(@This(), "VDSO_CGT_SYM")) {
473473 const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered);
474474 if (ptr) |fn_ptr| {
475475 const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
std/os/linux/riscv64.zig+2
......@@ -82,3 +82,5 @@ pub fn syscall6(
8282 : "memory"
8383 );
8484}
85
86pub extern fn clone(func: extern fn (arg: usize) u8, stack: usize, flags: u32, arg: usize, ptid: *i32, tls: usize, ctid: *i32) usize;
std/os/linux/tls.zig+1
......@@ -89,6 +89,7 @@ const tls_tp_offset = switch (builtin.arch) {
8989
9090const tls_dtv_offset = switch (builtin.arch) {
9191 .mipsel => 0x8000,
92 .riscv32, .riscv64 => 0x800,
9293 else => 0,
9394};
9495
std/special/c.zig+119-85
......@@ -182,10 +182,7 @@ comptime {
182182 @export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);
183183 }
184184 if (builtin.os == builtin.Os.linux) {
185 // TODO implement clone for riscv64
186 if (builtin.arch != .riscv64) {
187 @export("clone", clone, builtin.GlobalLinkage.Strong);
188 }
185 @export("clone", clone, builtin.GlobalLinkage.Strong);
189186 }
190187}
191188extern fn __stack_chk_fail() noreturn {
......@@ -196,87 +193,124 @@ extern fn __stack_chk_fail() noreturn {
196193// it causes a segfault in release mode. this is a workaround of calling it
197194// across .o file boundaries. fix comptime @ptrCast of nakedcc functions.
198195nakedcc fn clone() void {
199 if (builtin.arch == builtin.Arch.x86_64) {
200 asm volatile (
201 \\ xor %%eax,%%eax
202 \\ mov $56,%%al // SYS_clone
203 \\ mov %%rdi,%%r11
204 \\ mov %%rdx,%%rdi
205 \\ mov %%r8,%%rdx
206 \\ mov %%r9,%%r8
207 \\ mov 8(%%rsp),%%r10
208 \\ mov %%r11,%%r9
209 \\ and $-16,%%rsi
210 \\ sub $8,%%rsi
211 \\ mov %%rcx,(%%rsi)
212 \\ syscall
213 \\ test %%eax,%%eax
214 \\ jnz 1f
215 \\ xor %%ebp,%%ebp
216 \\ pop %%rdi
217 \\ call *%%r9
218 \\ mov %%eax,%%edi
219 \\ xor %%eax,%%eax
220 \\ mov $60,%%al // SYS_exit
221 \\ syscall
222 \\ hlt
223 \\1: ret
224 \\
225 );
226 } else if (builtin.arch == builtin.Arch.aarch64) {
227 // __clone(func, stack, flags, arg, ptid, tls, ctid)
228 // x0, x1, w2, x3, x4, x5, x6
229
230 // syscall(SYS_clone, flags, stack, ptid, tls, ctid)
231 // x8, x0, x1, x2, x3, x4
232 asm volatile (
233 \\ // align stack and save func,arg
234 \\ and x1,x1,#-16
235 \\ stp x0,x3,[x1,#-16]!
236 \\
237 \\ // syscall
238 \\ uxtw x0,w2
239 \\ mov x2,x4
240 \\ mov x3,x5
241 \\ mov x4,x6
242 \\ mov x8,#220 // SYS_clone
243 \\ svc #0
244 \\
245 \\ cbz x0,1f
246 \\ // parent
247 \\ ret
248 \\ // child
249 \\1: ldp x1,x0,[sp],#16
250 \\ blr x1
251 \\ mov x8,#93 // SYS_exit
252 \\ svc #0
253 );
254 } else if (builtin.arch == builtin.Arch.arm) {
255 asm volatile (
256 \\ stmfd sp!,{r4,r5,r6,r7}
257 \\ mov r7,#120
258 \\ mov r6,r3
259 \\ mov r5,r0
260 \\ mov r0,r2
261 \\ and r1,r1,#-16
262 \\ ldr r2,[sp,#16]
263 \\ ldr r3,[sp,#20]
264 \\ ldr r4,[sp,#24]
265 \\ svc 0
266 \\ tst r0,r0
267 \\ beq 1f
268 \\ ldmfd sp!,{r4,r5,r6,r7}
269 \\ bx lr
270 \\
271 \\1: mov r0,r6
272 \\ bl 3f
273 \\2: mov r7,#1
274 \\ svc 0
275 \\ b 2b
276 \\3: bx r5
277 );
278 } else {
279 @compileError("Implement clone() for this arch.");
196 switch (builtin.arch) {
197 .x86_64 => {
198 asm volatile (
199 \\ xor %%eax,%%eax
200 \\ mov $56,%%al // SYS_clone
201 \\ mov %%rdi,%%r11
202 \\ mov %%rdx,%%rdi
203 \\ mov %%r8,%%rdx
204 \\ mov %%r9,%%r8
205 \\ mov 8(%%rsp),%%r10
206 \\ mov %%r11,%%r9
207 \\ and $-16,%%rsi
208 \\ sub $8,%%rsi
209 \\ mov %%rcx,(%%rsi)
210 \\ syscall
211 \\ test %%eax,%%eax
212 \\ jnz 1f
213 \\ xor %%ebp,%%ebp
214 \\ pop %%rdi
215 \\ call *%%r9
216 \\ mov %%eax,%%edi
217 \\ xor %%eax,%%eax
218 \\ mov $60,%%al // SYS_exit
219 \\ syscall
220 \\ hlt
221 \\1: ret
222 \\
223 );
224 },
225 .aarch64 => {
226 // __clone(func, stack, flags, arg, ptid, tls, ctid)
227 // x0, x1, w2, x3, x4, x5, x6
228
229 // syscall(SYS_clone, flags, stack, ptid, tls, ctid)
230 // x8, x0, x1, x2, x3, x4
231 asm volatile (
232 \\ // align stack and save func,arg
233 \\ and x1,x1,#-16
234 \\ stp x0,x3,[x1,#-16]!
235 \\
236 \\ // syscall
237 \\ uxtw x0,w2
238 \\ mov x2,x4
239 \\ mov x3,x5
240 \\ mov x4,x6
241 \\ mov x8,#220 // SYS_clone
242 \\ svc #0
243 \\
244 \\ cbz x0,1f
245 \\ // parent
246 \\ ret
247 \\ // child
248 \\1: ldp x1,x0,[sp],#16
249 \\ blr x1
250 \\ mov x8,#93 // SYS_exit
251 \\ svc #0
252 );
253 },
254 .arm => {
255 asm volatile (
256 \\ stmfd sp!,{r4,r5,r6,r7}
257 \\ mov r7,#120
258 \\ mov r6,r3
259 \\ mov r5,r0
260 \\ mov r0,r2
261 \\ and r1,r1,#-16
262 \\ ldr r2,[sp,#16]
263 \\ ldr r3,[sp,#20]
264 \\ ldr r4,[sp,#24]
265 \\ svc 0
266 \\ tst r0,r0
267 \\ beq 1f
268 \\ ldmfd sp!,{r4,r5,r6,r7}
269 \\ bx lr
270 \\
271 \\1: mov r0,r6
272 \\ bl 3f
273 \\2: mov r7,#1
274 \\ svc 0
275 \\ b 2b
276 \\3: bx r5
277 );
278 },
279 .riscv64 => {
280 // __clone(func, stack, flags, arg, ptid, tls, ctid)
281 // a0, a1, a2, a3, a4, a5, a6
282
283 // syscall(SYS_clone, flags, stack, ptid, tls, ctid)
284 // a7 a0, a1, a2, a3, a4
285 asm volatile (
286 \\ # Save func and arg to stack
287 \\ addi a1, a1, -16
288 \\ sd a0, 0(a1)
289 \\ sd a3, 8(a1)
290 \\
291 \\ # Call SYS_clone
292 \\ mv a0, a2
293 \\ mv a2, a4
294 \\ mv a3, a5
295 \\ mv a4, a6
296 \\ li a7, 220 # SYS_clone
297 \\ ecall
298 \\
299 \\ beqz a0, 1f
300 \\ # Parent
301 \\ ret
302 \\
303 \\ # Child
304 \\1: ld a1, 0(sp)
305 \\ ld a0, 8(sp)
306 \\ jalr a1
307 \\
308 \\ # Exit
309 \\ li a7, 93 # SYS_exit
310 \\ ecall
311 );
312 },
313 else => @compileError("Implement clone() for this arch."),
280314 }
281315}
282316