commit 685f8282180016ac8d82ecf2fe7facc1a2d6b9f7 (tree)
parent b28c966e332623dc43b1481b34016d18ce3262fa
Author: David Rubin <daviru007@icloud.com>
Date: Mon, 25 Mar 2024 05:30:12 -0700
riscv: add a custom panic function
this provides a much better indication of when we are having a controlled panic with an error message
or when we are actually segfaulting, as before the `trap` as causing a segfault.
Diffstat:
2 files changed, 21 insertions(+), 3 deletions(-)
diff --git a/lib/std/builtin.zig b/lib/std/builtin.zig
@@ -767,13 +767,31 @@ pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace, ret_addr
builtin.zig_backend == .stage2_x86 or
(builtin.zig_backend == .stage2_x86_64 and (builtin.target.ofmt != .elf and builtin.target.ofmt != .macho)) or
builtin.zig_backend == .stage2_sparc64 or
- builtin.zig_backend == .stage2_spirv64 or
- builtin.zig_backend == .stage2_riscv64)
+ builtin.zig_backend == .stage2_spirv64)
{
while (true) {
@breakpoint();
}
}
+
+ if (builtin.zig_backend == .stage2_riscv64) {
+ asm volatile ("ecall"
+ :
+ : [number] "{a7}" (64),
+ [arg1] "{a0}" (1),
+ [arg2] "{a1}" (@intFromPtr("panicking!\n")),
+ [arg3] "{a2}" ("panicking!\n".len),
+ : "rcx", "r11", "memory"
+ );
+ asm volatile ("ecall"
+ :
+ : [number] "{a7}" (94),
+ [arg1] "{a0}" (127),
+ : "rcx", "r11", "memory"
+ );
+ unreachable;
+ }
+
switch (builtin.os.tag) {
.freestanding => {
while (true) {
diff --git a/src/target.zig b/src/target.zig
@@ -526,7 +526,7 @@ pub fn backendSupportsFeature(
feature: Feature,
) bool {
return switch (feature) {
- .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64,
+ .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64 or cpu_arch == .riscv64,
.panic_unwrap_error => ofmt == .c or use_llvm,
.safety_check_formatted => ofmt == .c or use_llvm,
.error_return_trace => use_llvm,