xref: /netbsd-src/sys/arch/riscv/include/locore.h (revision 9fb66d812c00ebfb445c0b47dea128f32aa6fe96)
1 /* $NetBSD: locore.h,v 1.7 2020/11/04 07:09:45 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 
32 #ifndef _RISCV_LOCORE_H_
33 #define _RISCV_LOCORE_H_
34 
35 #include <sys/lwp.h>
36 #include <sys/userret.h>
37 
38 #include <riscv/reg.h>
39 #include <riscv/sysreg.h>
40 
41 struct trapframe {
42 	struct reg tf_regs;
43 	register_t tf_tval;
44 	register_t tf_cause;
45 	register_t tf_sr;
46 #define tf_reg		tf_regs.r_reg
47 #define tf_pc		tf_regs.r_pc
48 #define tf_ra		tf_reg[_X_RA]
49 #define tf_sp		tf_reg[_X_SP]
50 #define tf_gp		tf_reg[_X_GP]
51 #define tf_tp		tf_reg[_X_TP]
52 #define tf_t0		tf_reg[_X_T0]
53 #define tf_t1		tf_reg[_X_T1]
54 #define tf_t2		tf_reg[_X_T2]
55 #define tf_s0		tf_reg[_X_S0]
56 #define tf_s1		tf_reg[_X_S1]
57 #define tf_a0		tf_reg[_X_A0]
58 #define tf_a1		tf_reg[_X_A1]
59 #define tf_a2		tf_reg[_X_A2]
60 #define tf_a3		tf_reg[_X_A3]
61 #define tf_a4		tf_reg[_X_A4]
62 #define tf_a5		tf_reg[_X_A5]
63 #define tf_a6		tf_reg[_X_A6]
64 #define tf_a7		tf_reg[_X_A7]
65 #define tf_s2		tf_reg[_X_S2]
66 #define tf_s3		tf_reg[_X_S3]
67 #define tf_s4		tf_reg[_X_S4]
68 #define tf_s5		tf_reg[_X_S5]
69 #define tf_s6		tf_reg[_X_S6]
70 #define tf_s7		tf_reg[_X_S7]
71 #define tf_s8		tf_reg[_X_S8]
72 #define tf_s9		tf_reg[_X_S9]
73 #define tf_s10		tf_reg[_X_S10]
74 #define tf_s11		tf_reg[_X_S11]
75 #define tf_t3		tf_reg[_X_T3]
76 #define tf_t4		tf_reg[_X_T4]
77 #define tf_t5		tf_reg[_X_T5]
78 #define tf_t6		tf_reg[_X_T6]
79 };
80 
81 // For COMPAT_NETBSD32 coredumps
82 struct trapframe32 {
83 	struct reg32 tf_regs;
84 	register32_t tf_tval;
85 	register32_t tf_cause;
86 	register32_t tf_sr;
87 };
88 
89 #define FB_A0	0
90 #define	FB_RA	1
91 #define	FB_SP	2
92 #define	FB_GP	3
93 #define	FB_S0	4
94 #define	FB_S1	5
95 #define	FB_S2	6
96 #define	FB_S3	7
97 #define	FB_S4	8
98 #define	FB_S5	9
99 #define	FB_S6	10
100 #define	FB_S7	11
101 #define	FB_S8	12
102 #define	FB_S9	13
103 #define	FB_S10	14
104 #define	FB_S11	15
105 #define FB_MAX	16
106 
107 struct faultbuf {
108 	register_t fb_reg[FB_MAX];
109 	register_t fb_sr;
110 };
111 
112 CTASSERT(sizeof(label_t) == sizeof(struct faultbuf));
113 
114 struct mainbus_attach_args {
115 	const char *maa_name;
116 	u_int maa_instance;
117 };
118 
119 #ifdef _KERNEL
120 extern int cpu_printfataltraps;
121 extern const pcu_ops_t pcu_fpu_ops;
122 
123 static inline vaddr_t
124 stack_align(vaddr_t sp)
125 {
126 	return sp & ~STACK_ALIGNBYTES;
127 }
128 
129 static inline void
130 userret(struct lwp *l)
131 {
132 	mi_userret(l);
133 }
134 
135 static inline void
136 fpu_load(void)
137 {
138 	pcu_load(&pcu_fpu_ops);
139 }
140 
141 static inline void
142 fpu_save(lwp_t *l)
143 {
144 	pcu_save(&pcu_fpu_ops, l);
145 }
146 
147 static inline void
148 fpu_discard(lwp_t *l)
149 {
150 	pcu_discard(&pcu_fpu_ops, l, false);
151 }
152 
153 static inline void
154 fpu_replace(lwp_t *l)
155 {
156 	pcu_discard(&pcu_fpu_ops, l, true);
157 }
158 
159 static inline bool
160 fpu_valid_p(lwp_t *l)
161 {
162 	return pcu_valid_p(&pcu_fpu_ops, l);
163 }
164 
165 void	__syncicache(const void *, size_t);
166 
167 int	cpu_set_onfault(struct faultbuf *, register_t) __returns_twice;
168 void	cpu_jump_onfault(struct trapframe *, const struct faultbuf *);
169 
170 static inline void
171 cpu_unset_onfault(void)
172 {
173 	curlwp->l_md.md_onfault = NULL;
174 }
175 
176 static inline struct faultbuf *
177 cpu_disable_onfault(void)
178 {
179 	struct faultbuf * const fb = curlwp->l_md.md_onfault;
180 	curlwp->l_md.md_onfault = NULL;
181 	return fb;
182 }
183 
184 static inline void
185 cpu_enable_onfault(struct faultbuf *fb)
186 {
187 	curlwp->l_md.md_onfault = fb;
188 }
189 
190 void	cpu_intr(struct trapframe */*tf*/, register_t /*epc*/,
191 	    register_t /*status*/, register_t /*cause*/);
192 void	cpu_trap(struct trapframe */*tf*/, register_t /*epc*/,
193 	    register_t /*status*/, register_t /*cause*/,
194 	    register_t /*badvaddr*/);
195 void	cpu_ast(struct trapframe *);
196 void	cpu_fast_switchto(struct lwp *, int);
197 
198 void	cpu_lwp_trampoline(void);
199 
200 void *	cpu_sendsig_getframe(struct lwp *, int, bool *);
201 
202 void	init_riscv(vaddr_t, vaddr_t);
203 #endif
204 
205 #endif /* _RISCV_LOCORE_H_ */
206