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