zig/lib/compiler_rt/clear_cache.zig
Alex Rønne Petersen c8ca05e93a
std.Target: Remove sparcel architecture tag.
What is `sparcel`, you might ask? Good question!

If you take a peek in the SPARC v8 manual, §2.2, it is quite explicit that SPARC
v8 is a big-endian architecture. No little-endian or mixed-endian support to be
found here.

On the other hand, the SPARC v9 manual, in §3.2.1.2, states that it has support
for mixed-endian operation, with big-endian mode being the default.

Ok, so `sparcel` must just be referring to SPARC v9 running in little-endian
mode, surely?

Nope:

* 40b4fd7a3e/llvm/lib/Target/Sparc/SparcTargetMachine.cpp (L226)
* 40b4fd7a3e/llvm/lib/Target/Sparc/SparcTargetMachine.cpp (L104)

So, `sparcel` in LLVM is referring to some sort of fantastical little-endian
SPARC v8 architecture. I've scoured the internet and I can find absolutely no
evidence that such a thing exists or has ever existed. In fact, I can find no
evidence that a little-endian implementation of SPARC v9 ever existed, either.
Or any SPARC version, actually!

The support was added here: https://reviews.llvm.org/D8741

Notably, there is no mention whatsoever of what CPU this might be referring to,
and no justification given for the "but some are little" comment added in the
patch.

My best guess is that this might have been some private exercise in creating a
little-endian version of SPARC that never saw the light of day. Given that SPARC
v8 explicitly doesn't support little-endian operation (let alone little-endian
instruction encoding!), and no CPU is known to be implemented as such, I think
it's very reasonable for us to just remove this support.
2024-07-30 06:30:25 +02:00

179 lines
6.3 KiB
Zig

