1 /* $NetBSD: mcontext.h,v 1.6 2020/03/14 16:12:16 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2014 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Matt Thomas of 3am Software Foundry. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 #ifndef _RISCV_MCONTEXT_H_ 32 #define _RISCV_MCONTEXT_H_ 33 34 /* 35 */ 36 37 #define _NGREG 32 /* GR1-31 */ 38 #define _NFREG 33 /* F0-31, FCSR */ 39 40 /* 41 * This fragment is common to <riscv/mcontext.h> and <riscv/reg.h> 42 */ 43 #ifndef _BSD_FPREG_T_ 44 union __fpreg { 45 __uint64_t u_u64; 46 double u_d; 47 }; 48 #define _BSD_FPREG_T_ union __fpreg 49 #endif 50 51 typedef __uint64_t __greg_t; 52 typedef __greg_t __gregset_t[_NGREG]; 53 typedef __uint32_t __greg32_t; 54 typedef __greg32_t __gregset32_t[_NGREG]; 55 typedef _BSD_FPREG_T_ __fregset_t[_NFREG]; 56 57 #define _REG_X1 0 58 #define _REG_X2 1 59 #define _REG_X3 2 60 #define _REG_X4 3 61 #define _REG_X5 4 62 #define _REG_X6 5 63 #define _REG_X7 6 64 #define _REG_X8 7 65 #define _REG_X9 8 66 #define _REG_X10 9 67 #define _REG_X11 10 68 #define _REG_X12 11 69 #define _REG_X13 12 70 #define _REG_X14 13 71 #define _REG_X15 14 72 #define _REG_X16 15 73 #define _REG_X17 16 74 #define _REG_X18 17 75 #define _REG_X19 18 76 #define _REG_X20 19 77 #define _REG_X21 20 78 #define _REG_X22 21 79 #define _REG_X23 22 80 #define _REG_X24 23 81 #define _REG_X25 24 82 #define _REG_X26 25 83 #define _REG_X27 26 84 #define _REG_X28 27 85 #define _REG_X29 28 86 #define _REG_X30 29 87 #define _REG_X31 30 88 #define _REG_PC 31 89 90 #define _REG_RA _REG_X1 91 #define _REG_SP _REG_X2 92 #define _REG_GP _REG_X3 93 #define _REG_TP _REG_X4 94 #define _REG_S0 _REG_X8 95 #define _REG_RV _REG_X10 96 #define _REG_A0 _REG_X10 97 98 #define _REG_F0 0 99 #define _REG_FPCSR 32 100 101 typedef struct { 102 __gregset_t __gregs; /* General Purpose Register set */ 103 __fregset_t __fregs; /* Floating Point Register set */ 104 __greg_t __private; /* copy of l_private */ 105 __greg_t __spare[8]; /* future proof */ 106 } mcontext_t; 107 108 typedef struct { 109 __gregset32_t __gregs; /* General Purpose Register set */ 110 __fregset_t __fregs; /* Floating Point Register set */ 111 __greg32_t __private; /* copy of l_private */ 112 __greg32_t __spare[8]; /* future proof */ 113 } mcontext32_t; 114 115 /* Machine-dependent uc_flags */ 116 #define _UC_SETSTACK 0x00010000 /* see <sys/ucontext.h> */ 117 #define _UC_CLRSTACK 0x00020000 /* see <sys/ucontext.h> */ 118 #define _UC_TLSBASE 0x00080000 /* see <sys/ucontext.h> */ 119 120 #define _UC_MACHINE_SP(uc) ((uc)->uc_mcontext.__gregs[_REG_SP]) 121 #define _UC_MACHINE_FP(uc) ((uc)->uc_mcontext.__gregs[_REG_S0]) 122 #define _UC_MACHINE_PC(uc) ((uc)->uc_mcontext.__gregs[_REG_PC]) 123 #define _UC_MACHINE_INTRV(uc) ((uc)->uc_mcontext.__gregs[_REG_RV]) 124 125 #define _UC_MACHINE_SET_PC(uc, pc) _UC_MACHINE_PC(uc) = (pc) 126 127 #if defined(_RTLD_SOURCE) || defined(_LIBC_SOURCE) || defined(__LIBPTHREAD_SOURCE__) 128 #include <sys/tls.h> 129 130 /* 131 * On RISCV, since displacements are signed 12-bit values, the TCB pointer is 132 * not and points to the first static entry. 133 */ 134 #define TLS_TP_OFFSET 0x0 135 #define TLS_DTV_OFFSET 0x800 136 __CTASSERT(TLS_TP_OFFSET + sizeof(struct tls_tcb) < 0x800); 137 138 static __inline void * 139 __lwp_getprivate_fast(void) 140 { 141 void *__tp; 142 __asm("move %0,tp" : "=r"(__tp)); 143 return __tp; 144 } 145 146 static __inline void * 147 __lwp_gettcb_fast(void) 148 { 149 void *__tcb; 150 151 __asm __volatile( 152 "addi %[__tcb],tp,%[__offset]" 153 : [__tcb] "=r" (__tcb) 154 : [__offset] "n" (-(TLS_TP_OFFSET + sizeof(struct tls_tcb)))); 155 156 return __tcb; 157 } 158 159 static __inline void 160 __lwp_settcb(void *__tcb) 161 { 162 __asm __volatile( 163 "addi tp,%[__tcb],%[__offset]" 164 : 165 : [__tcb] "r" (__tcb), 166 [__offset] "n" (TLS_TP_OFFSET + sizeof(struct tls_tcb))); 167 } 168 #endif /* _RTLD_SOURCE || _LIBC_SOURCE || __LIBPTHREAD_SOURCE__ */ 169 170 #endif /* !_RISCV_MCONTEXT_H_ */ 171