From f6f03cd90f5c0a8cce7981b16f384b088f41c319 Mon Sep 17 00:00:00 2001 From: Timon Kruiper Date: Tue, 31 Mar 2020 00:19:19 +0200 Subject: [PATCH 1/2] compiler-rt: implement clear_cache for arm32-linux --- lib/std/special/compiler_rt.zig | 2 +- lib/std/special/compiler_rt/clear_cache.zig | 46 +++++++++------------ 2 files changed, 20 insertions(+), 28 deletions(-) diff --git a/lib/std/special/compiler_rt.zig b/lib/std/special/compiler_rt.zig index 1f68d928c2..44db926b5f 100644 --- a/lib/std/special/compiler_rt.zig +++ b/lib/std/special/compiler_rt.zig @@ -11,7 +11,7 @@ comptime { switch (builtin.arch) { .i386, .x86_64 => @export(@import("compiler_rt/stack_probe.zig").zig_probe_stack, .{ .name = "__zig_probe_stack", .linkage = linkage }), - .aarch64, .aarch64_be, .aarch64_32 => { + .aarch64, .aarch64_be, .aarch64_32, .arm, .armeb, .thumb, .thumbeb => { @export(@import("compiler_rt/clear_cache.zig").clear_cache, .{ .name = "__clear_cache", .linkage = linkage }); }, else => {}, diff --git a/lib/std/special/compiler_rt/clear_cache.zig b/lib/std/special/compiler_rt/clear_cache.zig index 77470251d1..5d09a7692d 100644 --- a/lib/std/special/compiler_rt/clear_cache.zig +++ b/lib/std/special/compiler_rt/clear_cache.zig @@ -45,33 +45,25 @@ pub fn clear_cache(start: usize, end: usize) callconv(.C) void { @compileError("TODO"); // FlushInstructionCache(GetCurrentProcess(), start, end - start); } else if (arm32 and !apple) { - @compileError("TODO"); - //#if defined(__FreeBSD__) || defined(__NetBSD__) - // struct arm_sync_icache_args arg; - // - // arg.addr = (uintptr_t)start; - // arg.len = (uintptr_t)end - (uintptr_t)start; - // - // sysarch(ARM_SYNC_ICACHE, &arg); - //#elif defined(__linux__) - //// We used to include asm/unistd.h for the __ARM_NR_cacheflush define, but - //// it also brought many other unused defines, as well as a dependency on - //// kernel headers to be installed. - //// - //// This value is stable at least since Linux 3.13 and should remain so for - //// compatibility reasons, warranting it's re-definition here. - //#define __ARM_NR_cacheflush 0x0f0002 - // register int start_reg __asm("r0") = (int)(intptr_t)start; - // const register int end_reg __asm("r1") = (int)(intptr_t)end; - // const register int flags __asm("r2") = 0; - // const register int syscall_nr __asm("r7") = __ARM_NR_cacheflush; - // __asm __volatile("svc 0x0" - // : "=r"(start_reg) - // : "r"(syscall_nr), "r"(start_reg), "r"(end_reg), "r"(flags)); - // assert(start_reg == 0 && "Cache flush syscall failed."); - //#else - // compilerrt_abort(); - //#endif + if (os == .freebsd or os == .netbsd) { + // struct arm_sync_icache_args arg; + // + // arg.addr = (uintptr_t)start; + // arg.len = (uintptr_t)end - (uintptr_t)start; + // + // sysarch(ARM_SYNC_ICACHE, &arg); + @compileError("TODO: implement for NetBSD/FreeBSD"); + } else if (os == .linux) { + const result = std.os.linux.syscall3( + std.os.linux.SYS_cacheflush, + start, + end, + 0, + ); + std.debug.assert(result == 0); + } else { + @compileError("compilerrt_abort"); + } } else if (os == .linux and mips) { @compileError("TODO"); //const uintptr_t start_int = (uintptr_t)start; From 83ff94b1cc761a4f8a222314b26700b13de100a9 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 30 Mar 2020 23:15:07 -0400 Subject: [PATCH 2/2] compiler-rt: don't export __clear_cache when no impl available --- lib/std/special/compiler_rt.zig | 25 ++++++++++++++++++--- lib/std/special/compiler_rt/clear_cache.zig | 9 ++------ 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/std/special/compiler_rt.zig b/lib/std/special/compiler_rt.zig index 44db926b5f..6cdc127137 100644 --- a/lib/std/special/compiler_rt.zig +++ b/lib/std/special/compiler_rt.zig @@ -10,10 +10,29 @@ comptime { const strong_linkage = if (is_test) builtin.GlobalLinkage.Internal else builtin.GlobalLinkage.Strong; switch (builtin.arch) { - .i386, .x86_64 => @export(@import("compiler_rt/stack_probe.zig").zig_probe_stack, .{ .name = "__zig_probe_stack", .linkage = linkage }), - .aarch64, .aarch64_be, .aarch64_32, .arm, .armeb, .thumb, .thumbeb => { - @export(@import("compiler_rt/clear_cache.zig").clear_cache, .{ .name = "__clear_cache", .linkage = linkage }); + .i386, + .x86_64, + => @export(@import("compiler_rt/stack_probe.zig").zig_probe_stack, .{ + .name = "__zig_probe_stack", + .linkage = linkage, + }), + + .aarch64, + .aarch64_be, + .aarch64_32, + => @export(@import("compiler_rt/clear_cache.zig").clear_cache, .{ + .name = "__clear_cache", + .linkage = linkage, + }), + + .arm, .armeb, .thumb, .thumbeb => switch (builtin.os.tag) { + .linux => @export(@import("compiler_rt/clear_cache.zig").clear_cache, .{ + .name = "__clear_cache", + .linkage = linkage, + }), + else => {}, }, + else => {}, } diff --git a/lib/std/special/compiler_rt/clear_cache.zig b/lib/std/special/compiler_rt/clear_cache.zig index 5d09a7692d..984ef7e7ac 100644 --- a/lib/std/special/compiler_rt/clear_cache.zig +++ b/lib/std/special/compiler_rt/clear_cache.zig @@ -54,15 +54,10 @@ pub fn clear_cache(start: usize, end: usize) callconv(.C) void { // sysarch(ARM_SYNC_ICACHE, &arg); @compileError("TODO: implement for NetBSD/FreeBSD"); } else if (os == .linux) { - const result = std.os.linux.syscall3( - std.os.linux.SYS_cacheflush, - start, - end, - 0, - ); + const result = std.os.linux.syscall3(std.os.linux.SYS_cacheflush, start, end, 0); std.debug.assert(result == 0); } else { - @compileError("compilerrt_abort"); + @compileError("no __clear_cache implementation available for this target"); } } else if (os == .linux and mips) { @compileError("TODO");