| 1 | //===-- hwasan_setjmp_riscv64.S -------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file is a part of HWAddressSanitizer. |
| 10 | // setjmp interceptor for risc-v. |
| 11 | // HWAddressSanitizer runtime. |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "sanitizer_common/sanitizer_asm.h" |
| 15 | #include "builtins/assembly.h" |
| 16 | |
| 17 | #if HWASAN_WITH_INTERCEPTORS && defined(__riscv) && (__riscv_xlen == 64) |
| 18 | #include "sanitizer_common/sanitizer_platform.h" |
| 19 | |
| 20 | // We want to save the context of the calling function. |
| 21 | // That requires |
| 22 | // 1) No modification of the link register by this function. |
| 23 | // 2) No modification of the stack pointer by this function. |
| 24 | // 3) (no modification of any other saved register, but that's not really going |
| 25 | // to occur, and hence isn't as much of a worry). |
| 26 | // |
| 27 | // There's essentially no way to ensure that the compiler will not modify the |
| 28 | // stack pointer when compiling a C function. |
| 29 | // Hence we have to write this function in assembly. |
| 30 | |
| 31 | .section .text |
| 32 | .file "hwasan_setjmp_riscv64.S" |
| 33 | |
| 34 | .global ASM_WRAPPER_NAME(setjmp) |
| 35 | ASM_TYPE_FUNCTION(ASM_WRAPPER_NAME(setjmp)) |
| 36 | ASM_WRAPPER_NAME(setjmp): |
| 37 | CFI_STARTPROC |
| 38 | addi x11, x0, 0 |
| 39 | tail ASM_WRAPPER_NAME(sigsetjmp) |
| 40 | CFI_ENDPROC |
| 41 | ASM_SIZE(ASM_WRAPPER_NAME(setjmp)) |
| 42 | |
| 43 | .global ASM_WRAPPER_NAME(sigsetjmp) |
| 44 | ASM_TYPE_FUNCTION(ASM_WRAPPER_NAME(sigsetjmp)) |
| 45 | ASM_WRAPPER_NAME(sigsetjmp): |
| 46 | CFI_STARTPROC |
| 47 | sd ra, 0<<3(x10) |
| 48 | sd s0, 1<<3(x10) |
| 49 | sd s1, 2<<3(x10) |
| 50 | sd s2, 3<<3(x10) |
| 51 | sd s3, 4<<3(x10) |
| 52 | sd s4, 5<<3(x10) |
| 53 | sd s5, 6<<3(x10) |
| 54 | sd s6, 7<<3(x10) |
| 55 | sd s7, 8<<3(x10) |
| 56 | sd s8, 9<<3(x10) |
| 57 | sd s9, 10<<3(x10) |
| 58 | sd s10, 11<<3(x10) |
| 59 | sd s11, 12<<3(x10) |
| 60 | sd sp, 13<<3(x10) |
| 61 | #if __riscv_float_abi_double |
| 62 | fsd fs0, 14<<3(x10) |
| 63 | fsd fs1, 15<<3(x10) |
| 64 | fsd fs2, 16<<3(x10) |
| 65 | fsd fs3, 17<<3(x10) |
| 66 | fsd fs4, 18<<3(x10) |
| 67 | fsd fs5, 19<<3(x10) |
| 68 | fsd fs6, 20<<3(x10) |
| 69 | fsd fs7, 21<<3(x10) |
| 70 | fsd fs8, 22<<3(x10) |
| 71 | fsd fs9, 23<<3(x10) |
| 72 | fsd fs10, 24<<3(x10) |
| 73 | fsd fs11, 25<<3(x10) |
| 74 | #elif __riscv_float_abi_soft |
| 75 | #else |
| 76 | # error "Unsupported case" |
| 77 | #endif |
| 78 | // We always have the second argument to __sigjmp_save (savemask) set, since |
| 79 | // the _setjmp function above has set it for us as `false`. |
| 80 | // This function is defined in hwasan_interceptors.cc |
| 81 | tail __sigjmp_save |
| 82 | CFI_ENDPROC |
| 83 | ASM_SIZE(ASM_WRAPPER_NAME(sigsetjmp)) |
| 84 | |
| 85 | ASM_INTERCEPTOR_TRAMPOLINE(sigsetjmp) |
| 86 | ASM_TRAMPOLINE_ALIAS(__sigsetjmp, sigsetjmp) |
| 87 | ASM_INTERCEPTOR_TRAMPOLINE(setjmp) |
| 88 | ASM_TRAMPOLINE_ALIAS(_setjmp, setjmp) |
| 89 | #endif |
| 90 | |
| 91 | // We do not need executable stack. |
| 92 | NO_EXEC_STACK_DIRECTIVE |
| 93 | |