1 /* libunwind - a platform-independent unwind library 2 Copyright (C) 2002-2004 Hewlett-Packard Co 3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com> 4 5 Modified for riscv by Zhaofeng Li <hello@zhaofeng.li> 6 7 This file is part of libunwind. 8 9 Permission is hereby granted, free of charge, to any person obtaining 10 a copy of this software and associated documentation files (the 11 "Software"), to deal in the Software without restriction, including 12 without limitation the rights to use, copy, modify, merge, publish, 13 distribute, sublicense, and/or sell copies of the Software, and to 14 permit persons to whom the Software is furnished to do so, subject to 15 the following conditions: 16 17 The above copyright notice and this permission notice shall be 18 included in all copies or substantial portions of the Software. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 21 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 22 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 23 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 24 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 25 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 26 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ 27 28 #ifndef LIBUNWIND_H 29 #define LIBUNWIND_H 30 31 #if defined(__cplusplus) || defined(c_plusplus) 32 extern "C" { 33 #endif 34 35 #include <sys/types.h> 36 #include <inttypes.h> 37 #include <ucontext.h> 38 39 #define UNW_TARGET riscv 40 #define UNW_TARGET_RISCV 1 41 42 #define _U_TDEP_QP_TRUE 0 /* ignored - see libunwind-dynamic.h */ 43 44 /* This needs to be big enough to accommodate "struct cursor", while 45 leaving some slack for future expansion. Changing this value will 46 require recompiling all users of this library. Stack allocation is 47 relatively cheap and unwind-state copying is relatively rare, so we 48 want to err on making it rather too big than too small. */ 49 /* FIXME for riscv: Figure out a more reasonable size */ 50 #define UNW_TDEP_CURSOR_LEN 4096 51 52 #if __riscv_xlen == 32 53 typedef uint32_t unw_word_t; 54 typedef int32_t unw_sword_t; 55 #elif __riscv_xlen == 64 56 typedef uint64_t unw_word_t; 57 typedef int64_t unw_sword_t; 58 #endif 59 60 #if __riscv_flen == 64 61 typedef double unw_tdep_fpreg_t; 62 #elif __riscv_flen == 32 63 typedef float unw_tdep_fpreg_t; 64 #else 65 # error "Unsupported RISC-V floating-point size" 66 #endif 67 68 /* Also see src/riscv/Gglobal.c. This ordering is consistent with 69 https://github.com/riscv/riscv-elf-psabi-doc/blob/74ecf07bcebd0cb4bf3c39f3f9d96946cd6aba61/riscv-elf.md#dwarf-register-numbers- */ 70 71 typedef enum 72 { 73 /* integer registers */ 74 UNW_RISCV_X0, 75 UNW_RISCV_X1, 76 UNW_RISCV_X2, 77 UNW_RISCV_X3, 78 UNW_RISCV_X4, 79 UNW_RISCV_X5, 80 UNW_RISCV_X6, 81 UNW_RISCV_X7, 82 UNW_RISCV_X8, 83 UNW_RISCV_X9, 84 UNW_RISCV_X10, 85 UNW_RISCV_X11, 86 UNW_RISCV_X12, 87 UNW_RISCV_X13, 88 UNW_RISCV_X14, 89 UNW_RISCV_X15, 90 UNW_RISCV_X16, 91 UNW_RISCV_X17, 92 UNW_RISCV_X18, 93 UNW_RISCV_X19, 94 UNW_RISCV_X20, 95 UNW_RISCV_X21, 96 UNW_RISCV_X22, 97 UNW_RISCV_X23, 98 UNW_RISCV_X24, 99 UNW_RISCV_X25, 100 UNW_RISCV_X26, 101 UNW_RISCV_X27, 102 UNW_RISCV_X28, 103 UNW_RISCV_X29, 104 UNW_RISCV_X30, 105 UNW_RISCV_X31, 106 107 /* floating point registers */ 108 UNW_RISCV_F0, 109 UNW_RISCV_F1, 110 UNW_RISCV_F2, 111 UNW_RISCV_F3, 112 UNW_RISCV_F4, 113 UNW_RISCV_F5, 114 UNW_RISCV_F6, 115 UNW_RISCV_F7, 116 UNW_RISCV_F8, 117 UNW_RISCV_F9, 118 UNW_RISCV_F10, 119 UNW_RISCV_F11, 120 UNW_RISCV_F12, 121 UNW_RISCV_F13, 122 UNW_RISCV_F14, 123 UNW_RISCV_F15, 124 UNW_RISCV_F16, 125 UNW_RISCV_F17, 126 UNW_RISCV_F18, 127 UNW_RISCV_F19, 128 UNW_RISCV_F20, 129 UNW_RISCV_F21, 130 UNW_RISCV_F22, 131 UNW_RISCV_F23, 132 UNW_RISCV_F24, 133 UNW_RISCV_F25, 134 UNW_RISCV_F26, 135 UNW_RISCV_F27, 136 UNW_RISCV_F28, 137 UNW_RISCV_F29, 138 UNW_RISCV_F30, 139 UNW_RISCV_F31, 140 141 UNW_RISCV_PC, 142 143 UNW_TDEP_LAST_REG = UNW_RISCV_PC, 144 145 /* The CFA is the value of SP in previous frame */ 146 UNW_RISCV_CFA = UNW_RISCV_X2, 147 148 UNW_TDEP_IP = UNW_RISCV_PC, 149 UNW_TDEP_SP = UNW_RISCV_X2, 150 UNW_TDEP_EH = UNW_RISCV_X10, 151 } 152 riscv_regnum_t; 153 154 /* https://github.com/gcc-mirror/gcc/blob/16e2427f50c208dfe07d07f18009969502c25dc8/gcc/config/riscv/riscv.h#L104-L106 */ 155 #define UNW_TDEP_NUM_EH_REGS 4 156 157 typedef struct unw_tdep_save_loc 158 { 159 /* Additional target-dependent info on a save location. */ 160 char unused; 161 } 162 unw_tdep_save_loc_t; 163 164 /* On riscv, we can directly use ucontext_t as the unwind context. */ 165 typedef ucontext_t unw_tdep_context_t; 166 167 typedef struct 168 { 169 /* no riscv-specific auxiliary proc-info */ 170 char unused; 171 } 172 unw_tdep_proc_info_t; 173 174 #include "libunwind-dynamic.h" 175 #include "libunwind-common.h" 176 177 #define unw_tdep_getcontext UNW_ARCH_OBJ(getcontext) 178 #define unw_tdep_is_fpreg UNW_ARCH_OBJ(is_fpreg) 179 180 extern int unw_tdep_getcontext (unw_tdep_context_t *); 181 extern int unw_tdep_is_fpreg (int); 182 183 #if defined(__cplusplus) || defined(c_plusplus) 184 } 185 #endif 186 187 #endif /* LIBUNWIND_H */ 188