const std = @import("std");
const builtin = @import("builtin");
const arch = builtin.cpu.arch;
const os = builtin.os.tag;
pub const panic = @import("common.zig").panic;
// Ported from llvm-project d32170dbd5b0d54436537b6b75beaf44324e0c28
// The compiler generates calls to __clear_cache() when creating
// trampoline functions on the stack for use with nested functions.
// It is expected to invalidate the instruction cache for the
// specified range.
comptime {
_ = &clear_cache;
}
fn clear_cache(start: usize, end: usize) callconv(.C) void {
const x86 = switch (arch) {
.x86, .x86_64 => true,
else => false,
};
const arm32 = switch (arch) {
.arm, .armeb, .thumb, .thumbeb => true,
else => false,
};
const arm64 = switch (arch) {
.aarch64, .aarch64_be => true,
else => false,
};
const mips = switch (arch) {
.mips, .mipsel, .mips64, .mips64el => true,
else => false,
};
const riscv = switch (arch) {
.riscv32, .riscv64 => true,
else => false,
};
const powerpc64 = switch (arch) {
.powerpc64, .powerpc64le => true,
else => false,
};
const sparc = switch (arch) {
.sparc, .sparc64 => true,
else => false,
};
const apple = switch (os) {
.ios, .macos, .watchos, .tvos, .visionos => true,
else => false,
};
if (x86) {
// Intel processors have a unified instruction and data cache
// so there is nothing to do
exportIt();
} else if (os == .windows and (arm32 or arm64)) {
// TODO
// FlushInstructionCache(GetCurrentProcess(), start, end - start);
// exportIt();
} else if (arm32 and !apple) {
switch (os) {
.freebsd, .netbsd => {
var arg = arm_sync_icache_args{
.addr = start,
.len = end - start,
};
const result = sysarch(ARM_SYNC_ICACHE, @intFromPtr(&arg));
std.debug.assert(result == 0);
exportIt();
},
.linux => {
const result = std.os.linux.syscall3(.cacheflush, start, end, 0);
std.debug.assert(result == 0);
exportIt();
},
else => {},
}
} else if (os == .linux and mips) {
const flags = 3; // ICACHE | DCACHE
const result = std.os.linux.syscall3(.cacheflush, start, end - start, flags);
std.debug.assert(result == 0);
exportIt();
} else if (mips and os == .openbsd) {
// TODO
//cacheflush(start, (uintptr_t)end - (uintptr_t)start, BCACHE);
// exportIt();
} else if (os == .linux and riscv) {
const result = std.os.linux.syscall3(.riscv_flush_icache, start, end - start, 0);
std.debug.assert(result == 0);
exportIt();
} else if (arm64 and !apple) {
// Get Cache Type Info.
// TODO memoize this?
var ctr_el0: u64 = 0;
asm volatile (
\\mrs %[x], ctr_el0
\\
: [x] "=r" (ctr_el0),
);
// The DC and IC instructions must use 64-bit registers so we don't use
// uintptr_t in case this runs in an IPL32 environment.
var addr: u64 = undefined;
// If CTR_EL0.IDC is set, data cache cleaning to the point of unification
// is not required for instruction to data coherence.
if (((ctr_el0 >> 28) & 0x1) == 0x0) {
const dcache_line_size = @as(usize, 4) << @intCast((ctr_el0 >> 16) & 15);
addr = start & ~(dcache_line_size - 1);
while (addr < end) : (addr += dcache_line_size) {
asm volatile ("dc cvau, %[addr]"
:
: [addr] "r" (addr),
);
}
}
asm volatile ("dsb ish");
// If CTR_EL0.DIC is set, instruction cache invalidation to the point of
// unification is not required for instruction to data coherence.
if (((ctr_el0 >> 29) & 0x1) == 0x0) {
const icache_line_size = @as(usize, 4) << @intCast((ctr_el0 >> 0) & 15);
addr = start & ~(icache_line_size - 1);
while (addr < end) : (addr += icache_line_size) {
asm volatile ("ic ivau, %[addr]"
:
: [addr] "r" (addr),
);
}
}
asm volatile ("isb sy");
exportIt();
} else if (powerpc64) {
// TODO
//const size_t line_size = 32;
//const size_t len = (uintptr_t)end - (uintptr_t)start;
//
//const uintptr_t mask = ~(line_size - 1);
//const uintptr_t start_line = ((uintptr_t)start) & mask;
//const uintptr_t end_line = ((uintptr_t)start + len + line_size - 1) & mask;
//
//for (uintptr_t line = start_line; line < end_line; line += line_size)
// __asm__ volatile("dcbf 0, %0" : : "r"(line));
//__asm__ volatile("sync");
//
//for (uintptr_t line = start_line; line < end_line; line += line_size)
// __asm__ volatile("icbi 0, %0" : : "r"(line));
//__asm__ volatile("isync");
// exportIt();
} else if (sparc) {
// TODO
//const size_t dword_size = 8;
//const size_t len = (uintptr_t)end - (uintptr_t)start;
//
//const uintptr_t mask = ~(dword_size - 1);
//const uintptr_t start_dword = ((uintptr_t)start) & mask;
//const uintptr_t end_dword = ((uintptr_t)start + len + dword_size - 1) & mask;
//
//for (uintptr_t dword = start_dword; dword < end_dword; dword += dword_size)
// __asm__ volatile("flush %0" : : "r"(dword));
// exportIt();
} else if (apple) {
// On Darwin, sys_icache_invalidate() provides this functionality
sys_icache_invalidate(start, end - start);
exportIt();
}
}
const linkage = if (builtin.is_test) std.builtin.GlobalLinkage.internal else std.builtin.GlobalLinkage.weak;
fn exportIt() void {
@export(clear_cache, .{ .name = "__clear_cache", .linkage = linkage });
}
// Darwin-only
extern fn sys_icache_invalidate(start: usize, len: usize) void;
// BSD-only
const arm_sync_icache_args = extern struct {
addr: usize, // Virtual start address
len: usize, // Region size
};
const ARM_SYNC_ICACHE = 0;
extern "c" fn sysarch(number: i32, args: usize) i32;