authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-02 00:40:41+01:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2025-12-16 06:25:15+01:00
logda9733d44181f4582112cc74ee0ce6730ce8e66d
treef75649a71a960f4cef06e0dae1f0a8f607dfbacc
parent914005105e5ec4d60f99eab8819b7b2f367699f3
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

compiler-rt: implement __clear_cache() for arm/mips64-openbsd


1 files changed, 36 insertions(+), 22 deletions(-)

lib/compiler_rt/clear_cache.zig+36-22
...@@ -62,12 +62,13 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {...@@ -62,12 +62,13 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
62 // exportIt();62 // exportIt();
63 } else if (arm32 and !apple) {63 } else if (arm32 and !apple) {
64 switch (os) {64 switch (os) {
65 .freebsd, .netbsd => {65 // FreeBSD and NetBSD should be changed to do direct syscalls here...
66 var arg = arm_sync_icache_args{66 .freebsd, .netbsd, .openbsd => {
67 var arg: arm_sync_icache_args = .{
67 .addr = start,68 .addr = start,
68 .len = end - start,69 .size = end - start,
69 };70 };
70 const result = sysarch(ARM_SYNC_ICACHE, @intFromPtr(&arg));71 const result = sysarch(ARM_SYNC_ICACHE, &arg);
71 std.debug.assert(result == 0);72 std.debug.assert(result == 0);
72 exportIt();73 exportIt();
73 },74 },
...@@ -79,32 +80,32 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {...@@ -79,32 +80,32 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
79 else => {},80 else => {},
80 }81 }
81 } else if (os == .linux and mips) {82 } else if (os == .linux and mips) {
82 const flags = 3; // ICACHE | DCACHE83 const result = std.os.linux.syscall3(.cacheflush, start, end - start, ICACHE | DCACHE);
83 const result = std.os.linux.syscall3(.cacheflush, start, end - start, flags);
84 std.debug.assert(result == 0);84 std.debug.assert(result == 0);
85 exportIt();85 exportIt();
86 } else if (os == .netbsd and mips) {86 } else if (os == .netbsd and mips) {
87 // Replace with https://github.com/ziglang/zig/issues/23904 in the future.87 // Replace with https://github.com/ziglang/zig/issues/23904 in the future.
88 const cfa: extern struct {88 const cfa: mips_cacheflush_args = .{
89 va: usize,89 .addr = start,
90 nbytes: usize,90 .size = end - start,
91 whichcache: u32,91 .which = ICACHE | DCACHE,
92 } = .{
93 .va = start,
94 .nbytes = end - start,
95 .whichcache = 3, // ICACHE | DCACHE
96 };92 };
97 asm volatile ("syscall"93 asm volatile ("syscall"
98 :94 :
99 : [_] "{$2}" (165), // nr = SYS_sysarch95 : [_] "{$2}" (165), // nr = SYS_sysarch
100 [_] "{$4}" (0), // op = MIPS_CACHEFLUSH96 [_] "{$4}" (MIPS_CACHEFLUSH), // op
101 [_] "{$5}" (&cfa), // args = &cfa97 [_] "{$5}" (&cfa), // args = &cfa
102 : .{ .r1 = true, .r2 = true, .r3 = true, .r4 = true, .r5 = true, .r6 = true, .r7 = true, .r8 = true, .r9 = true, .r10 = true, .r11 = true, .r12 = true, .r13 = true, .r14 = true, .r15 = true, .r24 = true, .r25 = true, .hi = true, .lo = true, .memory = true });98 : .{ .r1 = true, .r2 = true, .r3 = true, .r4 = true, .r5 = true, .r6 = true, .r7 = true, .r8 = true, .r9 = true, .r10 = true, .r11 = true, .r12 = true, .r13 = true, .r14 = true, .r15 = true, .r24 = true, .r25 = true, .hi = true, .lo = true, .memory = true });
103 exportIt();99 exportIt();
104 } else if (mips and os == .openbsd) {100 } else if (mips and os == .openbsd) {
105 // TODO101 const cfa: mips_cacheflush_args = .{
106 //cacheflush(start, (uintptr_t)end - (uintptr_t)start, BCACHE);102 .addr = start,
107 // exportIt();103 .size = end - start,
104 .which = ICACHE | DCACHE,
105 };
106 const result = sysarch(MIPS_CACHEFLUSH, &cfa);
107 std.debug.assert(result == 0);
108 exportIt();
108 } else if (os == .linux and riscv) {109 } else if (os == .linux and riscv) {
109 const result = std.os.linux.syscall3(.riscv_flush_icache, start, end - start, 0);110 const result = std.os.linux.syscall3(.riscv_flush_icache, start, end - start, 0);
110 std.debug.assert(result == 0);111 std.debug.assert(result == 0);
...@@ -191,12 +192,25 @@ fn exportIt() void {...@@ -191,12 +192,25 @@ fn exportIt() void {
191 @export(&clear_cache, .{ .name = "__clear_cache", .linkage = common.linkage, .visibility = common.visibility });192 @export(&clear_cache, .{ .name = "__clear_cache", .linkage = common.linkage, .visibility = common.visibility });
192}193}
193194
195// MIPS-only
196const ICACHE = 0x1;
197const DCACHE = 0x2;
198
194// Darwin-only199// Darwin-only
195extern fn sys_icache_invalidate(start: usize, len: usize) void;200extern fn sys_icache_invalidate(start: usize, len: usize) void;
201
196// BSD-only202// BSD-only
203const ARM_SYNC_ICACHE = 0;
197const arm_sync_icache_args = extern struct {204const arm_sync_icache_args = extern struct {
198 addr: usize, // Virtual start address205 addr: usize,
199 len: usize, // Region size206 size: usize,
200};207};
201const ARM_SYNC_ICACHE = 0;208
202extern "c" fn sysarch(number: i32, args: usize) i32;209const MIPS_CACHEFLUSH = 0;
210const mips_cacheflush_args = extern struct {
211 addr: usize,
212 size: usize,
213 which: c_uint,
214};
215
216extern fn sysarch(op: c_uint, args: *const anyopaque) c_int;