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;...@@ -298,25 +298,25 @@ pub const SYS_fspick = 433;
298pub const SYS_pidfd_open = 434;298pub const SYS_pidfd_open = 434;
299pub const SYS_clone3 = 435;299pub const SYS_clone3 = 435;
300300
301pub const O_CREAT = 0100;301pub const O_CREAT = 0o100;
302pub const O_EXCL = 0200;302pub const O_EXCL = 0o200;
303pub const O_NOCTTY = 0400;303pub const O_NOCTTY = 0o400;
304pub const O_TRUNC = 01000;304pub const O_TRUNC = 0o1000;
305pub const O_APPEND = 02000;305pub const O_APPEND = 0o2000;
306pub const O_NONBLOCK = 04000;306pub const O_NONBLOCK = 0o4000;
307pub const O_DSYNC = 010000;307pub const O_DSYNC = 0o10000;
308pub const O_SYNC = 04010000;308pub const O_SYNC = 0o4010000;
309pub const O_RSYNC = 04010000;309pub const O_RSYNC = 0o4010000;
310pub const O_DIRECTORY = 0200000;310pub const O_DIRECTORY = 0o200000;
311pub const O_NOFOLLOW = 0400000;311pub const O_NOFOLLOW = 0o400000;
312pub const O_CLOEXEC = 02000000;312pub const O_CLOEXEC = 0o2000000;
313313
314pub const O_ASYNC = 020000;314pub const O_ASYNC = 0o20000;
315pub const O_DIRECT = 040000;315pub const O_DIRECT = 0o40000;
316pub const O_LARGEFILE = 0100000;316pub const O_LARGEFILE = 0o100000;
317pub const O_NOATIME = 01000000;317pub const O_NOATIME = 0o1000000;
318pub const O_PATH = 010000000;318pub const O_PATH = 0o10000000;
319pub const O_TMPFILE = 020200000;319pub const O_TMPFILE = 0o20200000;
320pub const O_NDELAY = O_NONBLOCK;320pub const O_NDELAY = O_NONBLOCK;
321321
322pub const F_DUPFD = 0;322pub const F_DUPFD = 0;
...@@ -386,3 +386,5 @@ pub const Stat = extern struct {...@@ -386,3 +386,5 @@ pub const Stat = extern struct {
386 return self.ctim;386 return self.ctim;
387 }387 }
388};388};
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);...@@ -469,7 +469,7 @@ var vdso_clock_gettime = @ptrCast(?*const c_void, init_vdso_clock_gettime);
469const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize;469const vdso_clock_gettime_ty = extern fn (i32, *timespec) usize;
470470
471pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {471pub fn clock_gettime(clk_id: i32, tp: *timespec) usize {
472 if (VDSO_CGT_SYM.len != 0) {472 if (@hasDecl(@This(), "VDSO_CGT_SYM")) {
473 const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered);473 const ptr = @atomicLoad(?*const c_void, &vdso_clock_gettime, .Unordered);
474 if (ptr) |fn_ptr| {474 if (ptr) |fn_ptr| {
475 const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);475 const f = @ptrCast(vdso_clock_gettime_ty, fn_ptr);
std/os/linux/riscv64.zig+2
...@@ -82,3 +82,5 @@ pub fn syscall6(...@@ -82,3 +82,5 @@ pub fn syscall6(
82 : "memory"82 : "memory"
83 );83 );
84}84}
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) {...@@ -89,6 +89,7 @@ const tls_tp_offset = switch (builtin.arch) {
8989
90const tls_dtv_offset = switch (builtin.arch) {90const tls_dtv_offset = switch (builtin.arch) {
91 .mipsel => 0x8000,91 .mipsel => 0x8000,
92 .riscv32, .riscv64 => 0x800,
92 else => 0,93 else => 0,
93};94};
9495
std/special/c.zig+119-85
...@@ -182,10 +182,7 @@ comptime {...@@ -182,10 +182,7 @@ comptime {
182 @export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);182 @export("__stack_chk_fail", __stack_chk_fail, builtin.GlobalLinkage.Strong);
183 }183 }
184 if (builtin.os == builtin.Os.linux) {184 if (builtin.os == builtin.Os.linux) {
185 // TODO implement clone for riscv64185 @export("clone", clone, builtin.GlobalLinkage.Strong);
186 if (builtin.arch != .riscv64) {
187 @export("clone", clone, builtin.GlobalLinkage.Strong);
188 }
189 }186 }
190}187}
191extern fn __stack_chk_fail() noreturn {188extern fn __stack_chk_fail() noreturn {
...@@ -196,87 +193,124 @@ extern fn __stack_chk_fail() noreturn {...@@ -196,87 +193,124 @@ extern fn __stack_chk_fail() noreturn {
196// it causes a segfault in release mode. this is a workaround of calling it193// it causes a segfault in release mode. this is a workaround of calling it
197// across .o file boundaries. fix comptime @ptrCast of nakedcc functions.194// across .o file boundaries. fix comptime @ptrCast of nakedcc functions.
198nakedcc fn clone() void {195nakedcc fn clone() void {
199 if (builtin.arch == builtin.Arch.x86_64) {196 switch (builtin.arch) {
200 asm volatile (197 .x86_64 => {
201 \\ xor %%eax,%%eax198 asm volatile (
202 \\ mov $56,%%al // SYS_clone199 \\ xor %%eax,%%eax
203 \\ mov %%rdi,%%r11200 \\ mov $56,%%al // SYS_clone
204 \\ mov %%rdx,%%rdi201 \\ mov %%rdi,%%r11
205 \\ mov %%r8,%%rdx202 \\ mov %%rdx,%%rdi
206 \\ mov %%r9,%%r8203 \\ mov %%r8,%%rdx
207 \\ mov 8(%%rsp),%%r10204 \\ mov %%r9,%%r8
208 \\ mov %%r11,%%r9205 \\ mov 8(%%rsp),%%r10
209 \\ and $-16,%%rsi206 \\ mov %%r11,%%r9
210 \\ sub $8,%%rsi207 \\ and $-16,%%rsi
211 \\ mov %%rcx,(%%rsi)208 \\ sub $8,%%rsi
212 \\ syscall209 \\ mov %%rcx,(%%rsi)
213 \\ test %%eax,%%eax210 \\ syscall
214 \\ jnz 1f211 \\ test %%eax,%%eax
215 \\ xor %%ebp,%%ebp212 \\ jnz 1f
216 \\ pop %%rdi213 \\ xor %%ebp,%%ebp
217 \\ call *%%r9214 \\ pop %%rdi
218 \\ mov %%eax,%%edi215 \\ call *%%r9
219 \\ xor %%eax,%%eax216 \\ mov %%eax,%%edi
220 \\ mov $60,%%al // SYS_exit217 \\ xor %%eax,%%eax
221 \\ syscall218 \\ mov $60,%%al // SYS_exit
222 \\ hlt219 \\ syscall
223 \\1: ret220 \\ hlt
224 \\221 \\1: ret
225 );222 \\
226 } else if (builtin.arch == builtin.Arch.aarch64) {223 );
227 // __clone(func, stack, flags, arg, ptid, tls, ctid)224 },
228 // x0, x1, w2, x3, x4, x5, x6225 .aarch64 => {
229226 // __clone(func, stack, flags, arg, ptid, tls, ctid)
230 // syscall(SYS_clone, flags, stack, ptid, tls, ctid)227 // x0, x1, w2, x3, x4, x5, x6
231 // x8, x0, x1, x2, x3, x4228
232 asm volatile (229 // syscall(SYS_clone, flags, stack, ptid, tls, ctid)
233 \\ // align stack and save func,arg230 // x8, x0, x1, x2, x3, x4
234 \\ and x1,x1,#-16231 asm volatile (
235 \\ stp x0,x3,[x1,#-16]!232 \\ // align stack and save func,arg
236 \\233 \\ and x1,x1,#-16
237 \\ // syscall234 \\ stp x0,x3,[x1,#-16]!
238 \\ uxtw x0,w2235 \\
239 \\ mov x2,x4236 \\ // syscall
240 \\ mov x3,x5237 \\ uxtw x0,w2
241 \\ mov x4,x6238 \\ mov x2,x4
242 \\ mov x8,#220 // SYS_clone239 \\ mov x3,x5
243 \\ svc #0240 \\ mov x4,x6
244 \\241 \\ mov x8,#220 // SYS_clone
245 \\ cbz x0,1f242 \\ svc #0
246 \\ // parent243 \\
247 \\ ret244 \\ cbz x0,1f
248 \\ // child245 \\ // parent
249 \\1: ldp x1,x0,[sp],#16246 \\ ret
250 \\ blr x1247 \\ // child
251 \\ mov x8,#93 // SYS_exit248 \\1: ldp x1,x0,[sp],#16
252 \\ svc #0249 \\ blr x1
253 );250 \\ mov x8,#93 // SYS_exit
254 } else if (builtin.arch == builtin.Arch.arm) {251 \\ svc #0
255 asm volatile (252 );
256 \\ stmfd sp!,{r4,r5,r6,r7}253 },
257 \\ mov r7,#120254 .arm => {
258 \\ mov r6,r3255 asm volatile (
259 \\ mov r5,r0256 \\ stmfd sp!,{r4,r5,r6,r7}
260 \\ mov r0,r2257 \\ mov r7,#120
261 \\ and r1,r1,#-16258 \\ mov r6,r3
262 \\ ldr r2,[sp,#16]259 \\ mov r5,r0
263 \\ ldr r3,[sp,#20]260 \\ mov r0,r2
264 \\ ldr r4,[sp,#24]261 \\ and r1,r1,#-16
265 \\ svc 0262 \\ ldr r2,[sp,#16]
266 \\ tst r0,r0263 \\ ldr r3,[sp,#20]
267 \\ beq 1f264 \\ ldr r4,[sp,#24]
268 \\ ldmfd sp!,{r4,r5,r6,r7}265 \\ svc 0
269 \\ bx lr266 \\ tst r0,r0
270 \\267 \\ beq 1f
271 \\1: mov r0,r6268 \\ ldmfd sp!,{r4,r5,r6,r7}
272 \\ bl 3f269 \\ bx lr
273 \\2: mov r7,#1270 \\
274 \\ svc 0271 \\1: mov r0,r6
275 \\ b 2b272 \\ bl 3f
276 \\3: bx r5273 \\2: mov r7,#1
277 );274 \\ svc 0
278 } else {275 \\ b 2b
279 @compileError("Implement clone() for this arch.");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."),
280 }314 }
281}315}
282316