Skip to content

Commit a1803ea

Browse files
[libc] implement sigsetjmp/siglongjmp for riscv (#137992)
See https://godbolt.org/z/jo7s6j7sq for compiled code. ```c++ #if __riscv_xlen == 64 #define STORE(A, B, C) "sd " #A ", %c[" #B "](" #C ")\n\t" #define LOAD(A, B, C) "ld " #A ", %c[" #B "](" #C ")\n\t" #elif __riscv_xlen == 32 #define STORE(A, B, C) "sw " #A ", %c[" #B "](" #C ")\n\t" #define LOAD(A, B, C) "lw " #A ", %c[" #B "](" #C ")\n\t" #else #error "Unsupported RISC-V architecture" #endif namespace LIBC_NAMESPACE_DECL { [[gnu::naked]] LLVM_LIBC_FUNCTION(int, sigsetjmp, (sigjmp_buf, int)) { // clang-format off asm("beqz a1, .Lnosave\n\t" STORE(ra, retaddr, a0) STORE(s0, extra, a0) "mv s0, a0\n\t" "call %c[setjmp]\n\t" "mv a1, a0\n\t" "mv a0, s0\n\t" LOAD(s0, extra, a0) LOAD(ra, retaddr, a0) "tail %c[epilogue]\n" ".Lnosave:\n\t" "tail %c[setjmp]" // clang-format on ::[retaddr] "i"(offsetof(__jmp_buf, sig_retaddr)), [extra] "i"(offsetof(__jmp_buf, sig_extra)), [setjmp] "i"(setjmp), [epilogue] "i"(sigsetjmp_epilogue) : "a0", "a1", "s0"); } ```
1 parent c51a3aa commit a1803ea

File tree

4 files changed

+69
-1
lines changed

4 files changed

+69
-1
lines changed

libc/config/linux/riscv/entrypoints.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,8 @@ if(LLVM_LIBC_FULL_BUILD)
877877
# setjmp.h entrypoints
878878
libc.src.setjmp.longjmp
879879
libc.src.setjmp.setjmp
880+
libc.src.setjmp.siglongjmp
881+
libc.src.setjmp.sigsetjmp
880882

881883
# stdio.h entrypoints
882884
libc.src.stdio.clearerr

libc/include/llvm-libc-types/jmp_buf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// TODO: implement sigjmp_buf related functions for other architectures
1313
// Issue: https://github.com/llvm/llvm-project/issues/136358
1414
#if defined(__linux__)
15-
#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__)
15+
#if defined(__i386__) || defined(__x86_64__) || defined(__aarch64__) || \
16+
defined(__riscv)
1617
#define __LIBC_HAS_SIGJMP_BUF
1718
#endif
1819
#endif

libc/src/setjmp/riscv/CMakeLists.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,22 @@ add_entrypoint_object(
1111
-fomit-frame-pointer
1212
)
1313

14+
if (TARGET libc.src.setjmp.sigsetjmp_epilogue)
15+
add_entrypoint_object(
16+
sigsetjmp
17+
SRCS
18+
sigsetjmp.cpp
19+
HDRS
20+
../sigsetjmp.h
21+
DEPENDS
22+
libc.hdr.types.jmp_buf
23+
libc.hdr.types.sigset_t
24+
libc.hdr.offsetof_macros
25+
libc.src.setjmp.sigsetjmp_epilogue
26+
libc.src.setjmp.setjmp
27+
)
28+
endif()
29+
1430
add_entrypoint_object(
1531
longjmp
1632
SRCS

libc/src/setjmp/riscv/sigsetjmp.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
//===-- Implementation of sigsetjmp ---------------------------------------===//
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+
#include "src/setjmp/sigsetjmp.h"
10+
#include "hdr/offsetof_macros.h"
11+
#include "src/__support/common.h"
12+
#include "src/__support/macros/config.h"
13+
#include "src/setjmp/setjmp_impl.h"
14+
#include "src/setjmp/sigsetjmp_epilogue.h"
15+
16+
#if __riscv_xlen == 64
17+
#define STORE(A, B, C) "sd " #A ", %c[" #B "](" #C ")\n\t"
18+
#define LOAD(A, B, C) "ld " #A ", %c[" #B "](" #C ")\n\t"
19+
#elif __riscv_xlen == 32
20+
#define STORE(A, B, C) "sw " #A ", %c[" #B "](" #C ")\n\t"
21+
#define LOAD(A, B, C) "lw " #A ", %c[" #B "](" #C ")\n\t"
22+
#else
23+
#error "Unsupported RISC-V architecture"
24+
#endif
25+
26+
namespace LIBC_NAMESPACE_DECL {
27+
[[gnu::naked]]
28+
LLVM_LIBC_FUNCTION(int, sigsetjmp, (sigjmp_buf, int)) {
29+
// clang-format off
30+
asm("beqz a1, .Lnosave\n\t"
31+
STORE(ra, retaddr, a0)
32+
STORE(s0, extra, a0)
33+
"mv s0, a0\n\t"
34+
"call %c[setjmp]\n\t"
35+
"mv a1, a0\n\t"
36+
"mv a0, s0\n\t"
37+
LOAD(s0, extra, a0)
38+
LOAD(ra, retaddr, a0)
39+
"tail %c[epilogue]\n"
40+
".Lnosave:\n\t"
41+
"tail %c[setjmp]"
42+
// clang-format on
43+
::[retaddr] "i"(offsetof(__jmp_buf, sig_retaddr)),
44+
[extra] "i"(offsetof(__jmp_buf, sig_extra)), [setjmp] "i"(setjmp),
45+
[epilogue] "i"(sigsetjmp_epilogue)
46+
: "a0", "a1", "s0");
47+
}
48+
49+
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)