Commit 685f828218

David Rubin <daviru007@icloud.com>
2024-03-25 13:30:12
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.
1 parent b28c966
Changed files (2)
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) {
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,