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 {
6262 // exportIt();
6363 } else if (arm32 and !apple) {
6464 switch (os) {
65 .freebsd, .netbsd => {
66 var arg = arm_sync_icache_args{
65 // FreeBSD and NetBSD should be changed to do direct syscalls here...
66 .freebsd, .netbsd, .openbsd => {
67 var arg: arm_sync_icache_args = .{
6768 .addr = start,
68 .len = end - start,
69 .size = end - start,
6970 };
70 const result = sysarch(ARM_SYNC_ICACHE, @intFromPtr(&arg));
71 const result = sysarch(ARM_SYNC_ICACHE, &arg);
7172 std.debug.assert(result == 0);
7273 exportIt();
7374 },
......@@ -79,32 +80,32 @@ fn clear_cache(start: usize, end: usize) callconv(.c) void {
7980 else => {},
8081 }
8182 } else if (os == .linux and mips) {
82 const flags = 3; // ICACHE | DCACHE
83 const result = std.os.linux.syscall3(.cacheflush, start, end - start, flags);
83 const result = std.os.linux.syscall3(.cacheflush, start, end - start, ICACHE | DCACHE);
8484 std.debug.assert(result == 0);
8585 exportIt();
8686 } else if (os == .netbsd and mips) {
8787 // Replace with https://github.com/ziglang/zig/issues/23904 in the future.
88 const cfa: extern struct {
89 va: usize,
90 nbytes: usize,
91 whichcache: u32,
92 } = .{
93 .va = start,
94 .nbytes = end - start,
95 .whichcache = 3, // ICACHE | DCACHE
88 const cfa: mips_cacheflush_args = .{
89 .addr = start,
90 .size = end - start,
91 .which = ICACHE | DCACHE,
9692 };
9793 asm volatile ("syscall"
9894 :
9995 : [_] "{$2}" (165), // nr = SYS_sysarch
100 [_] "{$4}" (0), // op = MIPS_CACHEFLUSH
96 [_] "{$4}" (MIPS_CACHEFLUSH), // op
10197 [_] "{$5}" (&cfa), // args = &cfa
10298 : .{ .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 });
10399 exportIt();
104100 } else if (mips and os == .openbsd) {
105 // TODO
106 //cacheflush(start, (uintptr_t)end - (uintptr_t)start, BCACHE);
107 // exportIt();
101 const cfa: mips_cacheflush_args = .{
102 .addr = start,
103 .size = end - start,
104 .which = ICACHE | DCACHE,
105 };
106 const result = sysarch(MIPS_CACHEFLUSH, &cfa);
107 std.debug.assert(result == 0);
108 exportIt();
108109 } else if (os == .linux and riscv) {
109110 const result = std.os.linux.syscall3(.riscv_flush_icache, start, end - start, 0);
110111 std.debug.assert(result == 0);
......@@ -191,12 +192,25 @@ fn exportIt() void {
191192 @export(&clear_cache, .{ .name = "__clear_cache", .linkage = common.linkage, .visibility = common.visibility });
192193}
193194
195// MIPS-only
196const ICACHE = 0x1;
197const DCACHE = 0x2;
198
194199// Darwin-only
195200extern fn sys_icache_invalidate(start: usize, len: usize) void;
201
196202// BSD-only
203const ARM_SYNC_ICACHE = 0;
197204const arm_sync_icache_args = extern struct {
198 addr: usize, // Virtual start address
199 len: usize, // Region size
205 addr: usize,
206 size: usize,
200207};
201const ARM_SYNC_ICACHE = 0;
202extern "c" fn sysarch(number: i32, args: usize) i32;
208
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;