1 /* $NetBSD: locore.h,v 1.12 2023/05/07 12:41:48 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 #define FB_A0 0
42 #define FB_RA 1
43 #define FB_SP 2
44 #define FB_GP 3
45 #define FB_S0 4
46 #define FB_S1 5
47 #define FB_S2 6
48 #define FB_S3 7
49 #define FB_S4 8
50 #define FB_S5 9
51 #define FB_S6 10
52 #define FB_S7 11
53 #define FB_S8 12
54 #define FB_S9 13
55 #define FB_S10 14
56 #define FB_S11 15
57 #define FB_MAX 16
58
59 struct faultbuf {
60 register_t fb_reg[FB_MAX];
61 register_t fb_sr;
62 };
63
64 CTASSERT(sizeof(label_t) == sizeof(struct faultbuf));
65
66 #ifdef _KERNEL
67 extern int cpu_printfataltraps;
68
69 #ifdef FPE
70 extern const pcu_ops_t pcu_fpu_ops;
71 #endif
72
73 static inline vaddr_t
stack_align(vaddr_t sp)74 stack_align(vaddr_t sp)
75 {
76 return sp & ~STACK_ALIGNBYTES;
77 }
78
79 static inline void
userret(struct lwp * l)80 userret(struct lwp *l)
81 {
82 mi_userret(l);
83 }
84
85 static inline void
fpu_load(void)86 fpu_load(void)
87 {
88 #ifdef FPE
89 pcu_load(&pcu_fpu_ops);
90 #endif
91 }
92
93 static inline void
fpu_save(lwp_t * l)94 fpu_save(lwp_t *l)
95 {
96 #ifdef FPE
97 pcu_save(&pcu_fpu_ops, l);
98 #endif
99 }
100
101 static inline void
fpu_discard(lwp_t * l)102 fpu_discard(lwp_t *l)
103 {
104 #ifdef FPE
105 pcu_discard(&pcu_fpu_ops, l, false);
106 #endif
107 }
108
109 static inline void
fpu_replace(lwp_t * l)110 fpu_replace(lwp_t *l)
111 {
112 #ifdef FPE
113 pcu_discard(&pcu_fpu_ops, l, true);
114 #endif
115 }
116
117 static inline bool
fpu_valid_p(lwp_t * l)118 fpu_valid_p(lwp_t *l)
119 {
120 #ifdef FPE
121 return pcu_valid_p(&pcu_fpu_ops, l);
122 #else
123 return false;
124 #endif
125 }
126
127 void __syncicache(const void *, size_t);
128
129 int cpu_set_onfault(struct faultbuf *, register_t) __returns_twice;
130 void cpu_jump_onfault(struct trapframe *, const struct faultbuf *);
131
132 static inline void
cpu_unset_onfault(void)133 cpu_unset_onfault(void)
134 {
135 curlwp->l_md.md_onfault = NULL;
136 }
137
138 static inline struct faultbuf *
cpu_disable_onfault(void)139 cpu_disable_onfault(void)
140 {
141 struct faultbuf * const fb = curlwp->l_md.md_onfault;
142 curlwp->l_md.md_onfault = NULL;
143 return fb;
144 }
145
146 static inline void
cpu_enable_onfault(struct faultbuf * fb)147 cpu_enable_onfault(struct faultbuf *fb)
148 {
149 curlwp->l_md.md_onfault = fb;
150 }
151
152 void cpu_intr(struct trapframe */*tf*/, register_t /*epc*/,
153 register_t /*status*/, register_t /*cause*/);
154 void cpu_trap(struct trapframe */*tf*/, register_t /*epc*/,
155 register_t /*status*/, register_t /*cause*/,
156 register_t /*badvaddr*/);
157 void cpu_ast(struct trapframe *);
158 void cpu_fast_switchto(struct lwp *, int);
159
160 void lwp_trampoline(void);
161
162 void * cpu_sendsig_getframe(struct lwp *, int, bool *);
163
164 #endif
165
166 #endif /* _RISCV_LOCORE_H_ */
167