authorgravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-03-25 05:30:12-07:00
committergravatar for david@vortan.devDavid Rubin <david@vortan.dev> 2024-05-11 02:17:11-07:00
log685f8282180016ac8d82ecf2fe7facc1a2d6b9f7
tree620d40ac62344a8e0bf97274b60d28ed7daa0da7
parentb28c966e332623dc43b1481b34016d18ce3262fa

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.

2 files changed, 21 insertions(+), 3 deletions(-)

lib/std/builtin.zig+20-2
......@@ -767,13 +767,31 @@ pub fn default_panic(msg: []const u8, error_return_trace: ?*StackTrace, ret_addr
767767 builtin.zig_backend == .stage2_x86 or
768768 (builtin.zig_backend == .stage2_x86_64 and (builtin.target.ofmt != .elf and builtin.target.ofmt != .macho)) or
769769 builtin.zig_backend == .stage2_sparc64 or
770 builtin.zig_backend == .stage2_spirv64 or
771 builtin.zig_backend == .stage2_riscv64)
770 builtin.zig_backend == .stage2_spirv64)
772771 {
773772 while (true) {
774773 @breakpoint();
775774 }
776775 }
776
777 if (builtin.zig_backend == .stage2_riscv64) {
778 asm volatile ("ecall"
779 :
780 : [number] "{a7}" (64),
781 [arg1] "{a0}" (1),
782 [arg2] "{a1}" (@intFromPtr("panicking!\n")),
783 [arg3] "{a2}" ("panicking!\n".len),
784 : "rcx", "r11", "memory"
785 );
786 asm volatile ("ecall"
787 :
788 : [number] "{a7}" (94),
789 [arg1] "{a0}" (127),
790 : "rcx", "r11", "memory"
791 );
792 unreachable;
793 }
794
777795 switch (builtin.os.tag) {
778796 .freestanding => {
779797 while (true) {
src/target.zig+1-1
......@@ -526,7 +526,7 @@ pub fn backendSupportsFeature(
526526 feature: Feature,
527527) bool {
528528 return switch (feature) {
529 .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64,
529 .panic_fn => ofmt == .c or use_llvm or cpu_arch == .x86_64 or cpu_arch == .riscv64,
530530 .panic_unwrap_error => ofmt == .c or use_llvm,
531531 .safety_check_formatted => ofmt == .c or use_llvm,
532532 .error_return_trace => use_llvm,